<p>The proper way to calculate a token price is by asking the liquidity pool (the pair of this token against the local PEG or some USD token) for the ratio of how much PEG was inserted agains how may tokens (for more details about what a liquidity pool represents, see <a href="https://uniswap.org/docs/v2/core-concepts/pools/" rel="nofollow noreferrer">https://uniswap.org/docs/v2/core-concepts/pools/</a>).</p>
<p>So for python use:</p>
<pre class="lang-py prettyprint-override"><code>from web3 import Web3
from web3.middleware import geth_poa_middleware # Needed for Binance

from json import loads
from decimal import Decimal


ETHER = 10 ** 18

WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'
CAKE_ROUTER_V2 = web3.toChecksumAddress('0x10ed43c718714eb63d5aa57b78b54704e256024e')

web3 = Web3(Web3.HTTPProvider('https://bsc-dataseed1.binance.org:443'))
web3.middleware_onion.inject(geth_poa_middleware, layer=0) # Again, this is needed for Binance, not Ethirium

ABI = loads('[{&quot;inputs&quot;:[],&quot;name&quot;:&quot;decimals&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;uint256&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;uint256&quot;}],&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;},{&quot;constant&quot;:true,&quot;inputs&quot;:[],&quot;name&quot;:&quot;token0&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;}],&quot;payable&quot;:false,&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;},{&quot;inputs&quot;:[],&quot;name&quot;:&quot;factory&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;}],&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;},{&quot;constant&quot;:true,&quot;inputs&quot;:[{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;},{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;}],&quot;name&quot;:&quot;getPair&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;}],&quot;payable&quot;:false,&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;},{&quot;constant&quot;:true,&quot;inputs&quot;:[],&quot;name&quot;:&quot;getReserves&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;uint112&quot;,&quot;name&quot;:&quot;_reserve0&quot;,&quot;type&quot;:&quot;uint112&quot;},{&quot;internalType&quot;:&quot;uint112&quot;,&quot;name&quot;:&quot;_reserve1&quot;,&quot;type&quot;:&quot;uint112&quot;},{&quot;internalType&quot;:&quot;uint32&quot;,&quot;name&quot;:&quot;_blockTimestampLast&quot;,&quot;type&quot;:&quot;uint32&quot;}],&quot;payable&quot;:false,&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;}]')

def get_price(token, decimals, pair_contract, is_reversed, is_price_in_peg):
    peg_reserve = 0
    token_reserve = 0
    (reserve0, reserve1, blockTimestampLast) = pair_contract.functions.getReserves().call()
    
    if is_reversed:
        peg_reserve = reserve0
        token_reserve = reserve1
    else:
        peg_reserve = reserve1
        token_reserve = reserve0
    
    if token_reserve and peg_reserve:
        if is_price_in_peg:
            # CALCULATE PRICE BY TOKEN PER PEG
            price = (Decimal(token_reserve) / 10 ** decimals) / (Decimal(peg_reserve) / ETHER)
        else:
            # CALCULATE PRICE BY PEG PER TOKEN
            price = (Decimal(peg_reserve) / ETHER) / (Decimal(token_reserve) / 10 ** decimals)
        
        return price
        
    return Decimal('0')


if __name__ == '__main__':
    CAKE_FACTORY_V2 = web3.eth.contract(address=CAKE_ROUTER_V2, abi=ABI).functions.factory().call()

    token = web3.toChecksumAddress('0x126f5f2a88451d24544f79d11f869116351d46e1')
    pair = web3.eth.contract(address=CAKE_FACTORY_V2, abi=ABI).functions.getPair(token, WBNB).call()
    pair_contract = web3.eth.contract(address=pair, abi=ABI)
    is_reversed = pair_contract.functions.token0().call() == WBNB
    decimals = web3.eth.contract(address=token, abi=ABI).functions.decimals().call()
    is_price_in_peg = True

    print(get_price(token, decimals, pair_contract, is_reversed, is_price_in_peg), 'BNB')


</code></pre>
<p>And for JS use:</p>
<pre class="lang-js prettyprint-override"><code>var ETHER = Math.pow(10, 18);

var WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
var CAKE_ROUTER_V2 = Web3.utils.toChecksumAddress('0x10ed43c718714eb63d5aa57b78b54704e256024e');

var web3 = new Web3('https://bsc-dataseed1.binance.org:443');

var ABI = [{&quot;inputs&quot;:[],&quot;name&quot;:&quot;decimals&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;uint256&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;uint256&quot;}],&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;},{&quot;constant&quot;:true,&quot;inputs&quot;:[],&quot;name&quot;:&quot;token0&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;}],&quot;payable&quot;:false,&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;},{&quot;inputs&quot;:[],&quot;name&quot;:&quot;factory&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;}],&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;},{&quot;constant&quot;:true,&quot;inputs&quot;:[{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;},{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;}],&quot;name&quot;:&quot;getPair&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;address&quot;,&quot;name&quot;:&quot;&quot;,&quot;type&quot;:&quot;address&quot;}],&quot;payable&quot;:false,&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;},{&quot;constant&quot;:true,&quot;inputs&quot;:[],&quot;name&quot;:&quot;getReserves&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;uint112&quot;,&quot;name&quot;:&quot;_reserve0&quot;,&quot;type&quot;:&quot;uint112&quot;},{&quot;internalType&quot;:&quot;uint112&quot;,&quot;name&quot;:&quot;_reserve1&quot;,&quot;type&quot;:&quot;uint112&quot;},{&quot;internalType&quot;:&quot;uint32&quot;,&quot;name&quot;:&quot;_blockTimestampLast&quot;,&quot;type&quot;:&quot;uint32&quot;}],&quot;payable&quot;:false,&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;}];


