Skip to content

Commit

Permalink
Merge pull request #347 from oraichain/feat/support-swap-oraidex-osmosis
Browse files Browse the repository at this point in the history
Feat/support swap oraidex osmosis
  • Loading branch information
haunv3 authored Oct 10, 2024
2 parents fd9dc2b + 9eecca6 commit 2f20b38
Show file tree
Hide file tree
Showing 30 changed files with 3,636 additions and 569 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ coverage
.nx/cache
.nx/workspace-data

.yarn
.yarn

demo-local
2 changes: 1 addition & 1 deletion packages/oraidex-common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraidex-common",
"version": "1.1.21",
"version": "1.1.23",
"main": "build/index.js",
"files": [
"build/"
Expand Down
54 changes: 54 additions & 0 deletions packages/oraidex-common/src/celestia-network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { CustomChainInfo, defaultBech32Config } from "./network";

export const celestiaNetwork: CustomChainInfo = {
bech32Config: {
bech32PrefixAccAddr: "celestia",
bech32PrefixAccPub: "celestiapub",
bech32PrefixConsAddr: "celestiavalcons",
bech32PrefixConsPub: "celestiavalconspub",
bech32PrefixValAddr: "celestiavaloper",
bech32PrefixValPub: "celestiavaloperpub"
},
bip44: {
coinType: 118
},
networkType: "cosmos",
chainId: "celestia",
chainName: "Celestia",
chainSymbolImageUrl:
"https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/celestia/chain.png",
currencies: [
{
coinDecimals: 6,
coinDenom: "TIA",
coinGeckoId: "celestia",
coinMinimalDenom: "utia",
coinImageUrl: "https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/celestia/chain.png"
}
],
features: [],
feeCurrencies: [
{
coinDecimals: 6,
coinDenom: "TIA",
coinGeckoId: "celestia",
coinMinimalDenom: "utia",
coinImageUrl: "https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/celestia/chain.png",
gasPriceStep: {
low: 0.01,
average: 0.02,
high: 0.1
}
}
],
rpc: "https://celestia.rpc.orai.io",
rest: "https://celestia.lcd.orai.io",
stakeCurrency: {
coinDecimals: 6,
coinDenom: "TIA",
coinGeckoId: "celestia",
coinMinimalDenom: "utia",
coinImageUrl: "https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/celestia/utia.png"
},
walletUrlForStaking: "https://wallet.keplr.app/chains/celestia"
};
3 changes: 2 additions & 1 deletion packages/oraidex-common/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ export enum COSMOS_CHAIN_ID_COMMON {
COSMOSHUB_CHAIN_ID = "cosmoshub-4",
INJECTVE_CHAIN_ID = "injective-1",
KAWAII_COSMOS_CHAIN_ID = "kawaii_6886-1",
NOBLE_CHAIN_ID = "noble-1"
NOBLE_CHAIN_ID = "noble-1",
CELESTIA_CHAIN_ID = "celestia"
}

// asset info token
Expand Down
7 changes: 5 additions & 2 deletions packages/oraidex-common/src/ibc-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const [kwt2oraichain, oraichain2kwt] = KWT_ORAICHAIN_CHANNELS.split(/\s+/);

// exclude evm chain

export const ibcInfos: IBCInfoMap = {
export const ibcInfos: Omit<IBCInfoMap, "celestia"> = {
"cosmoshub-4": {
Oraichain: {
source: "transfer",
Expand Down Expand Up @@ -180,7 +180,10 @@ export const ibcInfos: IBCInfoMap = {
}
};

export const ibcInfosOld: Omit<IBCInfoMap, "osmosis-1" | "cosmoshub-4" | "injective-1" | "noble-1" | "Neutaro-1"> = {
export const ibcInfosOld: Omit<
IBCInfoMap,
"osmosis-1" | "cosmoshub-4" | "injective-1" | "noble-1" | "Neutaro-1" | "celestia"
> = {
Oraichain: {
"oraibridge-subnet-2": {
source: "transfer",
Expand Down
1 change: 1 addition & 0 deletions packages/oraidex-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from "./bigdecimal";
export * from "./interface";
export * from "./config/chainInfosWithIcon";
export * from "./axios-request";
export * from "./celestia-network";
8 changes: 6 additions & 2 deletions packages/oraidex-common/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
HMSTR_ORAICHAIN_DENOM
} from "./constant";
import { listOsmosisToken } from "./alpha-network";
import { celestiaNetwork } from "./celestia-network";

export type NetworkName =
| "Oraichain"
Expand All @@ -76,7 +77,8 @@ export type NetworkName =
| "Tron Network"
| "Injective"
| "Noble"
| "Neutaro";
| "Neutaro"
| "Celestia";

export type CosmosChainId =
| "Oraichain" // oraichain
Expand All @@ -86,7 +88,8 @@ export type CosmosChainId =
| "injective-1" // injective network
| "kawaii_6886-1" // kawaii subnetwork
| "noble-1" // noble network
| "Neutaro-1"; //neutaro network;
| "Neutaro-1" // neutaro network;
| "celestia"; // Celestia

export type EvmChainId =
| "0x38" // bsc
Expand Down Expand Up @@ -541,6 +544,7 @@ export const oraichainNetwork: CustomChainInfo = {
export const chainInfos: CustomChainInfo[] = [
// networks to add on keplr
oraichainNetwork,
celestiaNetwork,
{
rpc: "https://bridge-v2.rpc.orai.io",
rest: "https://bridge-v2.lcd.orai.io",
Expand Down
5 changes: 3 additions & 2 deletions 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.12",
"version": "1.1.13",
"main": "build/index.js",
"files": [
"build/"
Expand All @@ -17,7 +17,8 @@
"ethers": "^5.0.15",
"tronweb": "5.3.2",
"@oraichain/common": "^1.0.3",
"ts-protoc-gen": "^0.15.0"
"ts-protoc-gen": "^0.15.0",
"@oraichain/osor-api-contracts-sdk": "^1.0.2"
},
"devDependencies": {
"cosmjs-types": "0.8.0",
Expand Down
103 changes: 75 additions & 28 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import {
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 @@ -313,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 @@ -339,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 @@ -423,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 @@ -442,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 @@ -480,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 @@ -504,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 @@ -1062,8 +1055,16 @@ export class UniversalSwapHandler {
// Oraichain will be use as a proxy
// TODO: write test cases
async swapCosmosToOtherNetwork(destinationReceiver: string) {
const { originalFromToken, originalToToken, sender, fromAmount, simulateAmount, alphaSmartRoutes, relayerFee } =
this.swapData;
const {
originalFromToken,
originalToToken,
sender,
fromAmount,
simulateAmount,
alphaSmartRoutes,
userSlippage,
relayerFee
} = this.swapData;
// guard check to see if from token has a pool on Oraichain or not. If not then return error

const { client } = await this.config.cosmosWallet.getCosmWasmClient(
Expand Down Expand Up @@ -1093,14 +1094,13 @@ export class UniversalSwapHandler {
if (this.config.swapOptions?.isIbcWasm) minimumReceive = await this.caculateMinimumReceive();

const { swapRoute: completeSwapRoute } = await UniversalSwapHelper.addOraiBridgeRoute(
oraiAddress,
{ obridgeAddress, sourceReceiver: oraiAddress, destReceiver: destinationReceiver },
originalFromToken,
originalToToken,
minimumReceive,
destinationReceiver,
userSlippage,
this.config.swapOptions,
alphaSmartRoutes,
obridgeAddress
alphaSmartRoutes
);
const swapRouteSplit = completeSwapRoute.split(":");
const swapRoute = swapRouteSplit.length === 1 ? "" : swapRouteSplit[1];
Expand Down Expand Up @@ -1149,9 +1149,35 @@ 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, relayerFee } = this.swapData;
const { originalFromToken, originalToToken, simulateAmount, recipientAddress, userSlippage, alphaSmartRoutes } =
this.swapData;
const { swapOptions } = this.config;
let toAddress = "";
const currentToNetwork = originalToToken.chainId;
Expand All @@ -1172,25 +1198,46 @@ export class UniversalSwapHandler {
});
}

let injAddress = undefined;
const [oraiAddress, obridgeAddress] = await Promise.all([
this.config.cosmosWallet.getKeplrAddr("Oraichain"),
this.config.cosmosWallet.getKeplrAddr("oraibridge-subnet-2")
this.config.cosmosWallet.getKeplrAddr(COSMOS_CHAIN_IDS.ORAICHAIN),
this.config.cosmosWallet.getKeplrAddr(COSMOS_CHAIN_IDS.ORAIBRIDGE)
]);

let minimumReceive = simulateAmount;
if (swapOptions?.isIbcWasm) minimumReceive = await this.caculateMinimumReceive();

if (swapOptions?.isAlphaIbcWasm) {
const routesFlatten = UniversalSwapHelper.flattenSmartRouters(alphaSmartRoutes.routes);
const hasInjectiveAddress = routesFlatten.some((route) =>
[route.chainId, route.tokenOutChainId].includes(COSMOS_CHAIN_IDS.INJECTVE)
);

if (hasInjectiveAddress) injAddress = await this.config.cosmosWallet.getKeplrAddr(COSMOS_CHAIN_IDS.INJECTVE);
}

const { swapRoute, universalSwapType } = await UniversalSwapHelper.addOraiBridgeRoute(
oraiAddress,
{
obridgeAddress,
injAddress,
sourceReceiver: oraiAddress,
destReceiver: toAddress,
recipientAddress
},
originalFromToken,
originalToToken,
minimumReceive,
toAddress,
userSlippage,
this.config.swapOptions,
this.swapData.alphaSmartRoutes,
obridgeAddress
alphaSmartRoutes
);

if (alphaSmartRoutes?.routes?.length && swapOptions.isAlphaIbcWasm) {
let receiverAddresses = UniversalSwapHelper.generateAddress({ oraiAddress, injAddress });
if (recipientAddress) receiverAddresses[currentToNetwork] = toAddress;
return this.alphaSmartRouterSwapNewMsg(swapRoute, universalSwapType, receiverAddresses);
}

if (
this.swapData?.alphaSmartRoutes?.routes?.length &&
["oraichain-to-oraichain", "oraichain-to-cosmos", "cosmos-to-others"].includes(universalSwapType) &&
Expand Down
Loading

0 comments on commit 2f20b38

Please sign in to comment.