import { Allocation as AppAllocation , Allocations as AppAllocations , AllocationItem as AppAllocationItem , DomainBudget as AppDomainBudget , ApproveBudgetAndFundParams as AppBudgetRequest } from '@statechannels/client-api-schema' ; import { constants } from 'ethers' ; import { Allocation , AllocationItem , SimpleAllocation , DomainBudget , AssetBudget , makeAddress } from '../../types' ; import { BN } from '../../bignumber' ; import { makeDestination } from '../../utils' ; import { zeroAddress } from '../../config' ; export function deserializeBudgetRequest ( budgetRequest , domain ) { const assetBudget = { asset : zeroAddress , availableSendCapacity : BN . from ( budgetRequest . requestedSendCapacity ) , availableReceiveCapacity : BN . from ( budgetRequest . requestedReceiveCapacity ) , channels : { } } ; return { domain , hubAddress : budgetRequest . hub . signingAddress , forAsset : { [ zeroAddress ] : assetBudget } } ; } export function deserializeDomainBudget ( DomainBudget ) { const assetBudgets = DomainBudget . budgets . map ( b => ( { asset : b . asset || constants . AddressZero , availableReceiveCapacity : BN . from ( b . availableReceiveCapacity ) , availableSendCapacity : BN . from ( b . availableSendCapacity ) , channels : b . channels . reduce ( ( record , item ) => { record [ item . channelId ] = { amount : BN . from ( item . amount ) } ; return record ; } , { } ) } ) ) ; const budgets = assetBudgets . reduce ( ( record , a ) => { record [ a . asset ] = a ; return record ; } , { } ) ; return { domain : DomainBudget . domain , hubAddress : DomainBudget . hubAddress , forAsset : budgets } ; } export function deserializeAllocations ( allocations ) { switch ( allocations . length ) { case 0 : throw new Error ( 'Allocations is empty' ) ; case 1 : return deserializeAllocation ( allocations [ 0 ] ) ; default : return { type : 'MixedAllocation' , simpleAllocations : allocations . map ( deserializeAllocation ) } ; } } function deserializeAllocation ( allocation ) { return { type : 'SimpleAllocation' , allocationItems : allocation . allocationItems . map ( deserializeAllocationItem ) , asset : makeAddress ( allocation . asset ) } ; } function deserializeAllocationItem ( allocationItem ) { return { destination : makeDestination ( allocationItem . destination ) , amount : BN . from ( allocationItem . amount ) } ; }