10

我想计算 BNB 中 BEP20 代币的价格。我检查了 poocoin 和 pancakeswap 他们是如何计算价格的。正如我所看到的,他们通过 web3.js 不断地调用 bsc-dataseed,但我还没有找到解决方案。

我的调查现状:

  1. 他们不断调用数据种子(webrpc)以获取最新的块号:
// Request params
{
    "jsonrp": "2.0",
    "method": "eth_blockNumber",
    "params": [],
    "id": 1
}
  1. 在他们收到最新的块后,他们立即使用以下参数开始另一个调用
{
  "jsonrpc": "2.0",
  "id": 53,
  "method": "eth_call",
  "params": [
    {
      "to": "0x1ee38d535d541c55c9dae27b12edf090c608e6fb",
      "data": "0x252dba420000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000048000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000006800000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000078000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000880000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009800000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a800000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000b800000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000c800000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000d800000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000e800000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000000f8000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001080000000000000000000000000000000000000000000000000000000000000110000000000000000000000000003ff6c83226cdb10c7a42a2c54c67d63a135ab69000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000003fc20bdafdac6b2ae8f7f5f885f0819c2b968f0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000008b018585d4949ae4714f057b66b8b033408bfa6000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000012c8db147721ac1e3e54e9ff73d99ef0ee4b1725000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000001b96b92314c44b159149f7e0303511fb2fc4774f000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000020bcc3b8a0091ddac2d0bc30f68e6cbb97de59cd000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000002c5d712cf39eaebc1ef2d1281eb48a70dcd34d49000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000003ab77e40340ab084c3e23be8e5a6f7afed9d41dc000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000003b872e6b1633f060680cf9c277b43dd1e254e564000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000056c77d59e82f33c712f919d09fceddf49660a829000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000006f656513f6e8eae19b6dc30cd372dd6ba4b5543a000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000070d8929d04b60af4fb9b58713ebcf18765ade422000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000007246d6ea86d34af343f03cc1eb952a4f98467a4e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000007380e10f5c5f9dff4857de3cf9c39bb16f4c6dcf000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000007561eee90e24f3b348e1087a005f78b4c8453524000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000088c4bcf826410a81b58bc9b79a17641b443dc52e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000092ee07c591c26775b4d31259d5417e00553e857c000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000009adc6fb78cefa07e13e9294f150c1e8c1dd566c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000cc6b11de64dce6e5052a84b67cbbfd210ed530f7000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000d1f12370b2ba1c79838337648f820a87edf5e1e6000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000b3c4217ab2b265bf8c69718d280e3708b5e50577000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000b8875e207ee8096a929d543c9981c9586992eacb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000c15fa3e22c912a276550f3e5fe3b0deb87b55acd000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000cee09ae86a78ce0cea3a4ea8cced5d41c0468e67000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000d9a0d1f5e02de2403f68bb71a15f8847a854b494000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000e7cc44de50b54906a9c1c48240650be2766481fc000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000fb7deb2236815222113d949d935cca4901531677000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000fa3107bca2ff6db4131bcd62b598cb00d9087f58000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000"
    },
    "latest"
  ]
}

据我所知,这0x1ee38d535d541c55c9dae27b12edf090c608e6fb是一份包含该multicall方法的合同。数据的前 10 个字符(0x252dba42)是aggregate方法。但是数据的剩余部分是什么?
这是aggregate方法的合同 ABI:

