Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/intergrate_common
Browse files Browse the repository at this point in the history
  • Loading branch information
trungbach committed Dec 20, 2024
2 parents 8f87343 + 8e6f873 commit 93ab0e2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
15 changes: 11 additions & 4 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,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 @@ -863,7 +870,8 @@ export class UniversalSwapHandler {
}

const msgs = alphaSmartRoutes.routes.map((route) => {
return generateMsgSwap(route, userSlippage / 100, receiverAddresses, this.oraidexCommon);
return generateMsgSwap(route, userSlippage / 100, receiverAddresses, this.oraidexCommon, recipientAddress);
// return generateMsgSwap(route, userSlippage / 100, receiverAddresses, recipientAddress);
});

const { client } = await this.config.cosmosWallet.getCosmWasmClient(
Expand Down Expand Up @@ -1073,7 +1081,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 @@ -500,8 +500,6 @@ export class UniversalSwapHelper {
cosmosChains: oraidexCommon.cosmosChains
});

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

const { memo } = generateMemoSwap(
{
...alphaRoutes,
Expand All @@ -510,6 +508,7 @@ export class UniversalSwapHelper {
userSlippage / 100,
receiverAddresses,
oraidexCommon,
addresses.recipientAddress,
alphaRoutes.paths[0].chainId
);

Expand Down
33 changes: 29 additions & 4 deletions packages/universal-swap/src/msg/msgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {
ORAI_BRIDGE_EVM_DENOM_PREFIX,
ORAI_BRIDGE_EVM_TRON_DENOM_PREFIX,
ORAI_BRIDGE_EVM_ETH_DENOM_PREFIX,
OraidexCommon
OraidexCommon,
checkValidateAddressWithNetwork
} from "@oraichain/oraidex-common";
import { Path, Route } from "../types";
import { CosmosMsg, OraichainMsg, OsmosisMsg } from "./chains";
import { MiddlewareResponse } from "./types";
import { EncodeObject } from "@cosmjs/proto-signing";
import { NetworkChainId } from "@oraichain/common";

const getDestPrefixForBridgeToEvmOnOrai = (chainId: string): string => {
const prefixMap: { [key: string]: string } = {
Expand Down Expand Up @@ -146,13 +148,25 @@ export const generateMsgSwap = (
route: Route,
slippage: number = 0.01,
addresses: { [chainId: string]: string },
oraidexCommon: OraidexCommon
oraidexCommon: OraidexCommon,
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,
oraidexCommon.cosmosChains
);
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 @@ -177,6 +191,7 @@ export const generateMemoSwap = (
slippage: number = 0.01,
addresses: { [chainId: string]: string },
oraidexCommon: OraidexCommon,
recipientAddress?: string,
previousChain?: string
): MiddlewareResponse => {
if (route.paths.length == 0) {
Expand All @@ -187,7 +202,17 @@ 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,
oraidexCommon.cosmosChains
);
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 93ab0e2

Please sign in to comment.