def test_all_asset(asset_amount, state, channel, token, events): half_amount = asset_amount / 2 assert token.transfer(tester.a1, half_amount) is True token1 = ABIContract( state, token.translator, token.address, default_key=tester.k1, ) assert token.approve(channel.address, half_amount) is True assert token1.approve(channel.address, half_amount) is True channel1 = ABIContract( state, channel.translator, channel.address, default_key=tester.k1, ) channel.deposit(half_amount) channel1.deposit(half_amount) _, deposit1, _, deposit2 = channel.addressAndBalance() assert deposit1 == half_amount assert deposit2 == half_amount assert token.balanceOf(channel.address) == asset_amount assert token.balanceOf(tester.a0) == 0 assert token.balanceOf(tester.a1) == 0
def test_settle(state, channel, token, asset_amount, events): half_amount = asset_amount / 2 assert token.transfer(tester.a1, half_amount) is True token1 = ABIContract( state, token.translator, token.address, default_key=tester.k1, ) assert token.approve(channel.address, half_amount) is True assert token1.approve(channel.address, half_amount) is True channel1 = ABIContract( state, channel.translator, channel.address, default_key=tester.k1, ) channel.deposit(half_amount) channel1.deposit(half_amount) secret1 = 'x' * 32 hashlock1 = sha3(secret1) lock_amount1 = 29 lock_expiration1 = 1158003 lock1 = Lock(lock_amount1, lock_expiration1, hashlock1) lockhash1 = sha3(lock1.as_bytes) merkleproof1 = [lockhash1] locksroot1 = merkleroot([lockhash1], merkleproof1) nonce1 = 1 asset = token.address transfered_amount1 = 1 recipient = tester.a1 locksroot = locksroot1 msg1 = DirectTransfer( nonce1, asset, transfered_amount1, recipient, locksroot, ) msg1.sign(tester.k0) packed = msg1.packed() direct_transfer1 = str(packed.data) secret2 = 'y' * 32 hashlock2 = sha3(secret2) lock_amount2 = 20 lock_expiration2 = 1158005 lock2 = Lock(lock_amount2, lock_expiration2, hashlock2) lockhash2 = sha3(lock2.as_bytes) merkleproof2 = [lockhash2] locksroot2 = merkleroot([lockhash2], merkleproof2) locksroot = locksroot2 nonce2 = 2 transfered_amount2 = 3 msg2 = DirectTransfer( nonce2, token.address, # asset transfered_amount2, tester.a0, # recipient locksroot, ) msg2.sign(tester.k1) packed = msg2.packed() direct_transfer2 = str(packed.data) # not yet closed. should fail with pytest.raises(TransactionFailed): channel.settle() channel.close(direct_transfer1, direct_transfer2) channel.unlock( str(lock1.as_bytes), ''.join(merkleproof1), secret1, ) channel.unlock(str(lock2.as_bytes), ''.join(merkleproof2), secret2, sender=tester.k1) secret4 = 'k' * 32 hashlock4 = sha3(secret4) lock_amount4 = 23 lock_expiration4 = 31 lock4 = Lock(lock_amount4, lock_expiration4, hashlock4) hashlock4 = sha3(lock4.as_bytes) merkleproof4 = [hashlock4] # has now message, should fail with pytest.raises(TransactionFailed): channel.unlock( str(lock4.as_bytes), ''.join(merkleproof4), secret4, sender=tester.k1, ) # still timeout with pytest.raises(TransactionFailed): channel.settle() state.block.number = state.block.number + 40 # timeout over channel.settle() balance1 = half_amount + (transfered_amount2 - transfered_amount1) + lock_amount1 - lock_amount2 balance2 = half_amount + (transfered_amount1 - transfered_amount2) - lock_amount1 + lock_amount2 assert token.balanceOf(tester.a0) == balance1 assert token.balanceOf(tester.a1) == balance2 # can settle only once with pytest.raises(TransactionFailed): channel.settle() assert len(events) == 6 assert events[0]['_event_type'] == 'ChannelNewBalance' assert events[0]['assetAddress'] == token.address.encode('hex') assert events[0]['participant'] == tester.a0.encode('hex') assert events[0]['balance'] == 50 assert events[1]['_event_type'] == 'ChannelNewBalance' assert events[1]['assetAddress'] == token.address.encode('hex') assert events[1]['participant'] == tester.a1.encode('hex') assert events[1]['balance'] == 50 assert events[2]['_event_type'] == 'ChannelClosed' assert events[2]['closingAddress'] == tester.a0.encode('hex') assert events[2]['blockNumber'] == 1158002 assert events[3]['_event_type'] == 'ChannelSecretRevealed' assert events[3]['secret'] == 'x' * 32 assert events[4]['_event_type'] == 'ChannelSecretRevealed' assert events[4]['secret'] == 'y' * 32 assert events[5]['_event_type'] == 'ChannelSettled' assert events[5]['blockNumber'] == state.block.number