Skip to content

Commit

Permalink
Merge pull request #170 from oraichain/feat/update-get-route-swap
Browse files Browse the repository at this point in the history
get swap route
  • Loading branch information
haunv3 authored Feb 19, 2024
2 parents a842ede + 3d3ca2c commit 1fa7650
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 31 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.67",
"version": "1.0.68",
"main": "build/index.js",
"files": [
"build/"
Expand Down
12 changes: 12 additions & 0 deletions packages/oraidex-common/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ export const ORAIXOCH_INFO = {
}
};

export const USDC_INFO = {
token: {
contract_addr: USDC_CONTRACT
}
};

export const NEUTARO_INFO = {
native_token: {
denom: NEUTARO_ORAICHAIN_DENOM
}
};

// slippage swap
export const OPTIONS_SLIPPAGE = [1, 3, 5];
export const DEFAULT_SLIPPAGE = OPTIONS_SLIPPAGE[0];
Expand Down
8 changes: 4 additions & 4 deletions packages/oraidex-common/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export const AtomToken: BridgeAppCurrency = {
};

export const NeutaroToken: BridgeAppCurrency = {
coinDenom: "NEUTARO",
coinDenom: "NTMPI",
coinMinimalDenom: "uneutaro",
coinDecimals: 6,
coinGeckoId: "neutaro",
Expand Down Expand Up @@ -312,7 +312,7 @@ export const oraichainNetwork: CustomChainInfo = {
coinImageUrl: "https://dhj8dql1kzq2v.cloudfront.net/white/atom.png"
},
{
coinDenom: "NEUTARO",
coinDenom: "NTMPI",
coinGeckoId: "neutaro",
coinMinimalDenom: NEUTARO_ORAICHAIN_DENOM,
bridgeTo: ["Neutaro-1"],
Expand Down Expand Up @@ -744,14 +744,14 @@ export const chainInfos: CustomChainInfo[] = [
},
bech32Config: defaultBech32Config("neutaro"),
stakeCurrency: {
coinDenom: "neutaro",
coinDenom: "ntmpi",
coinMinimalDenom: "uneutaro",
coinDecimals: 6,
coinImageUrl: "https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/Neutaro/chain.png"
},
feeCurrencies: [
{
coinDenom: "neutaro",
coinDenom: "ntmpi",
coinMinimalDenom: "uneutaro",
coinDecimals: 6,
coinImageUrl: "https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/Neutaro/chain.png",
Expand Down
5 changes: 5 additions & 0 deletions packages/oraidex-common/src/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
INJECTIVE_CONTRACT,
KWT_CONTRACT,
MILKY_CONTRACT,
NEUTARO_ORAICHAIN_DENOM,
ORAI,
ORAIX_CONTRACT,
OSMOSIS_ORAICHAIN_DENOM,
Expand Down Expand Up @@ -94,6 +95,10 @@ export const PAIRS: PairMapping[] = [
{
asset_infos: [{ native_token: { denom: ORAI } }, { token: { contract_addr: BTC_CONTRACT } }],
symbols: ["ORAI", "BTC"]
},
{
asset_infos: [{ native_token: { denom: NEUTARO_ORAICHAIN_DENOM } }, { token: { contract_addr: USDC_CONTRACT } }],
symbols: ["NTMPI", "USDC"]
}
];

Expand Down
2 changes: 1 addition & 1 deletion packages/universal-swap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraidex-universal-swap",
"version": "1.0.51",
"version": "1.0.52",
"main": "build/index.js",
"files": [
"build/"
Expand Down
77 changes: 53 additions & 24 deletions packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
StargateMsg,
IBC_WASM_HOOKS_CONTRACT,
isInPairList,
BigDecimal
BigDecimal,
NEUTARO_INFO,
USDC_INFO
} from "@oraichain/oraidex-common";
import { OraiBridgeRouteData, SimulateResponse, SwapDirection, SwapRoute, UniversalSwapConfig } from "./types";
import {
Expand Down Expand Up @@ -301,6 +303,40 @@ export const unmarshalOraiBridgeRoute = (destination: string) => {
return routeData;
};

export const generateSwapRoute = (offerAsset: AssetInfo, askAsset: AssetInfo, swapRoute: AssetInfo[]) => {
const swaps = [];
if (swapRoute.length === 0) {
swaps.push({
orai_swap: {
offer_asset_info: offerAsset,
ask_asset_info: askAsset
}
});
} else {
swaps.push({
orai_swap: {
offer_asset_info: offerAsset,
ask_asset_info: swapRoute[0]
}
});
for (let i = 0; i < swapRoute.length - 1; i++) {
swaps.push({
orai_swap: {
offer_asset_info: swapRoute[i],
ask_asset_info: swapRoute[i + 1]
}
});
}
swaps.push({
orai_swap: {
offer_asset_info: swapRoute[swapRoute.length - 1],
ask_asset_info: askAsset
}
});
}
return swaps;
};

// generate messages
export const generateSwapOperationMsgs = (offerInfo: AssetInfo, askInfo: AssetInfo): SwapOperation[] => {
const pairExist = PAIRS.some((pair) => {
Expand All @@ -311,29 +347,22 @@ export const generateSwapOperationMsgs = (offerInfo: AssetInfo, askInfo: AssetIn
);
});

return pairExist
? [
{
orai_swap: {
offer_asset_info: offerInfo,
ask_asset_info: askInfo
}
}
]
: [
{
orai_swap: {
offer_asset_info: offerInfo,
ask_asset_info: ORAI_INFO
}
},
{
orai_swap: {
offer_asset_info: ORAI_INFO,
ask_asset_info: askInfo
}
}
];
if (pairExist) return generateSwapRoute(offerInfo, askInfo, []);

// TODO: hardcode NTMPI -> USDC -> ORAI -> X
if (isEqual(offerInfo, NEUTARO_INFO)) {
const swapRoute = isEqual(askInfo, ORAI_INFO) ? [USDC_INFO] : [ORAI_INFO, USDC_INFO];
return generateSwapRoute(offerInfo, askInfo, swapRoute);
}

// TODO: X -> ORAI -> USDC -> NTMPI
if (isEqual(askInfo, NEUTARO_INFO)) {
const swapRoute = isEqual(offerInfo, ORAI_INFO) ? [USDC_INFO] : [USDC_INFO, ORAI_INFO];
return generateSwapRoute(offerInfo, askInfo, swapRoute);
}

// Default case: ORAI_INFO
return generateSwapRoute(offerInfo, askInfo, [ORAI_INFO]);
};

// simulate swap functions
Expand Down
127 changes: 126 additions & 1 deletion packages/universal-swap/tests/helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import {
EvmChainId,
KWT_BSC_CONTRACT,
MILKY_BSC_CONTRACT,
NEUTARO_INFO,
NetworkChainId,
ORAIX_INFO,
ORAI_BRIDGE_EVM_TRON_DENOM_PREFIX,
ORAI_BSC_CONTRACT,
ORAI_INFO,
USDC_INFO,
USDT_BSC_CONTRACT,
USDT_CONTRACT,
USDT_ETH_CONTRACT,
Expand All @@ -34,9 +38,12 @@ import {
getSourceReceiver,
isEvmNetworkNativeSwapSupported,
isEvmSwappable,
isSupportedNoPoolSwapEvm
isSupportedNoPoolSwapEvm,
generateSwapRoute
} from "../src/helper";
import { SwapRoute, UniversalSwapType } from "../src/types";
import { AssetInfo } from "@oraichain/oraidex-contracts-sdk";
import { SwapOperation } from "@oraichain/oraidex-contracts-sdk/build/OraiswapRouter.types";

describe("test helper functions", () => {
it("test-buildSwapRouterKey", () => {
Expand Down Expand Up @@ -429,4 +436,122 @@ describe("test helper functions", () => {
])("test-unmarshalOraiBridgeRoute-%s", (destination, routeData) => {
expect(universalHelper.unmarshalOraiBridgeRoute(destination)).toEqual(routeData);
});

it.each<[AssetInfo, AssetInfo, AssetInfo[], SwapOperation[]]>([
[
NEUTARO_INFO,
ORAI_INFO,
[USDC_INFO],
[
{
orai_swap: {
offer_asset_info: NEUTARO_INFO,
ask_asset_info: USDC_INFO
}
},
{
orai_swap: {
offer_asset_info: USDC_INFO,
ask_asset_info: ORAI_INFO
}
}
]
],
[
ORAI_INFO,
NEUTARO_INFO,
[USDC_INFO],
[
{
orai_swap: {
offer_asset_info: ORAI_INFO,
ask_asset_info: USDC_INFO
}
},
{
orai_swap: {
offer_asset_info: USDC_INFO,
ask_asset_info: NEUTARO_INFO
}
}
]
],
[
ORAI_INFO,
USDC_INFO,
[],
[
{
orai_swap: {
offer_asset_info: ORAI_INFO,
ask_asset_info: USDC_INFO
}
}
]
],
[
ORAIX_INFO,
NEUTARO_INFO,
[ORAI_INFO, USDC_INFO],
[
{
orai_swap: {
offer_asset_info: ORAIX_INFO,
ask_asset_info: ORAI_INFO
}
},
{
orai_swap: {
offer_asset_info: ORAI_INFO,
ask_asset_info: USDC_INFO
}
},
{
orai_swap: {
offer_asset_info: USDC_INFO,
ask_asset_info: NEUTARO_INFO
}
}
]
],
[
NEUTARO_INFO,
ORAIX_INFO,
[USDC_INFO, ORAI_INFO],
[
{
orai_swap: {
offer_asset_info: NEUTARO_INFO,
ask_asset_info: USDC_INFO
}
},
{
orai_swap: {
offer_asset_info: USDC_INFO,
ask_asset_info: ORAI_INFO
}
},
{
orai_swap: {
offer_asset_info: ORAI_INFO,
ask_asset_info: ORAIX_INFO
}
}
]
]
])("test-generateSwapRoute", (offerAsset, askAsset, swapRoute, expectSwapRoute) => {
const getSwapRoute: SwapOperation[] = generateSwapRoute(offerAsset, askAsset, swapRoute);
console.dir(getSwapRoute, { depth: null });
console.dir(expectSwapRoute, { depth: null });

expect(getSwapRoute).toEqual(expect.arrayContaining(expectSwapRoute));
getSwapRoute.forEach((swap) => {
expect(swap).toMatchObject({
orai_swap: expect.objectContaining({
offer_asset_info: expect.any(Object),
ask_asset_info: expect.any(Object)
})
});
});
});
});

0 comments on commit 1fa7650

Please sign in to comment.