ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
8k stars 1.86k forks source link

BaseProvider getStorageAt position is not spec-compliant #2982

Closed tkporter closed 2 months ago

tkporter commented 2 years ago

Ethers Version

@ethersproject/providers 5.6.5

Search Terms

BaseProvider,getStorageAt

Describe the Problem

I came across this while trying to use provider.getStorageAt(someAddress, 0); against a fresh install of Hardhat. The fresh install evidently used Hardhat v2.9.5 that includes a change to make Hardhat network's eth_getStorageAt spec compliant (Hardhat PR here, references spec found here). Apparently the spec requires the storage position passed into eth_getStorageAt to be a 32 byte hex string, which BaseProvider doesn't do.

Calling getStorageAt would give this Hardhat error: InvalidArgumentsError: Errors encountered in param 1: Storage slot argument must have a length of 66 ("0x" + 32 bytes), but '0x0' has a length of 3. Eventually I tracked it down to this line: https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/base-provider.ts#L1482

I was able to fix it locally by changing the hexValue(p) to hexZeroPad(p, 32). I'm happy to open a PR but I'm not sure if that's the best place to make the change / if it totally preserves backward compatibility for other use cases.

For any passerby, downgrading Hardhat version to <= v2.9.4 seems to be a sufficient workaround for now

Code Snippet

provider.getStorageAt(someAddress, 0);
// or even
provider.getStorageAt(someAddress, '0x0000000000000000000000000000000000000000000000000000000000000000');

Contract ABI

No response

Errors

No response

Environment

node.js (v12 or newer), Hardhat

Environment (Other)

No response

ricmoo commented 2 years ago

The change should probably go into JsonRpcProvider for going over the JSON-RPC wire protocol, since I don't want to change the behaviour of the the class BaseProvider which has other sub-classes.

Thanks! I'll fix this today. :)

(Note: in v6, the formatter has a storageSlot method, which is used to safely transform this value based on the network)

tkporter commented 2 years ago

Thanks so much! Makes sense to me :)

ricmoo commented 2 years ago

This should be fixed in 5.6.7. Try it out and let me know if you still have any problems.

Thanks! :)

alcuadrado commented 2 years ago

Sorry for the inconvenience guys. Fixing this was a tough call, as we accidentally deviated from the "standard" previously.

ricmoo commented 2 years ago

No worries; I'm a huge fan of things being standards compliant. :)

I've already made changes in v6 that are aligned with this too.

3commascapital commented 2 years ago

just tried this and am seeing the same error

ricmoo commented 2 years ago

Did you update to the latest version? 5.6.8?

3commascapital commented 2 years ago

i did! went there first actually. trying 5.6.7 now

ricmoo commented 2 years ago

5.6.7 should work too. I’ll re-open and look at it tomorrow.

3commascapital commented 2 years ago

i am seeing this most readily when i do a uniswap v3 swap then try to trace the transaction using hardhat-tracer if that is of any help

blgamann commented 2 years ago

both 5.6.7 and 5.6.8 are not working now.

> await ethers.provider.getStorageAt("0xaE80289290853f78219f2AfD3C69e0A3105fF96C", "0x0000000000000000000000000000000000000000000000000000000000000000");
Uncaught:
ProviderError: Errors encountered in param 1: Storage slot argument must have a length of 66 ("0x" + 32 bytes), but '0x0' has a length of 3
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at GanacheGasMultiplierProvider.request (/Users/sgnoo/Workspace/xociety-monorepo/packages/contracts/node_modules/hardhat/src/internal/core/providers/gas-providers.ts:312:34)
    at HttpProvider.request (/Users/sgnoo/Workspace/xociety-monorepo/packages/contracts/node_modules/hardhat/src/internal/core/providers/http.ts:78:19)
zemse commented 2 years ago

Can you once try removing lock files and reinstalling on your end if it solves the problem?

ricmoo commented 2 years ago

@3commascapital @0xdagarn Have you tried @zemse suggestion of removing any lock files and re-installing? Can you make sure the version of the @ethersproject/providers package in the node_modules/@ethersproject/providers/package.json is 5.6.8?

I've just tried to reproduce this against the latest version and it still works for me.

Skyge commented 1 year ago

Same error when I use 6.0.8, but if I use 5.6.8, it works well.

ricmoo commented 2 months ago

Closing older issues. But if this is still happening in v6, please re-open or start a new issue.

Thanks! :)