From bf5d21652dd414a4b59c5e27de95ad94d2aceaf9 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Van Date: Tue, 14 Jan 2025 14:11:28 +0700 Subject: [PATCH] add universal swap 1.1.27-beta.1 --- packages/universal-swap/package.json | 2 +- packages/universal-swap/src/handler.ts | 14 +++- packages/universal-swap/src/helper.ts | 6 +- .../universal-swap/src/msg/chains/chain.ts | 4 +- .../universal-swap/src/msg/chains/cosmos.ts | 12 +++- .../src/msg/chains/oraichain.ts | 10 +-- .../universal-swap/src/msg/chains/osmosis.ts | 16 +++-- packages/universal-swap/src/msg/msgs.ts | 67 +++++++++++++++---- 8 files changed, 100 insertions(+), 31 deletions(-) diff --git a/packages/universal-swap/package.json b/packages/universal-swap/package.json index 2d3f52e1..2935b770 100644 --- a/packages/universal-swap/package.json +++ b/packages/universal-swap/package.json @@ -1,6 +1,6 @@ { "name": "@oraichain/oraidex-universal-swap", - "version": "1.1.26", + "version": "1.1.27-beta.1", "main": "build/index.js", "files": [ "build/" diff --git a/packages/universal-swap/src/handler.ts b/packages/universal-swap/src/handler.ts index 426f2e68..6eac1300 100644 --- a/packages/universal-swap/src/handler.ts +++ b/packages/universal-swap/src/handler.ts @@ -704,7 +704,8 @@ export class UniversalSwapHandler { minimumReceive, userSlippage, this.config.swapOptions, - alphaSmartRoutes + alphaSmartRoutes, + this.swapData.affiliates ); const swapRouteSplit = completeSwapRoute.split(":"); const swapRoute = swapRouteSplit.length === 1 ? "" : swapRouteSplit[1]; @@ -807,7 +808,13 @@ export class UniversalSwapHandler { } const msgs = alphaSmartRoutes.routes.map((route) => { - return generateMsgSwap(route, userSlippage / 100, receiverAddresses, recipientAddress); + return generateMsgSwap( + route, + userSlippage / 100, + receiverAddresses, + recipientAddress, + this.swapData.affiliates + ); }); const { client } = await this.config.cosmosWallet.getCosmWasmClient( @@ -999,7 +1006,8 @@ export class UniversalSwapHandler { minimumReceive, userSlippage, this.config.swapOptions, - alphaSmartRoutes + alphaSmartRoutes, + this.swapData.affiliates ); if (swapOptions?.isAlphaIbcWasm) { diff --git a/packages/universal-swap/src/helper.ts b/packages/universal-swap/src/helper.ts index bce6058b..3cec3520 100644 --- a/packages/universal-swap/src/helper.ts +++ b/packages/universal-swap/src/helper.ts @@ -402,7 +402,8 @@ export class UniversalSwapHelper { ibcInfoTestMode?: boolean; isAlphaIbcWasm?: boolean; }, - alphaSmartRoute?: RouterResponse + alphaSmartRoute?: RouterResponse, + affiliate?: Affiliate[] ): Promise => { // TODO: recheck cosmos address undefined (other-chain -> oraichain) if (!addresses.sourceReceiver) throw generateError(`Cannot get source if the sourceReceiver is empty!`); @@ -490,7 +491,8 @@ export class UniversalSwapHelper { userSlippage / 100, receiverAddresses, addresses.recipientAddress, - alphaRoutes.paths[0].chainId + alphaRoutes.paths[0].chainId, + affiliate ); swapRoute = memo; diff --git a/packages/universal-swap/src/msg/chains/chain.ts b/packages/universal-swap/src/msg/chains/chain.ts index 1e92800f..e901d0f6 100644 --- a/packages/universal-swap/src/msg/chains/chain.ts +++ b/packages/universal-swap/src/msg/chains/chain.ts @@ -1,3 +1,4 @@ +import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types"; import { Path } from "../../types"; import { validatePath, validateReceiver } from "../common"; @@ -7,7 +8,8 @@ export class ChainMsg { protected minimumReceive: string, protected receiver: string, protected currentChainAddress: string, - protected memo: string = "" + protected memo: string = "", + protected affiliates: Affiliate[] = [] ) { // validate path validatePath(path); diff --git a/packages/universal-swap/src/msg/chains/cosmos.ts b/packages/universal-swap/src/msg/chains/cosmos.ts index 85c3b7a3..c2ecf300 100644 --- a/packages/universal-swap/src/msg/chains/cosmos.ts +++ b/packages/universal-swap/src/msg/chains/cosmos.ts @@ -11,10 +11,18 @@ import { import { EncodeObject } from "@cosmjs/proto-signing"; import { ChainMsg } from "./chain"; +import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types"; export class CosmosMsg extends ChainMsg { - constructor(path: Path, minimumReceive: string, receiver: string, currentChainAddress: string, memo: string = "") { - super(path, minimumReceive, receiver, currentChainAddress, memo); + constructor( + path: Path, + minimumReceive: string, + receiver: string, + currentChainAddress: string, + memo: string = "", + affiliates: Affiliate[] + ) { + super(path, minimumReceive, receiver, currentChainAddress, memo, affiliates); } setMinimumReceiveForSwap(slippage: number = 0.01) { diff --git a/packages/universal-swap/src/msg/chains/oraichain.ts b/packages/universal-swap/src/msg/chains/oraichain.ts index 1a4fc0a8..734feb91 100644 --- a/packages/universal-swap/src/msg/chains/oraichain.ts +++ b/packages/universal-swap/src/msg/chains/oraichain.ts @@ -23,6 +23,7 @@ import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx"; import { TransferBackMsg } from "@oraichain/common-contracts-sdk/build/CwIcs20Latest.types"; import { toUtf8 } from "@cosmjs/encoding"; import { ChainMsg } from "./chain"; +import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types"; export class OraichainMsg extends ChainMsg { SWAP_VENUE_NAME = "oraidex"; @@ -36,9 +37,10 @@ export class OraichainMsg extends ChainMsg { currentChainAddress: string, memo: string = "", protected destPrefix: string = undefined, - protected obridgeAddress: string = undefined + protected obridgeAddress: string = undefined, + affiliates: Affiliate[] = [] ) { - super(path, minimumReceive, receiver, currentChainAddress, memo); + super(path, minimumReceive, receiver, currentChainAddress, memo, affiliates); // check chainId = "Oraichain" if (path.chainId !== "Oraichain") { throw generateError("This path must be on Oraichain"); @@ -443,7 +445,7 @@ export class OraichainMsg extends ChainMsg { min_asset: min_asset, timeout_timestamp: +calculateTimeoutTimestamp(IBC_TRANSFER_TIMEOUT), post_swap_action: this.getPostAction(bridgeInfo), - affiliates: [] + affiliates: this.affiliates } }; @@ -613,7 +615,7 @@ export class OraichainMsg extends ChainMsg { // swap and action let msg: ExecuteMsg = { swap_and_action: { - affiliates: [], + affiliates: this.affiliates, min_asset, post_swap_action: this.getPostAction(bridgeInfo), timeout_timestamp: +calculateTimeoutTimestamp(IBC_TRANSFER_TIMEOUT), diff --git a/packages/universal-swap/src/msg/chains/osmosis.ts b/packages/universal-swap/src/msg/chains/osmosis.ts index afd55762..4ec0493d 100644 --- a/packages/universal-swap/src/msg/chains/osmosis.ts +++ b/packages/universal-swap/src/msg/chains/osmosis.ts @@ -15,13 +15,21 @@ import { EncodeObject } from "@cosmjs/proto-signing"; import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx"; import { toUtf8 } from "@cosmjs/encoding"; import { ChainMsg } from "./chain"; +import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types"; export class OsmosisMsg extends ChainMsg { SWAP_VENUE_NAME = "osmosis-poolmanager"; ENTRY_POINT_CONTRACT = "osmo1h3jkejkcpthl45xrrm5geed3eq75p5rgfce9taufkwfr89k63muqweu2y7"; - constructor(path: Path, minimumReceive: string, receiver: string, currentChainAddress: string, memo: string = "") { - super(path, minimumReceive, receiver, currentChainAddress, memo); + constructor( + path: Path, + minimumReceive: string, + receiver: string, + currentChainAddress: string, + memo: string = "", + affiliates: Affiliate[] = [] + ) { + super(path, minimumReceive, receiver, currentChainAddress, memo, affiliates); // check chainId = "osmosis-1" if (path.chainId !== "osmosis-1") { throw generateError("This path must be on Osmosis"); @@ -190,7 +198,7 @@ export class OsmosisMsg extends ChainMsg { min_asset: min_asset, timeout_timestamp: +calculateTimeoutTimestamp(IBC_TRANSFER_TIMEOUT), post_swap_action: this.getPostAction(bridgeInfo), - affiliates: [] + affiliates: this.affiliates } }; @@ -257,7 +265,7 @@ export class OsmosisMsg extends ChainMsg { // swap and action let msg: ExecuteMsg = { swap_and_action: { - affiliates: [], + affiliates: this.affiliates, min_asset, post_swap_action: this.getPostAction(bridgeInfo), timeout_timestamp: +calculateTimeoutTimestamp(IBC_TRANSFER_TIMEOUT), diff --git a/packages/universal-swap/src/msg/msgs.ts b/packages/universal-swap/src/msg/msgs.ts index 33562d7f..f0563018 100644 --- a/packages/universal-swap/src/msg/msgs.ts +++ b/packages/universal-swap/src/msg/msgs.ts @@ -12,6 +12,7 @@ import { Path, Route } from "../types"; import { CosmosMsg, OraichainMsg, OsmosisMsg } from "./chains"; import { MiddlewareResponse } from "./types"; import { EncodeObject } from "@cosmjs/proto-signing"; +import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types"; const getDestPrefixForBridgeToEvmOnOrai = (chainId: string): string => { const prefixMap: { [key: string]: string } = { @@ -35,7 +36,8 @@ const buildMemoSwap = ( memo: string, addresses: { [chainId: string]: string }, slippage: number = 0.01, - previousChain?: string + previousChain?: string, + affiliates?: Affiliate[] ): MiddlewareResponse => { let currentChain = path.chainId; let currentAddress = addresses[currentChain]; @@ -49,7 +51,16 @@ const buildMemoSwap = ( throw generateError(`Missing oBridge address for ${ORAIBRIDGE_SUBNET}`); } - let oraichainMsg = new OraichainMsg(path, "1", receiver, currentAddress, memo, prefix, oBridgeAddress); + let oraichainMsg = new OraichainMsg( + path, + "1", + receiver, + currentAddress, + memo, + prefix, + oBridgeAddress, + affiliates + ); oraichainMsg.setMinimumReceiveForSwap(slippage); // we have 2 cases: // - Previous chain use IBC bridge to Oraichain @@ -61,7 +72,7 @@ const buildMemoSwap = ( return msgInfo; } case "osmosis-1": { - let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo); + let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo, []); cosmosMsg.setMinimumReceiveForSwap(slippage); let msgInfo = cosmosMsg.genMemoAsMiddleware(); return msgInfo; @@ -73,7 +84,7 @@ const buildMemoSwap = ( if (currentChain.startsWith("0x")) { throw generateError("Don't support universal swap in EVM"); } - let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo); + let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo, []); cosmosMsg.setMinimumReceiveForSwap(slippage); let msgInfo = cosmosMsg.genMemoAsMiddleware(); return msgInfo; @@ -86,7 +97,8 @@ const buildExecuteMsg = ( receiver: string, memo: string, addresses: { [chainId: string]: string }, - slippage: number = 0.01 + slippage: number = 0.01, + affiliates?: Affiliate[] ): EncodeObject => { let currentChain = path.chainId; let currentAddress = addresses[currentChain]; @@ -99,12 +111,21 @@ const buildExecuteMsg = ( throw generateError(`Missing oBridge address for ${ORAIBRIDGE_SUBNET}`); } - let oraichainMsg = new OraichainMsg(path, "1", receiver, currentAddress, memo, prefix, oBridgeAddress); + let oraichainMsg = new OraichainMsg( + path, + "1", + receiver, + currentAddress, + memo, + prefix, + oBridgeAddress, + affiliates + ); oraichainMsg.setMinimumReceiveForSwap(slippage); return oraichainMsg.genExecuteMsg(); } case "osmosis-1": { - let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo); + let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo, []); cosmosMsg.setMinimumReceiveForSwap(slippage); return cosmosMsg.genExecuteMsg(); } @@ -115,7 +136,7 @@ const buildExecuteMsg = ( if (currentChain.startsWith("0x")) { throw generateError("Don't support universal swap in EVM"); } - let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo); + let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo, []); cosmosMsg.setMinimumReceiveForSwap(slippage); return cosmosMsg.genExecuteMsg(); } @@ -126,7 +147,8 @@ export const generateMsgSwap = ( route: Route, slippage: number = 0.01, addresses: { [chainId: string]: string }, - recipientAddress?: string + recipientAddress?: string, + affiliates?: Affiliate[] ): EncodeObject => { if (route.paths.length == 0) { throw generateError("Require at least 1 action"); @@ -143,12 +165,20 @@ export const generateMsgSwap = ( // generate memo for univeral swap for (let i = route.paths.length - 1; i > 0; i--) { - let swapInfo = buildMemoSwap(route.paths[i], receiver, memo, addresses, slippage, route.paths[i - 1].chainId); + let swapInfo = buildMemoSwap( + route.paths[i], + receiver, + memo, + addresses, + slippage, + route.paths[i - 1].chainId, + affiliates + ); memo = swapInfo.memo; receiver = swapInfo.receiver; } - return buildExecuteMsg(route.paths[0], receiver, memo, addresses, slippage); + return buildExecuteMsg(route.paths[0], receiver, memo, addresses, slippage, affiliates); }; export const generateMemoSwap = ( @@ -156,7 +186,8 @@ export const generateMemoSwap = ( slippage: number = 0.01, addresses: { [chainId: string]: string }, recipientAddress?: string, - previousChain?: string + previousChain?: string, + affiliates?: Affiliate[] ): MiddlewareResponse => { if (route.paths.length == 0) { return { @@ -176,9 +207,17 @@ export const generateMemoSwap = ( // generate memo for univeral swap for (let i = route.paths.length - 1; i > 0; i--) { - let swapInfo = buildMemoSwap(route.paths[i], receiver, memo, addresses, slippage, route.paths[i - 1].chainId); + let swapInfo = buildMemoSwap( + route.paths[i], + receiver, + memo, + addresses, + slippage, + route.paths[i - 1].chainId, + affiliates + ); memo = swapInfo.memo; receiver = swapInfo.receiver; } - return buildMemoSwap(route.paths[0], receiver, memo, addresses, slippage, previousChain); + return buildMemoSwap(route.paths[0], receiver, memo, addresses, slippage, previousChain, affiliates); };