UNPKG

@shogun-sdk/money-legos

Version:

Shogun Money Legos: clients and types for quotes, memes, prices, balances, fees, validations, etc.

58 lines (53 loc) 1.69 kB
import { getFeeAmount } from './fee.js'; import { formatAffiliateData } from './fee.js'; import { FormattedAffiliateFee } from '../types/affiliateFees.js'; import { getTransferTokenFeeCall } from './fee.js'; import { CallStruct } from '../types/multicall.js'; import { isNativeToken } from './address.js'; export function calculateAffiliateFeeAndUpdateAmountIn(config: { srcChain: number; amount: string; srcToken: string; affiliateFee: string; affiliateWallet: string; }): { formattedAffiliateFeeData: FormattedAffiliateFee; updatedAmountIn: bigint; } { const affiliateFeeAmount = getFeeAmount(Number(config.affiliateFee), BigInt(config.amount)); const updatedAmountIn = BigInt(config.amount) - affiliateFeeAmount; const formattedAffiliateFeeData = formatAffiliateData( { feeAmount: affiliateFeeAmount, feeToken: config.srcToken as `0x${string}`, }, config, ); return { formattedAffiliateFeeData, updatedAmountIn, }; } /** * Adds first call that collects affiliate fee as native token * @param bridgeCalls Bridge calls arrays that should be modified (mutated) * @param config Trade config * @param formattedAffiliateFeeData Formatted affiliate fee data */ export function collectAffiliateFeeBeforeSwap( bridgeCalls: CallStruct[], config: { srcToken: string; affiliateWallet: string; }, formattedAffiliateFeeData: FormattedAffiliateFee, ): void { bridgeCalls.unshift( getTransferTokenFeeCall( BigInt(formattedAffiliateFeeData.affiliateFee.feeAmount), config.affiliateWallet as `0x${string}`, config.srcToken as `0x${string}`, isNativeToken(config.srcToken), ), ); }