Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: invalid json response with provider.getEtherPrice() function #776

Closed
Gabriel1590 opened this issue Apr 1, 2020 · 26 comments
Closed
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.

Comments

@Gabriel1590
Copy link

I´m trying to obtain the Ether Price using ether.js. I tried to do it calling the function etherscanProvider.getEtherPrice()

const etherProvider = new ethers.providers.EtherscanProvider('homestead', 'MYETHERSCANAPIKEY');
etherProvider.getEtherPrice().then(data => {
   console.log(data);
}).catch(err => console.log(err));

And gives me this err:

Error: invalid json response at XMLHttpRequest.o.onreadystatechange [as __zone_symbol__ON_PROPERTYreadystatechange] (ethers.min.js:1) at XMLHttpRequest.wrapFn (zone-evergreen.js:1191) at ZoneDelegate.invokeTask (zone-evergreen.js:391) at Object.onInvokeTask (core.js:39680) at ZoneDelegate.invokeTask (zone-evergreen.js:390) at Zone.runTask (zone-evergreen.js:168) at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:465) at invokeTask (zone-evergreen.js:1603) at XMLHttpRequest.globalZoneAwareCallback (zone-evergreen.js:1629)

I also tried to call directly the api of etherscan:

this.http.get('https://api.etherscan.io/api?module=stats&action=ethprice&apikey=APIKEY')
.subscribe(data => console.log(data));

And its returning null

Also tried to call the etherscan api directly from the browser console, and also returns null

Typescript 3.7.5
Angular 8.3.23
Node 12.16.1
Ionic 5.4.15

@ricmoo
Copy link
Member

ricmoo commented Apr 1, 2020

Looks like they may have changed the format of the response?

If I hit the api url I get:

{"status":"1","message":"OK-Missing/Invalid API Key, rate limit of 1/3sec applied","result":{"ethbtc":"0.02093","ethbtc_timestamp":"1585709124","ethusd":"132.31","ethusd_timestamp":"1585709119"}}

I'll look into updating this asap.

@ricmoo ricmoo added bug Verified to be an issue. on-deck This Enhancement or Bug is currently being worked on. labels Apr 1, 2020
@ricmoo
Copy link
Member

ricmoo commented Apr 1, 2020

Also, quick question, are you using ethers v4 or the v5 beta?

@Gabriel1590
Copy link
Author

@ricmoo Yes, if you in your browser directly put the url it gives you the expected response. But that is with the Etherscan Api itself, what happend with ethers.js that gives me that response? how can I do? the only thing that I think of is instead of obtaining a json, obtaining a text, but how can I do that from ethers.js?

Also is as the network property I give 'ropsten' or another testnet instead of 'homestead' as the mainnet. Returns 0, that is correct because the price is 0$

@ricmoo
Copy link
Member

ricmoo commented Apr 1, 2020

@Gabriel1590 What version of ethers are you using? It looks like it should work fine in v4, so I just want to confirm you are using v5.

@Gabriel1590
Copy link
Author

@ricmoo ethers: ^4.0.46

@ricmoo
Copy link
Member

ricmoo commented Apr 1, 2020

Yes, if it is not on mainnet, it does not even query any API, it just returns 0. :)

When I try v4.0.46 it works fine for me, even if I hit it repeatedly. What if you try v5? Maybe the environment you are using has a less-than-ideal XMLHttpRequest implementation? In v5, it uses the fetch API instead. I'm not sure what this.http is in your above example...

@Gabriel1590
Copy link
Author

@ricmoo could you tell me the exact code that you´re implementing?

@ricmoo
Copy link
Member

ricmoo commented Apr 1, 2020

(oh but v5 has a bug in that call; fixing it now)

@ricmoo
Copy link
Member

ricmoo commented Apr 1, 2020

For v4? This is the code

@ricmoo
Copy link
Member

ricmoo commented Apr 1, 2020

And this is what I run to use it:

const provider = new ethers.providers.EtherscanProvider();
provider.getEtherPrice().then((price) => {
    console.log(price);
});

@Gabriel1590
Copy link
Author

@ricmoo I´ll try with the V5, give me a moment to download it. I´m getting network issues

@ricmoo
Copy link
Member

ricmoo commented Apr 1, 2020

The v5 test cases are still running. I'll publish once they pass.

@ricmoo
Copy link
Member

ricmoo commented Apr 1, 2020

Make sure you get 5.0.0-beta.179 (using npm install ethers@next if using the command line) and it should have a working getEtherPrice. :)

@ricmoo ricmoo added fixed/complete This Bug is fixed or Enhancement is complete and published. and removed on-deck This Enhancement or Bug is currently being worked on. labels Apr 1, 2020
@Gabriel1590
Copy link
Author

@ricmoo my app doesn´t work. It gives me this error:

