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

add Retro bridge cross chain provider #690

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { MesonCrossChainProvider } from '../providers/meson-provider/meson-cross
import { OrbiterBridgeProvider } from '../providers/orbiter-bridge/orbiter-bridge-provider';
import { OwlToBridgeProvider } from '../providers/owl-to-bridge/owl-to-bridge-provider';
import { RangoCrossChainProvider } from '../providers/rango-provider/rango-cross-chain-provider';
import { RetroBridgeProvider } from '../providers/retro-bridge/retro-bridge-provider';
import { StargateV2CrossChainProvider } from '../providers/stargate-v2-provider/stargate-v2-cross-chain-provider';
//import { StargateCrossChainProvider } from '../providers/stargate-provider/stargate-cross-chain-provider';
import { TaikoBridgeProvider } from '../providers/taiko-bridge/taiko-bridge-provider';
Expand All @@ -35,7 +36,8 @@ const proxyProviders = [
ArchonBridgeProvider,
MesonCrossChainProvider,
OwlToBridgeProvider,
EddyBridgeProvider
EddyBridgeProvider,
RetroBridgeProvider
] as const;

const nonProxyProviders = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const CROSS_CHAIN_TRADE_TYPE = {
LAYERZERO: 'layerzero',
ARCHON_BRIDGE: 'archon_bridge',
MESON: 'meson',
EDDY_BRIDGE: 'eddy_bridge'
EDDY_BRIDGE: 'eddy_bridge',
RETRO_BRIDGE: 'retro_bridge'
} as const;

export type CrossChainTradeType =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { BLOCKCHAIN_NAME } from 'src/core/blockchain/models/blockchain-name';

export const retroBridgeSupportedBlockchain = [
BLOCKCHAIN_NAME.ETHEREUM,
BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN,
BLOCKCHAIN_NAME.AVALANCHE,
BLOCKCHAIN_NAME.POLYGON,
BLOCKCHAIN_NAME.ARBITRUM,
BLOCKCHAIN_NAME.OPTIMISM,
BLOCKCHAIN_NAME.STARKNET,
BLOCKCHAIN_NAME.LINEA,
BLOCKCHAIN_NAME.ZK_SYNC,
BLOCKCHAIN_NAME.SCROLL,
BLOCKCHAIN_NAME.BASE,
BLOCKCHAIN_NAME.POLYGON_ZKEVM,
BLOCKCHAIN_NAME.MANTA_PACIFIC,
BLOCKCHAIN_NAME.MODE,
BLOCKCHAIN_NAME.MANTLE,
BLOCKCHAIN_NAME.KROMA,
BLOCKCHAIN_NAME.ZETACHAIN,
BLOCKCHAIN_NAME.TON,
BLOCKCHAIN_NAME.BLAST
];

export type RetroBridgeSupportedBlockchain = (typeof retroBridgeSupportedBlockchain)[number];
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export interface RetroBridgeQuoteSendParams {
source_chain: string;
destination_chain: string;
asset_from: string;
asset_to: string;
amount: string;
}

export interface RetroBridgeQuoteResponse {
amount_out: number;
platform_fee: number;
platform_fee_in_usd: number;
blockchain_fee: number;
blockchain_fee_in_usd: number;
swap_fee: number;
swap_fee_in_usd: number;
full_fee: number;
fee_asset: string;
}

export interface RetroBridgeTxSendParams extends RetroBridgeQuoteSendParams {
receiver_wallet: string;
}

