Skip to content

Commit

Permalink
add swap cosmos to cosmos with new msg
Browse files Browse the repository at this point in the history
  • Loading branch information
haunv3 committed Oct 8, 2024
1 parent 00ab354 commit d6bca87
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 113 deletions.
56 changes: 40 additions & 16 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { GasPrice } from "@cosmjs/stargate";
import { OraiswapRouterQueryClient } from "@oraichain/oraidex-contracts-sdk";
import { Affiliate } from "@oraichain/oraidex-contracts-sdk/build/OraiswapMixedRouter.types";
import { COSMOS_CHAIN_IDS } from "@oraichain/common";
import { generateMsgSwap } from "./msg/msgs";

const AFFILIATE_DECIMAL = 1e4; // 10_000
export class UniversalSwapHandler {
Expand Down Expand Up @@ -314,15 +315,6 @@ export class UniversalSwapHandler {
return [...msgExecuteSwap, ...msgExecuteTransfer];
}

private getAddress = (prefix: string, { address60, address118 }, coinType: number = 118) => {
const approve = {
118: address118,
60: address60
};
const { data } = fromBech32(approve[coinType]);
return toBech32(prefix, data);
};

private getReceiverIBCHooks = (chainId: string, receiver?: string) => {
if (chainId === "osmosis-1") return OSMOSIS_ROUTER_CONTRACT;
return receiver;
Expand All @@ -340,7 +332,7 @@ export class UniversalSwapHandler {
if (isOnlySwap) {
post_swap_action = {
transfer: {
to_address: this.getAddress(
to_address: UniversalSwapHelper.getAddress(
prefixReceiver,
{ address60: injAddress, address118: oraiAddress },
chainInfoReceiver.bip44.coinType
Expand Down Expand Up @@ -424,13 +416,13 @@ export class UniversalSwapHandler {
ibc_transfer: {
ibc_info: {
source_channel: route.bridgeInfo.channel,
receiver: this.getAddress(
receiver: UniversalSwapHelper.getAddress(
prefixReceiver,
{ address60: injAddress, address118: oraiAddress },
chainInfoReceiver.bip44.coinType
),
memo: "",
recover_address: this.getAddress(
recover_address: UniversalSwapHelper.getAddress(
prefixRecover,
{ address60: injAddress, address118: oraiAddress },
chainInfoRecover.bip44.coinType
Expand All @@ -443,7 +435,7 @@ export class UniversalSwapHandler {

public createForwardObject = (route: Routes, { oraiAddress, injAddress }, isLastRoute?: boolean) => {
const { prefixReceiver, chainInfoReceiver } = this.getPrefixCosmos(route);
const addressReceiver = this.getAddress(
const addressReceiver = UniversalSwapHelper.getAddress(
prefixReceiver,
{ address60: injAddress, address118: oraiAddress },
chainInfoReceiver.bip44.coinType
Expand Down Expand Up @@ -481,7 +473,7 @@ export class UniversalSwapHandler {
flagAffiliate?: boolean
) => {
const { prefixReceiver, prefixRecover, chainInfoRecover, chainInfoReceiver } = this.getPrefixCosmos(route);
const addressReceiver = this.getAddress(
const addressReceiver = UniversalSwapHelper.getAddress(
prefixReceiver,
{ address60: injAddress, address118: oraiAddress },
chainInfoReceiver.bip44.coinType
Expand All @@ -505,7 +497,7 @@ export class UniversalSwapHandler {
amount: tokenInAmount,
denom: route.tokenIn
},
sender: this.getAddress(
sender: UniversalSwapHelper.getAddress(
prefixRecover,
{ address60: injAddress, address118: oraiAddress },
chainInfoRecover.bip44.coinType
Expand Down Expand Up @@ -1157,6 +1149,31 @@ export class UniversalSwapHandler {
return client.signAndBroadcast(sender.cosmos, [msgTransferEncodeObj], "auto");
}

async alphaSmartRouterSwapNewMsg(swapRoute, universalSwapType, receiverAddresses) {
const { sender, originalFromToken, alphaSmartRoutes, userSlippage } = this.swapData;
if (
universalSwapType === "cosmos-to-others" ||
universalSwapType === "oraichain-to-oraichain" ||
universalSwapType === "oraichain-to-cosmos"
) {
const msgs = alphaSmartRoutes.routes.map((route) => {
return generateMsgSwap(route, userSlippage / 100, receiverAddresses);
});
const { client } = await this.config.cosmosWallet.getCosmWasmClient(
{
chainId: originalFromToken.chainId as CosmosChainId,
rpc: originalFromToken.rpc
},
{
gasPrice: this.getGasPriceFromToken()
}
);
return await client.signAndBroadcast(sender.cosmos, msgs, "auto");
}

return this.transferAndSwap(swapRoute);
}

async processUniversalSwap() {
const { evm, tron } = this.swapData.sender;
const { originalFromToken, originalToToken, simulateAmount, recipientAddress, userSlippage, alphaSmartRoutes } =
Expand Down Expand Up @@ -1211,7 +1228,14 @@ export class UniversalSwapHandler {
alphaSmartRoutes
);

if (alphaSmartRoutes?.routes?.length && swapOptions.isAlphaIbcWasm) return this.transferAndSwap(swapRoute);
if (alphaSmartRoutes?.routes?.length && swapOptions.isAlphaIbcWasm) {
return this.alphaSmartRouterSwapNewMsg(
swapRoute,
universalSwapType,
UniversalSwapHelper.generateAddress({ oraiAddress, injAddress })
);
}

if (
this.swapData?.alphaSmartRoutes?.routes?.length &&
["oraichain-to-oraichain", "oraichain-to-cosmos", "cosmos-to-others"].includes(universalSwapType) &&
Expand Down
19 changes: 19 additions & 0 deletions packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,25 @@ export class UniversalSwapHelper {
return toBech32(prefix, data);
};

static generateAddress = ({ oraiAddress, injAddress }) => {
return {
[COSMOS_CHAIN_ID_COMMON.ORAICHAIN_CHAIN_ID]: oraiAddress,
[COSMOS_CHAIN_ID_COMMON.COSMOSHUB_CHAIN_ID]: UniversalSwapHelper.getAddress("cosmos", {
address60: injAddress,
address118: oraiAddress
}),
[COSMOS_CHAIN_ID_COMMON.OSMOSIS_CHAIN_ID]: UniversalSwapHelper.getAddress("osmo", {
address60: injAddress,
address118: oraiAddress
}),
[COSMOS_CHAIN_ID_COMMON.INJECTVE_CHAIN_ID]: injAddress,
[COSMOS_CHAIN_ID_COMMON.CELESTIA_CHAIN_ID]: UniversalSwapHelper.getAddress("celestia", {
address60: injAddress,
address118: oraiAddress
})
};
};

static addOraiBridgeRoute = async (
addresses: { obridgeAddress?: string; sourceReceiver: string; injAddress?: string; destReceiver?: string },
fromToken: TokenItemType,
Expand Down
117 changes: 20 additions & 97 deletions packages/universal-swap/src/universal-demos/alpha-ibc-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,109 +4,31 @@ import { UniversalSwapHandler } from "../handler";
import { cosmosTokens, flattenTokens, generateError, getTokenOnOraichain, toAmount } from "@oraichain/oraidex-common";

const router = {
swapAmount: "2000000",
returnAmount: "241755",
swapAmount: "100000",
returnAmount: "9983",
routes: [
{
swapAmount: "2000000",
returnAmount: "241755",
swapAmount: "100000",
returnAmount: "9983",
paths: [
{
chainId: "0x38",
tokenIn: "0x55d398326f99059fF775485246999027B3197955",
tokenInAmount: "2000000",
tokenOut: "orai12hzjxfh77wl572gdzct2fxv2arxcwh6gykc7qh",
tokenOutAmount: "1366092",
tokenOutChainId: "Oraichain",
actions: [
{
type: "Bridge",
protocol: "Bridge",
tokenIn: "0x55d398326f99059fF775485246999027B3197955",
tokenInAmount: "2000000",
tokenOut: "orai12hzjxfh77wl572gdzct2fxv2arxcwh6gykc7qh",
tokenOutAmount: "1366092",
tokenOutChainId: "Oraichain",
bridgeInfo: {
port: "transfer",
channel: "channel-1"
}
}
]
},
{
chainId: "Oraichain",
tokenIn: "orai12hzjxfh77wl572gdzct2fxv2arxcwh6gykc7qh",
tokenInAmount: "1366092",
tokenOut: "uosmo",
tokenOutAmount: "2445755",
tokenOutChainId: "osmosis-1",
actions: [
{
type: "Swap",
protocol: "OraidexV3",
tokenIn: "orai12hzjxfh77wl572gdzct2fxv2arxcwh6gykc7qh",
tokenInAmount: "1366092",
tokenOut: "orai",
tokenOutAmount: "214856",
swapInfo: [
{
poolId: "orai-orai12hzjxfh77wl572gdzct2fxv2arxcwh6gykc7qh-3000000000-100",
tokenOut: "orai"
}
]
},
{
type: "Swap",
protocol: "Oraidex",
tokenIn: "orai",
tokenInAmount: "214856",
tokenOut: "ibc/9C4DCD21B48231D0BC2AC3D1B74A864746B37E4292694C93C617324250D002FC",
tokenOutAmount: "2445755",
swapInfo: [
{
poolId: "orai1d37artrk4tkhz2qyjmaulc2jzjkx7206tmpfug",
tokenOut: "ibc/9C4DCD21B48231D0BC2AC3D1B74A864746B37E4292694C93C617324250D002FC"
}
]
},
{
type: "Bridge",
protocol: "Bridge",
tokenIn: "ibc/9C4DCD21B48231D0BC2AC3D1B74A864746B37E4292694C93C617324250D002FC",
tokenInAmount: "2445755",
tokenOut: "uosmo",
tokenOutAmount: "2445755",
tokenOutChainId: "osmosis-1",
bridgeInfo: {
port: "transfer",
channel: "channel-13"
}
}
]
},
{
chainId: "osmosis-1",
tokenIn: "uosmo",
tokenInAmount: "2445755",
tokenInAmount: "100000",
tokenOut: "utia",
tokenOutAmount: "241755",
tokenOutAmount: "9983",
tokenOutChainId: "celestia",
actions: [
{
type: "Swap",
protocol: "Osmosis",
tokenIn: "uosmo",
tokenInAmount: "2445755",
tokenInAmount: "100000",
tokenOut: "ibc/D79E7D83AB399BFFF93433E54FAA480C191248FC556924A2A8351AE2638B3877",
tokenOutAmount: "241755",
tokenOutAmount: "9983",
swapInfo: [
{
poolId: "1263",
tokenOut: "ibc/498A0751C798A0D9A389AA3691123DADA57DAA4FE165D5C75894505B876BA6E4"
},
{
poolId: "1247",
poolId: "1347",
tokenOut: "ibc/D79E7D83AB399BFFF93433E54FAA480C191248FC556924A2A8351AE2638B3877"
}
]
Expand All @@ -115,9 +37,9 @@ const router = {
type: "Bridge",
protocol: "Bridge",
tokenIn: "ibc/D79E7D83AB399BFFF93433E54FAA480C191248FC556924A2A8351AE2638B3877",
tokenInAmount: "241755",
tokenInAmount: "9983",
tokenOut: "utia",
tokenOutAmount: "241755",
tokenOutAmount: "9983",
tokenOutChainId: "celestia",
bridgeInfo: {
port: "transfer",
Expand All @@ -130,13 +52,14 @@ const router = {
}
]
};

const alphaSwapToOraichain = async () => {
const wallet = new CosmosWalletImpl(process.env.MNEMONIC);
const sender = await wallet.getKeplrAddr("Oraichain");
const sender = await wallet.getKeplrAddr("osmosis-1");

const fromAmount = 2;
const fromAmount = 0.1;
console.log("sender: ", sender);
const originalFromToken = flattenTokens.find((t) => t.coinGeckoId === "tether" && t.chainId === "0x38");
const originalFromToken = flattenTokens.find((t) => t.coinGeckoId === "osmosis" && t.chainId === "osmosis-1");
const originalToToken = flattenTokens.find((t) => t.coinGeckoId === "celestia" && t.chainId === "celestia");

if (!originalToToken) throw generateError("Could not find original to token");
Expand All @@ -149,11 +72,11 @@ const alphaSwapToOraichain = async () => {
sender: { cosmos: sender, evm: "0x8c7E0A841269a01c0Ab389Ce8Fb3Cf150A94E797" },
fromAmount,
userSlippage: 1,
relayerFee: {
relayerAmount: "100000",
relayerDecimals: 6
},
simulatePrice: "147161",
// relayerFee: {
// relayerAmount: "100000",
// relayerDecimals: 6
// },
simulatePrice: "99956",
simulateAmount: toAmount(fromAmount, originalToToken.decimals).toString(),
alphaSmartRoutes: router
},
Expand Down

0 comments on commit d6bca87

Please sign in to comment.