gpt4 book ai didi

javascript - 解读 Uniswap V3 价格

转载 作者:行者123 更新时间:2023-12-05 05:46:21 36 4
gpt4 key购买 nike

我对 JavaScript 和 Uniswap 还很陌生。我正在使用 Uniswap V3 从 DAI/USDC 池中获取价格。我的“主要”功能如下所示:

async function main() {

const [immutables, state] = await Promise.all([
getPoolImmutables(),
getPoolState(),
]);

const DAI = new Token(1, immutables.token0, 18, "DAI", "Stablecoin");
const USDC = new Token(1, immutables.token1, 6, "USDC", "USD Coin");

const DAI_USDC_POOL = new Pool(
DAI,
USDC,
immutables.fee,
state.sqrtPriceX96.toString(),
state.liquidity.toString(),
state.tick
);

const token0Price = DAI_USDC_POOL.token0Price;

console.log("The price is: ", token0Price);
}

我得到以下输出:

The price is:  Price {
numerator: JSBI(6) [
435696740,
805184612,
508287463,
671994784,
427409972,
4,
sign: false
],
denominator: JSBI(7) [ 0, 0, 0, 0, 0, 0, 4096, sign: false ],
baseCurrency: Token {
chainId: 1,
decimals: 18,
symbol: 'DAI',
name: 'Stablecoin',
isNative: false,
isToken: true,
address: '0x6B175474E89094C44Da98b954EedeAC495271d0F'
},
quoteCurrency: Token {
chainId: 1,
decimals: 6,
symbol: 'USDC',
name: 'USD Coin',
isNative: false,
isToken: true,
address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
},
scalar: Fraction {
numerator: JSBI(2) [ 660865024, 931322574, sign: false ],
denominator: JSBI(1) [ 1000000, sign: false ]
}
}

USDC 的价格似乎有些道理(分母,1000000),但我不确定如何从输出中解释 DAI 的价格。如果有人可以提供任何提示或向我指出解释输出的资源,我将不胜感激。谢谢!

最佳答案

JSBI 代表 JavaScript BigInt :

a pure-JavaScript implementation of the ECMAScript BigInt proposal.

它是一个通过数组表示的大整数。要查看它的十进制形式,请使用 String(jsbi)jsbi.toString() .对于计算,您必须使用模块的方法:

<表类="s-表"><头>数学运算JSBI 操作<正文>加法 c = JSBI.add(a, b) 减法 c = JSBI.subtract(a, b) 乘法 c = JSBI.multiply(a, b)c = JSBI.divide(a, b)

使用你的例子的值我们可以计算

(async function asyncFnOnlyForAwait() {

const { default: JSBI } = await import ('https://cdn.skypack.dev/jsbi')

window.JSBI = JSBI;
window.priceJSBI = {
numerator: [435696740, 805184612, 508287463, 671994784, 427409972, 4,],
denominator: [0, 0, 0, 0, 0, 0, 4096, ],
scalarNumerator: [660865024, 931322574, ],
scalarDenominator: [1000000, ]
};
window.price = {};

// convert arrays to JSBI
for (const [prop, val] of Object.entries(priceJSBI))
priceJSBI[prop] = JSBI.from(val)

// calculate using JSBI methods
priceJSBI.value = JSBI.divide(priceJSBI.numerator, priceJSBI.denominator)
priceJSBI.scalarValue = JSBI.divide(priceJSBI.scalarNumerator, priceJSBI.scalarDenominator)

// convert JSBIs to numbers, losing precicion
for (const [prop, val] of Object.entries(priceJSBI))
price[prop] = JSBI.toNumber(val)

price.valueByNumbers = price.numerator / price.denominator

console.table(price)
document.write('<pre>price = '+JSON.stringify(price, null, 4))

}).call();

// In devtools console on this question, select the right iframe as scope to access these variables
// In Chromium browsers there's a dropdown for JS scopes next to the "console clear" button

奇怪的是,Uniswap 在桌面上发布了 JSBI。它实际上是作为旧版/移动浏览器的后备方案,只会使事情复杂化。 BigInts所有主要浏览器都支持并使用普通算术运算符。 JSBI其实应该是transpiled away对于具有 BigInts 的环境。

关于javascript - 解读 Uniswap V3 价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71209265/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com