From 3d8c73b8485c4dc8066e7c5fed5eab995377e855 Mon Sep 17 00:00:00 2001 From: Semen Loktionov Date: Tue, 18 Jul 2023 13:00:56 +0300 Subject: [PATCH] migrate to new abis --- .../bridge/src/bridgers/evm/evm-bridger.ts | 36 +- packages/bridge/src/errors/bridger.ts | 6 +- packages/bridge/src/types/bridger.ts | 21 +- .../src/operations/evm/evm-operation.ts | 41 +- packages/shared/src/api/dex.ts | 1 + .../shared/src/const/abis/bridge-facade.ts | 1037 +++++++++++++++++ .../shared/src/const/abis/bridge-router.ts | 288 ----- packages/shared/src/const/abis/fee-manager.ts | 374 ++++++ packages/shared/src/const/abis/index.ts | 3 +- packages/shared/src/types/chain.ts | 1 + packages/shared/src/types/internal.ts | 1 + .../src/swappers/evm/execute-data/bridge.ts | 30 +- .../src/swappers/evm/execute-data/payload.ts | 4 +- packages/swap/src/types/swapper.ts | 80 +- 14 files changed, 1512 insertions(+), 411 deletions(-) create mode 100644 packages/shared/src/const/abis/bridge-facade.ts delete mode 100644 packages/shared/src/const/abis/bridge-router.ts create mode 100644 packages/shared/src/const/abis/fee-manager.ts diff --git a/packages/bridge/src/bridgers/evm/evm-bridger.ts b/packages/bridge/src/bridgers/evm/evm-bridger.ts index 9299f65d9..f4eac8f0b 100644 --- a/packages/bridge/src/bridgers/evm/evm-bridger.ts +++ b/packages/bridge/src/bridgers/evm/evm-bridger.ts @@ -1,6 +1,7 @@ import { ref, toRaw } from '@distributedlab/reactivity' import type { IProvider } from '@rarimo/provider' import type { + Address, BridgeChain, ChainId, DestinationTransaction, @@ -10,13 +11,15 @@ import { Amount, ChainKind, ChainTypes, + FEE_MANAGER_ABI, getDestinationTx as fetchDestTx, getSupportedChains as getChains, + NATIVE_TOKEN_ADDRESS, } from '@rarimo/shared' +import { Contract } from 'ethers' import { errors } from '@/errors' -import type { Bridger, BridgerCreateFn } from '@/types' -import type { Token } from '@/types' +import type { Bridger, BridgerCreateFn, Token } from '@/types' import { approve as _approve, @@ -68,24 +71,46 @@ export const createEVMBridger: BridgerCreateFn = ( const approveIfNeeded = async ( token: Token, - operator: HexString, + operator: Address, amount?: Amount, ) => { return _approveIfNeeded(provider, operator, token, amount) } - const approve = async (token: Token, operator: HexString) => { + const approve = async (token: Token, operator: Address) => { return _approve(provider, operator, token) } const isApproveRequired = async ( token: Token, - operator: HexString, + operator: Address, amount?: Amount, ) => { return isApproveERC20Required(provider, operator, token, amount) } + const getCommission = async (chain: BridgeChain, token: Token) => { + if (Number(chain.id) !== Number(provider.chainId)) { + throw new TypeError('provided chain is not the same as current') + } + + const facade = new Contract( + chain.bridgeFacadeAddress, + FEE_MANAGER_ABI, + provider.getWeb3Provider?.(), + ) + + const amount = await facade.getCommission( + token.isNative ? NATIVE_TOKEN_ADDRESS : token.address, + ) + + const commission = Amount.fromBigInt(amount, token.decimals) + + if (commission.isZero) throw new errors.BridgerZeroCommissionError() + + return commission + } + return toRaw({ chainType: ChainTypes.EVM, provider, @@ -98,5 +123,6 @@ export const createEVMBridger: BridgerCreateFn = ( isApproveRequired, approve, approveIfNeeded, + getCommission, }) } diff --git a/packages/bridge/src/errors/bridger.ts b/packages/bridge/src/errors/bridger.ts index abd6fe389..adbb46649 100644 --- a/packages/bridge/src/errors/bridger.ts +++ b/packages/bridge/src/errors/bridger.ts @@ -7,9 +7,9 @@ export class BridgerInvalidChainTypeError extends RuntimeError { } } -export class BridgerChainNotSupportedError extends RuntimeError { - public name = 'BridgerChainNotSupportedError' - constructor(message = 'This chain is not supported yet') { +export class BridgerZeroCommissionError extends RuntimeError { + public name = 'BridgerZeroCommissionError' + constructor(message = 'Commission cannot be zero') { super(message) } } diff --git a/packages/bridge/src/types/bridger.ts b/packages/bridge/src/types/bridger.ts index e0c2e7b34..1c72e8dae 100644 --- a/packages/bridge/src/types/bridger.ts +++ b/packages/bridge/src/types/bridger.ts @@ -1,6 +1,7 @@ import type { Computed, Raw, Ref } from '@distributedlab/reactivity' import type { IProvider, TransactionResponse } from '@rarimo/provider' import type { + Address, Amount, BridgeChain, ChainId, @@ -30,7 +31,7 @@ export type Bridger = Raw<{ /** * Get the chain that are supported for the bridging by ID * - * @returns Supported chain and information about it or void + * @returns A supported chain and information about it or void */ getChainById(id: ChainId): BridgeChain | void @@ -41,7 +42,7 @@ export type Bridger = Raw<{ */ getDestinationTx( sourceChain: BridgeChain, - sourceTxHash: string, + sourceTxHash: HexString, ): Promise /** @@ -51,7 +52,7 @@ export type Bridger = Raw<{ */ isApproveRequired( token: Token, - operator: HexString, + operator: Address, amount?: Amount, ): Promise @@ -60,22 +61,24 @@ export type Bridger = Raw<{ * * @returns A Transaction Response or undefined if input token is native */ - approve( - token: Token, - operator: HexString, - ): Promise + approve(token: Token, operator: Address): Promise /** * Sets allowance for the provided operator address to spend the token if - * allowance amount is less than provided one + * allowance amount is less than the provided one * * @returns A Transaction Response or undefined if input token is native or allowance is enough */ approveIfNeeded( token: Token, - operator: HexString, + operator: Address, amount?: Amount, ): Promise + + /** + * @returns A fee amount for the bridging for the provided chain and token + */ + getCommission(chain: BridgeChain, token: Token): Promise }> export type BridgerCreateFn = (p: IProvider) => Bridger diff --git a/packages/nft-checkout/src/operations/evm/evm-operation.ts b/packages/nft-checkout/src/operations/evm/evm-operation.ts index dc4ed1e26..8beab668f 100644 --- a/packages/nft-checkout/src/operations/evm/evm-operation.ts +++ b/packages/nft-checkout/src/operations/evm/evm-operation.ts @@ -1,5 +1,4 @@ import { extend, ref, toRaw } from '@distributedlab/reactivity' -import { BN } from '@distributedlab/tools' import type { Token } from '@rarimo/bridge' import type { IProvider } from '@rarimo/provider' import { @@ -41,8 +40,6 @@ import { isSameChainOperation, } from './helpers' -const RARIMO_BRIDGE_FEE = 2.5 - /** * An operation on an EVM chain. * @@ -89,6 +86,7 @@ export const EVMOperation = (provider: IProvider): CheckoutOperation => { throw new errors.OperationInvalidChainPairError() } + await handleCorrectProviderChain(provider, chainFrom) await _loadTokens() isSameChain = isSameChainOperation(params) @@ -119,8 +117,6 @@ export const EVMOperation = (provider: IProvider): CheckoutOperation => { _setStatus(CheckoutOperationStatus.PaymentTokensLoading) - await handleCorrectProviderChain(provider, chainFrom) - const withPairs = await getPaymentTokensWithPairs({ provider, chainFrom, @@ -243,53 +239,44 @@ export const EVMOperation = (provider: IProvider): CheckoutOperation => { const _getChainToUSDCSwapAmountIn = async () => { const { chainIdTo, slippage, price } = params + const chainFromUSDC = getTokenByAddress( + chainFromTokens, + USDC_MAP[chainFrom.name]!, + )! + const usdcAddress = USDC_MAP[chainTo.name]! const getTokenArgs: [BridgeChain, Token[]] = [chainTo, chainToTokens] const from = getSameChainSwapToToken(...getTokenArgs, usdcAddress)! const to = getSameChainSwapToToken(...getTokenArgs, price.address)! + const fee = await swapper.getCommission(chainFrom, chainFromUSDC) const estimateArgs = { chainIdFrom: chainIdTo, chainIdTo, from, to, - amountOut: Amount.fromBN( - bnFromAmountLike(price).addPercent(RARIMO_BRIDGE_FEE), - ), + amountOut: price, slippage, } // estimate USDC -> Target Token on the destination chain to determine - // how much USDC is required to bridge with and without bridge % fee - const estimationWithFee = await getEstimation(estimateArgs) + // how much USDC is required to bridge + const estimation = await getEstimation(estimateArgs) // USDC could have different decimals on different chains, thus we need to // convert it to the same decimals as the chain from token. // If this method called - "swapToToken" always is USDC const chainFromUSDCAmountOut = bnFromAmountLike( - estimationWithFee.amountIn, + estimation.amountIn, ).toDecimals(swapToToken.decimals) - // Amount from which percent will be subtracted on the backend side has - // precision chainFromUSDCAmountOut.decimals, thus we need to cut off - // the extra precision from the chainFromUSDCAmountOut.raw, so we will create - // a new BN instance from the chainFromUSDCAmountOut.value - const amountIn = Amount.fromBN( - BN.fromBigInt( - chainFromUSDCAmountOut.value, - chainFromUSDCAmountOut.decimals, - ) - .subPercent(RARIMO_BRIDGE_FEE) - .toDecimals(estimationWithFee.amountIn.decimals), - ) - intermediateOpts = { - ...estimationWithFee, - amountIn, + ...estimation, + amountIn: estimation.amountIn, amountOut: price, } - return Amount.fromBN(chainFromUSDCAmountOut) + return Amount.fromBN(chainFromUSDCAmountOut.add(fee.bn)) } const _loadTokens = async () => { diff --git a/packages/shared/src/api/dex.ts b/packages/shared/src/api/dex.ts index 032118fe6..da220ca94 100644 --- a/packages/shared/src/api/dex.ts +++ b/packages/shared/src/api/dex.ts @@ -32,6 +32,7 @@ export const getSupportedChains = async ({ icon: chain.icon, isTestnet: chain.kind.name === ChainKind.Testnet, contractAddress: chain.swap_contract_address, + bridgeFacadeAddress: chain.bridge_facade_address, dexType: chain.swap_contract_version, token: { ...chain.native_token, diff --git a/packages/shared/src/const/abis/bridge-facade.ts b/packages/shared/src/const/abis/bridge-facade.ts new file mode 100644 index 000000000..97aa352f7 --- /dev/null +++ b/packages/shared/src/const/abis/bridge-facade.ts @@ -0,0 +1,1037 @@ +export const BRIDGE_FACADE_ABI = [ + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'feeAmount', + type: 'uint256', + }, + ], + name: 'AddedFeeToken', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'previousAdmin', + type: 'address', + }, + { + indexed: false, + internalType: 'address', + name: 'newAdmin', + type: 'address', + }, + ], + name: 'AdminChanged', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'beacon', + type: 'address', + }, + ], + name: 'BeaconUpgraded', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'uint8', + name: 'version', + type: 'uint8', + }, + ], + name: 'Initialized', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'feeAmount', + type: 'uint256', + }, + ], + name: 'RemovedFeeToken', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'feeAmount', + type: 'uint256', + }, + ], + name: 'UpdatedFeeToken', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'implementation', + type: 'address', + }, + ], + name: 'Upgraded', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'receiver', + type: 'address', + }, + { + indexed: false, + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'amount', + type: 'uint256', + }, + ], + name: 'WithdrawnFeeToken', + type: 'event', + }, + { + inputs: [ + { + internalType: 'address', + name: 'bridge_', + type: 'address', + }, + ], + name: '__BridgeFacade_init', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + internalType: 'address', + name: 'bridge_', + type: 'address', + }, + ], + name: '__FeeManager_init', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address[]', + name: 'feeTokens', + type: 'address[]', + }, + { + internalType: 'uint256[]', + name: 'feeAmounts', + type: 'uint256[]', + }, + { + internalType: 'bytes', + name: 'signature', + type: 'bytes', + }, + ], + internalType: 'struct IFeeManager.AddFeeTokenParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'addFeeToken', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [], + name: 'bridge', + outputs: [ + { + internalType: 'address', + name: '', + type: 'address', + }, + ], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + ], + internalType: 'struct IBridgeFacade.DepositFeeERC1155Parameters', + name: 'feeParams_', + type: 'tuple', + }, + { + components: [ + { + internalType: 'address', + name: 'token', + type: 'address', + }, + { + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + { + internalType: 'uint256', + name: 'amount', + type: 'uint256', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'string', + name: 'network', + type: 'string', + }, + { + internalType: 'string', + name: 'receiver', + type: 'string', + }, + { + internalType: 'bool', + name: 'isWrapped', + type: 'bool', + }, + ], + internalType: 'struct IERC1155Handler.DepositERC1155Parameters', + name: 'depositParams_', + type: 'tuple', + }, + ], + name: 'depositERC1155', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + ], + internalType: 'struct IBridgeFacade.DepositFeeERC20Parameters', + name: 'feeParams_', + type: 'tuple', + }, + { + components: [ + { + internalType: 'address', + name: 'token', + type: 'address', + }, + { + internalType: 'uint256', + name: 'amount', + type: 'uint256', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'string', + name: 'network', + type: 'string', + }, + { + internalType: 'string', + name: 'receiver', + type: 'string', + }, + { + internalType: 'bool', + name: 'isWrapped', + type: 'bool', + }, + ], + internalType: 'struct IERC20Handler.DepositERC20Parameters', + name: 'depositParams_', + type: 'tuple', + }, + ], + name: 'depositERC20', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + ], + internalType: 'struct IBridgeFacade.DepositFeeERC721Parameters', + name: 'feeParams_', + type: 'tuple', + }, + { + components: [ + { + internalType: 'address', + name: 'token', + type: 'address', + }, + { + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'string', + name: 'network', + type: 'string', + }, + { + internalType: 'string', + name: 'receiver', + type: 'string', + }, + { + internalType: 'bool', + name: 'isWrapped', + type: 'bool', + }, + ], + internalType: 'struct IERC721Handler.DepositERC721Parameters', + name: 'depositParams_', + type: 'tuple', + }, + ], + name: 'depositERC721', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + ], + internalType: 'struct IBridgeFacade.DepositFeeNativeParameters', + name: 'feeParams_', + type: 'tuple', + }, + { + components: [ + { + internalType: 'uint256', + name: 'amount', + type: 'uint256', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'string', + name: 'network', + type: 'string', + }, + { + internalType: 'string', + name: 'receiver', + type: 'string', + }, + ], + internalType: 'struct INativeHandler.DepositNativeParameters', + name: 'depositParams_', + type: 'tuple', + }, + ], + name: 'depositNative', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + ], + internalType: 'struct IBridgeFacade.DepositFeeSBTParameters', + name: 'feeParams_', + type: 'tuple', + }, + { + components: [ + { + internalType: 'address', + name: 'token', + type: 'address', + }, + { + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'string', + name: 'network', + type: 'string', + }, + { + internalType: 'string', + name: 'receiver', + type: 'string', + }, + ], + internalType: 'struct ISBTHandler.DepositSBTParameters', + name: 'depositParams_', + type: 'tuple', + }, + ], + name: 'depositSBT', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { + internalType: 'address', + name: 'feeToken_', + type: 'address', + }, + ], + name: 'getCommission', + outputs: [ + { + internalType: 'uint256', + name: 'commission_', + type: 'uint256', + }, + ], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'proxiableUUID', + outputs: [ + { + internalType: 'bytes32', + name: '', + type: 'bytes32', + }, + ], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address[]', + name: 'feeTokens', + type: 'address[]', + }, + { + internalType: 'uint256[]', + name: 'feeAmounts', + type: 'uint256[]', + }, + { + internalType: 'bytes', + name: 'signature', + type: 'bytes', + }, + ], + internalType: 'struct IFeeManager.RemoveFeeTokenParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'removeFeeToken', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address[]', + name: 'feeTokens', + type: 'address[]', + }, + { + internalType: 'uint256[]', + name: 'feeAmounts', + type: 'uint256[]', + }, + { + internalType: 'bytes', + name: 'signature', + type: 'bytes', + }, + ], + internalType: 'struct IFeeManager.UpdateFeeTokenParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'updateFeeToken', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + internalType: 'address', + name: 'newImplementation', + type: 'address', + }, + ], + name: 'upgradeTo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + internalType: 'address', + name: 'newImplementation', + type: 'address', + }, + { + internalType: 'bytes', + name: 'data', + type: 'bytes', + }, + ], + name: 'upgradeToAndCall', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { + internalType: 'address', + name: 'newImplementation_', + type: 'address', + }, + { + internalType: 'bytes', + name: 'signature_', + type: 'bytes', + }, + ], + name: 'upgradeToWithSig', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'token', + type: 'address', + }, + { + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + { + internalType: 'string', + name: 'tokenURI', + type: 'string', + }, + { + internalType: 'uint256', + name: 'amount', + type: 'uint256', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'bytes32', + name: 'originHash', + type: 'bytes32', + }, + { + internalType: 'address', + name: 'receiver', + type: 'address', + }, + { + internalType: 'bytes', + name: 'proof', + type: 'bytes', + }, + { + internalType: 'bool', + name: 'isWrapped', + type: 'bool', + }, + ], + internalType: 'struct IERC1155Handler.WithdrawERC1155Parameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'withdrawERC1155', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'token', + type: 'address', + }, + { + internalType: 'uint256', + name: 'amount', + type: 'uint256', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'bytes32', + name: 'originHash', + type: 'bytes32', + }, + { + internalType: 'address', + name: 'receiver', + type: 'address', + }, + { + internalType: 'bytes', + name: 'proof', + type: 'bytes', + }, + { + internalType: 'bool', + name: 'isWrapped', + type: 'bool', + }, + ], + internalType: 'struct IERC20Handler.WithdrawERC20Parameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'withdrawERC20', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'token', + type: 'address', + }, + { + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + { + internalType: 'string', + name: 'tokenURI', + type: 'string', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'bytes32', + name: 'originHash', + type: 'bytes32', + }, + { + internalType: 'address', + name: 'receiver', + type: 'address', + }, + { + internalType: 'bytes', + name: 'proof', + type: 'bytes', + }, + { + internalType: 'bool', + name: 'isWrapped', + type: 'bool', + }, + ], + internalType: 'struct IERC721Handler.WithdrawERC721Parameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'withdrawERC721', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'receiver', + type: 'address', + }, + { + internalType: 'address[]', + name: 'feeTokens', + type: 'address[]', + }, + { + internalType: 'uint256[]', + name: 'amounts', + type: 'uint256[]', + }, + { + internalType: 'bytes', + name: 'signature', + type: 'bytes', + }, + ], + internalType: 'struct IFeeManager.WithdrawFeeTokenParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'withdrawFeeToken', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'uint256', + name: 'amount', + type: 'uint256', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'bytes32', + name: 'originHash', + type: 'bytes32', + }, + { + internalType: 'address', + name: 'receiver', + type: 'address', + }, + { + internalType: 'bytes', + name: 'proof', + type: 'bytes', + }, + ], + internalType: 'struct INativeHandler.WithdrawNativeParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'withdrawNative', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'token', + type: 'address', + }, + { + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + { + internalType: 'string', + name: 'tokenURI', + type: 'string', + }, + { + components: [ + { + internalType: 'bytes32', + name: 'salt', + type: 'bytes32', + }, + { + internalType: 'bytes', + name: 'bundle', + type: 'bytes', + }, + ], + internalType: 'struct IBundler.Bundle', + name: 'bundle', + type: 'tuple', + }, + { + internalType: 'bytes32', + name: 'originHash', + type: 'bytes32', + }, + { + internalType: 'address', + name: 'receiver', + type: 'address', + }, + { + internalType: 'bytes', + name: 'proof', + type: 'bytes', + }, + ], + internalType: 'struct ISBTHandler.WithdrawSBTParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'withdrawSBT', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, +] diff --git a/packages/shared/src/const/abis/bridge-router.ts b/packages/shared/src/const/abis/bridge-router.ts deleted file mode 100644 index 79532fbd9..000000000 --- a/packages/shared/src/const/abis/bridge-router.ts +++ /dev/null @@ -1,288 +0,0 @@ -export const BRIDGE_ROUTER_ABI = [ - { - inputs: [], - name: 'BRIDGE_ROUTER_STORAGE_SLOT', - outputs: [ - { - internalType: 'bytes32', - name: '', - type: 'bytes32', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [], - name: 'MASTER_ROUTER_STORAGE_SLOT', - outputs: [ - { - internalType: 'bytes32', - name: '', - type: 'bytes32', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [], - name: 'OWNABLE_DIAMOND_STORAGE_SLOT', - outputs: [ - { - internalType: 'bytes32', - name: '', - type: 'bytes32', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [ - { - internalType: 'address', - name: 'token_', - type: 'address', - }, - { - internalType: 'uint256', - name: 'tokenId_', - type: 'uint256', - }, - { - internalType: 'uint256', - name: 'amount_', - type: 'uint256', - }, - { - components: [ - { - internalType: 'bytes32', - name: 'salt', - type: 'bytes32', - }, - { - internalType: 'bytes', - name: 'bundle', - type: 'bytes', - }, - ], - internalType: 'struct IBundler.Bundle', - name: 'bundle_', - type: 'tuple', - }, - { - internalType: 'string', - name: 'network_', - type: 'string', - }, - { - internalType: 'string', - name: 'receiver_', - type: 'string', - }, - { - internalType: 'bool', - name: 'isWrapped_', - type: 'bool', - }, - ], - name: 'bridgeERC1155', - outputs: [], - stateMutability: 'payable', - type: 'function', - }, - { - inputs: [ - { - internalType: 'address', - name: 'token_', - type: 'address', - }, - { - internalType: 'uint256', - name: 'amount_', - type: 'uint256', - }, - { - components: [ - { - internalType: 'bytes32', - name: 'salt', - type: 'bytes32', - }, - { - internalType: 'bytes', - name: 'bundle', - type: 'bytes', - }, - ], - internalType: 'struct IBundler.Bundle', - name: 'bundle_', - type: 'tuple', - }, - { - internalType: 'string', - name: 'network_', - type: 'string', - }, - { - internalType: 'string', - name: 'receiver_', - type: 'string', - }, - { - internalType: 'bool', - name: 'isWrapped_', - type: 'bool', - }, - ], - name: 'bridgeERC20', - outputs: [], - stateMutability: 'payable', - type: 'function', - }, - { - inputs: [ - { - internalType: 'address', - name: 'token_', - type: 'address', - }, - { - internalType: 'uint256', - name: 'tokenId_', - type: 'uint256', - }, - { - components: [ - { - internalType: 'bytes32', - name: 'salt', - type: 'bytes32', - }, - { - internalType: 'bytes', - name: 'bundle', - type: 'bytes', - }, - ], - internalType: 'struct IBundler.Bundle', - name: 'bundle_', - type: 'tuple', - }, - { - internalType: 'string', - name: 'network_', - type: 'string', - }, - { - internalType: 'string', - name: 'receiver_', - type: 'string', - }, - { - internalType: 'bool', - name: 'isWrapped_', - type: 'bool', - }, - ], - name: 'bridgeERC721', - outputs: [], - stateMutability: 'payable', - type: 'function', - }, - { - inputs: [ - { - internalType: 'uint256', - name: 'amount_', - type: 'uint256', - }, - { - components: [ - { - internalType: 'bytes32', - name: 'salt', - type: 'bytes32', - }, - { - internalType: 'bytes', - name: 'bundle', - type: 'bytes', - }, - ], - internalType: 'struct IBundler.Bundle', - name: 'bundle_', - type: 'tuple', - }, - { - internalType: 'string', - name: 'network_', - type: 'string', - }, - { - internalType: 'string', - name: 'receiver_', - type: 'string', - }, - ], - name: 'bridgeNative', - outputs: [], - stateMutability: 'payable', - type: 'function', - }, - { - inputs: [], - name: 'getBridgeAddress', - outputs: [ - { - internalType: 'address', - name: 'bridge_', - type: 'address', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [], - name: 'getCallerAddress', - outputs: [ - { - internalType: 'address', - name: 'caller_', - type: 'address', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [], - name: 'owner', - outputs: [ - { - internalType: 'address', - name: '', - type: 'address', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [ - { - internalType: 'address', - name: 'bridge_', - type: 'address', - }, - ], - name: 'setBridgeAddress', - outputs: [], - stateMutability: 'nonpayable', - type: 'function', - }, -] diff --git a/packages/shared/src/const/abis/fee-manager.ts b/packages/shared/src/const/abis/fee-manager.ts new file mode 100644 index 000000000..84c2d81f2 --- /dev/null +++ b/packages/shared/src/const/abis/fee-manager.ts @@ -0,0 +1,374 @@ +export const FEE_MANAGER_ABI = [ + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'feeAmount', + type: 'uint256', + }, + ], + name: 'AddedFeeToken', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'previousAdmin', + type: 'address', + }, + { + indexed: false, + internalType: 'address', + name: 'newAdmin', + type: 'address', + }, + ], + name: 'AdminChanged', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'beacon', + type: 'address', + }, + ], + name: 'BeaconUpgraded', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'uint8', + name: 'version', + type: 'uint8', + }, + ], + name: 'Initialized', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'feeAmount', + type: 'uint256', + }, + ], + name: 'RemovedFeeToken', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'feeAmount', + type: 'uint256', + }, + ], + name: 'UpdatedFeeToken', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'implementation', + type: 'address', + }, + ], + name: 'Upgraded', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'address', + name: 'receiver', + type: 'address', + }, + { + indexed: false, + internalType: 'address', + name: 'feeToken', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'amount', + type: 'uint256', + }, + ], + name: 'WithdrawnFeeToken', + type: 'event', + }, + { + inputs: [ + { + internalType: 'address', + name: 'bridge_', + type: 'address', + }, + ], + name: '__FeeManager_init', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address[]', + name: 'feeTokens', + type: 'address[]', + }, + { + internalType: 'uint256[]', + name: 'feeAmounts', + type: 'uint256[]', + }, + { + internalType: 'bytes', + name: 'signature', + type: 'bytes', + }, + ], + internalType: 'struct IFeeManager.AddFeeTokenParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'addFeeToken', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [], + name: 'bridge', + outputs: [ + { + internalType: 'address', + name: '', + type: 'address', + }, + ], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { + internalType: 'address', + name: 'feeToken_', + type: 'address', + }, + ], + name: 'getCommission', + outputs: [ + { + internalType: 'uint256', + name: 'commission_', + type: 'uint256', + }, + ], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'proxiableUUID', + outputs: [ + { + internalType: 'bytes32', + name: '', + type: 'bytes32', + }, + ], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address[]', + name: 'feeTokens', + type: 'address[]', + }, + { + internalType: 'uint256[]', + name: 'feeAmounts', + type: 'uint256[]', + }, + { + internalType: 'bytes', + name: 'signature', + type: 'bytes', + }, + ], + internalType: 'struct IFeeManager.RemoveFeeTokenParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'removeFeeToken', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address[]', + name: 'feeTokens', + type: 'address[]', + }, + { + internalType: 'uint256[]', + name: 'feeAmounts', + type: 'uint256[]', + }, + { + internalType: 'bytes', + name: 'signature', + type: 'bytes', + }, + ], + internalType: 'struct IFeeManager.UpdateFeeTokenParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'updateFeeToken', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + internalType: 'address', + name: 'newImplementation', + type: 'address', + }, + ], + name: 'upgradeTo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + internalType: 'address', + name: 'newImplementation', + type: 'address', + }, + { + internalType: 'bytes', + name: 'data', + type: 'bytes', + }, + ], + name: 'upgradeToAndCall', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { + internalType: 'address', + name: 'newImplementation_', + type: 'address', + }, + { + internalType: 'bytes', + name: 'signature_', + type: 'bytes', + }, + ], + name: 'upgradeToWithSig', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { + internalType: 'address', + name: 'receiver', + type: 'address', + }, + { + internalType: 'address[]', + name: 'feeTokens', + type: 'address[]', + }, + { + internalType: 'uint256[]', + name: 'amounts', + type: 'uint256[]', + }, + { + internalType: 'bytes', + name: 'signature', + type: 'bytes', + }, + ], + internalType: 'struct IFeeManager.WithdrawFeeTokenParameters', + name: 'params_', + type: 'tuple', + }, + ], + name: 'withdrawFeeToken', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, +] diff --git a/packages/shared/src/const/abis/index.ts b/packages/shared/src/const/abis/index.ts index 7fed0ae44..5b7421175 100644 --- a/packages/shared/src/const/abis/index.ts +++ b/packages/shared/src/const/abis/index.ts @@ -1,5 +1,6 @@ -export * from './bridge-router' +export * from './bridge-facade' export * from './erc-20' +export * from './fee-manager' export * from './master-router' export * from './multicall' export * from './swap-diamond' diff --git a/packages/shared/src/types/chain.ts b/packages/shared/src/types/chain.ts index 267b889a4..aef6a052e 100644 --- a/packages/shared/src/types/chain.ts +++ b/packages/shared/src/types/chain.ts @@ -21,6 +21,7 @@ export type Chain = { } export type BridgeChain = Chain & { + bridgeFacadeAddress: Address contractAddress: Address dexType: EVMDexType } diff --git a/packages/shared/src/types/internal.ts b/packages/shared/src/types/internal.ts index 29c5e79e9..388b1e0bf 100644 --- a/packages/shared/src/types/internal.ts +++ b/packages/shared/src/types/internal.ts @@ -24,6 +24,7 @@ export type InternalBridgeChain = { explorer_url: string icon: string swap_contract_address: Address + bridge_facade_address: Address swap_contract_version: EVMDexType native_token: { symbol: TokenSymbol diff --git a/packages/swap/src/swappers/evm/execute-data/bridge.ts b/packages/swap/src/swappers/evm/execute-data/bridge.ts index 4ab2d8f61..e3e253022 100644 --- a/packages/swap/src/swappers/evm/execute-data/bridge.ts +++ b/packages/swap/src/swappers/evm/execute-data/bridge.ts @@ -4,6 +4,7 @@ import { type BridgeChain, BUNDLE_SALT_BYTES, isUndefined, + NATIVE_TOKEN_ADDRESS, type TransactionBundle, } from '@rarimo/shared' import { utils } from 'ethers' @@ -21,7 +22,7 @@ export const getBridgeData = ( to: Token, amountOut: Amount, receiver: string, - bundle?: TransactionBundle, + _bundle?: TransactionBundle, isWrapped?: boolean, ): CommandPayload[] => { if (!isBridgingRequired) return [] @@ -32,21 +33,28 @@ export const getBridgeData = ( ) } - const bundleTuple = [ - bundle?.salt || utils.hexlify(utils.randomBytes(BUNDLE_SALT_BYTES)), - buildIntermediateBundleData(intermediateOpts, chainTo, receiver, bundle), - ] + const bundle = { + salt: _bundle?.salt || utils.hexlify(utils.randomBytes(BUNDLE_SALT_BYTES)), + bundle: buildIntermediateBundleData( + intermediateOpts, + chainTo, + receiver, + _bundle, + ), + } return [ buildPayload( to.isNative ? SwapCommands.BridgeNative : SwapCommands.BridgeErc20, [ - ...(to.isNative ? [] : [to.address]), - amountOut.value, - bundleTuple, - chainTo.name, - receiver, - ...(to.isNative ? [] : [isWrapped]), + { feeToken: to.isNative ? NATIVE_TOKEN_ADDRESS : to.address }, + { + ...(!to.isNative && { token: to.address, isWrapped }), + amount: amountOut.value, + bundle, + network: chainTo.name, + receiver, + }, ], ), ] diff --git a/packages/swap/src/swappers/evm/execute-data/payload.ts b/packages/swap/src/swappers/evm/execute-data/payload.ts index 554d85b38..f5545e69c 100644 --- a/packages/swap/src/swappers/evm/execute-data/payload.ts +++ b/packages/swap/src/swappers/evm/execute-data/payload.ts @@ -1,5 +1,5 @@ import { - BRIDGE_ROUTER_ABI, + BRIDGE_FACADE_ABI, MASTER_ROUTER_ABI, MULTICALL_ROUTER_ABI, TRADER_JOE_ROUTER_ABI, @@ -14,8 +14,8 @@ import { SWAP_COMMANDS_NAME_MAP } from '@/const' import type { SwapCommands } from '@/enums' const ABIS = [ + BRIDGE_FACADE_ABI, MASTER_ROUTER_ABI, - BRIDGE_ROUTER_ABI, MULTICALL_ROUTER_ABI, TRADER_JOE_ROUTER_ABI, TRANSFER_ROUTER_ABI, diff --git a/packages/swap/src/types/swapper.ts b/packages/swap/src/types/swapper.ts index b07fd44ca..f5a6e9102 100644 --- a/packages/swap/src/types/swapper.ts +++ b/packages/swap/src/types/swapper.ts @@ -1,72 +1,22 @@ -import type { Computed, Raw, Ref } from '@distributedlab/reactivity' -import type { Token } from '@rarimo/bridge' +import type { Raw } from '@distributedlab/reactivity' +import type { Bridger, Token } from '@rarimo/bridge' import type { IProvider, TransactionResponse } from '@rarimo/provider' -import type { - Amount, - BridgeChain, - ChainId, - ChainTypes, - DestinationTransaction, - HexString, - TransactionBundle, -} from '@rarimo/shared' +import type { Amount, BridgeChain, TransactionBundle } from '@rarimo/shared' export type SwapperCreateFn = (p: IProvider) => Swapper -export type Swapper = Raw<{ - provider: Ref - chains: Computed - chainType: ChainTypes - isInitialized: Ref - init(): Promise - /** - * @description Submits a transaction to the swap contract to swap, wrap, - * unwrap, bridge tokens - * @returns Transaction Response - */ - execute( - args: ExecuteArgs, - multiplePaymentOpts?: MultiplePaymentOpts, - ): Promise - /** - * Proxy function of {@link Bridger.getSupportedChains} - */ - getSupportedChains(): Promise - /** - * Proxy function of {@link Bridger.getChainById} - */ - getChainById(id: ChainId): BridgeChain | void - /** - * Proxy function of {@link Bridger.getDestinationTx} - */ - getDestinationTx( - sourceChain: BridgeChain, - sourceTxHash: string, - ): Promise - /** - * Proxy function of {@link Bridger.isApproveRequired} - */ - isApproveRequired( - token: Token, - operator: HexString, - amount?: Amount, - ): Promise - /** - * Proxy function of {@link Bridger.approve} - */ - approve( - token: Token, - operator: HexString, - ): Promise - /** - * Proxy function of {@link Bridger.approveIfNeeded} - */ - approveIfNeeded( - token: Token, - operator: HexString, - amount?: Amount, - ): Promise -}> +export type Swapper = Bridger & + Raw<{ + /** + * @description Submits a transaction to the swap contract to swap, wrap, + * unwrap, bridge tokens + * @returns Transaction Response + */ + execute( + args: ExecuteArgs, + multiplePaymentOpts?: MultiplePaymentOpts, + ): Promise + }> export type ExecuteArgs = { swapOpts: SwapOpts[]