ERROR in node_modules/@ethersproject/abi/lib/coders/abstract-coder.d.ts:22:9 - error TS1086: An accessor cannot be declared in an ambient context.      
[ng] 22     get data(): string;
[ng]            ~~~~
[ng] node_modules/@ethersproject/abi/lib/coders/abstract-coder.d.ts:23:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 23     get length(): number;
[ng]            ~~~~~~
[ng] node_modules/@ethersproject/abi/lib/coders/abstract-coder.d.ts:36:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 36     get data(): string;
[ng]            ~~~~
[ng] node_modules/@ethersproject/abi/lib/coders/abstract-coder.d.ts:37:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 37     get consumed(): number;
[ng]            ~~~~~~~~
[ng] node_modules/@ethersproject/hdnode/lib/index.d.ts:29:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 29     get extendedKey(): string;
[ng]            ~~~~~~~~~~~
[ng] node_modules/@ethersproject/providers/lib/base-provider.d.ts:21:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 21     get type(): string;
[ng]            ~~~~
[ng] node_modules/@ethersproject/providers/lib/base-provider.d.ts:22:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 22     get hash(): string;
[ng]            ~~~~
[ng] node_modules/@ethersproject/providers/lib/base-provider.d.ts:23:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 23     get filter(): Filter;
[ng]            ~~~~~~
[ng] node_modules/@ethersproject/providers/lib/base-provider.d.ts:61:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 61     get network(): Network;
[ng]            ~~~~~~~
[ng] node_modules/@ethersproject/providers/lib/base-provider.d.ts:63:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 63     get blockNumber(): number;
[ng]            ~~~~~~~~~~~
[ng] node_modules/@ethersproject/providers/lib/base-provider.d.ts:64:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 64     get polling(): boolean;
[ng]            ~~~~~~~
[ng] node_modules/@ethersproject/providers/lib/base-provider.d.ts:65:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 65     set polling(value: boolean);
[ng]            ~~~~~~~
[ng] node_modules/@ethersproject/providers/lib/base-provider.d.ts:66:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 66     get pollingInterval(): number;
[ng]            ~~~~~~~~~~~~~~~
[ng] node_modules/@ethersproject/providers/lib/base-provider.d.ts:67:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 67     set pollingInterval(value: number);
[ng]            ~~~~~~~~~~~~~~~
[ng] node_modules/@ethersproject/providers/lib/websocket-provider.d.ts:25:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 25     get pollingInterval(): number;
[ng]            ~~~~~~~~~~~~~~~
[ng] node_modules/@ethersproject/providers/lib/websocket-provider.d.ts:27:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 27     set pollingInterval(value: number);
[ng]            ~~~~~~~~~~~~~~~
[ng] node_modules/@ethersproject/providers/lib/websocket-provider.d.ts:29:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 29     set polling(value: boolean);
[ng]            ~~~~~~~
[ng] node_modules/@ethersproject/wallet/lib/index.d.ts:14:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 14     get mnemonic(): Mnemonic;
[ng]            ~~~~~~~~
[ng] node_modules/@ethersproject/wallet/lib/index.d.ts:15:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 15     get privateKey(): string;
[ng]            ~~~~~~~~~~
[ng] node_modules/@ethersproject/wallet/lib/index.d.ts:16:9 - error TS1086: An accessor cannot be declared in an ambient context.
[ng] 16     get publicKey(): string;

when I tried to initialize the app

@ricmoo
Copy link
Member

ricmoo commented Apr 4, 2020

What version of TypeScript are you using? I think you need to bump the version to 3.8, since the new TypeScript definition files use a getter in the interface file for readonly variables.

@Gabriel1590
Copy link
Author

@ricmoo I´m using Typescript 3.7.5

@ricmoo
Copy link
Member

ricmoo commented Apr 4, 2020

That should be a recent enough version. This breaking change was added in 3.7. Here is more info on it.

Can you run npx tsc --version to make sure that is the version being picked up by your build process?

You can also try cleaning your build, and re-running npm install and build?

@Gabriel1590
Copy link
Author

yes, it is 3.7.5. I already do that, but it doesnt work. The only think that does not work is the getEtherPrice(), the rest functions works properly. I solved it using the coingecko API, but i´ll like to use only ethers.js to all operations relate to Ethers

@ricmoo
Copy link
Member

ricmoo commented Apr 4, 2020

Looks like your error may be a combination of the version of Angular and TypeScript.

Here is someone with the same issue as you: storybookjs/storybook#9463

Maybe that will help?

@ricmoo
Copy link
Member

ricmoo commented Apr 8, 2020

Did that issue help?

@Gabriel1590
Copy link
Author

Gabriel1590 commented Apr 8, 2020

@ricmoo I'm sorry for the late response. I have not prove it. As I've been reading, It may be an error from TypeScript, but in this moment I have not The time to solve It. As soon as I Fix it, I'll upload The solution. Thank You for The help, I'll ask You again if I need some help

@ricmoo
Copy link
Member

ricmoo commented Apr 8, 2020

Ok. I'm going to close this ticket then, since the bug you identified in v5 has been resolved, and I think v4 should work fine once the TypeScript things are figured out.

But if you still have problems once the TypeScript stuff is sorted, feel free to re-open or open new issue. Sound good?

Thanks! :)

@ricmoo ricmoo closed this as completed Apr 8, 2020
@Gabriel1590
Copy link
Author

@ricmoo I solve The problem, I just pass to a backend service with NodeJs and TypeScript, and doing API Request to The info I need and It works perfectly. Thanks for allí The help

michaeltout pushed a commit to michaeltout/ethers.js that referenced this issue Aug 23, 2020
@dannybin
Copy link

dannybin commented Jun 7, 2022

I am trying to use the getEtherPrice() in v5.6.8, but getting an "not implemented" error. Is there plan to implement this function in the near future? Thanks!

@ricmoo
Copy link
Member

ricmoo commented Jun 7, 2022

@dannybin What provider are you using? This is currently only available in the EtherscanProvider, as it is the only service which offers an RPC for it.

@dannybin
Copy link

dannybin commented Jun 7, 2022

Thank you! I was using infura. will try with ethanscan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.
Projects
None yet
Development

No branches or pull requests

3 participants