使用Uniswap SDK获取现货价格时触发LOK错误求助
解决Uniswap V3 Quoter调用quoteExactInputSingle时的"LOK"错误
问题背景
创建两个ERC20测试代币后,通过UniswapV3Factory的createPool()方法创建资金池并获取池地址,调用Quoter合约的quoteExactInputSingle()获取现货价格时触发回滚错误,错误reason为"LOK"。
调用代码
const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle( immutables.token0, immutables.token1, immutables.fee, amountIn, 0 )
错误信息
Error: call revert exception; VM Exception while processing transaction: reverted with reason string "LOK" [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="quoteExactInputSingle(address,address,uint24,uint256,uint160)", data="0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034c4f4b0000000000000000000000000000000000000000000000000000000000", errorArgs=["LOK"], errorName="Error", errorSignature="Error(string)", reason="LOK", code=CALL_EXCEPTION, version=abi/5.7.0) at Logger.makeError (/Users/apple/Desktop/solidity/deploy/node_modules/@ethersproject/contracts/lib/index.js:20:58) at processTicksAndRejections (node:internal/process/task_queues:96:5) { reason: 'LOK', code: 'CALL_EXCEPTION', method: 'quoteExactInputSingle(address,address,uint24,uint256,uint160)', data: '0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034c4f4b0000000000000000000000000000000000000000000000000000000000', errorArgs: [ 'LOK' ], errorName: 'Error', errorSignature: 'Error(string)', address: '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6', args: [ '<Token-1-Address>', '<Token-2-Address>', 500, BigNumber { _hex: '0x0de0b6b3a7640000', _isBigNumber: true }, 0 ], transaction: { data: '0xf7729d4300000000000000000000000008a2e53a8ddd2dd1d895c18928fc63778d97a55a0000000000000000000000006d7a02e23505a74143199abb5fb07e6ea20c6d63000000000000000000000000000000000000000000000000000000000000001f40000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000000', to: '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6' } }
错误原因
"LOK"是Uniswap V3 Quoter合约的内置错误码,全称是Liquidity Overflow Killed,触发场景通常为:
- 刚创建的资金池未添加任何流动性,计算价格时无法获取有效储备数据,引发数值溢出。
- 传入的
amountIn数值过大,超出当前池内流动性支持的计算范围。
解决步骤
- 添加初始流动性:必须先给资金池注入流动性,可通过UniswapV3Pool的
mint()方法,或使用NonfungiblePositionManager合约完成操作,确保池内有足够代币储备。 - 调整amountIn数值:测试时使用合理的代币数量,避免输入的
amountIn远大于池内储备量,防止计算溢出。 - 校验参数一致性:
- 确认
token0和token1的顺序与资金池实际排序一致(Uniswap V3会按代币地址字典序自动确定token0和token1,并非创建池时的传入顺序)。 - 确保
fee参数与创建池时设置的手续费率完全匹配(比如代码中500对应0.05%手续费,需确认池的手续费确实为该值)。
- 确认
内容的提问来源于stack exchange,提问作者Akash Kumar
相关产品推荐
相关产品推荐

