merge-withdrawable-balances.ts
1 import { gql } from 'graphql-request'; 2 import type { MergeWithdrawableBalancesFragment } from './__generated__/gql.generated'; 3 import mergeAmounts from './amounts/merge-amounts'; 4 5 export const MERGE_WITHDRAWABLE_BALANCES_FRAGMENT = gql` 6 fragment MergeWithdrawableBalances on WithdrawableBalance { 7 tokenAddress 8 collectableAmount 9 receivableAmount 10 splittableAmount 11 } 12 `; 13 14 interface Amount { 15 tokenAddress: string; 16 amount: bigint; 17 } 18 19 export function mergeCollectableFunds(withdrawableBalances: MergeWithdrawableBalancesFragment[]) { 20 const amounts: Amount[] = withdrawableBalances.map(({ tokenAddress, collectableAmount }) => ({ 21 tokenAddress, 22 amount: BigInt(collectableAmount), 23 })); 24 25 return mergeAmounts(amounts); 26 } 27 28 export function mergeSplittableFunds(withdrawableBalances: MergeWithdrawableBalancesFragment[]) { 29 const amounts: Amount[] = withdrawableBalances.map(({ tokenAddress, splittableAmount }) => ({ 30 tokenAddress, 31 amount: BigInt(splittableAmount), 32 })); 33 34 return mergeAmounts(amounts); 35 } 36 37 export function mergeReceivableFunds(withdrawableBalances: MergeWithdrawableBalancesFragment[]) { 38 const amounts: Amount[] = withdrawableBalances.map(({ tokenAddress, receivableAmount }) => ({ 39 tokenAddress, 40 amount: BigInt(receivableAmount), 41 })); 42 43 return mergeAmounts(amounts); 44 } 45 46 export default function mergeWithdrawableBalances( 47 withdrawableBalances: MergeWithdrawableBalancesFragment[], 48 excludeReceivable = false, 49 ) { 50 return mergeAmounts( 51 mergeCollectableFunds(withdrawableBalances), 52 mergeSplittableFunds(withdrawableBalances), 53 excludeReceivable ? [] : mergeReceivableFunds(withdrawableBalances), 54 ); 55 }