使用 web3js 在 uniswap 和 sushiswap 交易所获得硬币价格而不使用他们的 api
Posted
技术标签:
【中文标题】使用 web3js 在 uniswap 和 sushiswap 交易所获得硬币价格而不使用他们的 api【英文标题】:Using web3js get coin price on uniswap and sushiswap exchange without using their api 【发布时间】:2021-11-12 22:29:26 【问题描述】:我想通过 web.js 获取 uniswap 和 sushiswap 交易所的代币价格
我是加密开发世界的新手,所以请尽可能详细说明,如果可能的话,一个例子将是救命稻草。
这是我能拍到的最好的镜头……但不知道如何获得V3_pool_ABI
的价值
const JSBI = require('jsbi');
const Web3 = require('web3');
(async () =>
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/d5658a9beace4cd98d2c1216cf66b56a'));
var factoryV3 = new web3.eth.Contract(
["inputs":[],"stateMutability":"nonpayable","type":"constructor","anonymous":false,"inputs":["indexed":true,"internalType":"uint24","name":"fee","type":"uint24","indexed":true,"internalType":"int24","name":"tickSpacing","type":"int24"],"name":"FeeAmountEnabled","type":"event","anonymous":false,"inputs":["indexed":true,"internalType":"address","name":"oldOwner","type":"address","indexed":true,"internalType":"address","name":"newOwner","type":"address"],"name":"OwnerChanged","type":"event","anonymous":false,"inputs":["indexed":true,"internalType":"address","name":"token0","type":"address","indexed":true,"internalType":"address","name":"token1","type":"address","indexed":true,"internalType":"uint24","name":"fee","type":"uint24","indexed":false,"internalType":"int24","name":"tickSpacing","type":"int24","indexed":false,"internalType":"address","name":"pool","type":"address"],"name":"PoolCreated","type":"event","inputs":["internalType":"address","name":"tokenA","type":"address","internalType":"address","name":"tokenB","type":"address","internalType":"uint24","name":"fee","type":"uint24"],"name":"createPool","outputs":["internalType":"address","name":"pool","type":"address"],"stateMutability":"nonpayable","type":"function","inputs":["internalType":"uint24","name":"fee","type":"uint24","internalType":"int24","name":"tickSpacing","type":"int24"],"name":"enableFeeAmount","outputs":[],"stateMutability":"nonpayable","type":"function","inputs":["internalType":"uint24","name":"","type":"uint24"],"name":"feeAmountTickSpacing","outputs":["internalType":"int24","name":"","type":"int24"],"stateMutability":"view","type":"function","inputs":["internalType":"address","name":"","type":"address","internalType":"address","name":"","type":"address","internalType":"uint24","name":"","type":"uint24"],"name":"getPool","outputs":["internalType":"address","name":"","type":"address"],"stateMutability":"view","type":"function","inputs":[],"name":"owner","outputs":["internalType":"address","name":"","type":"address"],"stateMutability":"view","type":"function","inputs":[],"name":"parameters","outputs":["internalType":"address","name":"factory","type":"address","internalType":"address","name":"token0","type":"address","internalType":"address","name":"token1","type":"address","internalType":"uint24","name":"fee","type":"uint24","internalType":"int24","name":"tickSpacing","type":"int24"],"stateMutability":"view","type":"function","inputs":["internalType":"address","name":"_owner","type":"address"],"name":"setOwner","outputs":[],"stateMutability":"nonpayable","type":"function"],
'0x1F98431c8aD98523631AE4a59f267346ea31F984'
);
var pool_address = await factoryV3.methods.getPool('0x6b175474e89094c44da98b954eedeac495271d0f', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', 3000).call();
var pool_1 = new web3.eth.Contract(V3_pool_ABI, pool_address);
var pool_balance = await pool_1.methods.slot0.call().call();
var sqrtPriceX96 = pool_balance[0];
var number_1 =JSBI.BigInt(sqrtPriceX96 *sqrtPriceX96* (1e18)/(1e18)/JSBI.BigInt(2) ** (JSBI.BigInt(192)));
)();
【问题讨论】:
【参考方案1】:每个池都是根据工厂合同创建的,它们都使用相同的 ABI。
您可以查找任何有效的池地址,例如您的问题中的代码结果为0xC2e9F25Be6257c210d7Adf0D4Cd6E3E881ba25f8,并且其 ABI 已发布。
var V3_pool_ABI = ["inputs":[],"stateMutability":"nonpayable","type":"constructor","anonymous":false,"inputs":["indexed":true,"internalType":"address","name":"owner","type":"address","indexed":true,"internalType":"int24","name":"tickLower","type":"int24","indexed":true,"internalType":"int24","name":"tickUpper","type":"int24","indexed":false,"internalType":"uint128","name":"amount","type":"uint128","indexed":false,"internalType":"uint256","name":"amount0","type":"uint256","indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"],"name":"Burn","type":"event","anonymous":false,"inputs":["indexed":true,"internalType":"address","name":"owner","type":"address","indexed":false,"internalType":"address","name":"recipient","type":"address","indexed":true,"internalType":"int24","name":"tickLower","type":"int24","indexed":true,"internalType":"int24","name":"tickUpper","type":"int24","indexed":false,"internalType":"uint128","name":"amount0","type":"uint128","indexed":false,"internalType":"uint128","name":"amount1","type":"uint128"],"name":"Collect","type":"event","anonymous":false,"inputs":["indexed":true,"internalType":"address","name":"sender","type":"address","indexed":true,"internalType":"address","name":"recipient","type":"address","indexed":false,"internalType":"uint128","name":"amount0","type":"uint128","indexed":false,"internalType":"uint128","name":"amount1","type":"uint128"],"name":"CollectProtocol","type":"event","anonymous":false,"inputs":["indexed":true,"internalType":"address","name":"sender","type":"address","indexed":true,"internalType":"address","name":"recipient","type":"address","indexed":false,"internalType":"uint256","name":"amount0","type":"uint256","indexed":false,"internalType":"uint256","name":"amount1","type":"uint256","indexed":false,"internalType":"uint256","name":"paid0","type":"uint256","indexed":false,"internalType":"uint256","name":"paid1","type":"uint256"],"name":"Flash","type":"event","anonymous":false,"inputs":["indexed":false,"internalType":"uint16","name":"observationCardinalityNextOld","type":"uint16","indexed":false,"internalType":"uint16","name":"observationCardinalityNextNew","type":"uint16"],"name":"IncreaseObservationCardinalityNext","type":"event","anonymous":false,"inputs":["indexed":false,"internalType":"uint160","name":"sqrtPriceX96","type":"uint160","indexed":false,"internalType":"int24","name":"tick","type":"int24"],"name":"Initialize","type":"event","anonymous":false,"inputs":["indexed":false,"internalType":"address","name":"sender","type":"address","indexed":true,"internalType":"address","name":"owner","type":"address","indexed":true,"internalType":"int24","name":"tickLower","type":"int24","indexed":true,"internalType":"int24","name":"tickUpper","type":"int24","indexed":false,"internalType":"uint128","name":"amount","type":"uint128","indexed":false,"internalType":"uint256","name":"amount0","type":"uint256","indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"],"name":"Mint","type":"event","anonymous":false,"inputs":["indexed":false,"internalType":"uint8","name":"feeProtocol0Old","type":"uint8","indexed":false,"internalType":"uint8","name":"feeProtocol1Old","type":"uint8","indexed":false,"internalType":"uint8","name":"feeProtocol0New","type":"uint8","indexed":false,"internalType":"uint8","name":"feeProtocol1New","type":"uint8"],"name":"SetFeeProtocol","type":"event","anonymous":false,"inputs":["indexed":true,"internalType":"address","name":"sender","type":"address","indexed":true,"internalType":"address","name":"recipient","type":"address","indexed":false,"internalType":"int256","name":"amount0","type":"int256","indexed":false,"internalType":"int256","name":"amount1","type":"int256","indexed":false,"internalType":"uint160","name":"sqrtPriceX96","type":"uint160","indexed":false,"internalType":"uint128","name":"liquidity","type":"uint128","indexed":false,"internalType":"int24","name":"tick","type":"int24"],"name":"Swap","type":"event","inputs":["internalType":"int24","name":"tickLower","type":"int24","internalType":"int24","name":"tickUpper","type":"int24","internalType":"uint128","name":"amount","type":"uint128"],"name":"burn","outputs":["internalType":"uint256","name":"amount0","type":"uint256","internalType":"uint256","name":"amount1","type":"uint256"],"stateMutability":"nonpayable","type":"function","inputs":["internalType":"address","name":"recipient","type":"address","internalType":"int24","name":"tickLower","type":"int24","internalType":"int24","name":"tickUpper","type":"int24","internalType":"uint128","name":"amount0Requested","type":"uint128","internalType":"uint128","name":"amount1Requested","type":"uint128"],"name":"collect","outputs":["internalType":"uint128","name":"amount0","type":"uint128","internalType":"uint128","name":"amount1","type":"uint128"],"stateMutability":"nonpayable","type":"function","inputs":["internalType":"address","name":"recipient","type":"address","internalType":"uint128","name":"amount0Requested","type":"uint128","internalType":"uint128","name":"amount1Requested","type":"uint128"],"name":"collectProtocol","outputs":["internalType":"uint128","name":"amount0","type":"uint128","internalType":"uint128","name":"amount1","type":"uint128"],"stateMutability":"nonpayable","type":"function","inputs":[],"name":"factory","outputs":["internalType":"address","name":"","type":"address"],"stateMutability":"view","type":"function","inputs":[],"name":"fee","outputs":["internalType":"uint24","name":"","type":"uint24"],"stateMutability":"view","type":"function","inputs":[],"name":"feeGrowthGlobal0X128","outputs":["internalType":"uint256","name":"","type":"uint256"],"stateMutability":"view","type":"function","inputs":[],"name":"feeGrowthGlobal1X128","outputs":["internalType":"uint256","name":"","type":"uint256"],"stateMutability":"view","type":"function","inputs":["internalType":"address","name":"recipient","type":"address","internalType":"uint256","name":"amount0","type":"uint256","internalType":"uint256","name":"amount1","type":"uint256","internalType":"bytes","name":"data","type":"bytes"],"name":"flash","outputs":[],"stateMutability":"nonpayable","type":"function","inputs":["internalType":"uint16","name":"observationCardinalityNext","type":"uint16"],"name":"increaseObservationCardinalityNext","outputs":[],"stateMutability":"nonpayable","type":"function","inputs":["internalType":"uint160","name":"sqrtPriceX96","type":"uint160"],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function","inputs":[],"name":"liquidity","outputs":["internalType":"uint128","name":"","type":"uint128"],"stateMutability":"view","type":"function","inputs":[],"name":"maxLiquidityPerTick","outputs":["internalType":"uint128","name":"","type":"uint128"],"stateMutability":"view","type":"function","inputs":["internalType":"address","name":"recipient","type":"address","internalType":"int24","name":"tickLower","type":"int24","internalType":"int24","name":"tickUpper","type":"int24","internalType":"uint128","name":"amount","type":"uint128","internalType":"bytes","name":"data","type":"bytes"],"name":"mint","outputs":["internalType":"uint256","name":"amount0","type":"uint256","internalType":"uint256","name":"amount1","type":"uint256"],"stateMutability":"nonpayable","type":"function","inputs":["internalType":"uint256","name":"","type":"uint256"],"name":"observations","outputs":["internalType":"uint32","name":"blockTimestamp","type":"uint32","internalType":"int56","name":"tickCumulative","type":"int56","internalType":"uint160","name":"secondsPerLiquidityCumulativeX128","type":"uint160","internalType":"bool","name":"initialized","type":"bool"],"stateMutability":"view","type":"function","inputs":["internalType":"uint32[]","name":"secondsAgos","type":"uint32[]"],"name":"observe","outputs":["internalType":"int56[]","name":"tickCumulatives","type":"int56[]","internalType":"uint160[]","name":"secondsPerLiquidityCumulativeX128s","type":"uint160[]"],"stateMutability":"view","type":"function","inputs":["internalType":"bytes32","name":"","type":"bytes32"],"name":"positions","outputs":["internalType":"uint128","name":"liquidity","type":"uint128","internalType":"uint256","name":"feeGrowthInside0LastX128","type":"uint256","internalType":"uint256","name":"feeGrowthInside1LastX128","type":"uint256","internalType":"uint128","name":"tokensOwed0","type":"uint128","internalType":"uint128","name":"tokensOwed1","type":"uint128"],"stateMutability":"view","type":"function","inputs":[],"name":"protocolFees","outputs":["internalType":"uint128","name":"token0","type":"uint128","internalType":"uint128","name":"token1","type":"uint128"],"stateMutability":"view","type":"function","inputs":["internalType":"uint8","name":"feeProtocol0","type":"uint8","internalType":"uint8","name":"feeProtocol1","type":"uint8"],"name":"setFeeProtocol","outputs":[],"stateMutability":"nonpayable","type":"function","inputs":[],"name":"slot0","outputs":["internalType":"uint160","name":"sqrtPriceX96","type":"uint160","internalType":"int24","name":"tick","type":"int24","internalType":"uint16","name":"observationIndex","type":"uint16","internalType":"uint16","name":"observationCardinality","type":"uint16","internalType":"uint16","name":"observationCardinalityNext","type":"uint16","internalType":"uint8","name":"feeProtocol","type":"uint8","internalType":"bool","name":"unlocked","type":"bool"],"stateMutability":"view","type":"function","inputs":["internalType":"int24","name":"tickLower","type":"int24","internalType":"int24","name":"tickUpper","type":"int24"],"name":"snapshotCumulativesInside","outputs":["internalType":"int56","name":"tickCumulativeInside","type":"int56","internalType":"uint160","name":"secondsPerLiquidityInsideX128","type":"uint160","internalType":"uint32","name":"secondsInside","type":"uint32"],"stateMutability":"view","type":"function","inputs":["internalType":"address","name":"recipient","type":"address","internalType":"bool","name":"zeroForOne","type":"bool","internalType":"int256","name":"amountSpecified","type":"int256","internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160","internalType":"bytes","name":"data","type":"bytes"],"name":"swap","outputs":["internalType":"int256","name":"amount0","type":"int256","internalType":"int256","name":"amount1","type":"int256"],"stateMutability":"nonpayable","type":"function","inputs":["internalType":"int16","name":"","type":"int16"],"name":"tickBitmap","outputs":["internalType":"uint256","name":"","type":"uint256"],"stateMutability":"view","type":"function","inputs":[],"name":"tickSpacing","outputs":["internalType":"int24","name":"","type":"int24"],"stateMutability":"view","type":"function","inputs":["internalType":"int24","name":"","type":"int24"],"name":"ticks","outputs":["internalType":"uint128","name":"liquidityGross","type":"uint128","internalType":"int128","name":"liquidityNet","type":"int128","internalType":"uint256","name":"feeGrowthOutside0X128","type":"uint256","internalType":"uint256","name":"feeGrowthOutside1X128","type":"uint256","internalType":"int56","name":"tickCumulativeOutside","type":"int56","internalType":"uint160","name":"secondsPerLiquidityOutsideX128","type":"uint160","internalType":"uint32","name":"secondsOutside","type":"uint32","internalType":"bool","name":"initialized","type":"bool"],"stateMutability":"view","type":"function","inputs":[],"name":"token0","outputs":["internalType":"address","name":"","type":"address"],"stateMutability":"view","type":"function","inputs":[],"name":"token1","outputs":["internalType":"address","name":"","type":"address"],"stateMutability":"view","type":"function"];
【讨论】:
Petr Hejda 你知道吗,如何分别获取 uniswap 和 sushiswap 的值 @xyzxyz 您当前的代码使用的是硬编码的 Uniswap 工厂地址。您需要使用 Sushiswap 工厂运行它,这将生成一个不同的对合约地址。然后,您只需从对合约发布的数据中复制粘贴 ABI。 Petr Hejda 嘿,我是新手,你能帮我举一个关于 uniswap 和 sushiswap 上的任何单一硬币价格的例子吗?这真的会帮助我学习所有这些......这意味着非常感谢!以上是关于使用 web3js 在 uniswap 和 sushiswap 交易所获得硬币价格而不使用他们的 api的主要内容,如果未能解决你的问题,请参考以下文章
如何创建没有 .lnk 扩展名的链接?就像我有文件“C\Test\sus.jpg”一样,我可以使用路径“C\Test\sus.jpg”创建快捷方式吗?
解析 DeFi 项目之Uniswap(二): 如何使用Uniswap?