miguelmota / ethereum-development-with-go-book

📖 A little guide book on Ethereum Development with Go (golang)
https://goethereumbook.org
Other
1.74k stars 427 forks source link

"403 Forbidden" when using "client.BalanceAt(context.Background(), account, blockNumber)" #42

Closed wulikai93 closed 4 years ago

wulikai93 commented 4 years ago

I run the code in this page. The logs are:

13565093091594031083
2019/12/09 16:21:15 403 Forbidden {"jsonrpc":"2.0","id":2,"error":{"code":-32002,"message":"project ID is required to access archive state","data":{"see":"https://infura.io/dashboard"}}}

It shows that client.BalanceAt(context.Background(), account, nil) return the balance, but balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber) not.

I wonder what's the reason here and how to fix it. Thanks a lot if anyone can help me.

miguelmota commented 4 years ago

@wulikai93 if using the infura rpc url, then you need to include the api key which you can get one from their dashboard https://infura.io/

I'll update the guides to mention this

thanks!

wulikai93 commented 4 years ago

@miguelmota I logged in the infura dashboard and create a project. Then i change the url as following( i use the real project ID rather than the string "MY-PROJECT-ID" in my code):

client, err := ethclient.Dial("https://mainnet.infura.io/v3/MY-PROJECT-ID")

But i also get a return:

2019/12/15 18:03:41 403 Forbidden {"jsonrpc":"2.0","id":2,"error":{"code":-32002,"message":"project ID does not have access to archive state","data":{"see":"https://infura.io/dashboard"}}}

In conclusion, when i don't offer a project ID, it says project ID is required to access archive state. But when i offer a project ID, it says project ID does not have access to archive state.

Is thera anything i done uncorrectly?

miguelmota commented 4 years ago

Looks like Infura changed their pricing model a few months ago:

Screenshot from 2020-04-28 02-25-13

The options now are to either pay for the api service, looks for an alternative rpc provider, or run your own archival node in order to retrieve historical data.

The cheapest option might be to run your own archival node locally or on something like EC2 or digital ocean.

You could try the cloudflare rpc provider (https://cloudflare-eth.com) but I don't think it supports archival data.