import { Commitment , Channel , CommitmentType } from 'fmg-core' ; import { bigNumberify } from 'ethers/utils' ; export interface PaymentCommitmentArgs { libraryAddress : string ; channelNonce : number ; asAddress : string ; bsAddress : string ; asBalance : string ; bsBalance : string ; turnNum : number ; commitmentCount : number ; } export enum PlayerIndex { A = 0 , B = 1 } function fromPaymentCommitmentArgs ( paymentArgs ) { const { turnNum , asAddress , bsAddress , channelNonce , libraryAddress , asBalance , bsBalance , commitmentCount , } = paymentArgs ; const channel = { channelType : libraryAddress , nonce : channelNonce , participants : [ asAddress , bsAddress ] , } ; return { channel , allocation : [ asBalance , bsBalance ] , destination : [ asAddress , bsAddress ] , commitmentCount , commitmentType : CommitmentType . App , appAttributes : '0x0' , turnNum , } ; } export function preFundSetupCommitment ( paymentArgs ) { return { ... fromPaymentCommitmentArgs ( paymentArgs ) , commitmentType : CommitmentType . PreFundSetup } ; } export function postFundSetupCommitment ( paymentArgs ) { return { ... fromPaymentCommitmentArgs ( paymentArgs ) , commitmentType : CommitmentType . PostFundSetup , } ; } export function appCommitment ( paymentArgs ) { return { ... fromPaymentCommitmentArgs ( paymentArgs ) , commitmentType : CommitmentType . App } ; } export function concludeCommitment ( paymentArgs ) { return { ... fromPaymentCommitmentArgs ( paymentArgs ) , commitmentType : CommitmentType . Conclude } ; } export function initialPreFundSetup ( libraryAddress , channelNonce , asAddress , bsAddress , asBalance , bsBalance ) { return preFundSetupCommitment ( { libraryAddress , channelNonce , asAddress , bsAddress , asBalance , bsBalance , turnNum : 0 , commitmentCount : 0 , } ) ; } export function nextPreFundSetup ( commitment ) { return { ... commitment , commitmentCount : commitment . commitmentCount + 1 , turnNum : commitment . turnNum + 1 , } ; } export function postFundSetup ( commitment ) { const commitmentCount = commitment . commitmentType === CommitmentType . PreFundSetup ? 0 : commitment . commitmentCount + 1 ; return { ... commitment , commitmentType : CommitmentType . PostFundSetup , turnNum : commitment . turnNum + 1 , commitmentCount , } ; } export function pay ( commitment , amount , ourIndex ) { const ourTurnOnExistingCommitment = commitment . turnNum % 2 === ourIndex ; const turnNum = ourTurnOnExistingCommitment ? commitment . turnNum : commitment . turnNum + 1 ; const theirIndex = 1 - ourIndex ; const ourAllocation = commitment . allocation [ ourIndex ] ; const theirAllocation = commitment . allocation [ theirIndex ] ; const ourUpdatedAllocation = bigNumberify ( ourAllocation ) . sub ( amount ) . toHexString ( ) ; const theirUpdatedAllocation = bigNumberify ( theirAllocation ) . add ( amount ) . toHexString ( ) ; const updatedAllocation = [ ... commitment . allocation ] ; updatedAllocation [ ourIndex ] = ourUpdatedAllocation ; updatedAllocation [ theirIndex ] = theirUpdatedAllocation ; return { ... commitment , commitmentType : CommitmentType . App , turnNum , allocation : updatedAllocation , commitmentCount : 0 , } ; } export function pass ( commitment ) { return { ... commitment , commitmentType : CommitmentType . App , turnNum : commitment . turnNum + 1 , commitmentCount : 0 , } ; } export function conclude ( commitment ) { const commitmentCount = commitment . commitmentType !== CommitmentType . Conclude ? 0 : commitment . commitmentCount + 1 ; return { ... commitment , commitmentType : CommitmentType . Conclude , turnNum : commitment . turnNum + 1 , commitmentCount , } ; }