Skip to content

Commit

Permalink
add pool ntmpi usdc (#163)
Browse files Browse the repository at this point in the history
* add pool ntmpi usdc

* add pool ntmpi usdc

* add pool ntmpi usdc

* fix import oraidex sync

* hardcode pool ntmpi usdc

* chore: pumb oraidex-common latest

* fix: handle contract data
for pool

* chore: ignore orai/btc pool

* chore: fix neutaro denom

* chore: fix neutaro denom

* chore: fix neutaro denom

* chore: fix neutaro denom

* chore: fix neutaro denom

* refactor: remove redundant code

---------

Co-authored-by: trungbach <[email protected]>
  • Loading branch information
haunv3 and trungbach authored Feb 20, 2024
1 parent 55321dd commit 49e38ea
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 47 deletions.
27 changes: 6 additions & 21 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,24 @@
"typescript": true
}
},
"plugins": [
"@typescript-eslint"
],
"plugins": ["@typescript-eslint"],
"rules": {
"no-console": "off",
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
]
"quotes": ["error", "double"],
"semi": ["error", "always"]
},
"overrides": [
{
"files": [
"*.ts",
"*.tsx"
],
"files": ["*.ts", "*.tsx"],
"rules": {
"@typescript-eslint/no-explicit-any": [
"warn"
],
"@typescript-eslint/no-explicit-any": ["warn"],
"@typescript-eslint/no-unused-vars": "warn",
"security/detect-object-injection": "off",
"@typescript-eslint/ban-ts-comment": "off"
}
},
{
"files": [
"*.js"
],
"files": ["*.js"],
"rules": {
"@typescript-eslint/no-var-requires": "warn"
}
Expand Down
7 changes: 6 additions & 1 deletion packages/oraidex-common/src/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
TRX_CONTRACT,
USDC_CONTRACT,
USDT_CONTRACT,
WETH_CONTRACT
WETH_CONTRACT,
NEUTARO_ORAICHAIN_DENOM as NEUTARO_ADDRESS
} from "./constant";
import { parseAssetInfo } from "./helper";
import { TokenItemType, assetInfoMap } from "./token";
Expand Down Expand Up @@ -92,6 +93,10 @@ export const PAIRS: PairMapping[] = [
asset_infos: [{ native_token: { denom: ORAI } }, { token: { contract_addr: WETH_CONTRACT } }],
symbols: ["ORAI", "WETH"]
},
{
asset_infos: [{ native_token: { denom: NEUTARO_ADDRESS } }, { token: { contract_addr: USDC_CONTRACT } }],
symbols: ["NTMPI", "USDC"]
},
{
asset_infos: [{ native_token: { denom: ORAI } }, { token: { contract_addr: BTC_CONTRACT } }],
symbols: ["ORAI", "BTC"]
Expand Down
4 changes: 3 additions & 1 deletion packages/oraidex-server/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import bech32 from "bech32";
import "dotenv/config";
import { DbQuery, LowHighPriceOfPairType } from "./db-query";
import { pairLpTokens } from "@oraichain/oraidex-common";

const rpcUrl = process.env.RPC_URL || "https://rpc.orai.io";
const ORAI_INJ = "ORAI_INJ";
Expand Down Expand Up @@ -214,7 +215,8 @@ export const getAllPoolsInfo = async () => {
} as PairInfoDataResponse;
});

