From b9d707f328bfd88b896f8342f0b1578519001514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= <998369+prevostc@users.noreply.github.com> Date: Sun, 30 Jun 2024 21:51:08 +0200 Subject: [PATCH] Fix strategy prices decoding (#22) * Fix strategy prices decoding * format --- src/clm/utils/clm-data.ts | 44 ++++++++++++++++++----------------- src/common/utils/multicall.ts | 3 ++- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/clm/utils/clm-data.ts b/src/clm/utils/clm-data.ts index 8639bf2..06b7528 100644 --- a/src/clm/utils/clm-data.ts +++ b/src/clm/utils/clm-data.ts @@ -125,27 +125,27 @@ export function fetchCLMData(clm: CLM): CLMData { // map multicall indices to variables const results = multicall(calls) - const totalSupplyRes = results[0] - const balanceRes = results[1] - const balanceOfPoolRes = results[2] - const priceRes = results[3] - const rangeRes = results[4] - const rewardPoolTotalSupplyRes = results[5] - const priceFeedRes = results[6] - let idxAfterRefresh = 6 + tokensToRefresh.length - const token0ToNativePriceRes = results[idxAfterRefresh + 1] - const token1ToNativePriceRes = results[idxAfterRefresh + 2] + + let idx = 0 + const totalSupplyRes = results[idx++] + const balanceRes = results[idx++] + const balanceOfPoolRes = results[idx++] + const priceRes = results[idx++] + const rangeRes = results[idx++] + const rewardPoolTotalSupplyRes = results[idx++] + const priceFeedRes = results[idx++] + for (let i = 0; i < tokensToRefresh.length; i++) { + idx++ + } + const token0ToNativePriceRes = results[idx++] + const token1ToNativePriceRes = results[idx++] const rewardTokenOutputAmountsRes = new Array() - let start = idxAfterRefresh + 3 - let end = start + rewardTokens.length - for (let i = start; i < end; i++) { - rewardTokenOutputAmountsRes.push(results[i]) + for (let i = 0; i < rewardTokens.length; i++) { + rewardTokenOutputAmountsRes.push(results[idx++]) } - start = end - end = start + outputTokens.length const outputTokenOutputAmountsRes = new Array() - for (let i = start; i < end; i++) { - outputTokenOutputAmountsRes.push(results[i]) + for (let i = 0; i < outputTokens.length; i++) { + outputTokenOutputAmountsRes.push(results[idx++]) } // extract the data @@ -180,9 +180,10 @@ export function fetchCLMData(clm: CLM): CLMData { log.error("Failed to fetch balanceOfPool for CLM {}", [clm.id.toHexString()]) } - // price is the amount of token1 per token0, expressed with 36 decimals + // price is the amount of token1 per token0, expressed with token0+token1 decimals + const priceDecimals = token0.decimals.plus(token1.decimals) + // this can revert when the liquidity is 0 - const priceDecimals = BigInt.fromU32(36) let priceOfToken0InToken1 = ZERO_BI if (!priceRes.reverted) { priceOfToken0InToken1 = changeValueEncoding(priceRes.value.toBigInt(), priceDecimals, token1.decimals) @@ -197,6 +198,7 @@ export function fetchCLMData(clm: CLM): CLMData { if (!rangeRes.reverted) { const range = rangeRes.value.toTuple() priceRangeMin1 = changeValueEncoding(range[0].toBigInt(), priceDecimals, token1.decimals) + priceRangeMin1 = changeValueEncoding(range[0].toBigInt(), priceDecimals, token1.decimals) priceRangeMax1 = changeValueEncoding(range[1].toBigInt(), priceDecimals, token1.decimals) } else { log.warning("Failed to fetch price range for CLM {}", [clm.id.toHexString()]) @@ -260,7 +262,7 @@ export function fetchCLMData(clm: CLM): CLMData { // only some clms have a reward pool token let rewardPoolTotalSupply = ZERO_BI - if (rewardPoolTotalSupplyRes != null) { + if (hasRewardPool) { if (!rewardPoolTotalSupplyRes.reverted) { rewardPoolTotalSupply = rewardPoolTotalSupplyRes.value.toBigInt() } else { diff --git a/src/common/utils/multicall.ts b/src/common/utils/multicall.ts index 040ff11..7ef3799 100644 --- a/src/common/utils/multicall.ts +++ b/src/common/utils/multicall.ts @@ -80,7 +80,8 @@ export function multicall(callParams: Array): Array