From de3693bba386f509fa44b0cc185157aa39c1fffc Mon Sep 17 00:00:00 2001 From: Zikriya Date: Sun, 12 May 2024 00:09:43 +0500 Subject: [PATCH] Improvements for fee distribution flow --- src/constants/FiberRouter.json | 355 ++++++++++++++++++++++------ src/constants/constants.ts | 12 +- src/interfaces/job.interface.ts | 1 + src/services/signature.service.ts | 8 +- src/services/transaction.service.ts | 1 + 5 files changed, 299 insertions(+), 78 deletions(-) diff --git a/src/constants/FiberRouter.json b/src/constants/FiberRouter.json index dcd42c9..a3d159b 100644 --- a/src/constants/FiberRouter.json +++ b/src/constants/FiberRouter.json @@ -3,6 +3,37 @@ "contractName": "FiberRouter", "sourceName": "contracts/upgradeable-Bridge/FiberRouter.sol", "abi": [ + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "token", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "preFeeAmount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "afterFeeAmount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "totalFee", + "type": "uint256" + } + ], + "name": "FeesDistributed", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -52,9 +83,9 @@ }, { "indexed": false, - "internalType": "bytes", + "internalType": "bytes4", "name": "selector", - "type": "bytes" + "type": "bytes4" } ], "name": "RouterAndSelectorWhitelisted", @@ -274,12 +305,49 @@ "name": "WithdrawRouter", "type": "event" }, + { + "inputs": [], + "name": "DISTRIBUTE_FEES_TYPEHASH", + "outputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "FEE_ALLOCATION_TYPEHASH", + "outputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "NAME", + "outputs": [{ "internalType": "string", "name": "", "type": "string" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "VERSION", + "outputs": [{ "internalType": "string", "name": "", "type": "string" }], + "stateMutability": "view", + "type": "function" + }, { "inputs": [ { "internalType": "address", "name": "router", "type": "address" }, - { "internalType": "bytes", "name": "selector", "type": "bytes" } + { "internalType": "bytes4[]", "name": "selectors", "type": "bytes4[]" } + ], + "name": "addRouterAndSelectors", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_signer", "type": "address" } ], - "name": "addRouterAndSelector", + "name": "addSigner", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -341,6 +409,15 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "inputs": [ + { "internalType": "address", "name": "_signer", "type": "address" } + ], + "name": "removeSigner", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, { "inputs": [], "name": "renounceOwnership", @@ -392,160 +469,291 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "inputs": [{ "internalType": "address", "name": "", "type": "address" }], + "name": "signers", + "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], + "stateMutability": "view", + "type": "function" + }, { "inputs": [ - { "internalType": "address", "name": "token", "type": "address" }, - { "internalType": "uint256", "name": "amount", "type": "uint256" }, + { "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "uint256", - "name": "targetNetwork", + "name": "minAmountOut", "type": "uint256" }, - { "internalType": "address", "name": "targetToken", "type": "address" }, + { "internalType": "address", "name": "fromToken", "type": "address" }, + { "internalType": "address", "name": "toToken", "type": "address" }, { "internalType": "address", "name": "targetAddress", "type": "address" }, - { - "internalType": "bytes32", - "name": "withdrawalData", - "type": "bytes32" - }, - { "internalType": "bool", "name": "cctpType", "type": "bool" } + { "internalType": "address", "name": "router", "type": "address" }, + { "internalType": "bytes", "name": "routerCalldata", "type": "bytes" } ], - "name": "swap", + "name": "swapOnSameNetwork", "outputs": [], - "stateMutability": "payable", + "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ - { "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "uint256", "name": "minAmountOut", "type": "uint256" }, - { "internalType": "address", "name": "fromToken", "type": "address" }, + { "internalType": "address", "name": "toToken", "type": "address" }, { "internalType": "address", - "name": "foundryToken", + "name": "targetAddress", "type": "address" }, { "internalType": "address", "name": "router", "type": "address" }, - { "internalType": "bytes", "name": "routerCalldata", "type": "bytes" }, - { - "internalType": "uint256", - "name": "crossTargetNetwork", - "type": "uint256" - }, - { - "internalType": "address", - "name": "crossTargetToken", - "type": "address" - }, + { "internalType": "bytes", "name": "routerCalldata", "type": "bytes" } + ], + "name": "swapOnSameNetworkETH", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "token", "type": "address" }, + { "internalType": "uint256", "name": "amount", "type": "uint256" }, { - "internalType": "address", - "name": "crossTargetAddress", - "type": "address" + "components": [ + { + "internalType": "uint256", + "name": "targetNetwork", + "type": "uint256" + }, + { + "internalType": "address", + "name": "targetToken", + "type": "address" + }, + { + "internalType": "address", + "name": "targetAddress", + "type": "address" + } + ], + "internalType": "struct FiberRouter.SwapCrossData", + "name": "sd", + "type": "tuple" }, { "internalType": "bytes32", "name": "withdrawalData", "type": "bytes32" }, - { "internalType": "bool", "name": "cctpType", "type": "bool" } + { "internalType": "bool", "name": "cctpType", "type": "bool" }, + { + "components": [ + { + "components": [ + { + "internalType": "address", + "name": "recipient", + "type": "address" + }, + { + "internalType": "uint16", + "name": "rateInBps", + "type": "uint16" + } + ], + "internalType": "struct FeeDistributor.FeeAllocation[]", + "name": "feeAllocations", + "type": "tuple[]" + }, + { "internalType": "bytes32", "name": "salt", "type": "bytes32" }, + { "internalType": "uint256", "name": "expiry", "type": "uint256" }, + { "internalType": "bytes", "name": "signature", "type": "bytes" } + ], + "internalType": "struct FeeDistributor.FeeDistributionData", + "name": "fd", + "type": "tuple" + } ], - "name": "swapAndCrossRouter", + "name": "swapSigned", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [ + { "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "uint256", "name": "minAmountOut", "type": "uint256" }, + { "internalType": "address", "name": "fromToken", "type": "address" }, { "internalType": "address", "name": "foundryToken", "type": "address" }, - { "internalType": "uint256", "name": "gasFee", "type": "uint256" }, { "internalType": "address", "name": "router", "type": "address" }, { "internalType": "bytes", "name": "routerCalldata", "type": "bytes" }, { - "internalType": "uint256", - "name": "crossTargetNetwork", - "type": "uint256" - }, - { - "internalType": "address", - "name": "crossTargetToken", - "type": "address" - }, - { - "internalType": "address", - "name": "crossTargetAddress", - "type": "address" + "components": [ + { + "internalType": "uint256", + "name": "targetNetwork", + "type": "uint256" + }, + { + "internalType": "address", + "name": "targetToken", + "type": "address" + }, + { + "internalType": "address", + "name": "targetAddress", + "type": "address" + } + ], + "internalType": "struct FiberRouter.SwapCrossData", + "name": "sd", + "type": "tuple" }, { "internalType": "bytes32", "name": "withdrawalData", "type": "bytes32" }, - { "internalType": "bool", "name": "cctpType", "type": "bool" } + { "internalType": "bool", "name": "cctpType", "type": "bool" }, + { + "components": [ + { + "components": [ + { + "internalType": "address", + "name": "recipient", + "type": "address" + }, + { + "internalType": "uint16", + "name": "rateInBps", + "type": "uint16" + } + ], + "internalType": "struct FeeDistributor.FeeAllocation[]", + "name": "feeAllocations", + "type": "tuple[]" + }, + { "internalType": "bytes32", "name": "salt", "type": "bytes32" }, + { "internalType": "uint256", "name": "expiry", "type": "uint256" }, + { "internalType": "bytes", "name": "signature", "type": "bytes" } + ], + "internalType": "struct FeeDistributor.FeeDistributionData", + "name": "fd", + "type": "tuple" + } ], - "name": "swapAndCrossRouterETH", + "name": "swapSignedAndCrossRouter", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [ - { "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "uint256", "name": "minAmountOut", "type": "uint256" }, - { "internalType": "address", "name": "fromToken", "type": "address" }, - { "internalType": "address", "name": "toToken", "type": "address" }, { "internalType": "address", - "name": "targetAddress", + "name": "foundryToken", "type": "address" }, + { "internalType": "uint256", "name": "gasFee", "type": "uint256" }, { "internalType": "address", "name": "router", "type": "address" }, - { "internalType": "bytes", "name": "routerCalldata", "type": "bytes" } + { "internalType": "bytes", "name": "routerCalldata", "type": "bytes" }, + { + "components": [ + { + "internalType": "uint256", + "name": "targetNetwork", + "type": "uint256" + }, + { + "internalType": "address", + "name": "targetToken", + "type": "address" + }, + { + "internalType": "address", + "name": "targetAddress", + "type": "address" + } + ], + "internalType": "struct FiberRouter.SwapCrossData", + "name": "sd", + "type": "tuple" + }, + { + "internalType": "bytes32", + "name": "withdrawalData", + "type": "bytes32" + }, + { "internalType": "bool", "name": "cctpType", "type": "bool" }, + { + "components": [ + { + "components": [ + { + "internalType": "address", + "name": "recipient", + "type": "address" + }, + { + "internalType": "uint16", + "name": "rateInBps", + "type": "uint16" + } + ], + "internalType": "struct FeeDistributor.FeeAllocation[]", + "name": "feeAllocations", + "type": "tuple[]" + }, + { "internalType": "bytes32", "name": "salt", "type": "bytes32" }, + { "internalType": "uint256", "name": "expiry", "type": "uint256" }, + { "internalType": "bytes", "name": "signature", "type": "bytes" } + ], + "internalType": "struct FeeDistributor.FeeDistributionData", + "name": "fd", + "type": "tuple" + } ], - "name": "swapOnSameNetwork", + "name": "swapSignedAndCrossRouterETH", "outputs": [], - "stateMutability": "nonpayable", + "stateMutability": "payable", "type": "function" }, { - "inputs": [ + "inputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "name": "targetNetworks", + "outputs": [ { - "internalType": "uint256", - "name": "minAmountOut", - "type": "uint256" + "internalType": "uint32", + "name": "targetNetworkDomain", + "type": "uint32" }, - { "internalType": "address", "name": "toToken", "type": "address" }, { "internalType": "address", - "name": "targetAddress", + "name": "targetFundManager", "type": "address" - }, - { "internalType": "address", "name": "router", "type": "address" }, - { "internalType": "bytes", "name": "routerCalldata", "type": "bytes" } + } ], - "name": "swapOnSameNetworkETH", - "outputs": [], - "stateMutability": "payable", + "stateMutability": "view", "type": "function" }, { @@ -557,6 +765,13 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "inputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], + "name": "usedSalt", + "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], + "stateMutability": "view", + "type": "function" + }, { "inputs": [], "name": "weth", diff --git a/src/constants/constants.ts b/src/constants/constants.ts index 9064c9d..94d69e4 100644 --- a/src/constants/constants.ts +++ b/src/constants/constants.ts @@ -30,12 +30,12 @@ export const NETWORKS = [ }, { chainId: '42161', - fundManagerAddress: '0x80944eeaec9634F02889ca7c165d3fc1A473AeCA', - fiberRouterAddress: '0xE6A7A9dD2C0F02D572F9BEbc875B98E013918522', + fundManagerAddress: '0xC7b23d5Da44f3d421aD27863788bEdcce4b34B0d', + fiberRouterAddress: '0x68902Ec3058fdBb37Ac3D832a62112d7764a08ed', foundaryTokenAddress: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831', aggregateRouterContractAddress: '0x111111125421ca6dc452d289314280a0f8842a65', - cctpFundManager: '0x98E4F18F63Cb0B815ad7C6DdFfAfbabb54550a0e', + cctpFundManager: '0x7bD02622F39E458D5d5eaAD51C304Cac064ACFf4', }, { chainId: '10', @@ -53,12 +53,12 @@ export const NETWORKS = [ }, { chainId: '8453', - fundManagerAddress: '0xEd2cE4f32779249682B0CcA7b7eC3Fc8B19Be9B6', - fiberRouterAddress: '0x4408ea5C17dC6E610eb7327361297D094762f9B0', + fundManagerAddress: '0x7022a2dE2434AbA5985e87A84E93E5C9Cc3AB05F', + fiberRouterAddress: '0x84250F8d4617485C3D5f1d7b1bC8C624e5695A80', foundaryTokenAddress: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', aggregateRouterContractAddress: '0x111111125421ca6dc452d289314280a0f8842a65', - cctpFundManager: '0xd0e48dDf333a3c445aC2391153991851045E8162', + cctpFundManager: '0x7EA16f3a967db7133D50807e5Fb3ca5FBc4A3dD6', }, { chainId: '324', diff --git a/src/interfaces/job.interface.ts b/src/interfaces/job.interface.ts index 88659be..7bbd7df 100644 --- a/src/interfaces/job.interface.ts +++ b/src/interfaces/job.interface.ts @@ -20,6 +20,7 @@ export interface JobRequestBody { destinationChaibId: string; slippage: number; isCCTP: boolean; + distributedFee: string; } export interface SignatureData { diff --git a/src/services/signature.service.ts b/src/services/signature.service.ts index 6a98931..349dcdb 100644 --- a/src/services/signature.service.ts +++ b/src/services/signature.service.ts @@ -84,6 +84,7 @@ export const getValidWithdrawalData = async ( decodedData.targetChainId, data.destinationAmountIn, decodedData.settledAmount, + data.distributedFee, )) ) { return { @@ -104,6 +105,7 @@ export const isValidSettledAmount = async ( destinationChainId: string, destinationAmountIn: any, settledAmount: any, + distributedFee: string, ): Promise => { const sWeb3 = new Web3(rpcNodeService.getRpcNodeByChainId(sourceChainId).url); const dWeb3 = new Web3( @@ -118,9 +120,11 @@ export const isValidSettledAmount = async ( web3Service.getFoundaryTokenAddress(destinationChainId), ); settledAmount = decimalsIntoNumber(settledAmount, sDecimal); + distributedFee = decimalsIntoNumber(distributedFee, sDecimal); destinationAmountIn = decimalsIntoNumber(destinationAmountIn, dDecimal); - console.log(settledAmount, destinationAmountIn); - if (Big(settledAmount).gte(Big(destinationAmountIn))) { + let sdAmount = Big(settledAmount).add(Big(distributedFee)); + console.log(settledAmount, destinationAmountIn, sdAmount?.toString()); + if (sdAmount.gte(Big(destinationAmountIn))) { return true; } return false; diff --git a/src/services/transaction.service.ts b/src/services/transaction.service.ts index 99ca9e1..f8cf689 100644 --- a/src/services/transaction.service.ts +++ b/src/services/transaction.service.ts @@ -31,6 +31,7 @@ export async function prepareObjectsAndVerifySignatures(tx: any) { destinationChaibId: destinationNetwork.chainId, slippage: tx.slippage, isCCTP: tx?.isCCTP ? tx?.isCCTP : false, + distributedFee: tx?.distributedFee ? tx?.distributedFee : '0', }; let signatureData: SignatureData = {