Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/oraichain/oraidex-sdk into …
Browse files Browse the repository at this point in the history
…main
  • Loading branch information
ducphamle2 committed Sep 27, 2023
2 parents 0d7f5fd + 47c098c commit 9dcc993
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 191 deletions.
2 changes: 1 addition & 1 deletion packages/oraidex-server/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
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.0",
"version": "1.0.1",
"main": "dist/index.js",
"bin": "dist/index.js",
"license": "MIT",
Expand Down
10 changes: 5 additions & 5 deletions packages/oraidex-server/src/only-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
8 changes: 1 addition & 7 deletions packages/oraidex-sync/tests/db.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]]>([
[
Expand Down
124 changes: 41 additions & 83 deletions packages/oraidex-sync/tests/helper.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -379,21 +378,25 @@ 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: [
{ info: oraiInfo, amount: "1" },
{ info: usdtInfo, amount: "1" }
],
total_share: "1"
},
{
assets: [
{ info: oraiInfo, amount: "4" },
{ info: { native_token: { denom: atomIbcDenom } }, amount: "4" }
],
total_share: "8"
}
];

Expand Down Expand Up @@ -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 }
});
});

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -737,28 +703,24 @@ describe("test-helper", () => {
});

describe("test-get-pair-liquidity", () => {
beforeEach(async () => {
duckDb = await DuckDb.create(":memory:");
});

it.each([
[0n, 0n, 0],
[1n, 1n, 4]
])(
"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),
Expand All @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
Loading

0 comments on commit 9dcc993

Please sign in to comment.