eth_getStorageAt

从给定地址的存储位置返回值,即返回合约存储的状态,该状态可能不会通过合约的方法公开。

Parameters

  • ADDRESS [必须] 存储地址。

  • STORAGE-POSITION [必须] 表示存储位置的整数。

  • BLOCK-NUMBER [必须] 十六进制块号,或字符串latest, earliest or pending默认块号参数

Request

curl https://eth-mainnet.solarpath.io/v1/YOUR-API-KEY \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc": "2.0", "id": 1, "method": "eth_getStorageAt", "params": ["0x295a70b2de5e3953354a6a8344e616ed314d7251", "0x0", "latest"]}'

Response

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

示例

计算正确位置取决于要检索的存储。通过地址0x391694e7e0b0cce554cb130d723a9d27458f9298 部署在 0x295a70b2de5e3953354a6a8344e616ed314d7251 的以下合约。

contract Storage {
    uint pos0;
    mapping(address => uint) pos1;
    function Storage() {
        pos0 = 1234;
        pos1[msg.sender] = 5678;
    }
}

检索 pos0 的值很简单:

curl https://eth-mainnet.solarpath.io/v1/YOUR-API-KEY \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc": "2.0", "id": 1, "method": "eth_getStorageAt", "params": ["0x295a70b2de5e3953354a6a8344e616ed314d7251", "0x0", "latest"]}'

检索映射的元素更难。 映射中的元素位置通过以下方式计算:

keccack(LeftPad32(key, 0), LeftPad32(map position, 0))

这意味着要检索 pos1["0x391694e7e0b0cce554cb130d723a9d27458f9298"] 上的存储,我们需要通过以下方法计算位置:

keccak(
  decodeHex(
    "000000000000000000000000391694e7e0b0cce554cb130d723a9d27458f9298" +
      "0000000000000000000000000000000000000000000000000000000000000001"
  )
)

可以使用 web3 库自带的 Geth 控制台进行计算:

> var key = "000000000000000000000000391694e7e0b0cce554cb130d723a9d27458f9298" + "0000000000000000000000000000000000000000000000000000000000000001"
undefined
> web3.sha3(key, {"encoding": "hex"})
"0x6661e9d6d8b923d5bbaab1b96e1dd51ff6ea2a93520fdc9eb75d059238b8c5e9"

现在获取存储:

curl https://eth-mainnet.solarpath.io/v1/YOUR-API-KEY \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc": "2.0", "id": 1, "method": "eth_getStorageAt", "params": ["0x295a70b2de5e3953354a6a8344e616ed314d7251", "0x6661e9d6d8b923d5bbaab1b96e1dd51ff6ea2a93520fdc9eb75d059238b8c5e9", "latest"]}'
{
    "id":1,
    "jsonrpc":"2.0",
    "result":"0x000000000000000000000000000000000000000000000000000000000000162e"
}

最后更新于