0%

Uniswap-getReserves获取储备量

Pre:

简单过一下uniswap的Pair合约的内容,主要了解清楚getReserves函数。

合约结构

根据文档简单过一遍

Event:

  • Mint 创造流动性token时触发

  • Burn 销毁流动性token时触发

  • Swap 交换时触发

  • Sync 储备量被更新时(mint、burn、swap、sync)触发

Read-Only Functions:

  • MINIMUM_LIQUIDITY 常量值1000

  • factory 返回工厂地址

  • token0 排序较低的token地址

  • token1 排序较高的token地址

  • getReserves 返回token0和token1的储备量,用于交易或分配流动性

  • price0CumulativeLast

  • price1CumulativeLast

  • kLast

State-Changing Functions:

  • mint 创建token

  • burn 销毁

  • swap 交换

  • skim

  • sync 同步

getReserves获取储备量

getReserves

1
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);

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.

主要是返回Pair里token0和token1的储备量

token0 & token1

token0

1
function token0() external view returns (address);

Returns the address of the pair token with the lower sort order.

token1

1
function token1() external view returns (address);

Returns the address of the pair token with the higher sort order.

可以在pair的合约里查到token0和token1分别的address

测试储备量的变化:

AddLiquidity:

在添加流动性之前,查询结果

1
2
>  _reserve0   uint112 :  5462692571130940(0.00546269257113094) USDT
> _reserve1 uint112 : 5383891113691435(0.005383891113691435) Token

分别添加流动性,各10个后,pair里的储备量结果发生变化

1
2
>  _reserve0   uint112 :  10151827943605108889(10.151827943605108889) USDT ↑
> _reserve1 uint112 : 10005383891113691435(10.005383891113691435) Token ↑

Sell:

1
2
>  _reserve0   uint112 :  10151827943605108889(10.151827943605108889) USDT
> _reserve1 uint112 : 10005383891113691435(10.005383891113691435) Token

token卖成USDT

1
2
>  _reserve0   uint112 :  9669575752865546229(9.669575752865546229) USDT ↓
> _reserve1 uint112 : 10505383891113691435(10.505383891113691435) Token ↑

token传进pair,token储备量增加
pair流出USDT,USDT储备量减少

Buy:

与Sell行为相反,USDT ↑、Token↓

行为 token0储备量(USDT) token1储备量(Token)
Buy
Sell
AddLiquidity
RemoveLiquidity

Refs: