Skip to content

Commit

Permalink
Merge pull request #66 from oraichain/feat/usdc-oraix
Browse files Browse the repository at this point in the history
Feat/usdc oraix
  • Loading branch information
trungbach authored Nov 30, 2023
2 parents 43745b2 + 910eb39 commit 69639ff
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 156 deletions.
2 changes: 1 addition & 1 deletion packages/oraidex-common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraidex-common",
"version": "1.0.41",
"version": "1.0.42",
"main": "build/index.js",
"files": [
"build/"
Expand Down
48 changes: 6 additions & 42 deletions packages/oraidex-common/src/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export const PAIRS: PairMapping[] = [
{
asset_infos: [{ token: { contract_addr: INJECTIVE_CONTRACT } }, { native_token: { denom: ORAI } }],
symbols: ["INJ", "ORAI"]
},
{
asset_infos: [{ token: { contract_addr: USDC_CONTRACT } }, { token: { contract_addr: ORAIX_CONTRACT } }],
symbols: ["ORAIX", "USDC"]
}
];

Expand All @@ -94,7 +98,8 @@ export enum pairLpTokens {
USDC_ORAI = "orai1e0x87w9ezwq2sdmvv5dq5ngzy98lt47tqfaf2m7zpkg49g5dj6fqred5d7",
TRX_ORAI = "orai1wgywgvumt5dxhm7vjpwx5es9ecrtl85qaqdspjqwx2lugy7vmw5qlwrn88",
SCATOM_ATOM = "orai1hcjne0hmdj6pjrc3xuksucr0yplsa9ny7v047c34y8k8hfflq6yqyjapnn",
INJ_ORAI = "orai1slqw6gfvs6l2jgvh5ryjayf4g77d7sgfv6fumtyzcr06a6g9gnrq6c4rgg"
INJ_ORAI = "orai1slqw6gfvs6l2jgvh5ryjayf4g77d7sgfv6fumtyzcr06a6g9gnrq6c4rgg",
USDC_ORAIX = "orai1nwpfd09mr4rf8d5c9mh43axzezkwyr7dq2lus23jsw4xw2jqkaxqxwmkd3"
}

// token identifier can be denom or contract addr
Expand All @@ -109,47 +114,6 @@ export const isInPairList = (tokenIdentifier: string) => {
);
};

/**
* Get list contract_addr | denom that make a pair when combined with input
* @param contractAddress
* @returns
*/
export const getPairSwapV2 = (contractAddress: string) => {
let arr = [];
let arrDenom = ORAI;
if (!contractAddress) return { arrLength: 0 };

const pairMapping = PAIRS.filter((p) =>
p.asset_infos.find(
(asset: {
token: {
contract_addr: string;
};
}) => asset?.token?.contract_addr === contractAddress
)
);

if (pairMapping.length) {
for (const info of pairMapping) {
const assets0 = parseAssetInfo(info?.asset_infos?.[0]);
const assets1 = parseAssetInfo(info?.asset_infos?.[1]);
if (assets0 !== contractAddress) arr.push(assets0);
if (assets1 !== contractAddress) arr.push(assets1);
}
}

if (arr.length) {
arrDenom = oraichainTokens.find((e) => e.contractAddress === arr[0])?.denom ?? arr[0];
}

return {
arr,
arrLength: arr.length,
arrDenom,
arrIncludesOrai: arr.includes(ORAI)
};
};

export const isFactoryV1 = (assetInfos: [AssetInfo, AssetInfo]): boolean => {
const pair = PAIRS.find(
(pair) =>
Expand Down
13 changes: 1 addition & 12 deletions packages/oraidex-common/tests/helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
} from "../src/helper";
import { CoinGeckoId, NetworkChainId, OraiToken } from "../src/network";
import { AssetInfo } from "@oraichain/oraidex-contracts-sdk";
import { getPairSwapV2, isFactoryV1 } from "../src/pairs";
import { isFactoryV1 } from "../src/pairs";
import { Coin } from "@cosmjs/amino";
import { toBinary } from "@cosmjs/cosmwasm-stargate";

Expand Down Expand Up @@ -101,17 +101,6 @@ describe("should helper functions in helper run exactly", () => {
expect(parseAssetInfo(assetInfo)).toEqual(expectedResult);
});

it.each<[string, string[], string, boolean]>([
[MILKY_CONTRACT, [USDT_CONTRACT], "usdt", false],
[USDC_CONTRACT, [ORAI], ORAI, true]
])("test-get-pair-swap", (contractAddress, expectedArr, exprectArrDenom, expectedArrIncludesOrai) => {
const { arr, arrLength, arrIncludesOrai, arrDenom } = getPairSwapV2(contractAddress);
expect(arr).toEqual(expectedArr);
expect(arrLength).toEqual(arr!.length);
expect(arrDenom).toEqual(exprectArrDenom);
expect(arrIncludesOrai).toEqual(expectedArrIncludesOrai);
});

it("test-isFactoryV1-true", () => {
const data = isFactoryV1([
{ native_token: { denom: ORAI } },
Expand Down
13 changes: 1 addition & 12 deletions packages/oraidex-common/tests/pairs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { MILKY_CONTRACT, OSMOSIS_ORAICHAIN_DENOM, USDC_CONTRACT, USDT_CONTRACT } from "../src/constant";
import { getPairSwapV2, isFactoryV1, isInPairList } from "../src/pairs";
import { isFactoryV1, isInPairList } from "../src/pairs";

describe("test pairs functions should behave correctly", () => {
it.each<[string, string[], string, boolean]>([
[MILKY_CONTRACT, [USDT_CONTRACT], "usdt", false],
[USDC_CONTRACT, ["orai"], "orai", true]
])("test-getPairSwapV2", (contractAddress, expectedArr, exprectArrDenom, expectedArrIncludesOrai) => {
const { arr, arrLength, arrIncludesOrai, arrDenom } = getPairSwapV2(contractAddress);
expect(arr).toEqual(expectedArr);
expect(arrLength).toEqual(arr!.length);
expect(arrDenom).toEqual(exprectArrDenom);
expect(arrIncludesOrai).toEqual(expectedArrIncludesOrai);
});

it("test-isFactoryV1", () => {
const oraiToken = { native_token: { denom: "orai" } };
expect(
Expand Down
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.21",
"version": "1.0.22",
"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.34",
"version": "1.0.36",
"main": "dist/index.js",
"bin": "dist/index.js",
"license": "MIT",
Expand Down
31 changes: 0 additions & 31 deletions packages/oraidex-sync/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,35 +110,6 @@ async function delay(timeout: number) {
return new Promise((resolve) => setTimeout(resolve, timeout));
}

function findMappedTargetedAssetInfo(targetedAssetInfo: AssetInfo): AssetInfo[] {
const mappedAssetInfos = [];

for (const pair of pairs) {
const infos = pair.asset_infos;
if (parseAssetInfo(infos[0]) === parseAssetInfo(targetedAssetInfo)) mappedAssetInfos.push(infos[1]);
else if (parseAssetInfo(infos[1]) === parseAssetInfo(targetedAssetInfo)) mappedAssetInfos.push(infos[0]);
else continue;
}

return mappedAssetInfos;
}

function findAssetInfoPathToUsdt(info: AssetInfo): AssetInfo[] {
// first, check usdt mapped target infos because if we the info pairs with usdt directly then we can easily calculate its price
// otherwise, we find orai mapped target infos, which can lead to usdt.
// finally, if not paired with orai, then we find recusirvely to find a path leading to usdt token
if (parseAssetInfo(info) === parseAssetInfo(usdtInfo)) return [info]; // means there's no path, the price should be 1
const mappedUsdtInfoList = findMappedTargetedAssetInfo(usdtInfo);
if (mappedUsdtInfoList.find((assetInfo) => parseAssetInfo(assetInfo) === parseAssetInfo(info)))
return [info, usdtInfo];
const mappedOraiInfoList = findMappedTargetedAssetInfo(oraiInfo);
if (mappedOraiInfoList.find((assetInfo) => parseAssetInfo(assetInfo) === parseAssetInfo(info)))
return [info, oraiInfo, usdtInfo];
const pairedInfo = findMappedTargetedAssetInfo(info);
if (pairedInfo.length === 0) return []; // cannot find any mapped target pair
return [info, ...findAssetInfoPathToUsdt(pairedInfo[0])]; // only need the first found paired token with the one we are matching
}

function generateSwapOperations(infoPath: AssetInfo[]): SwapOperation[] {
const swapOps: SwapOperation[] = [];
for (let i = 0; i < infoPath.length - 1; i++) {
Expand Down Expand Up @@ -483,8 +454,6 @@ export function getDate24hBeforeNow(time: Date) {

export {
delay,
findAssetInfoPathToUsdt,
findMappedTargetedAssetInfo,
findPairAddress,
generateSwapOperations,
getAllFees,
Expand Down
14 changes: 7 additions & 7 deletions packages/oraidex-sync/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class OraiDexSync {
console.time("timer-updateLatestPairInfos");
const pairInfos = await getAllPairInfos();
const allPools = await this.duckDb.getPools();
if (allPools.length > 0 && pairInfos.length === allPools.length) return;
if (allPools.length > 0 && pairInfos.length === allPools.length) return false;
await this.duckDb.insertPairInfos(
pairInfos.map((pair) => {
const symbols = getSymbolFromAsset(pair.asset_infos);
Expand All @@ -86,18 +86,18 @@ class OraiDexSync {
})
);
console.timeEnd("timer-updateLatestPairInfos");
return true;
} catch (error) {
console.log("error in updateLatestPairInfos: ", error);
}
}

private async updateLatestLpAmountHistory(currentHeight: number) {
private async updateLatestLpAmountHistory(currentHeight: number, isNewPool: boolean) {
try {
console.time("timer-updateLatestLpAmountHistory");
const countLpAmounts = await this.duckDb.getLpAmountHistory();
if (countLpAmounts > 0) return;
const pairInfos = await this.duckDb.getPools();

if (countLpAmounts > 0 && !isNewPool && !process.env.IS_MIGRATE_POOL) return;
const pairInfos = await this.duckDb.getPools(); // 13 pools
const poolInfos = await getPoolInfos(
pairInfos.map((pair) => pair.pairAddr),
currentHeight
Expand Down Expand Up @@ -181,10 +181,10 @@ class OraiDexSync {
}
console.log("current ind: ", currentInd);

await this.updateLatestPairInfos();
const isNewPool = await this.updateLatestPairInfos();

// update offer & ask, total share of pool history in the first time
await this.updateLatestLpAmountHistory(currentInd);
await this.updateLatestLpAmountHistory(currentInd, isNewPool);

// NOTE: need to updateLatestLpAmountHistory before update apr in the first time
// to get the offerAmount, askAmount from lp amount history table.
Expand Down
20 changes: 6 additions & 14 deletions packages/oraidex-sync/src/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,8 @@ import {
usdtCw20Address
} from "./constants";
import { PairMapping } from "./types";
import { pairLpTokens } from "@oraichain/oraidex-common";

export enum pairLpTokens {
AIRI_ORAI = "orai1hxm433hnwthrxneyjysvhny539s9kh6s2g2n8y",
ORAIX_ORAI = "orai1qmy3uuxktflvreanaqph6yua7stjn6j65rur62",
SCORAI_ORAI = "orai1ay689ltr57jt2snujarvakxrmtuq8fhuat5rnvq6rct89vjer9gqm2vde6",
ATOM_ORAI = "orai1g2prqry343kx566cp7uws9w7v78n5tejylvaz6",
USDT_ORAI = "orai1mav52eqhd07c3lwevcnqdykdzhh4733zf32jcn",
KWT_ORAI = "orai17rcfcrwltujfvx7w4l2ggyku8qrncy0hdvrzvc",
OSMO_ORAI = "orai19ltj97jmdqnz5mrd2amethetvcwsp0220kww3e",
MILKY_USDT = "orai18ywllw03hvy720l06rme0apwyyq9plk64h9ccf",
USDC_ORAI = "orai1e0x87w9ezwq2sdmvv5dq5ngzy98lt47tqfaf2m7zpkg49g5dj6fqred5d7",
TRX_ORAI = "orai1wgywgvumt5dxhm7vjpwx5es9ecrtl85qaqdspjqwx2lugy7vmw5qlwrn88",
SCATOM_ATOM = "orai1hcjne0hmdj6pjrc3xuksucr0yplsa9ny7v047c34y8k8hfflq6yqyjapnn",
INJ_ORAI = "orai1slqw6gfvs6l2jgvh5ryjayf4g77d7sgfv6fumtyzcr06a6g9gnrq6c4rgg"
}
// the orders are important! Do not change the order of the asset_infos.
export const pairs: PairMapping[] = [
{
Expand Down Expand Up @@ -106,6 +93,11 @@ export const pairs: PairMapping[] = [
asset_infos: [{ token: { contract_addr: injAddress } }, { native_token: { denom: ORAI } }],
lp_token: pairLpTokens.INJ_ORAI,
symbols: ["INJ", "ORAI"]
},
{
asset_infos: [{ token: { contract_addr: usdcCw20Address } }, { token: { contract_addr: oraixCw20Address } }],
lp_token: pairLpTokens.USDC_ORAIX,
symbols: ["ORAIX", "USDC"]
}
];

Expand Down
41 changes: 7 additions & 34 deletions packages/oraidex-sync/tests/helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
calculateBasePriceFromSwapOp,
calculatePriceByPool,
concatDataToUniqueKey,
findAssetInfoPathToUsdt,
findMappedTargetedAssetInfo,
findPairAddress,
findPairIndexFromDenoms,
getSwapDirection,
Expand All @@ -33,13 +31,14 @@ import {
toDecimal,
toDisplay
} from "../src/helper";
import { pairs, pairLpTokens } from "../src/pairs";
import { pairs } from "../src/pairs";
import { LpOpsData, PairInfoData, ProvideLiquidityOperationData, SwapDirection, SwapOperationData } from "../src/types";
import { DuckDb, collectAccumulateLpAndSwapData, getVolumePairByAsset, getVolumePairByUsdt } from "../src";
import * as poolHelper from "../src/pool-helper";
import * as helper from "../src/helper";
import * as parse from "../src/parse";
import { SwapOperation } from "@oraichain/oraidex-contracts-sdk/build/OraiswapRouter.types";
import { pairLpTokens } from "@oraichain/oraidex-common";

describe("test-helper", () => {
let duckDb: DuckDb;
Expand Down Expand Up @@ -126,37 +125,6 @@ describe("test-helper", () => {
});
});

it.each<[AssetInfo, number]>([
[{ token: { contract_addr: usdtCw20Address } }, 2],
[{ token: { contract_addr: usdcCw20Address } }, 1],
[{ native_token: { denom: "orai" } }, 10],
[{ token: { contract_addr: airiCw20Adress } }, 1]
])("test-findMappedTargetedAssetInfo", (info, expectedListLength) => {
// setup

// act
const result = findMappedTargetedAssetInfo(info);

// assert
expect(result.length).toEqual(expectedListLength);
});

it.each<[AssetInfo, number]>([
[{ token: { contract_addr: usdtCw20Address } }, 1],
[{ native_token: { denom: "orai" } }, 2],
[{ token: { contract_addr: airiCw20Adress } }, 3],
[{ token: { contract_addr: milkyCw20Address } }, 2],
[{ token: { contract_addr: scAtomCw20Address } }, 4]
])("test-findAssetInfoPathToUsdt", (info, expectedListLength) => {
// setup

// act
const result = findAssetInfoPathToUsdt(info);

// assert
expect(result.length).toEqual(expectedListLength);
});

it.each<[AssetInfo, string | undefined]>([
[{ token: { contract_addr: usdtCw20Address } }, "orai1c5s03c3l336dgesne7dylnmhszw8554tsyy9yt"],
[{ token: { contract_addr: "foo" } }, undefined]
Expand Down Expand Up @@ -259,6 +227,11 @@ describe("test-helper", () => {
asset_infos: [{ token: { contract_addr: injAddress } }, { native_token: { denom: ORAI } }],
lp_token: pairLpTokens.INJ_ORAI,
symbols: ["INJ", "ORAI"]
},
{
asset_infos: [{ token: { contract_addr: usdcCw20Address } }, { token: { contract_addr: oraixCw20Address } }],
lp_token: pairLpTokens.USDC_ORAIX,
symbols: ["ORAIX", "USDC"]
}
]);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/oraidex-sync/tests/pool-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { PairInfoData, PairMapping, PoolAmountHistory, ProvideLiquidityOperation
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";
import { pairLpTokens } from "../src/pairs";
import { pairLpTokens } from "@oraichain/oraidex-common";
describe("test-pool-helper", () => {
let duckDb: DuckDb;
beforeAll(async () => {
Expand Down

0 comments on commit 69639ff

Please sign in to comment.