From 9986a13dd40c03437b4545f02c9abfd58a729a5a Mon Sep 17 00:00:00 2001 From: Hau Nguyen Van Date: Wed, 9 Oct 2024 15:55:57 +0700 Subject: [PATCH] add features when has recipient address --- packages/universal-swap/src/handler.ts | 13 +++++----- packages/universal-swap/src/helper.ts | 33 +++++++++++++++++++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/packages/universal-swap/src/handler.ts b/packages/universal-swap/src/handler.ts index ce7cd05c..deb98a99 100644 --- a/packages/universal-swap/src/handler.ts +++ b/packages/universal-swap/src/handler.ts @@ -1209,7 +1209,10 @@ export class UniversalSwapHandler { if (swapOptions?.isAlphaIbcWasm) { const routesFlatten = UniversalSwapHelper.flattenSmartRouters(alphaSmartRoutes.routes); - const hasInjectiveAddress = routesFlatten.some((route) => route.chainId === COSMOS_CHAIN_IDS.INJECTVE); + const hasInjectiveAddress = routesFlatten.some((route) => + [route.chainId, route.tokenOutChainId].includes(COSMOS_CHAIN_IDS.INJECTVE) + ); + if (hasInjectiveAddress) injAddress = await this.config.cosmosWallet.getKeplrAddr(COSMOS_CHAIN_IDS.INJECTVE); } @@ -1229,11 +1232,9 @@ export class UniversalSwapHandler { ); if (alphaSmartRoutes?.routes?.length && swapOptions.isAlphaIbcWasm) { - return this.alphaSmartRouterSwapNewMsg( - swapRoute, - universalSwapType, - UniversalSwapHelper.generateAddress({ oraiAddress, injAddress }) - ); + let receiverAddresses = UniversalSwapHelper.generateAddress({ oraiAddress, injAddress }); + if (recipientAddress) receiverAddresses[currentToNetwork] = toAddress; + return this.alphaSmartRouterSwapNewMsg(swapRoute, universalSwapType, receiverAddresses); } if ( diff --git a/packages/universal-swap/src/helper.ts b/packages/universal-swap/src/helper.ts index 0fd2bb42..43ff3ff2 100644 --- a/packages/universal-swap/src/helper.ts +++ b/packages/universal-swap/src/helper.ts @@ -316,11 +316,22 @@ export class UniversalSwapHelper { 118: address118, 60: address60 }; + if (!(coinType in approve)) { + throw new Error(`Unsupported coinType: ${coinType}`); + } + + if (!approve[coinType]) { + throw new Error(`Address for coinType ${coinType} is not provided`); + } + const { data } = fromBech32(approve[coinType]); return toBech32(prefix, data); }; static generateAddress = ({ oraiAddress, injAddress }) => { + /** + * need update when support new chain + */ const addressFollowCoinType = { address60: injAddress, address118: oraiAddress }; return { [COSMOS_CHAIN_ID_COMMON.INJECTVE_CHAIN_ID]: injAddress, @@ -334,7 +345,13 @@ export class UniversalSwapHelper { }; static addOraiBridgeRoute = async ( - addresses: { obridgeAddress?: string; sourceReceiver: string; injAddress?: string; destReceiver?: string }, + addresses: { + obridgeAddress?: string; + sourceReceiver: string; + injAddress?: string; + destReceiver?: string; + recipientAddress?: string; + }, fromToken: TokenItemType, toToken: TokenItemType, minimumReceive: string, @@ -384,7 +401,7 @@ export class UniversalSwapHelper { /** * useAlphaIbcWasm case: (evm -> oraichain -> osmosis -> inj/tia not using wasm) */ - if (swapOption.isAlphaIbcWasm) { + if (swapOption.isAlphaIbcWasm && !fromToken.cosmosBased) { if (!alphaSmartRoute) throw generateError(`Missing router with alpha ibc wasm!`); const routes = alphaSmartRoute.routes; const alphaRoutes = routes[0]; @@ -392,16 +409,20 @@ export class UniversalSwapHelper { if (alphaSmartRoute.routes.length > 1) throw generateError(`Missing router with alpha ibc wasm max length!`); const paths = alphaRoutes.paths.filter((_, index) => index > 0); + + let receiverAddresses = UniversalSwapHelper.generateAddress({ + injAddress: addresses.injAddress, + oraiAddress: addresses.sourceReceiver + }); + + if (addresses?.recipientAddress) receiverAddresses[toToken.chainId] = addresses?.recipientAddress; const { memo } = generateMemoSwap( { ...alphaRoutes, paths: paths }, userSlippage / 100, - UniversalSwapHelper.generateAddress({ - injAddress: addresses.injAddress, - oraiAddress: addresses.sourceReceiver - }), + receiverAddresses, alphaRoutes.paths[0].chainId );