Skip to content

Commit

Permalink
fixed failed testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
trungbach committed Sep 15, 2023
1 parent 011a65f commit 69b3395
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/oraidex-sync/src/pool-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ export const calculateFeeByAsset = (asset: Asset, shareRatio: number): Asset =>
};
};

export const getPoolTotalShare = async (txHeight: number, pairAddr: string): Promise<PoolResponse> => {
const cosmwasmClient = await getCosmwasmClient();
cosmwasmClient.setQueryClientWithHeight(txHeight);

const pairContract = new OraiswapPairQueryClient(cosmwasmClient, pairAddr);
const poolInfo = await pairContract.pool();
return poolInfo;
};

/**
* First, calculate fee by offer asset & ask asset
* then, calculate fee of those asset to ORAI
Expand All @@ -185,13 +194,8 @@ export const calculateLiquidityFee = async (
txHeight: number,
withdrawnShare: number
): Promise<bigint> => {
const cosmwasmClient = await getCosmwasmClient();
cosmwasmClient.setQueryClientWithHeight(txHeight);

const pairContract = new OraiswapPairQueryClient(cosmwasmClient, pair.pairAddr);
const poolInfo = await pairContract.pool();
const totalShare = +poolInfo.total_share;
const shareRatio = withdrawnShare / totalShare;
const poolInfo = await getPoolTotalShare(txHeight, pair.pairAddr);
const shareRatio = withdrawnShare / +poolInfo.total_share;

const [feeByAssetFrom, feeByAssetTo] = [
calculateFeeByAsset(poolInfo.assets[0], shareRatio),
Expand Down
13 changes: 13 additions & 0 deletions packages/oraidex-sync/tests/pool-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,19 @@ describe("test-pool-helper", () => {
it("test-calculateLiquidityFee-should-return-correctly-fee-in-USDT", async () => {
// mock
jest.spyOn(poolHelper, "convertFeeAssetToUsdt").mockResolvedValue(1e6);
jest.spyOn(poolHelper, "getPoolTotalShare").mockResolvedValue({
total_share: "1",
assets: [
{
amount: "1",
info: oraiInfo
},
{
amount: "1",
info: usdtInfo
}
]
});

// act
const liquidityFee = await poolHelper.calculateLiquidityFee(
Expand Down

0 comments on commit 69b3395

Please sign in to comment.