diff --git a/app/controllers/api/v1/gasFees.ts b/app/controllers/api/v1/gasFees.ts index 166df04..a778e8e 100644 --- a/app/controllers/api/v1/gasFees.ts +++ b/app/controllers/api/v1/gasFees.ts @@ -1,4 +1,5 @@ let asyncMiddleware = require("../../../lib/response/asyncMiddleware"); +import { convertIntoFeeDistributionObject } from "../../../lib/middlewares/helpers/feeDistribution/feeDistributionHelper"; import { gasEstimationValidation, destinationGasEstimation, @@ -7,7 +8,7 @@ import { import { isSameNetworksSwap } from "../../../lib/middlewares/helpers/multiSwapHelper"; module.exports = function (router: any) { - router.get( + router.post( "/estimation", asyncMiddleware(async (req: any, res: any) => { let destinationGasPrices; @@ -16,6 +17,13 @@ module.exports = function (router: any) { req?.query?.destinationNetworkChainId ); gasEstimationValidation(req); + req.body.feeDistribution = convertIntoFeeDistributionObject( + req.body.feeDistribution, + req.query.sourceAmountIn, + req.query.sourceAmountOut, + req.body.originalDestinationAmountIn, + req.body.originalDestinationAmountOut + ); if (!isSameNetworks) { destinationGasPrices = await destinationGasEstimation(req); } diff --git a/app/controllers/api/v1/multiswap.ts b/app/controllers/api/v1/multiswap.ts index 7320974..b41fc4a 100644 --- a/app/controllers/api/v1/multiswap.ts +++ b/app/controllers/api/v1/multiswap.ts @@ -1,4 +1,5 @@ let asyncMiddleware = require("../../../lib/response/asyncMiddleware"); +import { convertIntoFeeDistributionObject } from "../../../lib/middlewares/helpers/feeDistribution/feeDistributionHelper"; import { getQuoteAndTokenTypeInformation, getSwapSigned, @@ -26,10 +27,17 @@ module.exports = function (router: any) { }) ); - router.get( + router.post( "/swap/signed", asyncMiddleware(async (req: any, res: any) => { swapSignedValidation(req); + req.body.feeDistribution = convertIntoFeeDistributionObject( + req.body.feeDistribution, + req.query.sourceAmountIn, + req.query.sourceAmountOut, + req.query.destinationAmountIn, + req.query.destinationAmountOut + ); if ( req.query.sourceNetworkChainId == req.query.destinationNetworkChainId ) { diff --git a/app/interfaces/feeDistributionInterface.ts b/app/interfaces/feeDistributionInterface.ts index f22dcc0..16134ed 100644 --- a/app/interfaces/feeDistributionInterface.ts +++ b/app/interfaces/feeDistributionInterface.ts @@ -1,5 +1,10 @@ export interface FeeDistribution { feeAllocations: any[]; + totalPlatformFee: string; + sourceAmountIn: string; + sourceAmountOut: string; + destinationAmountIn: string; + destinationAmountOut: string; salt: string; expiry: number; signature: string; diff --git a/app/lib/httpCalls/multiSwapAxiosHelper.ts b/app/lib/httpCalls/multiSwapAxiosHelper.ts index 5f52436..6b0fa98 100755 --- a/app/lib/httpCalls/multiSwapAxiosHelper.ts +++ b/app/lib/httpCalls/multiSwapAxiosHelper.ts @@ -74,7 +74,7 @@ export const getFeeDistributionDataByReferralCode = async (code: string) => { if ((global as any as any).utils.IS_LOCAL_ENV) { baseUrl = localHostUrl; } - let url = `${baseUrl}/community-member/multiSwap/referrals/fee-distribution?code=${code}`; + let url = `${baseUrl}/referrals/fee-distribution?code=${code}`; let res = await axios.get(url, config); console.log("res?.data?.body?.feeDistribution", res?.data?.body); return res?.data?.body?.feeDistribution; diff --git a/app/lib/middlewares/helpers/feeDistribution/feeDistributionHelper.ts b/app/lib/middlewares/helpers/feeDistribution/feeDistributionHelper.ts index 46ccd61..028fd11 100644 --- a/app/lib/middlewares/helpers/feeDistribution/feeDistributionHelper.ts +++ b/app/lib/middlewares/helpers/feeDistribution/feeDistributionHelper.ts @@ -1,3 +1,4 @@ +var { Big } = require("big.js"); import { FeeDistribution } from "../../../../interfaces/feeDistributionInterface"; import { getFeeDistributionSignature, @@ -5,41 +6,108 @@ import { } from "../feeDistribution/feeDistributionSignatureHelper"; import { getFeeDistributionDataByReferralCode } from "../../../httpCalls/multiSwapAxiosHelper"; -export async function getFeeDistributionData( - referralCode: string, - network: any +export async function getFeeDistributionObject( + feeDistribution: FeeDistribution, + network: any, + sourceAmountIn: string, + sourceAmountOut: string, + destinationAmountIn: string, + destinationAmountOut: string ): Promise { - let data: FeeDistribution = { - feeAllocations: [], - salt: "", - expiry: 0, - signature: "", - }; try { - const feeDistribution = await getFeeDistributionDataByReferralCode( - referralCode - ); - const feeAllocations = feeDistribution ? [feeDistribution] : []; - const { salt, expiry } = await getSaltAndExpiryData(); + const { salt, expiry } = getSaltAndExpiryData(); console.log({ salt, expiry }); const signature = await getFeeDistributionSignature( network.foundryTokenAddress, network, network.fiberRouter, - feeAllocations, + feeDistribution, salt, - expiry + expiry, + sourceAmountIn, + sourceAmountOut, + destinationAmountIn, + destinationAmountOut ); - console.log("feeDistribution signature", signature); + feeDistribution.salt = salt; + feeDistribution.expiry = expiry; + feeDistribution.signature = signature; + } catch (e) { + console.error(e); + } + console.log("getFeeDistributionObject", feeDistribution); + return feeDistribution; +} + +export async function getDataAfterCutDistributionFee( + referralCode: string, + decimalAmount: any +): Promise { + let amountAfterCut = decimalAmount; + let totalPlatformFee = "0"; + let feeAllocations: any = []; + let data: FeeDistribution = { + feeAllocations: [], + totalPlatformFee: "", + sourceAmountIn: "", + sourceAmountOut: "", + destinationAmountIn: "", + destinationAmountOut: "", + salt: "", + expiry: 0, + signature: "", + }; + try { + const response = await getFeeDistributionDataByReferralCode(referralCode); + if (response?.rateInBps && response?.recipient) { + const rateInBps = response?.rateInBps; + const rate = Number(rateInBps) / 100; + console.log("rate", rate); + amountAfterCut = await ( + global as any + ).commonFunctions.addSlippageInDecimal(decimalAmount, rate); + totalPlatformFee = Big(decimalAmount) + .minus(Big(amountAfterCut)) + .toString(); + feeAllocations.push({ + recipient: response?.recipient, + platformFee: totalPlatformFee?.toString(), + }); + } data = { feeAllocations: feeAllocations, - salt: salt, - expiry: expiry, - signature: signature, + totalPlatformFee: totalPlatformFee, + sourceAmountIn: "", + sourceAmountOut: "", + destinationAmountIn: "", + destinationAmountOut: "", + salt: "", + expiry: 0, + signature: "", }; } catch (e) { console.error(e); } - console.log("getFeeDistributionData", data); - return data; + return { + amountAfterCut: amountAfterCut, + data: data, + }; +} + +export function convertIntoFeeDistributionObject( + feeDistribution: FeeDistribution, + sourceAmountIn: string, + sourceAmountOut: string, + destinationAmountIn: string, + destinationAmountOut: string +): FeeDistribution { + try { + feeDistribution.sourceAmountIn = sourceAmountIn; + feeDistribution.sourceAmountOut = sourceAmountOut; + feeDistribution.destinationAmountIn = destinationAmountIn; + feeDistribution.destinationAmountOut = destinationAmountOut; + } catch (e) { + console.error(e); + } + return feeDistribution; } diff --git a/app/lib/middlewares/helpers/feeDistribution/feeDistributionSignatureHelper.ts b/app/lib/middlewares/helpers/feeDistribution/feeDistributionSignatureHelper.ts index 3b728f6..0682e5c 100644 --- a/app/lib/middlewares/helpers/feeDistribution/feeDistributionSignatureHelper.ts +++ b/app/lib/middlewares/helpers/feeDistribution/feeDistributionSignatureHelper.ts @@ -3,10 +3,11 @@ import Web3 from "web3"; import crypto from "crypto"; import { ethers } from "ethers"; import { _TypedDataEncoder } from "ethers/lib/utils"; +import { FeeDistribution } from "../../../../interfaces/feeDistributionInterface"; const CONTRACT_NAME = "FEE_DISTRIBUTOR"; const CONTRACT_VERSION = "000.001"; -export const getSaltAndExpiryData = async () => { +export const getSaltAndExpiryData = () => { const salt = Web3.utils.keccak256(crypto.randomBytes(512).toString("hex")); const expiry = getExpiry(); return { salt, expiry }; @@ -16,10 +17,27 @@ export const getFeeDistributionSignature = async ( token: string, network: any, fiberRouterContractAddress: any, - feeAllocations: any[], + feeDistribution: FeeDistribution, salt: any, - expiry: any + expiry: any, + sourceAmountIn: string, + sourceAmountOut: string, + destinationAmountIn: string, + destinationAmountOut: string ) => { + console.log("feeAllocations", feeDistribution?.feeAllocations); + console.log("totalPlatformFee", feeDistribution?.totalPlatformFee); + console.log( + "sourceAmountIn:", + sourceAmountIn, + "sourceAmountOut:", + sourceAmountOut, + "destinationAmountIn:", + destinationAmountIn, + "destinationAmountOut:", + destinationAmountOut + ); + const domain = { name: CONTRACT_NAME, version: CONTRACT_VERSION, @@ -29,25 +47,34 @@ export const getFeeDistributionSignature = async ( const types = { FeeAllocation: [ { name: "recipient", type: "address" }, - { name: "rateInBps", type: "uint16" }, + { name: "platformFee", type: "uint256" }, ], DistributeFees: [ { name: "token", type: "address" }, { name: "feeAllocations", type: "FeeAllocation[]" }, + { name: "totalPlatformFee", type: "uint256" }, + { name: "sourceAmountIn", type: "uint256" }, + { name: "sourceAmountOut", type: "uint256" }, + { name: "destinationAmountIn", type: "uint256" }, + { name: "destinationAmountOut", type: "uint256" }, { name: "salt", type: "bytes32" }, { name: "expiry", type: "uint256" }, ], }; const values = { token: token, - feeAllocations: feeAllocations, + feeAllocations: feeDistribution?.feeAllocations, + totalPlatformFee: feeDistribution?.totalPlatformFee, + sourceAmountIn: sourceAmountIn, + sourceAmountOut: sourceAmountOut, + destinationAmountIn: destinationAmountIn, + destinationAmountOut: destinationAmountOut, salt: salt, expiry: expiry, }; var wallet = new ethers.Wallet((global as any).environment.PRI_KEY); const signer = wallet.connect(network?.provider); const signature = await signer._signTypedData(domain, types, values); - console.log("getFeeDistributionSignature", signature); return signature; }; diff --git a/app/lib/middlewares/helpers/multiSwapHelper.ts b/app/lib/middlewares/helpers/multiSwapHelper.ts index 39727f9..ca235f0 100644 --- a/app/lib/middlewares/helpers/multiSwapHelper.ts +++ b/app/lib/middlewares/helpers/multiSwapHelper.ts @@ -22,6 +22,8 @@ export const getQuoteAndTokenTypeInformation = async function (req: any) { let gasEstimationDestinationAmount = req.query.gasEstimationDestinationAmount; let sourceSlippage = req.query.sourceSlippage; let destinationSlippage = req.query.destinationSlippage; + let referralCode = req.query.referralCode; + if ( isSameNetworksSwap( req.query.sourceNetworkChainId, @@ -55,7 +57,8 @@ export const getQuoteAndTokenTypeInformation = async function (req: any) { destinationWalletAddress, gasEstimationDestinationAmount, sourceSlippage, - destinationSlippage + destinationSlippage, + referralCode ); } let data: any = await getResponseForQuoteAndTokenTypeInformation( @@ -75,7 +78,8 @@ export const getSwapSigned = async function (req: any) { req.query.destinationNetworkChainId, req.query.sourceAmount, req.query.destinationWalletAddress, - req.query + req.query, + req.body ); return data; }; @@ -198,12 +202,16 @@ export const swapSignedValidation = function (req: any) { ) { throw "sourceWalletAddress & sourceTokenContractAddress & sourceNetworkChainId & sourceAmount & destinationTokenContractAddress & destinationNetworkChainId & sourceAssetType & destinationAssetType are missing"; } - if ( - req.query.sourceNetworkChainId != req.query.destinationNetworkChainId && - !req.query.gasPrice - ) { + const isSameNetworkSwap = isSameNetworksSwap( + req.query.sourceNetworkChainId, + req.query.destinationNetworkChainId + ); + if (!isSameNetworkSwap && !req.query.gasPrice) { throw "gasPrice is missing"; } + if (!isSameNetworkSwap && !req.body.feeDistribution) { + throw "feeDistribution is missing"; + } }; export const withdrawSignedValidation = function (req: any) { @@ -246,21 +254,20 @@ const getResponseForQuoteAndTokenTypeInformation = async function ( : categorizedInfo?.destination?.amount; let sourceCallData = ""; let destinationCallData = ""; - let sourceBridgeAmount = ""; if (categorizedInfo?.source?.callData) { sourceCallData = categorizedInfo?.source?.callData; } if (categorizedInfo?.destination?.callData) { destinationCallData = categorizedInfo?.destination?.callData; } - if (categorizedInfo?.source?.bridgeAmount) { - sourceBridgeAmount = categorizedInfo?.source?.bridgeAmount; - } let sourceTokenCategorizedInfo: any = {}; sourceTokenCategorizedInfo.type = categorizedInfo.source.type; sourceTokenCategorizedInfo.sourceAmount = req.query.sourceAmount; - sourceTokenCategorizedInfo.sourceBridgeAmount = sourceBridgeAmount; + sourceTokenCategorizedInfo.sourceAmountIn = + categorizedInfo?.source?.sourceAmountIn; + sourceTokenCategorizedInfo.sourceAmountOut = + categorizedInfo?.source?.sourceAmountOut; sourceTokenCategorizedInfo.sourceOneInchData = sourceCallData; let destinationTokenCategorizedInfo: any = {}; @@ -268,11 +275,9 @@ const getResponseForQuoteAndTokenTypeInformation = async function ( destinationTokenCategorizedInfo.destinationAmount = destinationAmount; destinationTokenCategorizedInfo.minDestinationAmount = minDestinationAmount; destinationTokenCategorizedInfo.destinationAmountIn = - categorizedInfo?.destination?.bridgeAmountIn; - destinationTokenCategorizedInfo.destinationAmountOut = categorizedInfo - ?.destination?.bridgeAmountOut - ? categorizedInfo?.destination?.bridgeAmountOut - : ""; + categorizedInfo?.destination?.destinationAmountIn; + destinationTokenCategorizedInfo.destinationAmountOut = + categorizedInfo?.destination?.destinationAmountOut; destinationTokenCategorizedInfo.destinationOneInchData = destinationCallData; data.sourceSlippage = await getSlippage(req.query.sourceSlippage); @@ -281,6 +286,7 @@ const getResponseForQuoteAndTokenTypeInformation = async function ( data.sourceTokenCategorizedInfo = sourceTokenCategorizedInfo; data.destinationTokenCategorizedInfo = destinationTokenCategorizedInfo; data.isCCTP = categorizedInfo?.isCCTP ? categorizedInfo?.isCCTP : false; + data.feeDistribution = categorizedInfo?.feeDistribution; } console.log("getTokenCategorizedInformation response", data); return data; diff --git a/app/lib/middlewares/helpers/tokenQuoteAndTypeHelpers/quoteAndTypeHelper.ts b/app/lib/middlewares/helpers/tokenQuoteAndTypeHelpers/quoteAndTypeHelper.ts index bb6f760..1377abc 100644 --- a/app/lib/middlewares/helpers/tokenQuoteAndTypeHelpers/quoteAndTypeHelper.ts +++ b/app/lib/middlewares/helpers/tokenQuoteAndTypeHelpers/quoteAndTypeHelper.ts @@ -240,15 +240,15 @@ const convertResponseForSameNetworksIntoDesire = ( response.source.type = sData.sourceAssetType; response.source.amount = sData.inputAmount; if (sData.amountOutIntoDecimals) { - response.source.bridgeAmount = sData.amountOutIntoDecimals; + response.source.sourceAmountOut = sData.amountOutIntoDecimals; } response.source.callData = sData?.oneInchData; response.destination.type = dData.destinationAssetType; response.destination.amount = dData.amountOutIntoNumber; response.destination.minAmount = dData.minAmountOutIntoNumber; - response.destination.bridgeAmountIn = dData.amountInIntoDecimals; - response.destination.bridgeAmountOut = dData.amountOutIntoDecimals; + response.destination.destinationAmountIn = dData.amountInIntoDecimals; + response.destination.destinationAmountOut = dData.amountOutIntoDecimals; response.destination.callData = dData?.callData; return response; diff --git a/artifacts/contracts/upgradeable-Bridge/FiberRouter.sol/FiberRouter.json b/artifacts/contracts/upgradeable-Bridge/FiberRouter.sol/FiberRouter.json index 22f3998..54fe24c 100644 --- a/artifacts/contracts/upgradeable-Bridge/FiberRouter.sol/FiberRouter.json +++ b/artifacts/contracts/upgradeable-Bridge/FiberRouter.sol/FiberRouter.json @@ -3,6 +3,37 @@ "contractName": "FiberRouter", "sourceName": "contracts/upgradeable-Bridge/FiberRouterExtended.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": [ @@ -274,34 +305,6 @@ "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" }, @@ -531,15 +534,40 @@ "type": "address" }, { - "internalType": "uint16", - "name": "rateInBps", - "type": "uint16" + "internalType": "uint256", + "name": "platformFee", + "type": "uint256" } ], "internalType": "struct FeeDistributor.FeeAllocation[]", "name": "feeAllocations", "type": "tuple[]" }, + { + "internalType": "uint256", + "name": "totalPlatformFee", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "sourceAmountIn", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "sourceAmountOut", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "destinationAmountIn", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "destinationAmountOut", + "type": "uint256" + }, { "internalType": "bytes32", "name": "salt", "type": "bytes32" }, { "internalType": "uint256", "name": "expiry", "type": "uint256" }, { "internalType": "bytes", "name": "signature", "type": "bytes" } @@ -608,15 +636,40 @@ "type": "address" }, { - "internalType": "uint16", - "name": "rateInBps", - "type": "uint16" + "internalType": "uint256", + "name": "platformFee", + "type": "uint256" } ], "internalType": "struct FeeDistributor.FeeAllocation[]", "name": "feeAllocations", "type": "tuple[]" }, + { + "internalType": "uint256", + "name": "totalPlatformFee", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "sourceAmountIn", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "sourceAmountOut", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "destinationAmountIn", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "destinationAmountOut", + "type": "uint256" + }, { "internalType": "bytes32", "name": "salt", "type": "bytes32" }, { "internalType": "uint256", "name": "expiry", "type": "uint256" }, { "internalType": "bytes", "name": "signature", "type": "bytes" } @@ -684,15 +737,40 @@ "type": "address" }, { - "internalType": "uint16", - "name": "rateInBps", - "type": "uint16" + "internalType": "uint256", + "name": "platformFee", + "type": "uint256" } ], "internalType": "struct FeeDistributor.FeeAllocation[]", "name": "feeAllocations", "type": "tuple[]" }, + { + "internalType": "uint256", + "name": "totalPlatformFee", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "sourceAmountIn", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "sourceAmountOut", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "destinationAmountIn", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "destinationAmountOut", + "type": "uint256" + }, { "internalType": "bytes32", "name": "salt", "type": "bytes32" }, { "internalType": "uint256", "name": "expiry", "type": "uint256" }, { "internalType": "bytes", "name": "signature", "type": "bytes" } diff --git a/scripts/fiberNode.ts b/scripts/fiberNode.ts index b800039..8947e42 100644 --- a/scripts/fiberNode.ts +++ b/scripts/fiberNode.ts @@ -20,6 +20,11 @@ import { import { isValidOneInchSelector } from "../app/lib/middlewares/helpers/configurationHelper"; import { query } from "express"; import { chooseProviderAndGetData } from "../app/lib/middlewares/helpers/tokenQuoteAndTypeHelpers/quoteProvidersHelper"; +import { + getDataAfterCutDistributionFee, + getFeeDistributionObject, +} from "../app/lib/middlewares/helpers/feeDistribution/feeDistributionHelper"; +import { FeeDistribution } from "../app/interfaces/feeDistributionInterface"; module.exports = { getQouteAndTypeForCrossNetworks: async function ( @@ -31,7 +36,8 @@ module.exports = { destinationWalletAddress: string, gasEstimationDestinationAmount: string, sourceSlippage: string, - destinationSlippage: string + destinationSlippage: string, + referralCode: string ) { const sourceNetwork = (global as any).commonFunctions.getNetworkByChainId( sourceChainId @@ -46,9 +52,11 @@ module.exports = { let destinationCallData; let destinationAmountOut; let minDestinationAmountOut; + let machineSourceAmountIn: any; let machineSourceAmountOut: any; let machineDestinationAmountIn: any; let machineDestinationAmountOut: any; + let feeDistribution: any; let targetFoundryTokenAddress; let isCCTP = false; @@ -70,17 +78,16 @@ module.exports = { const sourceTokenDecimal = await sourceTokenContract.decimals(); const sourceFoundryTokenDecimal = await sourceFoundryTokenContract.decimals(); - let amount = (global as any).commonFunctions.numberIntoDecimals( - inputAmount, - sourceTokenDecimal - ); + machineSourceAmountIn = ( + global as any + ).commonFunctions.numberIntoDecimals(inputAmount, sourceTokenDecimal); let sourceTypeResponse = await getSourceAssetTypes( sourceNetwork, await (global as any).commonFunctions.getWrappedNativeTokenAddress( sourceTokenAddress, sourceChainId ), - amount + machineSourceAmountIn ); const isFoundryAsset = sourceTypeResponse.isFoundryAsset; if (isFoundryAsset) { @@ -90,7 +97,17 @@ module.exports = { } if (isFoundryAsset) { - sourceBridgeAmount = inputAmount; + machineSourceAmountOut = machineSourceAmountIn; + const { amountAfterCut, data } = await getDataAfterCutDistributionFee( + referralCode, + machineSourceAmountOut + ); + machineSourceAmountOut = amountAfterCut; + feeDistribution = data; + sourceBridgeAmount = (global as any).commonFunctions.decimalsIntoNumber( + machineSourceAmountOut, + sourceFoundryTokenDecimal + ); } else { let response: any = await chooseProviderAndGetData( sourceChainId, @@ -99,7 +116,7 @@ module.exports = { sourceChainId ), sourceNetwork?.foundryTokenAddress, - amount, + machineSourceAmountIn, sourceSlippage, sourceNetwork?.fiberRouter, sourceNetwork?.fiberRouter @@ -112,6 +129,12 @@ module.exports = { machineSourceAmountOut, sourceSlippage ); + const { amountAfterCut, data } = await getDataAfterCutDistributionFee( + referralCode, + machineSourceAmountOut + ); + machineSourceAmountOut = amountAfterCut; + feeDistribution = data; sourceBridgeAmount = (global as any).commonFunctions.decimalsIntoNumber( machineSourceAmountOut, sourceFoundryTokenDecimal @@ -242,17 +265,31 @@ module.exports = { let data: any = { source: {}, destination: {}, isCCTP: isCCTP }; data.source.type = sourceAssetType; data.source.amount = inputAmount; + data.source.sourceAmountIn = machineSourceAmountIn; if (machineSourceAmountOut) { - data.source.bridgeAmount = machineSourceAmountOut; + data.source.sourceAmountOut = machineSourceAmountOut; } data.source.callData = sourceCallData; + machineDestinationAmountOut = machineDestinationAmountOut + ? machineDestinationAmountOut + : machineDestinationAmountIn; data.destination.type = targetAssetType; data.destination.amount = destinationAmountOut; data.destination.minAmount = minDestinationAmountOut; - data.destination.bridgeAmountIn = machineDestinationAmountIn; - data.destination.bridgeAmountOut = machineDestinationAmountOut; + data.destination.destinationAmountIn = machineDestinationAmountIn; + data.destination.destinationAmountOut = machineDestinationAmountOut; data.destination.callData = destinationCallData; + if (!gasEstimationDestinationAmount) { + data.feeDistribution = await getFeeDistributionObject( + feeDistribution, + sourceNetwork, + machineSourceAmountIn, + machineSourceAmountOut, + machineDestinationAmountIn, + machineDestinationAmountOut + ); + } return data; }, };