return allPoolsInfo.filter(Boolean);
// TODO: ignore pool ORAI/BTC and pool undefined
return allPoolsInfo.filter((pools) => pools && pools.liquidityAddr !== pairLpTokens.ORAI_BTC);
} catch (error) {
console.log({ errorGetAllPoolsInfo: error });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/oraidex-server/src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
// import { Tendermint34Client } from "@cosmjs/tendermint-rpc";

//@ts-ignore
BigInt.prototype.toJSON = function () {
Expand Down
2 changes: 1 addition & 1 deletion packages/oraidex-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@oraichain/cosmos-rpc-sync": "^1.0.7",
"@oraichain/oraidex-contracts-sdk": "^1.0.24",
"@oraichain/cw-simulate": "^2.8.72",
"@oraichain/oraidex-common": "^1.0.41",
"@oraichain/oraidex-common": "latest",
"duckdb-async": "^0.9.2",
"duckdb": "^0.9.2",
"apache-arrow": "^12.0.1",
Expand Down
1 change: 1 addition & 0 deletions packages/oraidex-sync/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const tronCw20Address = "orai1c7tpjenafvgjtgm9aqwm7afnke6c56hpdms8jc6md40
export const scOraiCw20Address = "orai1065qe48g7aemju045aeyprflytemx7kecxkf5m7u5h5mphd0qlcs47pclp";
export const usdcCw20Address = "orai15un8msx3n5zf9ahlxmfeqd2kwa5wm0nrpxer304m9nd5q6qq0g6sku5pdd";
export const atomIbcDenom = "ibc/A2E2EEC9057A4A1C2C0A6A4C78B0239118DF5F278830F50B4A6BDD7A66506B78";
export const neutaroDenom = "ibc/576B1D63E401B6A9A071C78A1D1316D016EC9333D2FEB14AD503FAC4B8731CD1";
export const osmosisIbcDenom = "ibc/9C4DCD21B48231D0BC2AC3D1B74A864746B37E4292694C93C617324250D002FC";
export const injAddress = "orai19rtmkk6sn4tppvjmp5d5zj6gfsdykrl5rw2euu5gwur3luheuuusesqn49";
export const tenAmountInDecimalSix = 10000000;
Expand Down
13 changes: 11 additions & 2 deletions packages/oraidex-sync/src/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
usdtCw20Address
} from "./constants";
import { PairMapping } from "./types";
import { WETH_CONTRACT } from "@oraichain/oraidex-common/build/constant";
import { pairLpTokens } from "@oraichain/oraidex-common/build/pairs";
import { WETH_CONTRACT, BTC_CONTRACT, pairLpTokens, NEUTARO_ORAICHAIN_DENOM } from "@oraichain/oraidex-common";

// the orders are important! Do not change the order of the asset_infos.
export const pairs: PairMapping[] = [
Expand Down Expand Up @@ -104,6 +103,16 @@ export const pairs: PairMapping[] = [
asset_infos: [{ native_token: { denom: ORAI } }, { token: { contract_addr: WETH_CONTRACT } }],
lp_token: pairLpTokens.ORAI_WETH,
symbols: ["ORAI", "WETH"]
},
{
asset_infos: [{ native_token: { denom: ORAI } }, { token: { contract_addr: BTC_CONTRACT } }],
lp_token: pairLpTokens.ORAI_BTC,
symbols: ["ORAI", "BTC"]
},
{
asset_infos: [{ native_token: { denom: NEUTARO_ORAICHAIN_DENOM } }, { token: { contract_addr: usdcCw20Address } }],
lp_token: pairLpTokens.NTMPI_USDC,
symbols: ["NTMPI", "USDC"]
}
];

Expand Down
8 changes: 4 additions & 4 deletions packages/oraidex-sync/src/pool-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ export const isPoolHasFee = (assetInfos: [AssetInfo, AssetInfo]): boolean => {

export const getPoolInfos = async (pairAddrs: string[], wantedHeight?: number): Promise<PoolResponse[]> => {
// adjust the query height to get data from the past
const cosmwasmClient = await getCosmwasmClient();
cosmwasmClient.setQueryClientWithHeight(wantedHeight);
const multicall = new MulticallQueryClient(cosmwasmClient, network.multicall);
const res = await queryPoolInfos(pairAddrs, multicall);
// const cosmwasmClient = await getCosmwasmClient();
// cosmwasmClient.setQueryClientWithHeight(wantedHeight);
// const multicall = new MulticallQueryClient(cosmwasmClient, network.multicall);
const res = await queryPoolInfos(pairAddrs, wantedHeight);
return res;
};

Expand Down
40 changes: 25 additions & 15 deletions packages/oraidex-sync/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,29 @@ import { generateSwapOperations, getCosmwasmClient, toDisplay } from "./helper";
import { pairs } from "./pairs";
import { parseAssetInfoOnlyDenom } from "./parse";

async function queryPoolInfos(pairAddrs: string[], multicall: MulticallReadOnlyInterface): Promise<PoolResponse[]> {
// adjust the query height to get data from the past
const res = await multicall.tryAggregate({
queries: pairAddrs.map((pair) => {
return {
address: pair,
data: toBinary({
pool: {}
})
};
})
async function queryPoolInfos(pairAddrs: string[], wantedHeight?: number): Promise<PoolResponse[]> {
const calls: Call[] = pairAddrs.map((pair) => {
return {
address: pair,
data: toBinary({
pool: {}
})
};
});
// reset query client to latest for other functions to call
return res.return_data.map((data) => (data.success ? fromBinary(data.data) : undefined)).filter((data) => data); // remove undefined items

const chunks = [];
const MAX_CHUNK_SIZE = 10;
for (let i = 0; i < calls.length; i += MAX_CHUNK_SIZE) {
chunks.push(calls.slice(i, i + MAX_CHUNK_SIZE));
}

try {
const res = (await Promise.all(chunks.map((chunk) => aggregateMulticall(chunk, wantedHeight)))) as any[][];
return res.flat().filter(Boolean);
} catch (error) {
console.log(`Error when trying to queryPoolInfos: ${JSON.stringify(error)}`);
throw new Error("queryPoolInfosFails::" + error?.message);
}
}

async function queryAllPairInfos(
Expand Down Expand Up @@ -99,15 +108,16 @@ async function simulateSwapPrice(pairPaths: AssetInfo[][], router: OraiswapRoute
const res = (await Promise.all(
chunks.map(aggregateMulticall<OraiswapRouterTypes.SimulateSwapOperationsResponse>)
)) as OraiswapRouterTypes.SimulateSwapOperationsResponse[][];
return res.flat().map((data, ind) => toDisplay(data.amount, dataCall[ind].sourceDecimals).toString());
return res.flat().map((data, ind) => toDisplay(data?.amount || "0", dataCall[ind].sourceDecimals).toString());
} catch (error) {
console.log(`Error when trying to simulate swap with pairs: ${JSON.stringify(pairPaths)} using router: ${error}`);
throw new Error("SwapSimulateSwapPriceFail::" + error.message); // error case. Will be handled by the caller function
}
}

async function aggregateMulticall<T>(queries: Call[]): Promise<T[]> {
async function aggregateMulticall<T>(queries: Call[], wantedHeight?: number): Promise<T[]> {
const client = await getCosmwasmClient();
client.setQueryClientWithHeight(wantedHeight);
const multicall = new MulticallQueryClient(client, network.multicall);
const res = await multicall.tryAggregate({ queries });
return res.return_data.map((data) => (data.success ? fromBinary(data.data) : undefined)) as T[];
Expand Down
15 changes: 14 additions & 1 deletion packages/oraidex-sync/tests/helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ 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 { WETH_CONTRACT, pairLpTokens } from "@oraichain/oraidex-common";
import { BTC_CONTRACT, NEUTARO_ORAICHAIN_DENOM, WETH_CONTRACT, pairLpTokens } from "@oraichain/oraidex-common";

describe("test-helper", () => {
let duckDb: DuckDb;
Expand Down Expand Up @@ -237,6 +237,19 @@ describe("test-helper", () => {
asset_infos: [{ native_token: { denom: ORAI } }, { token: { contract_addr: WETH_CONTRACT } }],
lp_token: pairLpTokens.ORAI_WETH,
symbols: ["ORAI", "WETH"]
},
{
asset_infos: [{ native_token: { denom: ORAI } }, { token: { contract_addr: BTC_CONTRACT } }],
lp_token: pairLpTokens.ORAI_BTC,
symbols: ["ORAI", "BTC"]
},
{
asset_infos: [
{ native_token: { denom: NEUTARO_ORAICHAIN_DENOM } },
{ token: { contract_addr: usdcCw20Address } }
],
lp_token: pairLpTokens.NTMPI_USDC,
symbols: ["NTMPI", "USDC"]
}
]);
});
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3363,6 +3363,24 @@
resolved "https://registry.yarnpkg.com/@oraichain/immutable/-/immutable-4.3.9.tgz#ff8d5a7b39b5b01f3f72a902cffbfea32ccb20c3"
integrity sha512-INpHnhL970OCkR7I71Kssb2aLl2l4Y/x8W6FlyRO0KmC8GHjxc/hlNB1t44BiI7lkOYmcWMRQoC8dwParsp1RQ==

"@oraichain/oraidex-common@latest":
version "1.0.67"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-common/-/oraidex-common-1.0.67.tgz#b16221be2b70ecb118e76462306dd643b16fdc71"
integrity sha512-4Jj4gPZJeRSm9z3PAk8+Q8lUZ8miFPAodqooct5MaqgtJloblpJkDr0/BGZYg2783nUcd7U7Ku95vsSMUhlewg==
dependencies:
"@cosmjs/amino" "^0.31.0"
"@cosmjs/cosmwasm-stargate" "^0.31.0"
"@cosmjs/crypto" "^0.31.0"
"@cosmjs/proto-signing" "^0.31.0"
"@cosmjs/stargate" "^0.31.0"
"@cosmjs/tendermint-rpc" "0.31.0"
"@ethersproject/providers" "^5.0.10"
"@keplr-wallet/types" "^0.11.38"
"@oraichain/oraidex-contracts-sdk" "^1.0.30"
bignumber.js "^9.0.1"
cosmjs-types "^0.8.0"
ethers "^5.0.15"

"@oraichain/[email protected]":
version "1.0.14"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-universal-swap/-/oraidex-universal-swap-1.0.14.tgz#fd4b60e7cfc21984dbaa14a7b9a2c560f02da37d"
Expand Down

0 comments on commit 49e38ea

Please sign in to comment.