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.
// Request params
{
"jsonrp": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}
{
"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?
Do you know an easier way?
The pool determines the price based on supply and demand in comparison to the paired coin.
The initial token price PancakeSwap is determined by the ratio at which you supply tokens to the liquidity pool. When you provide liquidity, you provide the 2 assets to which Pancakeswap will determine as equals. This should be your token and a certain amount of BNB or a stablecoin.
BEP20 basically refers to a token standard used for creating tokens on BSC, just like ERC20 for creating tokens on Ethereum. It is a critical tool for facilitating smart contract development on the Binance Smart Chain.
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_blockNumber
is used to get the transactions passed in the latest block and displays value here :
eth_call
is called with method sha3 0x252dba42
which corresponds to the aggregator
function as you mentioned. The code is the following:
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;
}
}
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 of eth_call
by 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_call
contained in the bigger structure.
For example, given the following query (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
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_call
it will gives you:
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
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_call
are used to determine the token price. Though if you find it out, I'll be interested.
Cheers,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With