Skip to content

Commit

Permalink
add features when has recipient address
Browse files Browse the repository at this point in the history
  • Loading branch information
haunv3 committed Oct 9, 2024
1 parent 7282679 commit 9986a13
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
13 changes: 7 additions & 6 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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 (
Expand Down
33 changes: 27 additions & 6 deletions packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -384,24 +401,28 @@ 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];

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
);

Expand Down

0 comments on commit 9986a13

Please sign in to comment.