{
    "constant": true,
    "inputs": [
      {
        "components": [
            {"name": "target","type": "address"},
            {"name": "callData","type": "bytes"}
        ],
        "name": "calls",
        "type": "tuple[]"
      }
    ],
    "name": "aggregate",
    "outputs": [
      {
        "name": "blockNumber",
        "type": "uint256"
      },
      {
        "name": "returnData",
        "type": "bytes[]"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  }

问题:

需要第二个电话吗?
如何根据这些数据计算价格?
你知道更简单的方法吗?

4

1 回答 1

1

关于是否可以从 web3 计算代币价格的问题的答案在这里:https ://ethereum.stackexchange.com/questions/98302/how-can-we-get-the-current-spot-price-为-a-bep20-token-used-web3

但是:部分回答您的问题。

eth_blockNumber用于获取最新区块中传递的交易并在此处显示值: 最后一个令牌 txn

eth_call使用与您提到0x252dba42的函数相对应的方法 sha3 调用。aggregator代码如下:

contract Multicall {
struct Call {
    address target;
    bytes callData;
}
function aggregate(Call[] memory calls) public returns (uint256 blockNumber, bytes[] memory returnData) {
    blockNumber = block.number;
    returnData = new bytes[](calls.length);
    for(uint256 i = 0; i < calls.length; i++) {
        (bool success, bytes memory ret) = calls[i].target.call(calls[i].callData);
        require(success);
        returnData[i] = ret;
    }
}

如您所见,它只是执行多个 eth_callcalls[i].target.call(calls[i].callData);的一种方法,这意味着它是一种通过仅调用 Web3 一次来执行大量操作的有效方法eth_call,因此可以大幅增加执行时间。

然后进入细节,你将不得不解析eth_call更大结构中包含的不同。

例如,给定以下查询 (eth_call) :

0x252dba42
0000000000000000000000000000000000000000000000000000000000000020
000000000000000000000000000000000000000000000000000000000000000d
00000000000000000000000000000000000000000000000000000000000001a0
0000000000000000000000000000000000000000000000000000000000000220
00000000000000000000000000000000000000000000000000000000000002a0
0000000000000000000000000000000000000000000000000000000000000320
00000000000000000000000000000000000000000000000000000000000003a0
0000000000000000000000000000000000000000000000000000000000000420
00000000000000000000000000000000000000000000000000000000000004a0
0000000000000000000000000000000000000000000000000000000000000520
00000000000000000000000000000000000000000000000000000000000005c0
0000000000000000000000000000000000000000000000000000000000000660
0000000000000000000000000000000000000000000000000000000000000700
0000000000000000000000000000000000000000000000000000000000000780
0000000000000000000000000000000000000000000000000000000000000800
000000000000000000000000b443d19e5448c8a58916ca8e0a33c41212668ad3
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
0902f1ac > getReserves() -> pancake
00000000000000000000000000000000000000000000000000000000
000000000000000000000000b443d19e5448c8a58916ca8e0a33c41212668ad3
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
d21220a7 > token1 -> pancake
00000000000000000000000000000000000000000000000000000000
000000000000000000000000b443d19e5448c8a58916ca8e0a33c41212668ad3
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
18160ddd > totalSupply -> swole
00000000000000000000000000000000000000000000000000000000
000000000000000000000000f5318b3941b3f1239dd910c941bbe2259f32311b
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
313ce567 > decimals > swole
00000000000000000000000000000000000000000000000000000000
000000000000000000000000f5318b3941b3f1239dd910c941bbe2259f32311b
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
18160ddd > totalSupply > swole
00000000000000000000000000000000000000000000000000000000
000000000000000000000000f5318b3941b3f1239dd910c941bbe2259f32311b
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
95d89b41 > symbol > swole
00000000000000000000000000000000000000000000000000000000
000000000000000000000000f5318b3941b3f1239dd910c941bbe2259f32311b
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
06fdde03 > name > swole 
00000000000000000000000000000000000000000000000000000000
000000000000000000000000f5318b3941b3f1239dd910c941bbe2259f32311b
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000024
70a08231 > balanceOf > swole
00000000000000000000000000000000000000000000000000000000
0000dead00000000000000000000000000000000000000000000000000000000
000000000000000000000000f5318b3941b3f1239dd910c941bbe2259f32311b
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000024
70a08231 > balanceOf > swole
00000000000000000000000000000000000000000000000000000000
00000ded00000000000000000000000000000000000000000000000000000000
000000000000000000000000f5318b3941b3f1239dd910c941bbe2259f32311b
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000024
70a08231 > balanceOf > pancake
00000000000000000000000000000000000000000000000000000000
0000000100000000000000000000000000000000000000000000000000000000
000000000000000000000000b443d19e5448c8a58916ca8e0a33c41212668ad3
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
0902f1ac > getReserves > pancake
00000000000000000000000000000000000000000000000000000000
000000000000000000000000b443d19e5448c8a58916ca8e0a33c41212668ad3
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
d21220a7 > Token1 (pancake)
00000000000000000000000000000000000000000000000000000000
000000000000000000000000b443d19e5448c8a58916ca8e0a33c41212668ad3
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000004
18160ddd > TotalSupply 
00000000000000000000000000000000000000000000000000000000

如果你想找到它的sha3所涉及的方法的名称,你可以参考这个网站:https ://pkg.go.dev/github.com/y2labs-0sh/dada-api/contractabi

作为给定的结果,eth_call它将为您提供:

0x
000000000000000000000000000000000000000000000000000000000071f9b8
0000000000000000000000000000000000000000000000000000000000000040
000000000000000000000000000000000000000000000000000000000000000d
00000000000000000000000000000000000000000000000000000000000001a0
0000000000000000000000000000000000000000000000000000000000000220
0000000000000000000000000000000000000000000000000000000000000260
00000000000000000000000000000000000000000000000000000000000002a0
00000000000000000000000000000000000000000000000000000000000002e0
0000000000000000000000000000000000000000000000000000000000000320
00000000000000000000000000000000000000000000000000000000000003a0
0000000000000000000000000000000000000000000000000000000000000420
0000000000000000000000000000000000000000000000000000000000000460
00000000000000000000000000000000000000000000000000000000000004a0
00000000000000000000000000000000000000000000000000000000000004e0
0000000000000000000000000000000000000000000000000000000000000560
00000000000000000000000000000000000000000000000000000000000005a0
0000000000000000000000000000000000000000000000000000000000000060
0000000000000000000000000000000000000000000000000d04526e1cee610a
0000000000000000000000000000000000000000096f9d05ebe63c03e58cc07a
0000000000000000000000000000000000000000000000000000000060a16f16
0000000000000000000000000000000000000000000000000000000000000020
000000000000000000000000f5318b3941b3f1239dd910c941bbe2259f32311b
0000000000000000000000000000000000000000000000000000000000000020
000000000000000000000000000000000000000000000a968163f0a57b400000
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000012 > decimals
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000001431e0fae6d7217caa0000000
0000000000000000000000000000000000000000000000000000000000000060
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000004
534f474500000000000000000000000000000000000000000000000000000000 > SOGE
0000000000000000000000000000000000000000000000000000000000000060
0000000000000000000000000000000000000000000000000000000000000020
000000000000000000000000000000000000000000000000000000000000000a
53776f6c6520446f676500000000000000000000000000000000000000000000 > Swole Doge
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000060
0000000000000000000000000000000000000000000000000d04526e1cee610a
0000000000000000000000000000000000000000096f9d05ebe63c03e58cc07a
0000000000000000000000000000000000000000000000000000000060a16f16
0000000000000000000000000000000000000000000000000000000000000020
000000000000000000000000f5318b3941b3f1239dd910c941bbe2259f32311b
0000000000000000000000000000000000000000000000000000000000000020
000000000000000000000000000000000000000000000a968163f0a57b400000

另请注意,poocoin 每次都会使用不同的令牌地址进行多次 eth_call。

poocoin 如何利用这些数据来获取价格?我不知道,现在甚至可以确定这些eth_call是用来确定代币价格的。虽然如果你发现它,我会感兴趣的。

干杯,

于 2021-05-17T06:11:17.710 回答