如何从web3获取节点数据库某个合约内的指定数据?

合约案例:

  • 此为原始版本,于2021年9月编辑完成,需进行润色

PancakePair:

contract PancakePair is IPancakePair, PancakeERC20 {
    
    
    using SafeMath  for uint;
    using UQ112x112 for uint224;

    uint public constant MINIMUM_LIQUIDITY = 10**3;
    bytes4 private constant SELECTOR = bytes4(keccak256(bytes('transfer(address,uint256)')));

    address public factory;5
    address public token0;6
    address public token1;7 8
    uint112 private reserve0;           // uses single storage slot, accessible via getReserves
    uint112 private reserve1;           // uses single storage slot, accessible via getReserves
    uint32  private blockTimestampLast; // uses single storage slot, accessible via getReserves

    uint public price0CumulativeLast;
    uint public price1CumulativeLast;
    uint public kLast; // reserve0 * reserve1, as of immediately after the most recent liquidity event

0x00:totalSupply
0x01:balanceOf(mapping)
后面以此类推
用getStorageAt


Web3调用合约方式

web3.eth.call({to:“0x3Ad45a76920f2E6Dc3A46fFb1ACDf8fa69379c0E”,data:“0902f1ac”})

节点数据库数据读取

web3.utils.soliditySha3
({ type: “uint”, value: “0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2” }, {type: “uint”, value: 0})

对于 mapping 等复杂类型,其中变量的 position 较为复杂,例如,mapping 中,key 的 position 为
keccack(LeftPad32(key, 0), LeftPad32(map position, 0)),即把 key 的值向左补 0
扩充至 256 位,将 map position(这里 map 的 position 是 1)向左补 0 扩充至 256
位,然后将两部分串接起来,做 keccak256 哈希运算,得到的哈希值即为 key 对应的 position。
————————————————
在进行获取mapping的插槽时,必须保证输入的两个key+slot中间用逗号连接,且使用uint类型的address

web3.utils.soliditySha3({ type: “uint”, value: “0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2” }, {type: “uint”, value: 0})

web3.eth.getStorageAt(“0x3Ad45a76920f2E6Dc3A46fFb1ACDf8fa69379c0E”,“0x4956ee41b239b76504f5c42c793a9e40f8e368732e907c4c04661da69d43461c”)

web3.eth.getStorageAt(“0x3Ad45a76920f2E6Dc3A46fFb1ACDf8fa69379c0E”,“5”)

web3.eth.getStorageAt(“0x3Ad45a76920f2E6Dc3A46fFb1ACDf8fa69379c0E”,“1”)


0x02:allowance(mapping)
0x03:DOMAIN_SEPARATOR
0x04:nonces(mapping)
0x05:factory
0x06:token0
0x07:token1
0x08:blockTimestampLast(16进制)+reserve0(16进制)+reserve1(16进制)
0x09:price0CumulativeLast
0x0a:price1CumulativeLast
0x0b:kLast
0x0c:1?
0x00-0x0f
0x10-0x1f

0x3f

猜你喜欢

转载自blog.csdn.net/BradMoon/article/details/123180244