值得信赖的区块链资讯!
价格预言机的使用总结(二):UniswapV2篇
前言
该系列的前一篇文章介绍了 Chainlink 价格预言机的使用,其目前也被大部分 DeFi 应用所使用,但依然存在局限性。首先是所支持的 Token 的覆盖率还不全,尤其是长尾资产,大多还未支持,比如 SHIB,目前只在 BSC 主网有 SHIB/USD 的 Price Feed,而其它网络的都还没有,连 Ethereum 的都还没支持。其次,有些资产的偏差阈值较大,价格更新也比较慢,可能长达十几二十个小时才会更新价格,比如 BNT。
这时候就需要考虑其它价格预言机了,而 UniswapV2 和 UniswapV3 都是不错的选择。
本篇先来聊聊如何使用 UniswapV2 作为价格预言机。
UniswapV2 价格预言机
UniswapV2 使用的价格预言机称为 TWAP(Time-Weighted Average Price),即时间加权平均价格。不同于链下聚合的 Chainlink 取自多个不同交易所的数据作为数据源,TWAP 的数据源来自于 Uniswap 自身的交易数据,价格的计算也都是在链上执行的,因此,TWAP 属于链上预言机。
TWAP 的原理比较简单,首先,在 UniswapV2Pair 合约中,会存储两个变量 price0CumulativeLast 和 price1CumulativeLast,在 _update() 函数中会更新这两个变量,其相关代码如下:
contract UniswapV2Pair { … uint32 private blockTimestampLast; uint public price0CumulativeLast; uint public price1CumulativeLast; … // update reserves and, on the first call per block, price accumulators function _update(uint balance0, uint balance1, uint112 _reserve0, uint112 _reserve1) private { … uint32 blockTimestamp = uint32(block.timestamp % 2**32); uint32 timeElapsed = blockTimestamp – blockTimestampLast; if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) { // * never overflows, and + overflow is desired price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed; price1CumulativeLast += uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) * timeElapsed; } blockTimestampLast = blockTimestamp; … }}
price0CumulativeLast 和 price1CumulativeLast 分别记录了 token0 和 token1 的累计价格。所谓累计价格,其代表的是整个合约历史中每一秒的 Uniswap 价格总和。且只会在每个区块第一笔交易时执行累加计算,累加的值不是当前区块的第一笔交易的价格,而是在这之前的最后一笔交易的价格,所以至少也是上个区块的价格。取自之前区块的价格,可以大大提高操控价格的成本,所以自然也提高了安全性。
如上图所示,合约的第一个区块为 Block 122,这时候,价格和时间差都为 0,所以累计价格也为 0。到了下一个区块 Block 123,这时候取自上个区块的最后一口价格 10.2,且经过的时间差为 7,因此就可以计算出累计价格 priceCumulative = 10.2 * 7 = 71.4。再到下个区块 Block 124,取自上一口价格 10.3,两个区块间的时间差为 8,那此时的累计价格就变成了 71.4 + (10.3 *
= 153.8。Block 125 的时候也同理,上口价格为 10.5,区块时间差为 5,所以最新的累计价格就变成了 153.8 + (10.5 * 5) = 206.3。
有了这个基础之后,就可以计算 TWAP 了。
固定时间窗口 TWAP
计算 TWAP 的原理也是非常简单,如上图所示,这是计算时间间隔为 1 小时的 TWAP,取自开始和结束时的累计价格和两区块当时的时间戳,两者的累计价格相减,再除以两者之间的时间差,就算出这 1 小时内的 TWAP 价格了。
这是 TWAP 最简单的计算方式,也称为固定时间窗口的 TWAP。下面来讲讲具体如何实现。
Uniswap 官方也有提供了一个示例代码来计算固定时间窗口的 TWAP,其代码放在 v2-periphery 项目中:
-
https://github.com/Uniswap/v2-periphery/blob/master/contracts/examples/ExampleOracleSimple.sol
该示例代码也比较简单,我们直接贴上代码看看:
pragma solidity =0.6.6;import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol';import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol';import '@uniswap/lib/contracts/libraries/FixedPoint.sol';import '../libraries/UniswapV2OracleLibrary.sol';import '../libraries/UniswapV2Library.sol';// fixed window oracle that recomputes the average price for the entire period once every period// note that the price average is only guaranteed to be over at least 1 period, but may be over a longer periodcontract ExampleOracleSimple { using FixedPoint for *; uint public constant PERIOD = 24 hours; IUniswapV2Pair immutable pair; address public immutable token0; address public immutable token1; uint public price0CumulativeLast; uint public price1CumulativeLast; uint32 public blockTimestampLast; FixedPoint.uq112x112 public price0Average; FixedPoint.uq112x112 public price1Average; constructor(address factory, address tokenA, address tokenB) public { IUniswapV2Pair _pair = IUniswapV2Pair(UniswapV2Library.pairFor(factory, tokenA, tokenB)); pair = _pair; token0 = _pair.token0(); token1 = _pair.token1(); price0CumulativeLast = _pair.price0CumulativeLast(); // fetch the current accumulated price value (1 / 0) price1CumulativeLast = _pair.price1CumulativeLast(); // fetch the current accumulated price value (0 / 1) uint112 reserve0; uint112 reserve1; (reserve0, reserve1, blockTimestampLast) = _pair.getReserves(); require(reserve0 != 0 && reserve1 != 0, 'ExampleOracleSimple: NO_RESERVES'); // ensure that there's liquidity in the pair } function update() external { (uint price0Cumulative, uint price1Cumulative, uint32 blockTimestamp) = UniswapV2OracleLibrary.currentCumulativePrices(address(pair)); uint32 timeElapsed = blockTimestamp – blockTimestampLast; // overflow is desired // ensure that at least one full period has passed since the last update require(timeElapsed >= PERIOD, 'ExampleOracleSimple: PERIOD_NOT_ELAPSED'); // overflow is desired, casting never truncates // cumulative price is in (uq112x112 price * seconds) units so we simply wrap it after division by time elapsed price0Average = FixedPoint.uq112x112(uint224((price0Cumulative – price0CumulativeLast) / timeElapsed)); price1Average = FixedPoint.uq112x112(uint224((price1Cumulative – price1CumulativeLast) / timeElapsed)); price0CumulativeLast = price0Cumulative; price1CumulativeLast = price1Cumulative; blockTimestampLast = blockTimestamp; } // note this will always return 0 before update has been called successfully for the first time. function consult(address token, uint amountIn) external view returns (uint amountOut) { if (token == token0) { amountOut = price0Average.mul(amountIn).decode144(); } else { require(token == token1, 'ExampleOracleSimple: INVALID_TOKEN'); amountOut = price1Average.mul(amountIn).decode144(); } }}
PERIOD 指定为了 24 小时,说明这个示例计算 TWAP 的固定时间窗口为 24 小时,即每隔 24 小时才更新一次价格。
该示例也只保存一个交易对的价格,即 token0-token1 的价格。price0Average 和 price1Average 分别就是 token0 和 token1 的 TWAP 价格。比如,token0 为 WETH,token1 为 USDC,那 price0Average 就是 WETH 对 USDC 的价格,而 price1Average 则是 USDC 对 WETH 的价格。
update() 函数就是更新 TWAP 价格的函数,这一般需要链下程序的定时任务来触发,按照这个示例的话,就是链下的定时任务需要每隔 24 小时就定时触发调用 update() 函数。
update() 函数的实现逻辑也和上面所述的公式一致:
-
读取出当前最新的累计价格和当前的时间戳;
-
计算出当前时间和上一次更新价格时的时间差 timeElapsed,要求该时间差需要达 24 小时;
-
根据公式 TWAP = (priceCumulative – priceCumulativeLast) / timeElapsed 计算得到最新的 TWAP,即 priceAverage;
-
更新 priceCumulativeLast 和 blockTimestampLast 为当前最新的累计价格和时间戳。
不过,有一点需要注意,因为 priceCumulative 本身计算存储时是做了左移 112 位的操作的,所以计算所得的 priceAverage 也是左移了 112 位的。
consult() 函数则可查询出用 TWAP 价格计算可兑换的数量。比如,token0 为 WETH,token1 为 USDC,假设 WETH 的价格为 3000 USDC,查询 consult() 时,若传入的参数 token 为 token0 的地址,amountIn 为 2,那输出的 amountOut 则为 3000 * 2 = 6000,可理解为若支付 2 WETH,就可根据价格换算成 6000 USDC。
滑动时间窗口 TWAP
固定时间窗口 TWAP 的原理和实现,比较简单,但其最大的不足就是价格变化不够平滑,时间窗口越长,价格变化就可能会越陡峭。因此,在实际应用中,更多其实是用滑动时间窗口的 TWAP。
所谓滑动时间窗口 TWAP,就是说,计算 TWAP 的时间窗口并非固定的,而是滑动的。这种算法的主要原理就是将时间窗口划分为多个时间片段,每过一个时间片段,时间窗口就会往右滑动一格,如下图所示:
上图所示的时间窗口为 1 小时,划分为了 6 个时间片段,每个时间片段则为 10 分钟。那每过 10 分钟,整个时间窗口就会往右滑动一格。而计算 TWAP 时的公式则没有变,依然还是取自时间窗口的起点和终点。如果时间窗口为 24 小时,按照固定时间窗口算法,每隔 24 小时 TWAP 价格才会更新,但使用滑动时间窗口算法后,假设时间片段为 1 小时,则 TWAP 价格是每隔 1 小时就会更新。
Uniswap 官方也同样提供了这种滑动时间窗口 TWAP 实现的示例代码,其 Github 地址为:
-
https://github.com/Uniswap/v2-periphery/blob/master/contracts/examples/ExampleSlidingWindowOracle.sol
我们也贴上代码看看:
pragma solidity =0.6.6;import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol';import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol';import '@uniswap/lib/contracts/libraries/FixedPoint.sol';import '../libraries/SafeMath.sol';import '../libraries/UniswapV2Library.sol';import '../libraries/UniswapV2OracleLibrary.sol';// sliding window oracle that uses observations collected over a window to provide moving price averages in the past// `windowSize` with a precision of `windowSize / granularity`// note this is a singleton oracle and only needs to be deployed once per desired parameters, which// differs from the simple oracle which must be deployed once per pair.contract ExampleSlidingWindowOracle { using FixedPoint for *; using SafeMath for uint; struct Observation { uint timestamp; uint price0Cumulative; uint price1Cumulative; } address public immutable factory; // the desired amount of time over which the moving average should be computed, e.g. 24 hours uint public immutable windowSize; // the number of observations stored for each pair, i.e. how many price observations are stored for the window. // as granularity increases from 1, more frequent updates are needed, but moving averages become more precise. // averages are computed over intervals with sizes in the range: // [windowSize - (windowSize / granularity) * 2, windowSize] // e.g. if the window size is 24 hours, and the granularity is 24, the oracle will return the average price for // the period: // [now - [22 hours, 24 hours], now] uint8 public immutable granularity; // this is redundant with granularity and windowSize, but stored for gas savings & informational purposes. uint public immutable periodSize; // mapping from pair address to a list of price observations of that pair mapping(address => Observation[]) public pairObservations; constructor(address factory_, uint windowSize_, uint8 granularity_) public { require(granularity_ > 1, 'SlidingWindowOracle: GRANULARITY'); require( (periodSize = windowSize_ / granularity_) * granularity_ == windowSize_, 'SlidingWindowOracle: WINDOW_NOT_EVENLY_DIVISIBLE' ); factory = factory_; windowSize = windowSize_; granularity = granularity_; } // returns the index of the observation corresponding to the given timestamp function observationIndexOf(uint timestamp) public view returns (uint8 index) { uint epochPeriod = timestamp / periodSize; return uint8(epochPeriod % granularity); } // returns the observation from the oldest epoch (at the beginning of the window) relative to the current time function getFirstObservationInWindow(address pair) private view returns (Observation storage firstObservation) { uint8 observationIndex = observationIndexOf(block.timestamp); // no overflow issue. if observationIndex + 1 overflows, result is still zero. uint8 firstObservationIndex = (observationIndex + 1) % granularity; firstObservation = pairObservations[pair][firstObservationIndex]; } // update the cumulative price for the observation at the current timestamp. each observation is updated at most // once per epoch period. function update(address tokenA, address tokenB) external { address pair = UniswapV2Library.pairFor(factory, tokenA, tokenB); // populate the array with empty observations (first call only) for (uint i = pairObservations[pair].length; i < granularity; i++) { pairObservations[pair].push(); } // get the observation for the current period uint8 observationIndex = observationIndexOf(block.timestamp); Observation storage observation = pairObservations[pair][observationIndex]; // we only want to commit updates once per period (i.e. windowSize / granularity) uint timeElapsed = block.timestamp – observation.timestamp; if (timeElapsed > periodSize) { (uint price0Cumulative, uint price1Cumulative,) = UniswapV2OracleLibrary.currentCumulativePrices(pair); observation.timestamp = block.timestamp; observation.price0Cumulative = price0Cumulative; observation.price1Cumulative = price1Cumulative; } } // given the cumulative prices of the start and end of a period, and the length of the period, compute the average // price in terms of how much amount out is received for the amount in function computeAmountOut( uint priceCumulativeStart, uint priceCumulativeEnd, uint timeElapsed, uint amountIn ) private pure returns (uint amountOut) { // overflow is desired. FixedPoint.uq112x112 memory priceAverage = FixedPoint.uq112x112( uint224((priceCumulativeEnd – priceCumulativeStart) / timeElapsed) ); amountOut = priceAverage.mul(amountIn).decode144(); } // returns the amount out corresponding to the amount in for a given token using the moving average over the time // range [now - [windowSize, windowSize - periodSize * 2], now] // update must have been called for the bucket corresponding to timestamp `now – windowSize` function consult(address tokenIn, uint amountIn, address tokenOut) external view returns (uint amountOut) { address pair = UniswapV2Library.pairFor(factory, tokenIn, tokenOut); Observation storage firstObservation = getFirstObservationInWindow(pair); uint timeElapsed = block.timestamp – firstObservation.timestamp; require(timeElapsed <= windowSize, 'SlidingWindowOracle: MISSING_HISTORICAL_OBSERVATION'); // should never happen. require(timeElapsed >= windowSize – periodSize * 2, 'SlidingWindowOracle: UNEXPECTED_TIME_ELAPSED'); (uint price0Cumulative, uint price1Cumulative,) = UniswapV2OracleLibrary.currentCumulativePrices(pair); (address token0,) = UniswapV2Library.sortTokens(tokenIn, tokenOut); if (token0 == tokenIn) { return computeAmountOut(firstObservation.price0Cumulative, price0Cumulative, timeElapsed, amountIn); } else { return computeAmountOut(firstObservation.price1Cumulative, price1Cumulative, timeElapsed, amountIn); } }}
要实现滑动时间窗口算法,就需要将时间分段,还需要保存每个时间段的 priceCumulative。在这实现的示例代码中,定义了结构体 Observation,用来保存每个时间片段的数据,包括两个 token 的 priceCumulative 和记录的时间点 timestamp。还定义了 pairObservations 用来存储每个 pair 的 Observation 数组,而数组实际的长度取决于将整个时间窗口划分为多少个时间片段。
windowSize 表示时间窗口大小,比如 24 小时,granularity 是划分的时间片段数量,比如 24 段,periodSize 则是每时间片段的大小,比如 1 小时,是由 windowSize / granularity 计算所得。这几个值都在构造函数中进行了初始化。
触发 update() 函数则更新存储最新时间片段的 observation,如时间片段大小为 1 小时,即每隔 1 小时就要触发 update() 函数一次。因为这个示例中是支持多个 pair 的,所以 update() 时需要指定所要更新的两个 token。
而查询当前 TWAP 价格的计算就在 consult() 函数里实现了。首先,先获取到当前时间窗口里的第一个时间片段的 observation,也算出当前时间与第一个 observation 时间的时间差,且读取出当前最新的 priceCumulative,之后就在 computeAmountOut() 函数里计算得到最新的 TWAP 价格 priceAverage,且根据 amountIn 算出了 amountOut 并返回。
总结
本文我们主要介绍了被广泛使用的一种链上预言机 TWAP(时间加权平均价格),且介绍了固定时间窗口和滑点时间窗口两种算法的 TWAP。虽然,TWAP 是由 Uniswap 推出的,但因为很多其他 DEX 也采用了和 Uniswap 一样的底层实现,如 SushiSwap、PancakeSwap 等,所以这些 DEX 也可以用同样的算法计算出对应的 TWAP。
但使用 UniswapV2 的 TWAP,其主要缺陷就是需要链下程序定时触发 update() 函数,存在维护成本。UniswapV3 的 TWAP 则解决了这个问题,下一篇会来聊聊其具体是如何实现的。
比推快讯
更多 >>- Metalpha 从 Kraken 提取 6000 枚 ETH,价值 1867 万美元
- Binance:至少拥有 246 分的用户可申领 40 枚 DN 空投
- 哥伦比亚国家税务总局要求加密交易所提交用户数据以打击逃税
- IMU 代币经济学:16%将分配给早期支持者
- 美元兑日元升至三周高点,现报 157.375
- 韩国政府计划今年制定稳定币监管法案,并引入数字资产现货 ETF
- 数据:过去 24h Binance 净流出 4.33 亿 USDT
- 某鲸鱼做空 397.9 枚 BTC 同时做多 4383 枚 ETH,开启对冲头寸
- 法国再现扳手攻击事件,三名蒙面男子持枪入室抢走存有加密数据的 U 盘
- 某巨鲸向 HyperLiquid 存入 809 万 U 买入 59,458 枚 SOL
- 当前主流 CEX、DEX 资金费率显示市场偏向看空
- 韩国财政部宣布推出数字资产现货 ETF
- 30 次做多屡败屡战巨鲸新建 HYPE 多单,账户浮亏 17.1 万美元
- SEI 链发生 24 万美元闪电贷攻击,用户误转资金成漏洞触发点
- 高盛前瞻非农:数据需大幅意外才能撼动美联储 4 月降息预期
- Zcash 基金会:Zcash 为去中心化开源协议,不受任何单一贡献者或团队控制
- 数据:当前加密恐慌贪婪指数为 26,处于恐慌状态
- WhiteWhale兄弟叙事Meme 币 BlackWhale 大涨 460%,项目方持续买入位居持仓榜首
- Immunefi:IMU 代币将于 1 月 22 日推出
- 12 月主流 CEX 现货交易量环比下降 35%,合约交易量下降 26%
- a16z crypto 研究顾问:2026 年预测市场规模将扩大、覆盖范围更广,且会变得更加智能
- 数据:24 小时资金净流入量 TOP 3 的 CEX 分别为 Binance、Bitget、Bitfinex
- 马斯克预告 Grok Code 重大升级,下月将支持“一键生成复杂代码”
- Morph 启动 1.5 亿美元规模支付加速器计划
- Truebit Protocol:已与执法部门联系处理恶意攻击事件,勿与受影响合约交互
- 彭博社:英伟达支持的 AI 基础设施公司 Nscale 正洽谈 20 亿美元融资
- 以太坊第 172 次共识层开发者会议,BPO2 激活,Blob Target 提升,EIP-8070 纳入 Glamsterdam
- Truebit 代币 TRU 在本次黑客事件中归零
- 数据:GMGN KOL 榜单显示 VVM 备受关注,获取多位 KOL 净流入
- RootData:VANA 将于一周后解锁价值约 613 万美元的代币
- Santiment:ZEC 开发活动已降至 2021 年 11 月以来最低水平
- 昨日美国 Solana 现货 ETF 净流入 1360 万美元
- 曾在 GOAT 获利 207 万美元的交易员花费 10.5 万美元押注伊朗政权将在 1 月 31 日前垮台
- PEPE 最大多头平仓止盈,获利 96.3 万美元
- 中国 2025 年新增 7.5 万家区块链企业,总数近 29 万创历史新高
- 财新:数字人民币与以太坊智能合约不同,微信、支付宝等将陆续获得钱包开立权限
- 某交易员 10 天前买入 84 美元我踏马来了,现浮盈近 10 万美元
- 数据:沪指时隔 10 年再度站上 4100 点
- 数据:Hyperliquid 平台鲸鱼当前持仓 68.6 亿美元,多空持仓比为 0.94
- 以 11 万美元买入 WBTC 的某巨鲸疑似清仓,持仓 3 个月亏损近 750 万美元
- 数据:6503.4 枚 BNB 从 Binance 转出,价值约 578 万美元
- 柬埔寨央行宣布对太子银行启动清算程序
- Zcash 原开发团队 CEO:将推出一款新的 Zcash 钱包,现已开放预申请
- Bitmine 以太坊质押量突破 100 万枚,升至 103.2 万枚
- Cathie Wood:美国政府或将直接买入比特币,充实国家战略储备
- 加密恐慌指数跌至 27,市场再度遇冷
- 马斯克旗下 xAI 季度亏损进一步扩大
- 2025 年稳定币交易量创历史新高,全年达 33 万亿美元,USDC 领跑
- 从 Kaito 多签合约收到代币的地址 8 小时前将 500 万枚 $KAITO 转入 Binance
- 总统亲自QE?特朗普宣布 2000 亿美元抵押贷款债券购买计划
比推专栏
更多 >>观点
比推热门文章
- Binance:至少拥有 246 分的用户可申领 40 枚 DN 空投
- 哥伦比亚国家税务总局要求加密交易所提交用户数据以打击逃税
- IMU 代币经济学:16%将分配给早期支持者
- For Builders, By Builders — 全球 Web3 開發者大會 2026 即将登陸香港
- 美元兑日元升至三周高点,现报 157.375
- 韩国政府计划今年制定稳定币监管法案,并引入数字资产现货 ETF
- 数据:过去 24h Binance 净流出 4.33 亿 USDT
- 某鲸鱼做空 397.9 枚 BTC 同时做多 4383 枚 ETH,开启对冲头寸
- 法国再现扳手攻击事件,三名蒙面男子持枪入室抢走存有加密数据的 U 盘
- 某巨鲸向 HyperLiquid 存入 809 万 U 买入 59,458 枚 SOL
比推 APP



