Skip to content
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

update func gen smart router osmosis pool #279

Merged
merged 1 commit into from
Jul 3, 2024
Merged
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.0.89-beta.2",
"version": "1.0.89-beta.3",
"main": "build/index.js",
"files": [
"build/"
Expand Down
74 changes: 53 additions & 21 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export class UniversalSwapHandler {
return receiver;
};

private getSwapAndActionInOsmosis(
public getSwapAndActionInOsmosis(
route: Routes,
{ oraiAddress, injAddress },
isOnlySwap: boolean,
Expand Down Expand Up @@ -420,7 +420,7 @@ export class UniversalSwapHandler {
return { msgTransferInfo };
}

private createForwardObject = (route: Routes, { oraiAddress, injAddress }) => {
public createForwardObject = (route: Routes, { oraiAddress, injAddress }) => {
const { prefixReceiver, chainInfoReceiver } = this.getPrefixCosmos(route);
return {
msgForwardObject: {
Expand Down Expand Up @@ -456,7 +456,7 @@ export class UniversalSwapHandler {
return { prefixRecover, prefixReceiver, chainInfoRecover, chainInfoReceiver };
};

private getMsgTransfer = (route: Routes, { oraiAddress, injAddress }) => {
public getMsgTransfer = (route: Routes, { oraiAddress, injAddress }) => {
const { prefixReceiver, prefixRecover, chainInfoRecover, chainInfoReceiver } = this.getPrefixCosmos(route);
return {
sourcePort: route.bridgeInfo.port,
Expand Down Expand Up @@ -486,10 +486,12 @@ export class UniversalSwapHandler {
getMessagesAndMsgTransfers = (routeFlatten: Routes[], { oraiAddress, injAddress }) => {
let messages = [];
let msgTransfers = [];
let pathProperty = "";
let pathProperty = [];
let pathReceiver = [];

routeFlatten.forEach((route: Routes, index: number, routes: Routes[]) => {
const isLastRoute = index + 1 === routes.length;
const isLastRoute = route.isLastPath;

const isOsmosisChain = route.chainId === "osmosis-1";
const isSwap = route.type === "Swap";
const isConvert = route.type === "Convert";
Expand All @@ -511,30 +513,46 @@ export class UniversalSwapHandler {
true
);
msgTransfers[route.path] = msgActionSwap;
pathProperty = "msg.swap_and_action.post_swap_action";
pathProperty[route.path] = "msg.swap_and_action.post_swap_action";
pathReceiver[route.path] = isLastRoute
? pathProperty[route.path] + ".transfer.to_address"
: pathProperty[route.path] + ".ibc_transfer.ibc_info.receiver";
} else {
msgTransfers[route.path] = this.getMsgTransfer(route, { oraiAddress, injAddress });
pathProperty = "memo";
pathProperty[route.path] = "memo";
pathReceiver[route.path] = "receiver";
}
} else {
if (isOsmosisChain) {
if (isSwap) {
const { msgActionSwap } = this.getSwapAndActionInOsmosis(route, { oraiAddress, injAddress }, isLastRoute);
this.updateNestedProperty(msgTransfers[route.path], pathProperty, msgActionSwap);
pathProperty += ".wasm.msg.swap_and_action.post_swap_action";
this.updateNestedProperty(msgTransfers[route.path], pathProperty[route.path], msgActionSwap);
pathProperty[route.path] += ".wasm.msg.swap_and_action.post_swap_action";
pathReceiver[route.path] = isLastRoute
? pathProperty[route.path] + ".transfer.to_address"
: pathProperty[route.path] + ".ibc_transfer.ibc_info.receiver";
} else if (index > 0 && routes[index - 1].chainId === route.chainId) {
const { msgTransferInfo } = this.getIbcTransferInfo(route, { oraiAddress, injAddress });
this.updateNestedProperty(msgTransfers[route.path], pathProperty, msgTransferInfo);
pathProperty += ".ibc_transfer.ibc_info.memo";
this.updateNestedProperty(msgTransfers[route.path], pathProperty[route.path], msgTransferInfo);
pathReceiver[route.path] = pathProperty[route.path] + ".ibc_transfer.ibc_info.receiver";
pathProperty[route.path] += ".ibc_transfer.ibc_info.memo";
}
} else {
const { msgForwardObject } = this.createForwardObject(route, { oraiAddress, injAddress });
this.updateNestedProperty(msgTransfers[route.path], pathProperty, msgForwardObject);
pathProperty += ".forward.next";
this.updateNestedProperty(msgTransfers[route.path], pathProperty[route.path], msgForwardObject);
pathProperty[route.path] += ".forward.next";
pathReceiver[route.path] = pathProperty[route.path] + ".receiver";
}
}
}
});

if (this.swapData?.recipientAddress && msgTransfers?.length) {
msgTransfers.forEach((msg, i) =>
this.updateNestedReceiveProperty(msg, pathReceiver[i], this.swapData.recipientAddress)
);
}

return { messages, msgTransfers };
};

Expand All @@ -553,7 +571,7 @@ export class UniversalSwapHandler {
}
);

const { routesFlatten } = this.flattenSmartRouters(alphaSmartRoutes.routes);
const routesFlatten = this.flattenSmartRouters(alphaSmartRoutes.routes);
const [oraiAddress, injAddress] = await Promise.all([
this.config.cosmosWallet.getKeplrAddr("Oraichain"),
this.config.cosmosWallet.getKeplrAddr("injective-1")
Expand Down Expand Up @@ -589,17 +607,18 @@ export class UniversalSwapHandler {
return client.signAndBroadcast(this.swapData.sender.cosmos, [...msgExecuteSwap, ...transferStringifyMemo], "auto");
}

private flattenSmartRouters(routers: Route[]) {
flattenSmartRouters(routers: Route[]): Routes[] {
const routesFlatten = routers.flatMap((router, i) =>
router.paths.flatMap((path) =>
path.actions.map((action) => ({
router.paths.flatMap((path, ind, arrPath) =>
path.actions.map((action, index, arrAction) => ({
...action,
path: i,
chainId: path.chainId
chainId: path.chainId,
isLastPath: arrPath.length === ind + 1 && !arrAction[index + 1]
}))
)
);
return { routesFlatten };
return routesFlatten;
}

private updateNestedProperty = (obj, key, value) => {
Expand All @@ -610,6 +629,19 @@ export class UniversalSwapHandler {
}, obj)[keys[keys.length - 1]] = value;
};

private updateNestedReceiveProperty = (obj, path: string, value: any) => {
const keys = path.split(".");
let current = obj;
for (let i = 0; i < keys.length - 1; i++) {
if (!current[keys[i]]) {
throw new Error(`Invalid path: ${keys.slice(0, i + 1).join(".")}`);
}
current = current[keys[i]];
}
current[keys[keys.length - 1]] = value;
return obj;
};

private stringifyMemos = (obj) => {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
Expand Down Expand Up @@ -1056,15 +1088,15 @@ export class UniversalSwapHandler {
msg: {
send: {
contract: network.converter,
amount: route.tokenOutAmount,
amount: route.tokenInAmount,
msg: toBinary({
convert_reverse: {
from: { native_token: { denom: route.tokenOut } }
}
})
}
},
fund: undefined
funds: []
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/universal-swap/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ interface RouteBase {
tokenInAmount: string;
tokenOut: string;
tokenOutAmount: string;
isLastPath: boolean;
}

export interface Routes extends RouteBase {
Expand Down
Loading