Skip to content

Commit

Permalink
Merge pull request #383 from oraidex/feat/build-memo-with-ton
Browse files Browse the repository at this point in the history
fix recipient address when universal swap
  • Loading branch information
haunv3 authored Dec 20, 2024
2 parents de9618c + a034391 commit 8e6f873
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
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.1.25",
"version": "1.1.26",
"main": "build/index.js",
"files": [
"build/"
Expand Down
15 changes: 10 additions & 5 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,15 +482,14 @@ export class UniversalSwapHelper {
evmInfo
});

if (addresses?.recipientAddress) receiverAddresses[toToken.chainId] = addresses?.recipientAddress;

const { memo } = generateMemoSwap(
{
...alphaRoutes,
paths: paths
},
userSlippage / 100,
receiverAddresses,
addresses.recipientAddress,
alphaRoutes.paths[0].chainId
);

Expand Down
25 changes: 21 additions & 4 deletions packages/universal-swap/src/msg/msgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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--) {
Expand All @@ -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) {
Expand All @@ -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--) {
Expand Down

0 comments on commit 8e6f873

Please sign in to comment.