Skip to content

Commit

Permalink
add universal swap 1.1.27-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
haunv3 committed Jan 14, 2025
1 parent a229111 commit bf5d216
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 31 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.26",
"version": "1.1.27-beta.1",
"main": "build/index.js",
"files": [
"build/"
Expand Down
14 changes: 11 additions & 3 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ export class UniversalSwapHandler {
minimumReceive,
userSlippage,
this.config.swapOptions,
alphaSmartRoutes
alphaSmartRoutes,
this.swapData.affiliates
);
const swapRouteSplit = completeSwapRoute.split(":");
const swapRoute = swapRouteSplit.length === 1 ? "" : swapRouteSplit[1];
Expand Down Expand Up @@ -807,7 +808,13 @@ export class UniversalSwapHandler {
}

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

const { client } = await this.config.cosmosWallet.getCosmWasmClient(
Expand Down Expand Up @@ -999,7 +1006,8 @@ export class UniversalSwapHandler {
minimumReceive,
userSlippage,
this.config.swapOptions,
alphaSmartRoutes
alphaSmartRoutes,
this.swapData.affiliates
);

if (swapOptions?.isAlphaIbcWasm) {
Expand Down
6 changes: 4 additions & 2 deletions packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ export class UniversalSwapHelper {
ibcInfoTestMode?: boolean;
isAlphaIbcWasm?: boolean;
},
alphaSmartRoute?: RouterResponse
alphaSmartRoute?: RouterResponse,
affiliate?: Affiliate[]
): Promise<SwapRoute> => {
// TODO: recheck cosmos address undefined (other-chain -> oraichain)
if (!addresses.sourceReceiver) throw generateError(`Cannot get source if the sourceReceiver is empty!`);
Expand Down Expand Up @@ -490,7 +491,8 @@ export class UniversalSwapHelper {
userSlippage / 100,
receiverAddresses,
addresses.recipientAddress,
alphaRoutes.paths[0].chainId
alphaRoutes.paths[0].chainId,
affiliate
);

swapRoute = memo;
Expand Down
4 changes: 3 additions & 1 deletion packages/universal-swap/src/msg/chains/chain.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types";
import { Path } from "../../types";
import { validatePath, validateReceiver } from "../common";

Expand All @@ -7,7 +8,8 @@ export class ChainMsg {
protected minimumReceive: string,
protected receiver: string,
protected currentChainAddress: string,
protected memo: string = ""
protected memo: string = "",
protected affiliates: Affiliate[] = []
) {
// validate path
validatePath(path);
Expand Down
12 changes: 10 additions & 2 deletions packages/universal-swap/src/msg/chains/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ import {

import { EncodeObject } from "@cosmjs/proto-signing";
import { ChainMsg } from "./chain";
import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types";

export class CosmosMsg extends ChainMsg {
constructor(path: Path, minimumReceive: string, receiver: string, currentChainAddress: string, memo: string = "") {
super(path, minimumReceive, receiver, currentChainAddress, memo);
constructor(
path: Path,
minimumReceive: string,
receiver: string,
currentChainAddress: string,
memo: string = "",
affiliates: Affiliate[]
) {
super(path, minimumReceive, receiver, currentChainAddress, memo, affiliates);
}

setMinimumReceiveForSwap(slippage: number = 0.01) {
Expand Down
10 changes: 6 additions & 4 deletions packages/universal-swap/src/msg/chains/oraichain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx";
import { TransferBackMsg } from "@oraichain/common-contracts-sdk/build/CwIcs20Latest.types";
import { toUtf8 } from "@cosmjs/encoding";
import { ChainMsg } from "./chain";
import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types";

export class OraichainMsg extends ChainMsg {
SWAP_VENUE_NAME = "oraidex";
Expand All @@ -36,9 +37,10 @@ export class OraichainMsg extends ChainMsg {
currentChainAddress: string,
memo: string = "",
protected destPrefix: string = undefined,
protected obridgeAddress: string = undefined
protected obridgeAddress: string = undefined,
affiliates: Affiliate[] = []
) {
super(path, minimumReceive, receiver, currentChainAddress, memo);
super(path, minimumReceive, receiver, currentChainAddress, memo, affiliates);
// check chainId = "Oraichain"
if (path.chainId !== "Oraichain") {
throw generateError("This path must be on Oraichain");
Expand Down Expand Up @@ -443,7 +445,7 @@ export class OraichainMsg extends ChainMsg {
min_asset: min_asset,
timeout_timestamp: +calculateTimeoutTimestamp(IBC_TRANSFER_TIMEOUT),
post_swap_action: this.getPostAction(bridgeInfo),
affiliates: []
affiliates: this.affiliates
}
};

Expand Down Expand Up @@ -613,7 +615,7 @@ export class OraichainMsg extends ChainMsg {
// swap and action
let msg: ExecuteMsg = {
swap_and_action: {
affiliates: [],
affiliates: this.affiliates,
min_asset,
post_swap_action: this.getPostAction(bridgeInfo),
timeout_timestamp: +calculateTimeoutTimestamp(IBC_TRANSFER_TIMEOUT),
Expand Down
16 changes: 12 additions & 4 deletions packages/universal-swap/src/msg/chains/osmosis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ import { EncodeObject } from "@cosmjs/proto-signing";
import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx";
import { toUtf8 } from "@cosmjs/encoding";
import { ChainMsg } from "./chain";
import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types";

export class OsmosisMsg extends ChainMsg {
SWAP_VENUE_NAME = "osmosis-poolmanager";
ENTRY_POINT_CONTRACT = "osmo1h3jkejkcpthl45xrrm5geed3eq75p5rgfce9taufkwfr89k63muqweu2y7";

constructor(path: Path, minimumReceive: string, receiver: string, currentChainAddress: string, memo: string = "") {
super(path, minimumReceive, receiver, currentChainAddress, memo);
constructor(
path: Path,
minimumReceive: string,
receiver: string,
currentChainAddress: string,
memo: string = "",
affiliates: Affiliate[] = []
) {
super(path, minimumReceive, receiver, currentChainAddress, memo, affiliates);
// check chainId = "osmosis-1"
if (path.chainId !== "osmosis-1") {
throw generateError("This path must be on Osmosis");
Expand Down Expand Up @@ -190,7 +198,7 @@ export class OsmosisMsg extends ChainMsg {
min_asset: min_asset,
timeout_timestamp: +calculateTimeoutTimestamp(IBC_TRANSFER_TIMEOUT),
post_swap_action: this.getPostAction(bridgeInfo),
affiliates: []
affiliates: this.affiliates
}
};

Expand Down Expand Up @@ -257,7 +265,7 @@ export class OsmosisMsg extends ChainMsg {
// swap and action
let msg: ExecuteMsg = {
swap_and_action: {
affiliates: [],
affiliates: this.affiliates,
min_asset,
post_swap_action: this.getPostAction(bridgeInfo),
timeout_timestamp: +calculateTimeoutTimestamp(IBC_TRANSFER_TIMEOUT),
Expand Down
67 changes: 53 additions & 14 deletions packages/universal-swap/src/msg/msgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Path, Route } from "../types";
import { CosmosMsg, OraichainMsg, OsmosisMsg } from "./chains";
import { MiddlewareResponse } from "./types";
import { EncodeObject } from "@cosmjs/proto-signing";
import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types";

const getDestPrefixForBridgeToEvmOnOrai = (chainId: string): string => {
const prefixMap: { [key: string]: string } = {
Expand All @@ -35,7 +36,8 @@ const buildMemoSwap = (
memo: string,
addresses: { [chainId: string]: string },
slippage: number = 0.01,
previousChain?: string
previousChain?: string,
affiliates?: Affiliate[]
): MiddlewareResponse => {
let currentChain = path.chainId;
let currentAddress = addresses[currentChain];
Expand All @@ -49,7 +51,16 @@ const buildMemoSwap = (
throw generateError(`Missing oBridge address for ${ORAIBRIDGE_SUBNET}`);
}

let oraichainMsg = new OraichainMsg(path, "1", receiver, currentAddress, memo, prefix, oBridgeAddress);
let oraichainMsg = new OraichainMsg(
path,
"1",
receiver,
currentAddress,
memo,
prefix,
oBridgeAddress,
affiliates
);
oraichainMsg.setMinimumReceiveForSwap(slippage);
// we have 2 cases:
// - Previous chain use IBC bridge to Oraichain
Expand All @@ -61,7 +72,7 @@ const buildMemoSwap = (
return msgInfo;
}
case "osmosis-1": {
let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo);
let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo, []);
cosmosMsg.setMinimumReceiveForSwap(slippage);
let msgInfo = cosmosMsg.genMemoAsMiddleware();
return msgInfo;
Expand All @@ -73,7 +84,7 @@ const buildMemoSwap = (
if (currentChain.startsWith("0x")) {
throw generateError("Don't support universal swap in EVM");
}
let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo);
let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo, []);
cosmosMsg.setMinimumReceiveForSwap(slippage);
let msgInfo = cosmosMsg.genMemoAsMiddleware();
return msgInfo;
Expand All @@ -86,7 +97,8 @@ const buildExecuteMsg = (
receiver: string,
memo: string,
addresses: { [chainId: string]: string },
slippage: number = 0.01
slippage: number = 0.01,
affiliates?: Affiliate[]
): EncodeObject => {
let currentChain = path.chainId;
let currentAddress = addresses[currentChain];
Expand All @@ -99,12 +111,21 @@ const buildExecuteMsg = (
throw generateError(`Missing oBridge address for ${ORAIBRIDGE_SUBNET}`);
}

let oraichainMsg = new OraichainMsg(path, "1", receiver, currentAddress, memo, prefix, oBridgeAddress);
let oraichainMsg = new OraichainMsg(
path,
"1",
receiver,
currentAddress,
memo,
prefix,
oBridgeAddress,
affiliates
);
oraichainMsg.setMinimumReceiveForSwap(slippage);
return oraichainMsg.genExecuteMsg();
}
case "osmosis-1": {
let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo);
let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo, []);
cosmosMsg.setMinimumReceiveForSwap(slippage);
return cosmosMsg.genExecuteMsg();
}
Expand All @@ -115,7 +136,7 @@ const buildExecuteMsg = (
if (currentChain.startsWith("0x")) {
throw generateError("Don't support universal swap in EVM");
}
let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo);
let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo, []);
cosmosMsg.setMinimumReceiveForSwap(slippage);
return cosmosMsg.genExecuteMsg();
}
Expand All @@ -126,7 +147,8 @@ export const generateMsgSwap = (
route: Route,
slippage: number = 0.01,
addresses: { [chainId: string]: string },
recipientAddress?: string
recipientAddress?: string,
affiliates?: Affiliate[]
): EncodeObject => {
if (route.paths.length == 0) {
throw generateError("Require at least 1 action");
Expand All @@ -143,20 +165,29 @@ export const generateMsgSwap = (

// generate memo for univeral swap
for (let i = route.paths.length - 1; i > 0; i--) {
let swapInfo = buildMemoSwap(route.paths[i], receiver, memo, addresses, slippage, route.paths[i - 1].chainId);
let swapInfo = buildMemoSwap(
route.paths[i],
receiver,
memo,
addresses,
slippage,
route.paths[i - 1].chainId,
affiliates
);
memo = swapInfo.memo;
receiver = swapInfo.receiver;
}

return buildExecuteMsg(route.paths[0], receiver, memo, addresses, slippage);
return buildExecuteMsg(route.paths[0], receiver, memo, addresses, slippage, affiliates);
};

export const generateMemoSwap = (
route: Route,
slippage: number = 0.01,
addresses: { [chainId: string]: string },
recipientAddress?: string,
previousChain?: string
previousChain?: string,
affiliates?: Affiliate[]
): MiddlewareResponse => {
if (route.paths.length == 0) {
return {
Expand All @@ -176,9 +207,17 @@ export const generateMemoSwap = (

// generate memo for univeral swap
for (let i = route.paths.length - 1; i > 0; i--) {
let swapInfo = buildMemoSwap(route.paths[i], receiver, memo, addresses, slippage, route.paths[i - 1].chainId);
let swapInfo = buildMemoSwap(
route.paths[i],
receiver,
memo,
addresses,
slippage,
route.paths[i - 1].chainId,
affiliates
);
memo = swapInfo.memo;
receiver = swapInfo.receiver;
}
return buildMemoSwap(route.paths[0], receiver, memo, addresses, slippage, previousChain);
return buildMemoSwap(route.paths[0], receiver, memo, addresses, slippage, previousChain, affiliates);
};

0 comments on commit bf5d216

Please sign in to comment.