Uniswap学习笔记
简介
v1版本,基于恒定乘积做市商模型(x×y=k)
v2版本,包含Core和Periphery
- Factory 管生(创建池子)
- Pair 管养(存钱、算价、交易)
- Router 管用(让用户方便地操作)
其支持预言机机制,TWAP(Time-Weighted Average Price)时间加权平均价格
UniswapV2Pair
v2交易对,其主要成员有reserve0/reserve1:表示交易对的两种代币的储备金量totalSupply:表示所有用户的LP代币总数量factory:表示创建交易对的工厂合约token0/token1:表示交易对的两种代币合约地址price0CumulativeLast/price1CumulativeLast:表示TWAP预言机的核心变量,是对“边际价格 × 持续时间”进行连续积分,其计算公式为price0CumulativeLast=∑reserve1∗2112reserve0∗Δtprice0CumulativeLast=\sum{\frac{reserve1 * 2^{112}} {reserve0}} * \Delta tprice0CumulativeLast=∑reserve0reserve1∗2112∗Δt
price1CumulativeLast=∑reserve0∗2112reserve1∗Δtprice1CumulativeLast =\sum{\frac{reserve0 * 2^{112}}{reserve1}} * \Delta tprice1CumulativeLast=∑reserve1reserve0∗2112∗Δt
其中Δt=blockTimestampLast−blockTimestampLastlast\Delta t = blockTimestampLast - blockTimestampLast_{last}Δt=blockTimestampLast−blockTimestampLastlastblockTimestampLast:表示上次计算连续积分时的区块时间戳对2322^{32}232的模
关键操作
| 名称 | 说明 |
|---|---|
mint |
增加流动性 |
burn |
减少流动性 |
mint:对于初次增加流动性,其流动性计算公式为amount0∗amount1−103\sqrt{amount0 * amount1} - 10^3amount0∗amount1−103追加情况时计算公式为min{amount0reserve0∗totalSupply,amount1reserve1∗totalSupply}\min \{ \frac{amount0}{reserve0} *totalSupply, \frac{amount1}{reserve1} * totalSupply \}min{reserve0amount0∗totalSupply,reserve1amount1∗totalSupply} 其中amount0/amount1为存入的交易对代币值
如果设置了要收协议费,即factory的feeTo()接收地址不为0,协议费计算公式为k−klast5k+klast∗totalSupply\frac{k - k_{last}}{5k + k_{last}} * totalSupply5k+klastk−klast∗totalSupply 其中k=reserve0∗reserve1k=\sqrt{reserve0 * reserve1}k=reserve0∗reserve1,klastk_{last}klast表示上一次计算的kkk
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)