Skip to content

Commit

Permalink
Merge pull request #159 from Cryptorubic/feature/cronos-oke-cross-chain
Browse files Browse the repository at this point in the history
Feature/cronos oke cross chain
  • Loading branch information
ottebrut authored Aug 15, 2022
2 parents 7c66cff + 4ffb876 commit 7731bd2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubic-sdk",
"version": "2.9.5",
"version": "2.9.9",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ export const lifiCrossChainSupportedBlockchains = [
BLOCKCHAIN_NAME.MOONRIVER,
BLOCKCHAIN_NAME.ARBITRUM,
BLOCKCHAIN_NAME.OPTIMISM,
BLOCKCHAIN_NAME.CRONOS,
BLOCKCHAIN_NAME.OKE_X_CHAIN,
BLOCKCHAIN_NAME.GNOSIS,
BLOCKCHAIN_NAME.FUSE,
BLOCKCHAIN_NAME.MOONBEAM
BLOCKCHAIN_NAME.MOONBEAM,
BLOCKCHAIN_NAME.CELO
] as const;

export type LifiCrossChainSupportedBlockchain = typeof lifiCrossChainSupportedBlockchains[number];
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CROSS_CHAIN_TRADE_TYPE } from 'src/features';
import { BlockchainName, BlockchainsInfo, PriceToken, Web3Pure } from 'src/core';
import { CROSS_CHAIN_TRADE_TYPE, LiFiTradeSubtype, TradeType } from 'src/features';
import { BLOCKCHAIN_NAME, BlockchainName, BlockchainsInfo, PriceToken, Web3Pure } from 'src/core';
import BigNumber from 'bignumber.js';
import {
LifiCrossChainSupportedBlockchain,
lifiCrossChainSupportedBlockchains
} from 'src/features/cross-chain/providers/lifi-trade-provider/constants/lifi-cross-chain-supported-blockchain';
import LIFI, { RouteOptions } from '@lifi/sdk';
import LIFI, { Route, RouteOptions } from '@lifi/sdk';
import { LifiCrossChainTrade } from 'src/features/cross-chain/providers/lifi-trade-provider/lifi-cross-chain-trade';
import { Injector } from 'src/core/sdk/injector';
import { WrappedCrossChainTrade } from 'src/features/cross-chain/providers/common/models/wrapped-cross-chain-trade';
Expand Down Expand Up @@ -72,14 +72,20 @@ export class LifiCrossChainTradeProvider extends CrossChainTradeProvider {
);
const tokenAmountIn = from.weiAmount.minus(feeAmount).toFixed(0);

const routeOptions: RouteOptions = {
let routeOptions: RouteOptions = {
slippage: options.slippageTolerance,
order: 'RECOMMENDED',
allowSwitchChain: false,
bridges: {
deny: ['multichain'] // @TODO remove after whitelisting
}
allowSwitchChain: false
};
// @TODO remove after whitelisting
if (fromBlockchain === BLOCKCHAIN_NAME.CRONOS || toBlockchain === BLOCKCHAIN_NAME.CRONOS) {
routeOptions = {
...routeOptions,
bridges: {
deny: ['multichain']
}
};
}

const fromChainId = BlockchainsInfo.getBlockchainByName(fromBlockchain).id;
const toChainId = BlockchainsInfo.getBlockchainByName(toBlockchain).id;
Expand Down Expand Up @@ -122,21 +128,7 @@ export class LifiCrossChainTradeProvider extends CrossChainTradeProvider {
? await LifiCrossChainTrade.getGasData(from, to, bestRoute)
: null;

const steps = (bestRoute.steps[0] as LifiStep).includedSteps;
const sourceDex =
steps?.[0] && steps[0].action.fromChainId === steps[0].action.toChainId
? steps?.[0].tool
: undefined;

const [, ...stepsWithoutFirst] = steps;
const targetDex = stepsWithoutFirst.find(
provider => provider.action.fromChainId === provider.action.toChainId
)?.tool;

const itType = {
from: sourceDex ? lifiProviders[sourceDex] : undefined,
to: targetDex ? lifiProviders[targetDex] : undefined
};
const { itType, subType } = this.parseTradeTypes(bestRoute);

const trade = new LifiCrossChainTrade(
{
Expand All @@ -147,7 +139,8 @@ export class LifiCrossChainTradeProvider extends CrossChainTradeProvider {
toTokenAmountMin: Web3Pure.fromWei(bestRoute.toAmountMin, to.decimals),
feeInfo,
priceImpact,
itType
itType,
subType
},
options.providerAddress
);
Expand Down Expand Up @@ -213,4 +206,35 @@ export class LifiCrossChainTradeProvider extends CrossChainTradeProvider {
cryptoFee: null
};
}

private parseTradeTypes(route: Route): {
subType: LiFiTradeSubtype;
itType: { from: TradeType | undefined; to: TradeType | undefined };
} {
const steps =
route.steps.length === 1 ? (route.steps[0] as LifiStep).includedSteps : route.steps;
const sourceDex =
steps?.[0] && steps[0].action.fromChainId === steps[0].action.toChainId
? steps?.[0].toolDetails.name.toLowerCase()
: undefined;

const [, ...stepsWithoutFirst] = steps;
const targetDex = stepsWithoutFirst
.find(provider => provider.action.fromChainId === provider.action.toChainId)
?.toolDetails.name.toLowerCase();

const subType = steps?.find(
provider => provider.action.fromChainId !== provider.action.toChainId
)?.tool as LiFiTradeSubtype;

const itType = {
from: sourceDex ? lifiProviders[sourceDex] : undefined,
to: targetDex ? lifiProviders[targetDex] : undefined
};

return {
subType,
itType
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import BigNumber from 'bignumber.js';
import { BlockchainsInfo, PriceTokenAmount, Web3Public, Web3Pure } from 'src/core';
import {
CROSS_CHAIN_TRADE_TYPE,
Li_FI_TRADE_SUBTYPE,
SwapTransactionOptions,
TRADE_TYPE,
TradeType
Expand Down Expand Up @@ -57,7 +58,8 @@ export class LifiCrossChainTrade extends CrossChainTrade {
itType: {
from: TRADE_TYPE.ONE_INCH,
to: TRADE_TYPE.ONE_INCH
}
},
subType: Li_FI_TRADE_SUBTYPE.CONNEXT
},
EMPTY_ADDRESS
).getContractParams({});
Expand Down Expand Up @@ -126,6 +128,7 @@ export class LifiCrossChainTrade extends CrossChainTrade {
feeInfo: FeeInfo;
priceImpact: number;
itType: { from: TradeType | undefined; to: TradeType | undefined };
subType: LiFiTradeSubtype;
},
providerAddress: string
) {
Expand All @@ -136,7 +139,7 @@ export class LifiCrossChainTrade extends CrossChainTrade {
this.route = crossChainTrade.route;
this.gasData = crossChainTrade.gasData;
this.toTokenAmountMin = crossChainTrade.toTokenAmountMin;
this.subType = this.route?.steps?.[0]?.tool as LiFiTradeSubtype;
this.subType = crossChainTrade.subType;
this.feeInfo = crossChainTrade.feeInfo;

this.priceImpact = crossChainTrade.priceImpact;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const Li_FI_TRADE_SUBTYPE = {
CONNECT: 'connext',
CONNEXT: 'connext',
HOP: 'hop',
CELER_BRIDGE: 'cbridge',
MULTICHAIN: 'multichain',
Expand Down

0 comments on commit 7731bd2

Please sign in to comment.