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

Feat/support swap oraidex osmosis #347

Merged
merged 42 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9025ed8
chore: add osor sdk
trung2891 Oct 1, 2024
def2bd1
feat: build msg swap on oraichain
trung2891 Oct 3, 2024
34e0ebb
feat: build swap & action on osmosis and cosmos ecosystem
trung2891 Oct 3, 2024
7172104
chore: refactor
trung2891 Oct 3, 2024
f45d930
fix: get last operations
trung2891 Oct 3, 2024
967e5e0
chore: update enrty point contract for oraichain & osmosis
trung2891 Oct 3, 2024
9d5ed54
fix: wrong first asset on build msg
trung2891 Oct 3, 2024
91da451
fix: memo not undefined
trung2891 Oct 3, 2024
42bdc06
chore: return receiver on build middleware msg
trung2891 Oct 3, 2024
40e9135
chore: done logic build msg swap from smart route
trung2891 Oct 3, 2024
fa54662
feat: calc minimum receive
trung2891 Oct 4, 2024
8e2e16f
feat: add msg generate memo for ibc bridge
trung2891 Oct 4, 2024
a43efd7
beta universal swap
haunv3 Oct 4, 2024
c7c622f
Merge branch 'main' of github.com:oraichain/oraidex-sdk into feat/sup…
haunv3 Oct 4, 2024
14883e4
1.1.13 universal swap
haunv3 Oct 4, 2024
3e7112c
fix: wrong chainID of obridge in build msg swap
trung2891 Oct 5, 2024
bfba1b8
chore: update test case for build swap msg
trung2891 Oct 7, 2024
267ffcf
chore: test build oraichain msg with valid path with swap + ibc bridge
trung2891 Oct 7, 2024
09ad0e0
chore: test gen msg on oraichain
trung2891 Oct 7, 2024
10dfe29
fix: wrong parse ibcWasmContractAddress from source port
trung2891 Oct 7, 2024
7badad2
update params query smart route
haunv3 Oct 7, 2024
5a9a9dc
Merge branch 'feat/support-swap-oraidex-osmosis' of github.com:oraich…
haunv3 Oct 7, 2024
8ccebb3
chore: test build msg bridge to evm missing params
trung2891 Oct 7, 2024
43fca1f
support swap evm -> inj or tia
haunv3 Oct 7, 2024
9b516c6
Merge branch 'feat/support-swap-oraidex-osmosis' of github.com:oraich…
haunv3 Oct 7, 2024
e09ac6e
chore: test universal swap from cosmos-base ecosystem
trung2891 Oct 7, 2024
1e23583
chore: test build msg swap from boble
trung2891 Oct 7, 2024
1ad1cb6
update network celestia
haunv3 Oct 8, 2024
db5622b
Merge branch 'feat/support-swap-oraidex-osmosis' of github.com:oraich…
haunv3 Oct 8, 2024
a438bc6
chore: use mainnet contract Oraidex entrypoint
trung2891 Oct 8, 2024
34ce351
fix: test build msg swap
trung2891 Oct 8, 2024
00ab354
update simulate
haunv3 Oct 8, 2024
d6bca87
add swap cosmos to cosmos with new msg
haunv3 Oct 8, 2024
c9fe2fd
chore: refactor
trung2891 Oct 9, 2024
1be1f92
chore: refactor
trung2891 Oct 9, 2024
c20c33a
update check address inj
haunv3 Oct 9, 2024
03a28c0
fix: generate msg send on osmosis
trung2891 Oct 9, 2024
6e01c3d
update address noble
haunv3 Oct 9, 2024
7a534f1
Merge branch 'feat/support-swap-oraidex-osmosis' of github.com:oraich…
haunv3 Oct 9, 2024
7282679
update generate address noble
haunv3 Oct 9, 2024
9986a13
add features when has recipient address
haunv3 Oct 9, 2024
9eecca6
recipientAddress with new msg
haunv3 Oct 9, 2024
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
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
101 changes: 73 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
trung2891 marked this conversation as resolved.
Show resolved Hide resolved
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
),
trung2891 marked this conversation as resolved.
Show resolved Hide resolved
memo: "",
recover_address: this.getAddress(
recover_address: UniversalSwapHelper.getAddress(
prefixRecover,
{ address60: injAddress, address118: oraiAddress },
chainInfoRecover.bip44.coinType
trung2891 marked this conversation as resolved.
Show resolved Hide resolved
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
trung2891 marked this conversation as resolved.
Show resolved Hide resolved
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;
trung2891 marked this conversation as resolved.
Show resolved Hide resolved
// 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);
}
trung2891 marked this conversation as resolved.
Show resolved Hide resolved

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,44 @@ 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 === 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
},
originalFromToken,
originalToToken,
minimumReceive,
toAddress,
userSlippage,
this.config.swapOptions,
this.swapData.alphaSmartRoutes,
obridgeAddress
alphaSmartRoutes
);

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
Loading
Loading