Skip to content

Commit

Permalink
LayerZero bridge for ALGB token (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilchizh authored Feb 7, 2024
2 parents 82f748a + 83842fd commit c6c526e
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 6 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": "4.54.3",
"version": "4.55.0",
"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 @@ -10,6 +10,7 @@ import { SquidrouterCrossChainProvider } from 'src/features/cross-chain/calculat
import { SymbiosisCrossChainProvider } from 'src/features/cross-chain/calculation-manager/providers/symbiosis-provider/symbiosis-cross-chain-provider';
import { XyCrossChainProvider } from 'src/features/cross-chain/calculation-manager/providers/xy-provider/xy-cross-chain-provider';

import { LayerZeroBridgeProvider } from '../providers/layerzero-bridge/layerzero-bridge-provider';
import { RangoCrossChainProvider } from '../providers/rango-provider/rango-cross-chain-provider';
import { StargateCrossChainProvider } from '../providers/stargate-provider/stargate-cross-chain-provider';
import { TaikoBridgeProvider } from '../providers/taiko-bridge/taiko-bridge-provider';
Expand All @@ -30,7 +31,8 @@ const nonProxyProviders = [
BridgersCrossChainProvider,
ChangenowCrossChainProvider,
ArbitrumRbcBridgeProvider,
TaikoBridgeProvider
TaikoBridgeProvider,
LayerZeroBridgeProvider
// ScrollBridgeProvider
] as const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const CROSS_CHAIN_TRADE_TYPE = {
SCROLL_BRIDGE: 'scroll_bridge',
TAIKO_BRIDGE: 'taiko_bridge',
RANGO: 'rango',
PULSE_CHAIN_BRIDGE: 'pulsechain_bridge'
PULSE_CHAIN_BRIDGE: 'pulsechain_bridge',
LAYERZERO: 'layerzero'
} as const;

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

import { LayerZeroBridgeSupportedBlockchain } from '../models/layerzero-bridge-supported-blockchains';

export const ALGB_TOKEN: Record<LayerZeroBridgeSupportedBlockchain, string> = {
[BLOCKCHAIN_NAME.ARBITRUM]: '0x9f018bda8f6b507a0c9e6f290b2f7c49c2f8daf8',
[BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN]: '0xe374116f490b461764e2438f98eab3fff383367b',
[BLOCKCHAIN_NAME.POLYGON]: '0x0169ec1f8f639b32eec6d923e24c2a2ff45b9dd6'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BLOCKCHAIN_NAME } from 'src/core/blockchain/models/blockchain-name';

import { LayerZeroBridgeSupportedBlockchain } from '../models/layerzero-bridge-supported-blockchains';
import { ALGB_TOKEN } from './algb-token-addresses';

export const layerZeroProxyOFT: Record<LayerZeroBridgeSupportedBlockchain, string> = {
[BLOCKCHAIN_NAME.ARBITRUM]: ALGB_TOKEN.ARBITRUM,
[BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN]: ALGB_TOKEN.BSC,
[BLOCKCHAIN_NAME.POLYGON]: '0xDef87c507ef911Fd99c118c53171510Eb7967738'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BLOCKCHAIN_NAME } from 'src/core/blockchain/models/blockchain-name';

import { LayerZeroBridgeSupportedBlockchain } from '../models/layerzero-bridge-supported-blockchains';

export const layerZeroChainIds: Record<LayerZeroBridgeSupportedBlockchain, string> = {
[BLOCKCHAIN_NAME.ARBITRUM]: '110',
[BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN]: '102',
[BLOCKCHAIN_NAME.POLYGON]: '109'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { NotSupportedTokensError } from 'src/common/errors';
import { PriceToken, PriceTokenAmount } from 'src/common/tokens';
import { BlockchainName, EvmBlockchainName } from 'src/core/blockchain/models/blockchain-name';
import { RequiredCrossChainOptions } from 'src/features/cross-chain/calculation-manager/models/cross-chain-options';
import { CROSS_CHAIN_TRADE_TYPE } from 'src/features/cross-chain/calculation-manager/models/cross-chain-trade-type';
import { CbridgeCrossChainSupportedBlockchain } from 'src/features/cross-chain/calculation-manager/providers/cbridge/constants/cbridge-supported-blockchains';
import { CrossChainProvider } from 'src/features/cross-chain/calculation-manager/providers/common/cross-chain-provider';
import { CalculationResult } from 'src/features/cross-chain/calculation-manager/providers/common/models/calculation-result';
import { FeeInfo } from 'src/features/cross-chain/calculation-manager/providers/common/models/fee-info';
import { RubicStep } from 'src/features/cross-chain/calculation-manager/providers/common/models/rubicStep';

import { LayerZeroBridgeTrade } from './layerzero-bridge-trade';
import {
LayerZeroBridgeSupportedBlockchain,
layerZeroBridgeSupportedBlockchains
} from './models/layerzero-bridge-supported-blockchains';

export class LayerZeroBridgeProvider extends CrossChainProvider {
public readonly type = CROSS_CHAIN_TRADE_TYPE.LAYERZERO;

public isSupportedBlockchain(
blockchain: BlockchainName
): blockchain is LayerZeroBridgeSupportedBlockchain {
return layerZeroBridgeSupportedBlockchains.some(
supportedBlockchain => supportedBlockchain === blockchain
);
}

public async calculate(
fromToken: PriceTokenAmount<EvmBlockchainName>,
toToken: PriceToken<EvmBlockchainName>,
options: RequiredCrossChainOptions
): Promise<CalculationResult> {
const fromBlockchain = fromToken.blockchain as LayerZeroBridgeSupportedBlockchain;
const toBlockchain = toToken.blockchain as LayerZeroBridgeSupportedBlockchain;

if (!this.areSupportedBlockchains(fromBlockchain, toBlockchain)) {
return {
trade: null,
error: new NotSupportedTokensError(),
tradeType: this.type
};
}

try {
const to = new PriceTokenAmount({
...toToken.asStruct,
tokenAmount: fromToken.tokenAmount
});

const gasData =
options.gasCalculation === 'enabled'
? await LayerZeroBridgeTrade.getGasData(fromToken, to, options)
: null;

return {
trade: new LayerZeroBridgeTrade(
{
from: fromToken,
to,
gasData
},
options.providerAddress,
await this.getRoutePath(fromToken, to)
),
tradeType: this.type
};
} catch (err) {
const rubicSdkError = CrossChainProvider.parseError(err);

return {
trade: null,
error: rubicSdkError,
tradeType: this.type
};
}
}

protected async getFeeInfo(
_fromBlockchain: CbridgeCrossChainSupportedBlockchain,
_providerAddress: string,
_percentFeeToken: PriceTokenAmount,
_useProxy: boolean
): Promise<FeeInfo> {
return {};
}

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

0 comments on commit c6c526e

Please sign in to comment.