2

I am looking up getReserves result of a pair contract.

It produces the following result:

Result {
  _reserve0: '3219645034999541693795',
  _reserve1: '86735629384854886759666',
  _blockTimestampLast: '1621295406'
}

This specific pair contract establishes CAKE/BUSD price, i.e. It tells us that 1 CAKE buys USD 26.94 (86735629384854886759666/3219645034999541693795=26.94).

As far as I understand, this is the spot price (current price in the marketplace), and it will not (cannot ?) change until the next block.

Assuming that I am right about everything else, what is _blockTimestampLast for?

5
  • 1
    The price can change. It changes every time a trade was made (even in one block)
    – jjj
    May 18, 2021 at 22:41
  • How does one calculate price change after a trade within a block?
    – Gajus
    May 18, 2021 at 22:42
  • 1
    The EVM works on transactions. It does not care about what block they are in (slightly simplified). The variables of all smart contracts that are involved are updated in the EVM (the pool, both erc20), when the transaction is executed. So the price is always calculated the same way, just by looking at the reserves. To be honest, I don't exactly know about pancakeswap, but about other pool based exchanges. On others the product of both reserves must stay constant when you exchange, this means that you don't exchange for that exact price, but that the price also depends on how much you exchange.
    – jjj
    May 18, 2021 at 22:56
  • 1
    Fixing the price within one block would also allow for massive manipulation of the price, because you can exchange until one reserve is nearly empty without any additional costs. In the next block the price would then be fixed at an absurd level that could be exploited
    – jjj
    May 18, 2021 at 22:59
  • 1
    The price you see outside of the block chain is always the price at the end of one block (because those are the valid units). But inside the blockchain it changes for every transaction.
    – jjj
    May 18, 2021 at 23:04

1 Answer 1

1

This is a direct copy of the Uniswap code, so it's often easier to refer to their documentation.

So for getReserves():

Returns the reserves of token0 and token1 used to price trades and distribute liquidity. See Pricing. Also returns the block.timestamp (mod 2**32) of the last block during which an interaction occured for the pair

4
  • I clearly was sleep deprived yesterday, because even though I read that description several times, I didn't understand it.
    – Gajus
    May 18, 2021 at 14:17
  • So just to clarify, the spot price is deducted token0/token1 and block.timestamp does not play any role in this. It is only there to indicate when the last swap happened.
    – Gajus
    May 18, 2021 at 14:18
  • I guess I got confused because I was reading about merkle proofs and how we can use them to get historic data up to 256 blocks back, and I thought that this value somehow plays a role in that.
    – Gajus
    May 18, 2021 at 14:20
  • _blockTimestampLast is used to work out the time elapsed between updates of the cumulative prices (see line 373 in the contract) - basically to check that some time has actually elapsed. (I'm not sure why they've used block.timestamp instead of block.number.) May 18, 2021 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.