I would like to calculate the price of a BEP20 token in BNB. I checked poocoin and pancakeswap how they calculate the price. As I see they call continously the bsc-dataseed through web3.js, but I didn't find the solution yet.
The current status of my investigation:
- they calling continously the dataseed (webrpc) to get the latest blocknumber:
// Request params
{
"jsonrp": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}
- After they received the latest block they immediately start another call with the following parameters
{
"jsonrpc": "2.0",
"id": 53,
"method": "eth_call",
"params": [
{
"to": "0x1ee38d535d541c55c9dae27b12edf090c608e6fb",
"data": "0x252dba420000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000048000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000006800000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000078000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000880000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009800000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a800000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000b800000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000c800000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000d800000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000e800000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000000f8000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001080000000000000000000000000000000000000000000000000000000000000110000000000000000000000000003ff6c83226cdb10c7a42a2c54c67d63a135ab69000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000003fc20bdafdac6b2ae8f7f5f885f0819c2b968f0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000008b018585d4949ae4714f057b66b8b033408bfa6000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000012c8db147721ac1e3e54e9ff73d99ef0ee4b1725000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000001b96b92314c44b159149f7e0303511fb2fc4774f000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000020bcc3b8a0091ddac2d0bc30f68e6cbb97de59cd000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000002c5d712cf39eaebc1ef2d1281eb48a70dcd34d49000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000003ab77e40340ab084c3e23be8e5a6f7afed9d41dc000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000003b872e6b1633f060680cf9c277b43dd1e254e564000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000056c77d59e82f33c712f919d09fceddf49660a829000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000006f656513f6e8eae19b6dc30cd372dd6ba4b5543a000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000070d8929d04b60af4fb9b58713ebcf18765ade422000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000007246d6ea86d34af343f03cc1eb952a4f98467a4e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000007380e10f5c5f9dff4857de3cf9c39bb16f4c6dcf000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000007561eee90e24f3b348e1087a005f78b4c8453524000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000088c4bcf826410a81b58bc9b79a17641b443dc52e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac0000000000000000000000000000000000000000000000000000000000000000000000000000000092ee07c591c26775b4d31259d5417e00553e857c000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac000000000000000000000000000000000000000000000000000000000000000000000000000000009adc6fb78cefa07e13e9294f150c1e8c1dd566c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000cc6b11de64dce6e5052a84b67cbbfd210ed530f7000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000d1f12370b2ba1c79838337648f820a87edf5e1e6000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000b3c4217ab2b265bf8c69718d280e3708b5e50577000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000b8875e207ee8096a929d543c9981c9586992eacb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000c15fa3e22c912a276550f3e5fe3b0deb87b55acd000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000cee09ae86a78ce0cea3a4ea8cced5d41c0468e67000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000d9a0d1f5e02de2403f68bb71a15f8847a854b494000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000e7cc44de50b54906a9c1c48240650be2766481fc000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000fb7deb2236815222113d949d935cca4901531677000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000000000000000000000000000fa3107bca2ff6db4131bcd62b598cb00d9087f58000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040902f1ac00000000000000000000000000000000000000000000000000000000"
},
"latest"
]
}
As far as I know the 0x1ee38d535d541c55c9dae27b12edf090c608e6fb is a contract which contains the the multicall method. The first 10 characters of the data (0x252dba42) is the aggregate method. But what is the remaining part of the data?
This is the Contract ABI for aggregate method:
{
"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"
}
Is the 2nd call needed?
How can I calculate the price from those data?
The answer to your question regarding whether it is possible to compute the token price from web3 is here : https://ethereum.stackexchange.com/questions/98302/how-can-we-get-the-current-spot-price-for-a-bep20-token-used-web3
However : to answer partially to your question.
eth_blockNumberis used to get the transactions passed in the latest block and displays value here :eth_callis called with method sha30x252dba42which corresponds to theaggregatorfunction as you mentioned. The code is the following:As you see it is only a way to execute several eth_call
calls[i].target.call(calls[i].callData);meaning it's a efficient way of doing a lot ofeth_callby only calling Web3 once, thus is order to increase drastically the execution time.Then going into details you will have to parse the different
eth_callcontained in the bigger structure.For example, given the following query (eth_call) :
if you want to find the name of the method involved by it's sha3 you can refer this website: https://pkg.go.dev/github.com/y2labs-0sh/dada-api/contractabi
As a result of the given
eth_callit will gives you:Please also note that poocoin is doing several eth_call with different token addresses each time.
How poocoin exploits this data to get the price? I don't know, now even sure these
eth_callare used to determine the token price. Though if you find it out, I'll be interested.Cheers,