import { constants } from 'ethers' ; import { Allocation , AllocationType } from '@statechannels/exit-format' ; import { getTestProvider , setupContract } from '../../test-helpers' ; import { TESTNitroAdjudicator } from '../../../typechain/TESTNitroAdjudicator' ; import TESTNitroAdjudicatorArtifact from '../../../artifacts/contracts/test/TESTNitroAdjudicator.sol/TESTNitroAdjudicator.json' ; const testNitroAdjudicator = ( setupContract ( getTestProvider ( ) , TESTNitroAdjudicatorArtifact , process . env . TEST_NITRO_ADJUDICATOR_ADDRESS ) as unknown ) as TESTNitroAdjudicator ; import { computeClaimEffectsAndInteractions } from '../../../src/contract/multi-asset-holder' ; import { encodeGuaranteeData } from '../../../src/contract/outcome' ; const Alice = '0x000000000000000000000000000000000000000000000000000000000000000a' ; const Bob = '0x000000000000000000000000000000000000000000000000000000000000000b' ; interface TestCaseInputs { initialHoldings : string ; sourceAllocations : Allocation [ ] ; targetAllocations : Allocation [ ] ; indexOfTargetInSource : number ; targetAllocationIndicesToPayout : number [ ] ; } interface TestCaseOutputs { newSourceAllocations : Allocation [ ] ; newTargetAllocations : Allocation [ ] ; exitAllocations : Allocation [ ] ; totalPayouts : string ; } interface TestCase { inputs : TestCaseInputs ; outputs : TestCaseOutputs ; } const testcase1 = { inputs : { initialHoldings : '0x0f' , indexOfTargetInSource : 1 , targetAllocationIndicesToPayout : [ ] , sourceAllocations : [ { destination : constants . HashZero , amount : '0x08' , allocationType : AllocationType . guarantee , metadata : encodeGuaranteeData ( [ Alice , Bob ] ) , } , { destination : constants . HashZero , amount : '0x07' , allocationType : AllocationType . guarantee , metadata : encodeGuaranteeData ( [ Bob , Alice ] ) , } , ] , targetAllocations : [ { destination : Alice , amount : '0x08' , allocationType : AllocationType . simple , metadata : '0x' , } , { destination : Bob , amount : '0x08' , allocationType : AllocationType . simple , metadata : '0x' , } , ] , } , outputs : { newSourceAllocations : [ { destination : constants . HashZero , amount : '0x08' , allocationType : AllocationType . guarantee , metadata : encodeGuaranteeData ( [ Alice , Bob ] ) , } , { destination : constants . HashZero , amount : '0x00' , allocationType : AllocationType . guarantee , metadata : encodeGuaranteeData ( [ Bob , Alice ] ) , } , ] , newTargetAllocations : [ { destination : Alice , amount : '0x08' , allocationType : AllocationType . simple , metadata : '0x' , } , { destination : Bob , amount : '0x01' , allocationType : AllocationType . simple , metadata : '0x' , } , ] , exitAllocations : [ { destination : Alice , amount : '0x00' , allocationType : AllocationType . simple , metadata : '0x' , } , { destination : Bob , amount : '0x07' , allocationType : AllocationType . simple , metadata : '0x' , } , ] , totalPayouts : '0x07' , } , } ; const testCases = [ [ testcase1 ] ] ; describe ( 'computeClaimEffectsAndInteractions' , ( ) => { it . each ( testCases ) ( 'off chain method matches expectation' , ( testCase ) => { const offChainResult = computeClaimEffectsAndInteractions ( testCase . inputs . initialHoldings , testCase . inputs . sourceAllocations , testCase . inputs . targetAllocations , testCase . inputs . indexOfTargetInSource , testCase . inputs . targetAllocationIndicesToPayout ) ; expect ( offChainResult . newSourceAllocations ) . toMatchObject ( testCase . outputs . newSourceAllocations ) ; expect ( offChainResult . newTargetAllocations ) . toMatchObject ( testCase . outputs . newTargetAllocations ) ; expect ( offChainResult . exitAllocations ) . toMatchObject ( testCase . outputs . exitAllocations ) ; expect ( offChainResult . totalPayouts ) . toEqual ( testCase . outputs . totalPayouts ) ; } ) ; it . each ( testCases ) ( 'on chain method matches expectation' , async ( testCase ) => { const onChainResult = await testNitroAdjudicator . compute_claim_effects_and_interactions ( testCase . inputs . initialHoldings , testCase . inputs . sourceAllocations , testCase . inputs . targetAllocations , testCase . inputs . indexOfTargetInSource , testCase . inputs . targetAllocationIndicesToPayout ) ; expect ( onChainResult . newSourceAllocations . map ( convertAmountToHexString ) ) . toMatchObject ( testCase . outputs . newSourceAllocations ) ; expect ( onChainResult . newTargetAllocations . map ( convertAmountToHexString ) ) . toMatchObject ( testCase . outputs . newTargetAllocations ) ; expect ( onChainResult . exitAllocations . map ( convertAmountToHexString ) ) . toMatchObject ( testCase . outputs . exitAllocations ) ; expect ( onChainResult . totalPayouts . toHexString ( ) ) . toEqual ( testCase . outputs . totalPayouts ) ; } ) ; const convertAmountToHexString = a => ( { ... a , amount : a . amount . toHexString ( ) } ) ; } ) ;