export interface RetroBridgeTxResponse {
transaction_id: string;
hot_wallet_address: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const RETRO_BRIDGE_TX_STATUS = {
PENDING: 'Pending',
WAIT_DEPOSIT: 'Wait deposit',
DEPOSITED: 'Deposited',
SENDING: 'Sending',
SENT: 'Sent',
COMPLETED: 'Completed',
SEND_FAILED: 'Send failed',
REJECTED: 'Rejected'
} as const;

export type RetroBridgeTxStatus =
(typeof RETRO_BRIDGE_TX_STATUS)[keyof typeof RETRO_BRIDGE_TX_STATUS];

export interface RetroBridgeStatusResponse {
data: {
status: RetroBridgeTxStatus;
destination_tx_hash: string;
source_tx_hash: string;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import BigNumber from 'bignumber.js';
import { MaxAmountError, MinAmountError } from 'src/common/errors';
import { PriceToken, PriceTokenAmount } from 'src/common/tokens';
import { parseError } from 'src/common/utils/errors';
import { BlockchainName, EvmBlockchainName } from 'src/core/blockchain/models/blockchain-name';
import { Web3Pure } from 'src/core/blockchain/web3-pure/web3-pure';
import { getFromWithoutFee } from 'src/features/common/utils/get-from-without-fee';

import { RequiredCrossChainOptions } from '../../models/cross-chain-options';
import { CROSS_CHAIN_TRADE_TYPE } from '../../models/cross-chain-trade-type';
import { CrossChainProvider } from '../common/cross-chain-provider';
import { CalculationResult } from '../common/models/calculation-result';
import { RubicStep } from '../common/models/rubicStep';
import {
RetroBridgeSupportedBlockchain,
retroBridgeSupportedBlockchain
} from './constants/retro-bridge-supported-blockchain';
import { RetroBridgeQuoteSendParams } from './models/retro-bridge-quote-send-params';
import { RetroBridgeTrade } from './retro-bridge-trade';
import { RetroBridgeApiService } from './services/retro-bridge-api-service';

export class RetroBridgeProvider extends CrossChainProvider {
public readonly type = CROSS_CHAIN_TRADE_TYPE.RETRO_BRIDGE;

public isSupportedBlockchain(blockchain: BlockchainName): boolean {
return retroBridgeSupportedBlockchain.some(chain => chain === blockchain);
}

public async calculate(
from: PriceTokenAmount<EvmBlockchainName>,
toToken: PriceToken<EvmBlockchainName>,
options: RequiredCrossChainOptions
): Promise<CalculationResult> {
const useProxy = false;
const fromBlockchain = from.blockchain as RetroBridgeSupportedBlockchain;
try {
await this.checkMinMaxAmount(from, toToken);

const feeInfo = await this.getFeeInfo(
fromBlockchain,
options.providerAddress,
from,
useProxy
);

const fromWithoutFee = getFromWithoutFee(
from,
feeInfo.rubicProxy?.platformFee?.percent
);
const quoteSendParams: RetroBridgeQuoteSendParams = {
source_chain: fromWithoutFee.blockchain,
destination_chain: toToken.blockchain,
asset_from: from.symbol,
asset_to: toToken.symbol,
amount: Web3Pure.fromWei(
axtezy marked this conversation as resolved.
Show resolved Hide resolved
fromWithoutFee.stringWeiAmount,
fromWithoutFee.decimals
).toFixed()
};
const retroBridgeQuoteConfig = await RetroBridgeApiService.getQuote(quoteSendParams);

const to = new PriceTokenAmount({
...toToken.asStruct,
tokenAmount: new BigNumber(retroBridgeQuoteConfig.amount_out)
});
const gasData =
options.gasCalculation === 'enabled'
? await RetroBridgeTrade.getGasData(
from,
to,
feeInfo,
options.slippageTolerance,
options.providerAddress,
quoteSendParams
)
: null;
const routePath = await this.getRoutePath(from, to);
const trade = new RetroBridgeTrade(
{
from,
to,
feeInfo,
priceImpact: from.calculatePriceImpactPercent(to),
slippage: options.slippageTolerance,
gasData,
quoteSendParams
},
options.providerAddress,
routePath
);

return {
trade,
tradeType: this.type
};
} catch (err) {
return {
trade: null,
error: parseError(err),
tradeType: this.type
};
}
}

protected async getRoutePath(
from: PriceTokenAmount<EvmBlockchainName>,
toToken: PriceTokenAmount<EvmBlockchainName>
): Promise<RubicStep[]> {
return [{ type: 'cross-chain', provider: this.type, path: [from, toToken] }];
}

private async checkMinMaxAmount(
axtezy marked this conversation as resolved.
Show resolved Hide resolved
from: PriceTokenAmount<EvmBlockchainName>,
toToken: PriceToken<EvmBlockchainName>
): Promise<void | never> {
const tokenLimits = await RetroBridgeApiService.getTokenLimits(
from.blockchain,
toToken.blockchain,
from.symbol,
toToken.symbol
);
const minSendAmount = new BigNumber(Web3Pure.toWei(tokenLimits.min_send, from.decimals));
const maxSendAmount = new BigNumber(Web3Pure.toWei(tokenLimits.max_send, from.decimals));

if (from.weiAmount.lt(minSendAmount)) {
throw new MinAmountError(minSendAmount, from.symbol);
}
if (from.weiAmount.gt(maxSendAmount)) {
throw new MaxAmountError(maxSendAmount, from.symbol);
}
}
}
Loading
Loading