Skip to content

Feat/add affiliates #392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.3.2",
"version": "1.3.3",
"main": "build/index.js",
"files": [
"build/"
Expand Down
13 changes: 10 additions & 3 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,8 @@ export class UniversalSwapHandler {
simulateAmount,
alphaSmartRoutes,
userSlippage,
recipientAddress
recipientAddress,
affiliates
} = this.swapData;

const universalSwapTypeFromCosmos = [
Expand Down Expand Up @@ -870,8 +871,14 @@ export class UniversalSwapHandler {
}

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

const { client } = await this.config.cosmosWallet.getCosmWasmClient(
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,6 +1,7 @@
import { OraidexCommon } from "@oraichain/oraidex-common";
import { Path } from "../../types";
import { validatePath, validateReceiver } from "../common";
import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types";

export class ChainMsg {
constructor(
Expand All @@ -9,7 +10,8 @@ export class ChainMsg {
protected receiver: string,
protected currentChainAddress: string,
protected memo: string = "",
protected oraidexCommon: OraidexCommon
protected oraidexCommon: OraidexCommon,
protected affiliates: Affiliate[] = []
) {
// validate path
validatePath(path);
Expand Down
7 changes: 4 additions & 3 deletions packages/universal-swap/src/msg/chains/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BridgeMsgInfo, MiddlewareResponse } from "../types";
import { ActionType, Path } from "../../types";
import { Action } from "@oraichain/osor-api-contracts-sdk/src/EntryPoint.types";
import { Action, Affiliate } from "@oraichain/osor-api-contracts-sdk/src/EntryPoint.types";
import {
BigDecimal,
calculateTimeoutTimestamp,
Expand All @@ -19,9 +19,10 @@ export class CosmosMsg extends ChainMsg {
receiver: string,
currentChainAddress: string,
memo: string = "",
oraidexCommon: OraidexCommon
oraidexCommon: OraidexCommon,
affiliates: Affiliate[]
) {
super(path, minimumReceive, receiver, currentChainAddress, memo, oraidexCommon);
super(path, minimumReceive, receiver, currentChainAddress, memo, oraidexCommon, affiliates);
}

setMinimumReceiveForSwap(slippage: number = 0.01) {
Expand Down
12 changes: 6 additions & 6 deletions packages/universal-swap/src/msg/chains/oraichain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BridgeMsgInfo, MiddlewareResponse } from "../types";
import { ActionType, Path } from "../../types";
import { SwapOperation } from "@oraichain/osor-api-contracts-sdk/src/types";
import { Action, ExecuteMsg } from "@oraichain/osor-api-contracts-sdk/src/EntryPoint.types";
import { Action, Affiliate, ExecuteMsg } from "@oraichain/osor-api-contracts-sdk/src/EntryPoint.types";
import { isCw20Token } from "../common";
import {
BigDecimal,
Expand Down Expand Up @@ -36,11 +36,11 @@ export class OraichainMsg extends ChainMsg {
currentChainAddress: string,
memo: string = "",
oraidexCommon: OraidexCommon,

protected destPrefix: string = undefined,
protected obridgeAddress: string = undefined
protected obridgeAddress: string = undefined,
affiliates: Affiliate[] = []
) {
super(path, minimumReceive, receiver, currentChainAddress, memo, oraidexCommon);
super(path, minimumReceive, receiver, currentChainAddress, memo, oraidexCommon, affiliates);
// check chainId = "Oraichain"
if (path.chainId !== "Oraichain") {
throw generateError("This path must be on Oraichain");
Expand Down Expand Up @@ -445,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 @@ -615,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
11 changes: 6 additions & 5 deletions packages/universal-swap/src/msg/chains/osmosis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BridgeMsgInfo, MiddlewareResponse } from "../types";
import { ActionType, Path } from "../../types";
import { SwapOperation } from "@oraichain/osor-api-contracts-sdk/src/types";
import { Swap, Action, ExecuteMsg } from "@oraichain/osor-api-contracts-sdk/src/EntryPoint.types";
import { Swap, Action, ExecuteMsg, Affiliate } from "@oraichain/osor-api-contracts-sdk/src/EntryPoint.types";
import { isCw20Token } from "../common";
import {
BigDecimal,
Expand All @@ -26,9 +26,10 @@ export class OsmosisMsg extends ChainMsg {
receiver: string,
currentChainAddress: string,
memo: string = "",
oraidexCommon: OraidexCommon
oraidexCommon: OraidexCommon,
affiliates: Affiliate[] = []
) {
super(path, minimumReceive, receiver, currentChainAddress, memo, oraidexCommon);
super(path, minimumReceive, receiver, currentChainAddress, memo, oraidexCommon, affiliates);
// check chainId = "osmosis-1"
if (path.chainId !== "osmosis-1") {
throw generateError("This path must be on Osmosis");
Expand Down Expand Up @@ -197,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 @@ -264,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
35 changes: 22 additions & 13 deletions packages/universal-swap/src/msg/msgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CosmosMsg, OraichainMsg, OsmosisMsg } from "./chains";
import { MiddlewareResponse } from "./types";
import { EncodeObject } from "@cosmjs/proto-signing";
import { NetworkChainId } from "@oraichain/common";
import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types";

const getDestPrefixForBridgeToEvmOnOrai = (chainId: string): string => {
const prefixMap: { [key: string]: string } = {
Expand All @@ -37,7 +38,8 @@ const buildMemoSwap = (
addresses: { [chainId: string]: string },
slippage: number = 0.01,
oraidexCommon: OraidexCommon,
previousChain?: string
previousChain?: string,
affiliates?: Affiliate[]
): MiddlewareResponse => {
let currentChain = path.chainId;
let currentAddress = addresses[currentChain];
Expand All @@ -59,7 +61,8 @@ const buildMemoSwap = (
memo,
oraidexCommon,
prefix,
oBridgeAddress
oBridgeAddress,
affiliates
);
oraichainMsg.setMinimumReceiveForSwap(slippage);
// we have 2 cases:
Expand All @@ -73,7 +76,7 @@ const buildMemoSwap = (
return msgInfo;
}
case "osmosis-1": {
let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo, oraidexCommon);
let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo, oraidexCommon, []);
cosmosMsg.setMinimumReceiveForSwap(slippage);
let msgInfo = cosmosMsg.genMemoAsMiddleware();
return msgInfo;
Expand All @@ -85,7 +88,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, oraidexCommon);
let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo, oraidexCommon, []);
cosmosMsg.setMinimumReceiveForSwap(slippage);
let msgInfo = cosmosMsg.genMemoAsMiddleware();
return msgInfo;
Expand All @@ -99,7 +102,8 @@ const buildExecuteMsg = (
memo: string,
addresses: { [chainId: string]: string },
slippage: number = 0.01,
oraidexCommon: OraidexCommon
oraidexCommon: OraidexCommon,
affiliates?: Affiliate[]
): EncodeObject => {
let currentChain = path.chainId;
let currentAddress = addresses[currentChain];
Expand All @@ -120,13 +124,14 @@ const buildExecuteMsg = (
memo,
oraidexCommon,
prefix,
oBridgeAddress
oBridgeAddress,
affiliates
);
oraichainMsg.setMinimumReceiveForSwap(slippage);
return oraichainMsg.genExecuteMsg();
}
case "osmosis-1": {
let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo, oraidexCommon);
let cosmosMsg = new OsmosisMsg(path, "1", receiver, currentAddress, memo, oraidexCommon, []);
cosmosMsg.setMinimumReceiveForSwap(slippage);
return cosmosMsg.genExecuteMsg();
}
Expand All @@ -137,7 +142,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, oraidexCommon);
let cosmosMsg = new CosmosMsg(path, "1", receiver, currentAddress, memo, oraidexCommon, []);
cosmosMsg.setMinimumReceiveForSwap(slippage);
return cosmosMsg.genExecuteMsg();
}
Expand All @@ -149,7 +154,8 @@ export const generateMsgSwap = (
slippage: number = 0.01,
addresses: { [chainId: string]: string },
oraidexCommon: OraidexCommon,
recipientAddress?: string
recipientAddress?: string,
affiliates?: Affiliate[]
): EncodeObject => {
if (route.paths.length == 0) {
throw generateError("Require at least 1 action");
Expand Down Expand Up @@ -177,13 +183,14 @@ export const generateMsgSwap = (
addresses,
slippage,
oraidexCommon,
route.paths[i - 1].chainId
route.paths[i - 1].chainId,
affiliates
);
memo = swapInfo.memo;
receiver = swapInfo.receiver;
}

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

export const generateMemoSwap = (
Expand All @@ -192,7 +199,8 @@ export const generateMemoSwap = (
addresses: { [chainId: string]: string },
oraidexCommon: OraidexCommon,
recipientAddress?: string,
previousChain?: string
previousChain?: string,
affiliates?: Affiliate[]
): MiddlewareResponse => {
if (route.paths.length == 0) {
return {
Expand Down Expand Up @@ -223,7 +231,8 @@ export const generateMemoSwap = (
addresses,
slippage,
oraidexCommon,
route.paths[i - 1].chainId
route.paths[i - 1].chainId,
affiliates
);
memo = swapInfo.memo;
receiver = swapInfo.receiver;
Expand Down
Loading