diff --git a/packages/oraidex-server/package.staging.json b/packages/oraidex-server/package.staging.json index aa5b8b4d..2db91d05 100644 --- a/packages/oraidex-server/package.staging.json +++ b/packages/oraidex-server/package.staging.json @@ -1,6 +1,6 @@ { "name": "@oraichain/oraidex-server-staging", - "version": "1.0.41", + "version": "1.0.42", "main": "dist/index.js", "bin": "dist/index.js", "license": "MIT", diff --git a/packages/oraidex-sync/src/tx-parsing.ts b/packages/oraidex-sync/src/tx-parsing.ts index 321a3b7a..089a5249 100644 --- a/packages/oraidex-sync/src/tx-parsing.ts +++ b/packages/oraidex-sync/src/tx-parsing.ts @@ -216,6 +216,25 @@ export const calculateSwapOpsWithPoolAmount = async (swapOps: SwapOperationData[ } }; +export async function calculateRewardAssetPrice(rewardAssetDenom: string): Promise { + // hardcode price for reward OCH + if (rewardAssetDenom === ORAIXOCH_INFO.token.contract_addr) { + return OCH_PRICE; + } else { + const rewardAssetInfo = parseCw20DenomToAssetInfo(rewardAssetDenom); + return await getPriceAssetByUsdt(rewardAssetInfo); + } +} + +export function calculateEarnAmountInUsdt( + rewardAssetPrice: number, + earnAmount: number, + oraiEarnedAmount: number, + oraiPrice: number +): number { + return rewardAssetPrice * earnAmount + oraiPrice * oraiEarnedAmount; +} + async function extractClaimOperations( txData: BasicTxData, wasmAttributes: (readonly Attribute[])[], @@ -266,15 +285,8 @@ async function extractClaimOperations( } for (let i = 0; i < stakerAddresses.length; i++) { - let rewardAssetPrice = 0; - // hardcode price for reward OCH - if (rewardAssetDenoms[i] === ORAIXOCH_INFO.token.contract_addr) rewardAssetPrice = OCH_PRICE; - else { - const rewardAssetInfo = parseCw20DenomToAssetInfo(rewardAssetDenoms[i]); - rewardAssetPrice = await getPriceAssetByUsdt(rewardAssetInfo); - } - - const earnAmountInUsdt = rewardAssetPrice * earnAmounts[i] + oraiPrice * oraiEarnedAmount; + const rewardAssetPrice = await calculateRewardAssetPrice(rewardAssetDenoms[i]); + const earnAmountInUsdt = calculateEarnAmountInUsdt(rewardAssetPrice, earnAmounts[i], oraiEarnedAmount, oraiPrice); claimData.push({ uniqueKey: concatEarnedHistoryToUniqueKey({ txheight: txData.txheight, diff --git a/packages/oraidex-sync/tests/tx-parsing.spec.ts b/packages/oraidex-sync/tests/tx-parsing.spec.ts index 343a8585..26b33768 100644 --- a/packages/oraidex-sync/tests/tx-parsing.spec.ts +++ b/packages/oraidex-sync/tests/tx-parsing.spec.ts @@ -2,10 +2,11 @@ import * as parse from "../src/tx-parsing"; import { Tx } from "@oraichain/cosmos-rpc-sync"; import { parseTxToMsgExecuteContractMsgs } from "../src/tx-parsing"; import { Tx as CosmosTx } from "cosmjs-types/cosmos/tx/v1beta1/tx"; -import { DuckDb, ORAI, SwapDirection, SwapOperationData, oraiInfo, usdtCw20Address, usdtInfo } from "../src"; +import { DuckDb, OCH_PRICE, ORAI, SwapDirection, SwapOperationData, oraiInfo, usdtCw20Address, usdtInfo } from "../src"; import * as poolHelper from "../src/pool-helper"; import { PoolResponse } from "@oraichain/oraidex-contracts-sdk/build/OraiswapPair.types"; import { AssetInfo } from "@oraichain/oraidex-contracts-sdk"; +import { ORAIXOCH_INFO } from "@oraichain/oraidex-common"; describe("test-tx-parsing", () => { it.each<[string, string[]]>([ @@ -249,4 +250,32 @@ describe("test-tx-parsing", () => { expect(updatedSwapOps[0].basePoolAmount).toEqual(99n); // 100n - 1n expect(updatedSwapOps[0].quotePoolAmount).toEqual(202n); // 200n + 2n }); + + it.each([ + [ORAIXOCH_INFO.token.contract_addr, OCH_PRICE], + ["other_denom", 1] + ])("should-calculate-reward-asset-price", async (denom: string, expectedPrice: number) => { + // setup + jest.spyOn(poolHelper, "getPriceAssetByUsdt").mockResolvedValue(expectedPrice); + + // act + const price = await parse.calculateRewardAssetPrice(denom); + + // assertion + expect(price).toBe(expectedPrice); + }); + + it("test-calculateEarnAmountInUsdt-should-calculate-earn-amount-in-USDT", async () => { + // setup + const earnAmount = 100; + const rewardAssetPrice = 0.5; + const oraiEarnedAmount = 50; + const oraiPrice = 4; + + // act + const result = parse.calculateEarnAmountInUsdt(rewardAssetPrice, earnAmount, oraiEarnedAmount, oraiPrice); + + // assertion + expect(result).toBe(250); + }); });