import AppWithAction from "@counterfactual/cf-adjudicator-contracts/expected-build-artifacts/AppWithAction.json" ; import { OutcomeType } from "@counterfactual/types" ; import { Contract , ContractFactory , Wallet } from "ethers" ; import { Provider } from "ethers/providers" ; import { bigNumberify } from "ethers/utils" ; import { CONVENTION_FOR_ETH_TOKEN_ADDRESS } from "../../../../src/constants" ; import { Protocol } from "../../../../src/engine" ; import { StateChannel } from "../../../../src/models" ; import { toBeEq } from "../bignumber-jest-matcher" ; import { connectToGanache } from "../connect-ganache" ; import { TestRunner } from "./test-runner" ; let wallet ; let appWithAction ; expect . extend ( { toBeEq } ) ; enum ActionType { SUBMIT_COUNTER_INCREMENT , ACCEPT_INCREMENT } beforeAll ( async ( ) => { [ { } , wallet , { } ] = await connectToGanache ( ) ; appWithAction = await new ContractFactory ( AppWithAction . abi , AppWithAction . evm . bytecode , wallet ) . deploy ( ) ; } ) ; describe ( "Three mininodes" , ( ) => { it ( "Can run all the protocols" , async ( ) => { const tr = new TestRunner ( ) ; await tr . connectToGanache ( ) ; await tr . setup ( ) ; await tr . mininodeA . protocolRunner . initiateProtocol ( Protocol . InstallVirtualApp , { initiatorXpub : tr . mininodeA . xpub , intermediaryXpub : tr . mininodeB . xpub , responderXpub : tr . mininodeC . xpub , defaultTimeout : 100 , appInterface : { addr : appWithAction . address , stateEncoding : "tuple(uint256 counter)" , actionEncoding : "tuple(uint8 actionType, uint256 increment)" } , initialState : { counter : 0 } , appSeqNo : 0 , initiatorBalanceDecrement : bigNumberify ( 0 ) , responderBalanceDecrement : bigNumberify ( 0 ) , tokenAddress : CONVENTION_FOR_ETH_TOKEN_ADDRESS , outcomeType : OutcomeType . TWO_PARTY_FIXED_OUTCOME } ) ; const [ virtualAppInstance ] = [ ... StateChannel . fromJson ( tr . mininodeA . store . sharedData . stateChannelsMap [ tr . multisigAC ] ) . appInstances . values ( ) ] ; expect ( virtualAppInstance . isVirtualApp ) ; await tr . mininodeA . protocolRunner . initiateProtocol ( Protocol . Update , { initiatorXpub : tr . mininodeA . xpub , responderXpub : tr . mininodeC . xpub , multisigAddress : tr . multisigAC , appIdentityHash : virtualAppInstance . identityHash , newState : { counter : 1 } } ) ; await tr . mininodeA . protocolRunner . initiateProtocol ( Protocol . TakeAction , { initiatorXpub : tr . mininodeA . xpub , responderXpub : tr . mininodeC . xpub , multisigAddress : tr . multisigAC , appIdentityHash : virtualAppInstance . identityHash , action : { actionType : ActionType . SUBMIT_COUNTER_INCREMENT , increment : 1 } } ) ; await tr . mininodeA . protocolRunner . initiateProtocol ( Protocol . UninstallVirtualApp , { initiatorXpub : tr . mininodeA . xpub , intermediaryXpub : tr . mininodeB . xpub , responderXpub : tr . mininodeC . xpub , targetAppIdentityHash : virtualAppInstance . identityHash , targetOutcome : await virtualAppInstance . computeOutcome ( { counter : 2 } , appWithAction . provider as Provider ) } ) ; } ) ; } ) ;