Skip to content

Commit

Permalink
chore: seperate func to test
Browse files Browse the repository at this point in the history
  • Loading branch information
trungbach committed Dec 7, 2023
1 parent 1bea6cf commit 50a583e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/oraidex-server/package.staging.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
30 changes: 21 additions & 9 deletions packages/oraidex-sync/src/tx-parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,25 @@ export const calculateSwapOpsWithPoolAmount = async (swapOps: SwapOperationData[
}
};

export async function calculateRewardAssetPrice(rewardAssetDenom: string): Promise<number> {
// 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[])[],
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 30 additions & 1 deletion packages/oraidex-sync/tests/tx-parsing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]]>([
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 50a583e

Please sign in to comment.