import { DomainBudget , unreachable , Uint256 } from '@statechannels/wallet-core' ; import { Interpreter } from 'xstate' ; import { zeroAddress } from '../config' ; import { WorkflowState as AppWorkflowState , StateValue as AppStateValue } from '../workflows/application' ; export function getApplicationStateValue ( applicationWorkflowState ) { if ( typeof applicationWorkflowState . value === 'string' ) { return applicationWorkflowState . value as AppStateValue ; } else { return Object . keys ( applicationWorkflowState . value ) [ 0 ] as AppStateValue ; } } export function getConfirmCreateChannelService ( applicationWorkflowState ) { return applicationWorkflowState . children . invokeCreateChannelConfirmation as any ; } export function getChallengeChannelService ( applicationWorkflowState ) { return applicationWorkflowState . children . invokeChallengingProtocol as any ; } export function isConfirmCreateChannel ( applicationWorkflowState ) { return applicationWorkflowState . value === 'confirmingWithUser' ; } export function isApplicationChallenging ( applicationWorkflowState ) { const stateValue = getApplicationStateValue ( applicationWorkflowState ) ; return stateValue === 'sendChallenge' ; } export function isApplicationOpening ( applicationWorkflowState ) { const stateValue = getApplicationStateValue ( applicationWorkflowState ) ; return ( stateValue === 'joiningChannel' || stateValue === 'creatingChannel' || stateValue === 'openChannelAndFundProtocol' ) ; } export function getApplicationOpenProgress ( applicationWorkflowState ) { const stateValue = getApplicationStateValue ( applicationWorkflowState ) ; switch ( stateValue ) { case 'confirmingWithUser' : return 0.25 ; case 'joiningChannel' : case 'creatingChannel' : case 'fundingChannel' : return 0.5 ; case 'openChannelAndFundProtocol' : return 0.75 ; case 'running' : return 1 ; case 'closing' : case 'sendChallenge' : case 'done' : case 'failure' : case 'branchingOnFundingStrategy' : throw Error ( 'Should not be in this state' ) ; default : return unreachable ( stateValue ) ; } } export function getAmountsFromBudget ( budget ) { const pending = budget . forAsset [ zeroAddress ] ; if ( ! pending ) throw new Error ( 'No eth budget found' ) ; const { availableReceiveCapacity , availableSendCapacity } = pending ; return { playerAmount : availableSendCapacity , hubAmount : availableReceiveCapacity } ; }