var get_price = async function(token, decimals, pair_contract, is_reverse, is_price_in_peg) {
    var price,
        peg_reserve = 0,
        token_reserve = 0,
        res = await pair_contract.methods.getReserves().call(),
        reserve0 = res[0],
        reserve1 = res[1];
        
    if (is_reverse) {
        peg_reserve = reserve0;
        token_reserve = reserve1;
    } else {
        peg_reserve = reserve1;
        token_reserve = reserve0;
    }
    
    if (token_reserve &amp;&amp; peg_reserve) {
        if (is_price_in_peg) {
            // CALCULATE PRICE BY TOKEN PER PEG
            price = (Number(token_reserve) / Number(Math.pow(10, decimals))) / (Number(peg_reserve) / Number(ETHER));
        } else {
            // CALCULATE PRICE BY PEG PER TOKEN
            price = (Number(peg_reserve) / Number(ETHER)) / (Number(token_reserve) / Number(Math.pow(10, decimals)));
        }
            
        return price;
    }
    
    return Number(0);
};


var token = Web3.utils.toChecksumAddress('0x126f5f2a88451d24544f79d11f869116351d46e1');
var pair = await (await (new web3.eth.Contract(ABI, CAKE_FACTORY_V2))).methods.getPair(token, WBNB).call();
var pair_contract = await new web3.eth.Contract(ABI, pair);
var is_reversed = (await pair_contract.methods.token0().call()) == WBNB;
var decimals = await (await new web3.eth.Contract(ABI, token)).methods.decimals().call();
var is_price_in_peg = true;

console.log(await get_price(token, decimals, pair_contract, is_reversed, is_price_in_peg), 'BNB')
</code></pre>
<p>NOTE 1: This applies only for tokens that have liquidity against WBNB. If the liquidity is against some other coin, you have to recursively understand all the prices in that chain and correlate them one to another until you reach WBNB (or any other PEG in other networks).</p>
<p>NOTE 2. According to <a href="https://arxiv.org/pdf/2009.14021.pdf" rel="nofollow noreferrer">https://arxiv.org/pdf/2009.14021.pdf</a> :</p>
<blockquote>
<p>Expected Execution Price (E[P]): When a liquidity
taker issues a trade on X/Y , the taker wishes to
execute the trade with the expected execution price
E[P] (based on the AMM algorithm and X/Y state),
given the expected slippage.</p>
</blockquote>
<blockquote>
<p>Execution Price (P): During the time difference between a liquidity taker issuing a transaction, and the
transaction being executed (e.g. mined in a block),
the state of the AMM market X/Y may change.
This state change may induce unexpected slippage
resulting in an execution price P != E[P].</p>
</blockquote>
<blockquote>
<p>Unexpected Price Slippage (P − E[P]): is the difference between P and E[P].</p>
</blockquote>
<blockquote>
<p>Unexpected Slippage Rate ((P − E[P]) / E[P]): is the
unexpected slippage over the expected price.</p>
</blockquote>
<p>So in our situation, <code>E[P]</code> is the result of our <code>get_price()</code> and <code>P</code> is the result from <code>getAmounsOut()</code> of an amount (1Kwei for example) divided by the amount we provided (1K in this example) and thus we can even calculate the slippage by eventually subtracting <code>P − E[P]</code></p>
Discrepancy in web3.contract.functions.getAmount()
<p>I'm using Web3.py and I'm experiencing something strange.</p>
<p>For the following code (with Pancake Router V2):</p>
<pre class="lang-py prettyprint-override"><code>from web3 import Web3
from web3.middleware import geth_poa_middleware

web3 = Web3(Web3.HTTPProvider('https://bsc-dataseed1.binance.org:443'))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)

ABI = {&quot;inputs&quot;:[{&quot;internalType&quot;:&quot;uint256&quot;,&quot;name&quot;:&quot;amountIn&quot;,&quot;type&quot;:&quot;uint256&quot;},{&quot;internalType&quot;:&quot;address[]&quot;,&quot;name&quot;:&quot;path&quot;,&quot;type&quot;:&quot;address[]&quot;}],&quot;name&quot;:&quot;getAmountsOut&quot;,&quot;outputs&quot;:[{&quot;internalType&quot;:&quot;uint256[]&quot;,&quot;name&quot;:&quot;amounts&quot;,&quot;type&quot;:&quot;uint256[]&quot;}],&quot;stateMutability&quot;:&quot;view&quot;,&quot;type&quot;:&quot;function&quot;}

CAKE_ROUTER_V2 = web3.toChecksumAddress('0x10ed43c718714eb63d5aa57b78b54704e256024e')
router_contract = web3.eth.contract(address=CAKE_ROUTER_V2, abi=ABI),

WBNB = web3.toChecksumAddress('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c')
CAKE = web3.toChecksumAddress('0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82')
KONGSHIBA = web3.toChecksumAddress('0x126f5f2a88451d24544f79d11f869116351d46e1')

print(router_contract.functions.getAmountsOut(1, [WBNB, CAKE]).call())
print(router_contract.functions.getAmountsOut(1, [WBNB, KONGSHIBA]).call())
</code></pre>
<p>And I'm getting the following:</p>
<pre><code>[1, 19]
[1, 160]
</code></pre>
<p>WBNB and CAKE have 18 decimals and KONGSHIBA has 17.<br>
While CAKE's worth is currently about $27.7, WBNB is $545.41291093<br>and KONGSHIBA is $0.00000000000000000332.<br>
So I should have got back:</p>
<pre><code>[1, 19]
[1, 16000000000000000000]
</code></pre>
<p>Please advise.</p>