0

I am a javascript noob. I have the following function below but for some reason I cannot access the JSON object with the findTokenAddress variable.

Here is my sample code below. An example of a valid findTokenAddress could be: 0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd.

Which should return:

{"symbol":"BTC++","name":"PieDAO BTC++","decimals":18,"address":"0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd","logoURI":"https://tokens.1inch.io/0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd.png","tags":["tokens"]}

But for whatever reason this does not work. Could someone tell me what I'm missing here?

function getTokenDetails(findTokenAddress) {

  var url = `https://api.1inch.io/v4.0/1/tokens`;

  var xhr = new XMLHttpRequest();
  xhr.open("GET", url);

  xhr.setRequestHeader("accept", "application/json");

  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      if (xhr.status === 200) {
        const jsonResponse = JSON.parse(xhr.responseText).tokens;
        if (findTokenAddress in jsonResponse) {
          console.log(jsonResponse[findTokenAddress]);
          const tokenDetails = jsonResponse[findTokenAddress];
          console.log(tokenDetails);
        } else {
          console.log(findTokenAddress in jsonResponse);
          alert("Could not find token in json?");
        }
      } else {
        alert("Error!");
      }
    }
  };

  xhr.send();
}

getTokenDetails('0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd');

Here's what the JSON looks like from the url:

{"tokens":{"0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd":{"symbol":"BTC++","name":"PieDAO BTC++","decimals":18,"address":"0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd","logoURI":"https://tokens.1inch.io/0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd.png","tags":["tokens"]},"0x04fa0d235c4abf4bcf4787af4cf447de572ef828":{"symbol":"UMA","name":"UMA Voting Token v1","decimals":18,"address":"0x04fa0d235c4abf4bcf4787af4cf447de572ef828","logoURI":"https://tokens.1inch.io/0x04fa0d235c4abf4bcf4787af4cf447de572ef828.png","tags":["tokens"]},"0x08d967bb0134f2d07f7cfb6e246680c53927dd30":{"symbol":"MATH","name":"MATH Token","address":"0x08d967bb0134f2d07f7cfb6e246680c53927dd30","decimals":18,"logoURI":"https://tokens.1inch.io/0x08d967bb0134f2d07f7cfb6e246680c53927dd30.png","tags":["tokens"]},"0x0a913bead80f321e7ac35285ee10d9d922659cb7":{"symbol":"DOS","name":"DOS Network Token","decimals":18,"address":"0x0a913bead80f321e7ac35285ee10d9d922659cb7","logoURI":"https://tokens.1inch.io/0x0a913bead80f321e7ac35285ee10d9d922659cb7.png","tags":["tokens"]},"0x0ae055097c6d159879521c384f1d2123d1f195e6":{"symbol":"STAKE","name":"STAKE","address":"0x0ae055097c6d159879521c384f1d2123d1f195e6","decimals":18,"logoURI":"https://tokens.1inch.io/0x0ae055097c6d159879521c384f1d2123d1f195e6.png","eip2612":true,"tags":["tokens"]},"0x88df592f8eb5d7bd38bfef7deb0fbc02cf3778a0":{"symbol":"TRB","name":"Tellor Tributes","address":"0x88df592f8eb5d7bd38bfef7deb0fbc02cf3778a0","decimals":18,"logoURI":"https://tokens.1inch.io/0x0ba45a8b5d5575935b8158a88c631e9f9c95a2e5.png","tags":["tokens"]}...
Barmar
  • 741,623
  • 53
  • 500
  • 612
John Smith
  • 3
  • 1
  • 7
  • @Phil Go to the URL https://api.1inch.io/v4.0/1/tokens. It's simple JSON. And the key he's looking for is the first property. – Barmar Jun 10 '22 at 01:07
  • @Phil Chrome and MacOS are pretty good at protecting you from malware at websites. – Barmar Jun 10 '22 at 01:09
  • I tried your function in the console and it found the token. – Barmar Jun 10 '22 at 01:10
  • @Barmar Really? When I try it in the chrome console it doesn't get found. I wonder what I'm doing wrong. – John Smith Jun 10 '22 at 01:12
  • I've converted your code to an executable snippet, it works there as well. – Barmar Jun 10 '22 at 01:13
  • 1
    Are you passing `findTokenAddress` as a string or a number? Object keys are **always** strings and `(0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd).toString()` is `"1.7998194582028347e+46"` – Phil Jun 10 '22 at 01:13
  • @Phil It's being passed as a string. – John Smith Jun 10 '22 at 01:15
  • @Barmar That is so strange! Wonder why it won't work on my chrome console – John Smith Jun 10 '22 at 01:16
  • @Phil It's being fetched from the html input field via input_field.value – John Smith Jun 10 '22 at 01:16
  • 1
    Try trimming the input to remove extraneous spaces: `input_field.value.trim()` – Barmar Jun 10 '22 at 01:17
  • Throw in `console.log("findTokenAddress", JSON.stringify(findTokenAddress))` and see what gets logged. That should help you narrow down the problem – Phil Jun 10 '22 at 01:19
  • @Phil That just returns 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48. One with double quotes and one without – John Smith Jun 10 '22 at 01:23
  • @Phil Apologies but this is what it returns: findTokenAddress "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" – John Smith Jun 10 '22 at 01:25
  • Your casing is incorrect for that particular key. The one in the JSON is `0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48`, **all lowercase** – Phil Jun 10 '22 at 01:27
  • Does this answer your question? [Access JavaScript property case-insensitively?](https://stackoverflow.com/q/12484386/283366) – Phil Jun 10 '22 at 01:29
  • @Phil you are a lifesaver! I totally missed the fact that i've been putting in the wrong case this entire time. Thank you! – John Smith Jun 10 '22 at 01:31
  • It would have been solved quicker if the example in your question matched reality – Phil Jun 10 '22 at 01:32

0 Answers0