diff --git a/packages/oraidex-server/package.json b/packages/oraidex-server/package.json index 9f490a13..41a764ea 100644 --- a/packages/oraidex-server/package.json +++ b/packages/oraidex-server/package.json @@ -1,6 +1,6 @@ { "name": "@oraichain/oraidex-server", - "version": "1.0.8", + "version": "1.0.9", "main": "dist/index.js", "bin": "dist/index.js", "license": "MIT", diff --git a/packages/oraidex-server/package.staging.json b/packages/oraidex-server/package.staging.json index 4becbf15..fb591155 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.0", + "version": "1.0.1", "main": "dist/index.js", "bin": "dist/index.js", "license": "MIT", diff --git a/packages/oraidex-server/src/only-sync.ts b/packages/oraidex-server/src/only-sync.ts index 95206e88..55ab5d78 100644 --- a/packages/oraidex-server/src/only-sync.ts +++ b/packages/oraidex-server/src/only-sync.ts @@ -3,11 +3,11 @@ import { DuckDb } from "@oraichain/oraidex-sync"; import "dotenv/config"; const start = async () => { - const duckDb = await DuckDb.create(process.env.DUCKDB_FILENAME || ":memory:"); - const oraidexSync = await OraiDexSync.create(duckDb, process.env.RPC_URL || "https://rpc.orai.io", { - ...(process.env as any) - } as Env); - await oraidexSync.sync(); + const duckDb = await DuckDb.create(process.env.DUCKDB_FILENAME || ":memory:"); + const oraidexSync = await OraiDexSync.create(duckDb, process.env.RPC_URL || "https://rpc.orai.io", { + ...(process.env as any) + } as Env); + await oraidexSync.sync(); }; start(); diff --git a/packages/oraidex-sync/tests/db.spec.ts b/packages/oraidex-sync/tests/db.spec.ts index 12b78c24..a574f117 100644 --- a/packages/oraidex-sync/tests/db.spec.ts +++ b/packages/oraidex-sync/tests/db.spec.ts @@ -3,13 +3,7 @@ import { isoToTimestampNumber } from "../src/helper"; import { GetFeeSwap, GetVolumeQuery, ProvideLiquidityOperationData } from "../src/types"; describe("test-duckdb", () => { let duckDb: DuckDb; - afterAll(jest.resetModules); - - afterEach(() => { - jest.clearAllMocks(); - jest.restoreAllMocks(); - jest.resetAllMocks(); - }); + afterEach(jest.restoreAllMocks); it.each<[string[], number[]]>([ [ diff --git a/packages/oraidex-sync/tests/helper.spec.ts b/packages/oraidex-sync/tests/helper.spec.ts index 24b1046e..9dd60827 100644 --- a/packages/oraidex-sync/tests/helper.spec.ts +++ b/packages/oraidex-sync/tests/helper.spec.ts @@ -1,4 +1,4 @@ -import { AssetInfo, SwapOperation } from "@oraichain/oraidex-contracts-sdk"; +import { AssetInfo } from "@oraichain/oraidex-contracts-sdk"; import { PoolResponse } from "@oraichain/oraidex-contracts-sdk/build/OraiswapPair.types"; import { ORAI, @@ -37,16 +37,15 @@ import { LpOpsData, PairInfoData, ProvideLiquidityOperationData, SwapDirection, import { DuckDb, collectAccumulateLpAndSwapData, getVolumePairByAsset, getVolumePairByUsdt } from "../src"; import * as poolHelper from "../src/pool-helper"; import * as helper from "../src/helper"; +import { SwapOperation } from "@oraichain/oraidex-contracts-sdk/build/OraiswapRouter.types"; describe("test-helper", () => { let duckDb: DuckDb; - - afterAll(jest.resetModules); - afterEach(() => { - jest.clearAllMocks(); - jest.restoreAllMocks(); - jest.resetAllMocks(); + beforeAll(async () => { + duckDb = await DuckDb.create(":memory:"); }); + afterEach(jest.restoreAllMocks); + describe("bigint", () => { describe("toAmount", () => { it("toAmount-percent", () => { @@ -379,7 +378,18 @@ describe("test-helper", () => { ); it("test-collectAccumulateLpAndSwapData-should-aggregate-ops-with-same-pairs", async () => { - // setup, test with orai/usdt & orai/atom pair + // setup, test with orai/usdt + jest.spyOn(duckDb, "getPoolByAssetInfos").mockResolvedValue({ + firstAssetInfo: JSON.stringify(oraiInfo), + secondAssetInfo: JSON.stringify(usdtInfo), + commissionRate: "", + pairAddr: "oraiUsdtPairAddr", + liquidityAddr: "", + oracleAddr: "", + symbols: "1", + fromIconUrl: "1", + toIconUrl: "1" + }); const poolResponses: PoolResponse[] = [ { assets: [ @@ -387,13 +397,6 @@ describe("test-helper", () => { { info: usdtInfo, amount: "1" } ], total_share: "1" - }, - { - assets: [ - { info: oraiInfo, amount: "4" }, - { info: { native_token: { denom: atomIbcDenom } }, amount: "4" } - ], - total_share: "8" } ]; @@ -433,52 +436,15 @@ describe("test-helper", () => { direction: "Sell", height: 1, timestamp: 1 - }, - { - baseTokenAmount: 1, - baseTokenDenom: ORAI, - quoteTokenAmount: -1, - quoteTokenDenom: atomIbcDenom, - direction: "Sell", - height: 1, - timestamp: 1 } ]; - duckDb = await DuckDb.create(":memory:"); - await duckDb.createPairInfosTable(); - - await duckDb.insertPairInfos([ - { - firstAssetInfo: JSON.stringify(oraiInfo), - secondAssetInfo: JSON.stringify(usdtInfo), - commissionRate: "", - pairAddr: "oraiUsdtPairAddr", - liquidityAddr: "", - oracleAddr: "", - symbols: "1", - fromIconUrl: "1", - toIconUrl: "1" - }, - { - firstAssetInfo: JSON.stringify(oraiInfo), - secondAssetInfo: JSON.stringify({ native_token: { denom: atomIbcDenom } }), - commissionRate: "", - pairAddr: "oraiAtomPairAddr", - liquidityAddr: "", - oracleAddr: "", - symbols: "1", - fromIconUrl: "1", - toIconUrl: "1" - } - ]); // act const accumulatedData = await collectAccumulateLpAndSwapData(lpOpsData, poolResponses); // assertion expect(accumulatedData).toStrictEqual({ - oraiUsdtPairAddr: { askPoolAmount: 2n, height: 1, offerPoolAmount: 2n, timestamp: 1 }, - oraiAtomPairAddr: { askPoolAmount: 3n, height: 1, offerPoolAmount: 5n, timestamp: 1 } + oraiUsdtPairAddr: { askPoolAmount: 2n, height: 1, offerPoolAmount: 2n, timestamp: 1 } }); }); @@ -687,16 +653,16 @@ describe("test-helper", () => { } ); - // it.each([ - // ["case-asset-info-pairs-is-NOT-reversed", [oraiInfo, usdtInfo], false], - // ["case-asset-info-pairs-is-reversed", [usdtInfo, oraiInfo], true] - // ])( - // "test-isAssetInfoPairReverse-should-return-correctly", - // (_caseName: string, assetInfos: AssetInfo[], expectedResult: boolean) => { - // const result = helper.isAssetInfoPairReverse(assetInfos); - // expect(result).toBe(expectedResult); - // } - // ); + it.each([ + ["case-asset-info-pairs-is-NOT-reversed", [oraiInfo, usdtInfo], false], + ["case-asset-info-pairs-is-reversed", [usdtInfo, oraiInfo], true] + ])( + "test-isAssetInfoPairReverse-should-return-correctly", + (_caseName: string, assetInfos: AssetInfo[], expectedResult: boolean) => { + const result = helper.isAssetInfoPairReverse(assetInfos); + expect(result).toBe(expectedResult); + } + ); it("test-getSymbolFromAsset-should-throw-error-for-assetInfos-not-valid", () => { const asset_infos = [oraiInfo, { token: { contract_addr: "invalid-token" } }] as [AssetInfo, AssetInfo]; @@ -737,10 +703,6 @@ describe("test-helper", () => { }); describe("test-get-pair-liquidity", () => { - beforeEach(async () => { - duckDb = await DuckDb.create(":memory:"); - }); - it.each([ [0n, 0n, 0], [1n, 1n, 4] @@ -748,17 +710,17 @@ describe("test-helper", () => { "test-getPairLiquidity-should-return-correctly-liquidity-by-USDT", async (offerPoolAmount: bigint, askPoolAmount: bigint, expectedResult: number) => { // setup - await duckDb.createLpAmountHistoryTable(); - await duckDb.insertPoolAmountHistory([ - { - offerPoolAmount, - askPoolAmount, - timestamp: 1, - height: 1, - pairAddr: "oraiUsdtPairAddr", - uniqueKey: "1" - } - ]); + jest.spyOn(duckDb, "getLatestLpPoolAmount").mockResolvedValue({ + offerPoolAmount, + askPoolAmount, + timestamp: 1, + height: 1, + pairAddr: "oraiUsdtPairAddr", + uniqueKey: "1" + }); + jest.spyOn(poolHelper, "getPriceAssetByUsdt").mockResolvedValue(2); + + // act const poolInfo: PairInfoData = { firstAssetInfo: JSON.stringify(oraiInfo), secondAssetInfo: JSON.stringify(usdtInfo), @@ -770,9 +732,7 @@ describe("test-helper", () => { fromIconUrl: "1", toIconUrl: "1" }; - jest.spyOn(poolHelper, "getPriceAssetByUsdt").mockResolvedValue(2); - // act const result = await helper.getPairLiquidity(poolInfo); // assertion @@ -783,8 +743,7 @@ describe("test-helper", () => { describe("test-get-volume-pairs", () => { it("test-getVolumePairByAsset-should-return-correctly-sum-volume-swap-&-liquidity", async () => { - //setup mock - duckDb = await DuckDb.create(":memory:"); + // setup mock jest.spyOn(duckDb, "getVolumeSwap").mockResolvedValue(1n); jest.spyOn(duckDb, "getVolumeLiquidity").mockResolvedValue(1n); @@ -828,7 +787,6 @@ describe("test-helper", () => { describe("test-get-fee-pair", () => { it("test-getFeePair-should-return-correctly-sum-fee-swap-&-liquidity", async () => { //setup mock - duckDb = await DuckDb.create(":memory:"); jest.spyOn(duckDb, "getFeeSwap").mockResolvedValue(1n); jest.spyOn(duckDb, "getFeeLiquidity").mockResolvedValue(1n); diff --git a/packages/oraidex-sync/tests/pool-helper.spec.ts b/packages/oraidex-sync/tests/pool-helper.spec.ts index a0da655e..e7148154 100644 --- a/packages/oraidex-sync/tests/pool-helper.spec.ts +++ b/packages/oraidex-sync/tests/pool-helper.spec.ts @@ -13,16 +13,15 @@ import { import * as helper from "../src/helper"; import { DuckDb, pairs } from "../src/index"; import * as poolHelper from "../src/pool-helper"; -import { PairInfoData, PairMapping, ProvideLiquidityOperationData } from "../src/types"; +import { PairInfoData, PairMapping, PoolAmountHistory, ProvideLiquidityOperationData } from "../src/types"; import { Tx } from "@oraichain/cosmos-rpc-sync"; import { Tx as CosmosTx } from "cosmjs-types/cosmos/tx/v1beta1/tx"; import * as txParsing from "../src/tx-parsing"; - describe("test-pool-helper", () => { let duckDb: DuckDb; - - afterAll(jest.resetModules); - afterEach(jest.resetModules); + beforeAll(async () => { + duckDb = await DuckDb.create(":memory:") + }) afterEach(jest.restoreAllMocks); it.each<[string, [AssetInfo, AssetInfo], boolean]>([ @@ -102,51 +101,42 @@ describe("test-pool-helper", () => { describe("test-calculate-price-group-funcs", () => { // use orai/usdt in this test suite - // it("test-getPriceByAsset-when-duckdb-empty-should-return-0", async () => { - // // setup - // duckDb = await DuckDb.create(":memory:"); - // await Promise.all([duckDb.createPairInfosTable()]); + it("test-getPriceByAsset-when-duckdb-empty-should-return-0", async () => { + // setup + jest.spyOn(duckDb, "getPoolByAssetInfos").mockResolvedValue(null); - // // act & assertion - // const result = await poolHelper.getPriceByAsset([oraiInfo, usdtInfo], "base_in_quote"); - // expect(result).toEqual(0); - // }); + // act & assertion + const result = await poolHelper.getPriceByAsset([oraiInfo, usdtInfo], "base_in_quote"); + expect(result).toEqual(0); + }); it.each<[[AssetInfo, AssetInfo], poolHelper.RatioDirection, number]>([ - [[oraiInfo, { token: { contract_addr: "invalid-token" } }], "base_in_quote", 0], [[oraiInfo, usdtInfo], "base_in_quote", 0.5], [[oraiInfo, usdtInfo], "quote_in_base", 2] ])("test-getPriceByAsset-should-return-correctly-price", async (assetInfos, ratioDirection, expectedPrice) => { // setup - duckDb = await DuckDb.create(":memory:"); - await Promise.all([duckDb.createPairInfosTable(), duckDb.createLpAmountHistoryTable()]); - const pairAddr = "orai1c5s03c3l336dgesne7dylnmhszw8554tsyy9yt"; - let pairInfoData: PairInfoData[] = [ - { - firstAssetInfo: JSON.stringify({ native_token: { denom: ORAI } } as AssetInfo), - secondAssetInfo: JSON.stringify({ token: { contract_addr: usdtCw20Address } } as AssetInfo), - commissionRate: "", - pairAddr, - liquidityAddr: "", - oracleAddr: "", - symbols: "1", - fromIconUrl: "1", - toIconUrl: "1" - } - ]; - await duckDb.insertPairInfos(pairInfoData); - await duckDb.insertPoolAmountHistory([ - { - offerPoolAmount: 1n, - askPoolAmount: 1n, - height: 1, - timestamp: 1, - pairAddr, - uniqueKey: "1" - } - ]); - + const pairInfoData: PairInfoData = { + firstAssetInfo: JSON.stringify({ native_token: { denom: ORAI } } as AssetInfo), + secondAssetInfo: JSON.stringify({ token: { contract_addr: usdtCw20Address } } as AssetInfo), + commissionRate: "1", + pairAddr, + liquidityAddr: "1", + oracleAddr: "1", + symbols: "1", + fromIconUrl: "1", + toIconUrl: "1" + } + const poolAmountHistory: PoolAmountHistory = { + offerPoolAmount: 1n, + askPoolAmount: 1n, + height: 1, + timestamp: 1, + pairAddr, + uniqueKey: "1" + } + jest.spyOn(duckDb, "getPoolByAssetInfos").mockResolvedValue(pairInfoData); + jest.spyOn(duckDb, "getLatestLpPoolAmount").mockResolvedValue(poolAmountHistory); jest.spyOn(helper, "calculatePriceByPool").mockReturnValue(0.5); // assert @@ -155,9 +145,10 @@ describe("test-pool-helper", () => { }); it.each([ - ["asset-is-cw20-USDT", usdtInfo, 1], + ["case-1:-asset-is-cw20-USDT", usdtInfo, 1], + ["case-2:-asset-is-ORAI", oraiInfo, 2], [ - "asset-is-MILKY-that-mapped-with-USDT", + "case-3:-asset-is-MILKY-that-mapped-with-USDT", { token: { contract_addr: milkyCw20Address @@ -165,9 +156,8 @@ describe("test-pool-helper", () => { }, 0.5 ], - ["asset-is-ORAI", oraiInfo, 2], [ - "asset-is-pair-with-ORAI", + "case-4:-asset-is-pair-with-ORAI", { native_token: { denom: atomIbcDenom @@ -176,7 +166,7 @@ describe("test-pool-helper", () => { 1 ], [ - "asset-is-NOT-pair-with-ORAI", + "case-5.1:-asset-is-NOT-pair-with-ORAI", { token: { contract_addr: scAtomCw20Address @@ -187,53 +177,7 @@ describe("test-pool-helper", () => { ])( "test-getPriceAssetByUsdt-with-%p-should-return-correctly-price-of-asset-in-USDT", async (_caseName: string, assetInfo: AssetInfo, expectedPrice: number) => { - // setup & mock - duckDb = await DuckDb.create(":memory:"); - await Promise.all([duckDb.createPairInfosTable(), duckDb.createLiquidityOpsTable()]); - const pairInfoData: PairInfoData[] = [ - { - firstAssetInfo: JSON.stringify(oraiInfo as AssetInfo), - secondAssetInfo: JSON.stringify(usdtInfo as AssetInfo), - commissionRate: "", - pairAddr: "orai1c5s03c3l336dgesne7dylnmhszw8554tsyy9yt", - liquidityAddr: "", - oracleAddr: "", - symbols: "1", - fromIconUrl: "1", - toIconUrl: "1" - }, - { - firstAssetInfo: JSON.stringify(oraiInfo as AssetInfo), - secondAssetInfo: JSON.stringify({ native_token: { denom: atomIbcDenom } } as AssetInfo), - commissionRate: "", - pairAddr: "orai/atom", - liquidityAddr: "", - oracleAddr: "", - symbols: "1", - fromIconUrl: "1", - toIconUrl: "1" - } - ]; - await duckDb.insertPairInfos(pairInfoData); - const lpOpsData: ProvideLiquidityOperationData[] = [ - { - basePrice: 1, - baseTokenAmount: 1, - baseTokenDenom: "orai", - baseTokenReserve: 1000000000 / 2, - opType: "withdraw", - uniqueKey: "2", - quoteTokenAmount: 2, - quoteTokenDenom: usdtCw20Address, - quoteTokenReserve: 1000000000, - timestamp: 1, - txCreator: "foobar", - txhash: "foo", - txheight: 1, - taxRate: 1n - } - ]; - await duckDb.insertLpOps(lpOpsData); + // mock jest.spyOn(poolHelper, "getOraiPrice").mockResolvedValue(2); jest.spyOn(poolHelper, "getPriceByAsset").mockResolvedValue(0.5);