From a0343912fdd7b44645e66f145afb6b63fbd4f139 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Van Date: Thu, 19 Dec 2024 09:55:20 +0700 Subject: [PATCH] fix recipient address when universal swap --- packages/universal-swap/package.json | 2 +- packages/universal-swap/src/handler.ts | 15 ++++++++++----- packages/universal-swap/src/helper.ts | 3 +-- packages/universal-swap/src/msg/msgs.ts | 25 +++++++++++++++++++++---- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/packages/universal-swap/package.json b/packages/universal-swap/package.json index 6e78231d..2d3f52e1 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.25", + "version": "1.1.26", "main": "build/index.js", "files": [ "build/" diff --git a/packages/universal-swap/src/handler.ts b/packages/universal-swap/src/handler.ts index 32305826..426f2e68 100644 --- a/packages/universal-swap/src/handler.ts +++ b/packages/universal-swap/src/handler.ts @@ -66,7 +66,6 @@ import { OraiswapRouterQueryClient } from "@oraichain/oraidex-contracts-sdk"; import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types"; import { COSMOS_CHAIN_IDS, EVM_CHAIN_IDS } from "@oraichain/common"; import { generateMsgSwap } from "./msg/msgs"; -// import { calculateTimeoutTimestampTon, createTonBridgeHandler } from "@oraichain/tonbridge-sdk"; import { toNano } from "@ton/core"; import { getHttpEndpoint } from "@orbs-network/ton-access"; @@ -763,8 +762,15 @@ export class UniversalSwapHandler { } async alphaSmartRouterSwapNewMsg(swapRoute, universalSwapType, receiverAddresses) { - const { sender, originalFromToken, originalToToken, simulateAmount, alphaSmartRoutes, userSlippage } = - this.swapData; + const { + sender, + originalFromToken, + originalToToken, + simulateAmount, + alphaSmartRoutes, + userSlippage, + recipientAddress + } = this.swapData; const universalSwapTypeFromCosmos = [ "oraichain-to-oraichain", @@ -801,7 +807,7 @@ export class UniversalSwapHandler { } const msgs = alphaSmartRoutes.routes.map((route) => { - return generateMsgSwap(route, userSlippage / 100, receiverAddresses); + return generateMsgSwap(route, userSlippage / 100, receiverAddresses, recipientAddress); }); const { client } = await this.config.cosmosWallet.getCosmWasmClient( @@ -998,7 +1004,6 @@ export class UniversalSwapHandler { if (swapOptions?.isAlphaIbcWasm) { let receiverAddresses = UniversalSwapHelper.generateAddress(addressParams); - if (recipientAddress) receiverAddresses[originalToToken.chainId] = toAddress; return this.alphaSmartRouterSwapNewMsg(swapRoute, universalSwapType, receiverAddresses); } diff --git a/packages/universal-swap/src/helper.ts b/packages/universal-swap/src/helper.ts index 9525be91..bce6058b 100644 --- a/packages/universal-swap/src/helper.ts +++ b/packages/universal-swap/src/helper.ts @@ -482,8 +482,6 @@ export class UniversalSwapHelper { evmInfo }); - if (addresses?.recipientAddress) receiverAddresses[toToken.chainId] = addresses?.recipientAddress; - const { memo } = generateMemoSwap( { ...alphaRoutes, @@ -491,6 +489,7 @@ export class UniversalSwapHelper { }, userSlippage / 100, receiverAddresses, + addresses.recipientAddress, alphaRoutes.paths[0].chainId ); diff --git a/packages/universal-swap/src/msg/msgs.ts b/packages/universal-swap/src/msg/msgs.ts index fbf136e0..33562d7f 100644 --- a/packages/universal-swap/src/msg/msgs.ts +++ b/packages/universal-swap/src/msg/msgs.ts @@ -4,7 +4,9 @@ import { isTonChain, ORAI_BRIDGE_EVM_DENOM_PREFIX, ORAI_BRIDGE_EVM_TRON_DENOM_PREFIX, - ORAI_BRIDGE_EVM_ETH_DENOM_PREFIX + ORAI_BRIDGE_EVM_ETH_DENOM_PREFIX, + checkValidateAddressWithNetwork, + NetworkChainId } from "@oraichain/oraidex-common"; import { Path, Route } from "../types"; import { CosmosMsg, OraichainMsg, OsmosisMsg } from "./chains"; @@ -123,13 +125,21 @@ const buildExecuteMsg = ( export const generateMsgSwap = ( route: Route, slippage: number = 0.01, - addresses: { [chainId: string]: string } + addresses: { [chainId: string]: string }, + recipientAddress?: string ): EncodeObject => { if (route.paths.length == 0) { throw generateError("Require at least 1 action"); } let memo: string = ""; - let receiver = addresses[route.paths.at(-1)?.tokenOutChainId]; + const tokenOutChainId = route.paths.at(-1)?.tokenOutChainId; + let receiver = addresses[tokenOutChainId]; + + if (recipientAddress) { + const isValidRecipient = checkValidateAddressWithNetwork(recipientAddress, tokenOutChainId as NetworkChainId); + if (!isValidRecipient.isValid) throw generateError("Recipient address invalid when generateMsgSwap!"); + receiver = recipientAddress; + } // generate memo for univeral swap for (let i = route.paths.length - 1; i > 0; i--) { @@ -145,6 +155,7 @@ export const generateMemoSwap = ( route: Route, slippage: number = 0.01, addresses: { [chainId: string]: string }, + recipientAddress?: string, previousChain?: string ): MiddlewareResponse => { if (route.paths.length == 0) { @@ -155,7 +166,13 @@ export const generateMemoSwap = ( } let memo: string = ""; - let receiver = addresses[route.paths.at(-1)?.tokenOutChainId]; + const tokenOutChainId = route.paths.at(-1)?.tokenOutChainId; + let receiver = addresses[tokenOutChainId]; + if (recipientAddress) { + const isValidRecipient = checkValidateAddressWithNetwork(recipientAddress, tokenOutChainId as NetworkChainId); + if (!isValidRecipient.isValid) throw generateError("Recipient address invalid when generateMemoSwap!"); + receiver = recipientAddress; + } // generate memo for univeral swap for (let i = route.paths.length - 1; i > 0; i--) {