From 6dba1bb361b1729567e6c1bda6cd285967c025d1 Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 21 Jul 2020 21:22:25 +0100 Subject: [PATCH] test: add tests for return values on different StreamUtilities functions --- .../contracts/mocks/StreamUtilitiesMock.sol | 56 ++++++++++-------- .../StreamUtilities/processCancellation.js | 37 ++++++++++++ .../test/StreamUtilities/processDeposit.js | 10 ++-- .../test/StreamUtilities/processWithdrawal.js | 8 +-- .../StreamUtilities/validateRatioProof.js | 59 ++++++++++++++++++- 5 files changed, 135 insertions(+), 35 deletions(-) diff --git a/packages/contracts/contracts/mocks/StreamUtilitiesMock.sol b/packages/contracts/contracts/mocks/StreamUtilitiesMock.sol index ca15722..c48aa0d 100644 --- a/packages/contracts/contracts/mocks/StreamUtilitiesMock.sol +++ b/packages/contracts/contracts/mocks/StreamUtilitiesMock.sol @@ -7,6 +7,11 @@ import '../StreamUtilities.sol'; contract StreamUtilitiesMock { // The provided struct object is stored here as StreamUtilities expects a storage variable. Types.AztecStream public stream; + event ValidateRatioProof(bytes32 withdrawalNoteHash); + event ValidateJoinSplitProof(bytes32 withdrawalNoteHash); + event ProcessDeposit(bytes32 streamNoteHash); + event ProcessWithdrawal(bytes32 newStreamNoteHash); + event ProcessCancellation(bool cancellationSuccess); function getRatio(bytes memory _proofData) public @@ -23,13 +28,13 @@ contract StreamUtilitiesMock { Types.AztecStream memory _stream ) public returns (bytes32) { stream = _stream; - return - StreamUtilities._validateRatioProof( - _aceContractAddress, - _proof1, - _withdrawDuration, - stream - ); + bytes32 withdrawalNoteHash = StreamUtilities._validateRatioProof( + _aceContractAddress, + _proof1, + _withdrawDuration, + stream + ); + emit ValidateRatioProof(withdrawalNoteHash); } function validateJoinSplitProof( @@ -39,7 +44,7 @@ contract StreamUtilitiesMock { Types.AztecStream memory _stream ) public returns (bytes memory) { stream = _stream; - return + bytes memory proofOutputs = StreamUtilities._validateJoinSplitProof( _aceContractAddress, _proof2, @@ -56,15 +61,15 @@ contract StreamUtilitiesMock { address _recipient, address _tokenAddress ) public returns (bytes32) { - return - StreamUtilities._processDeposit( - _proof, - _proofSignature, - _aceContractAddress, - _sender, - _recipient, - _tokenAddress - ); + bytes32 newStreamNoteHash = StreamUtilities._processDeposit( + _proof, + _proofSignature, + _aceContractAddress, + _sender, + _recipient, + _tokenAddress + ); + emit ProcessDeposit(newStreamNoteHash); } function processWithdrawal( @@ -74,13 +79,13 @@ contract StreamUtilitiesMock { Types.AztecStream memory _stream ) public returns (bytes32) { stream = _stream; - return - StreamUtilities._processWithdrawal( - _aceContractAddress, - _proof2, - _withdrawalNoteHash, - stream - ); + bytes32 newStreamNoteHash = StreamUtilities._processWithdrawal( + _aceContractAddress, + _proof2, + _withdrawalNoteHash, + stream + ); + emit ProcessWithdrawal(newStreamNoteHash); } function processCancellation( @@ -90,12 +95,13 @@ contract StreamUtilitiesMock { Types.AztecStream memory _stream ) public returns (bool) { stream = _stream; - return + bool cancellationSuccess = StreamUtilities._processCancellation( _aceContractAddress, _proof2, _proof1OutputNotes, stream ); + emit ProcessCancellation(cancellationSuccess); } } diff --git a/packages/contracts/test/StreamUtilities/processCancellation.js b/packages/contracts/test/StreamUtilities/processCancellation.js index a6662ad..c683617 100644 --- a/packages/contracts/test/StreamUtilities/processCancellation.js +++ b/packages/contracts/test/StreamUtilities/processCancellation.js @@ -159,4 +159,41 @@ describe('StreamUtilities - processCancellation', function () { .to.emit(zkAsset, 'DestroyNote') .withArgs(streamUtilitiesMock.address, streamNote.noteHash); }); + + it('transfers the zkAssets to the sender and recipient and returns true', async function () { + const withdrawalNote = await createNote( + streamNote.k.toNumber() / 4, + recipient.address, + [recipient.address] + ); + + const refundNote = await createNote( + (streamNote.k.toNumber() * 3) / 4, + sender.address, + [sender.address] + ); + + const proof = new JoinSplitProof( + [streamNote], + [withdrawalNote, refundNote], + streamUtilitiesMock.address, + 0, + sender.address + ); + + const proofData = proof.encodeABI(zkAsset.address); + + await expect( + streamUtilitiesMock.processCancellation( + ace.address, + proofData, + withdrawalNote.noteHash, + streamObject + ) + ) + .to.emit(zkAsset, 'DestroyNote') + .withArgs(streamUtilitiesMock.address, streamNote.noteHash) + .and.emit(streamUtilitiesMock, 'ProcessCancellation') + .withArgs(true); + }); }); diff --git a/packages/contracts/test/StreamUtilities/processDeposit.js b/packages/contracts/test/StreamUtilities/processDeposit.js index 8bc8c4c..08b24c5 100644 --- a/packages/contracts/test/StreamUtilities/processDeposit.js +++ b/packages/contracts/test/StreamUtilities/processDeposit.js @@ -209,8 +209,8 @@ describe('StreamUtilities - processDeposit', function () { ) ).to.be.revertedWith("stream recipient can't view stream note"); }); - it('transfers the zkAssets to the contract', async function () { - const { depositProof } = await createStreamDepositProof( + it('transfers the zkAssets to the contract and returns the new stream note hash', async function () { + const { depositProof, streamNote } = await createStreamDepositProof( [depositOutputNote], streamUtilitiesMock.address, sender.address, @@ -235,8 +235,8 @@ describe('StreamUtilities - processDeposit', function () { ) ) .to.emit(zkAsset, 'DestroyNote') - .withArgs(sender.address, depositOutputNote.noteHash); + .withArgs(sender.address, depositOutputNote.noteHash) + .and.emit(streamUtilitiesMock, 'ProcessDeposit') + .withArgs(streamNote.noteHash); }); - - it('returns the hash of the new stream note'); }); diff --git a/packages/contracts/test/StreamUtilities/processWithdrawal.js b/packages/contracts/test/StreamUtilities/processWithdrawal.js index cd9dfac..5caedf4 100644 --- a/packages/contracts/test/StreamUtilities/processWithdrawal.js +++ b/packages/contracts/test/StreamUtilities/processWithdrawal.js @@ -187,7 +187,7 @@ describe('StreamUtilities - processWithdrawal', function () { ).to.be.revertedWith("stream recipient can't view new stream note"); }); - it('transfers the zkAssets to the sender and recipient', async function () { + it('transfers the zkAssets to the sender and recipient and returns the hash of the new stream note', async function () { const withdrawalNote = await createNote( streamNote.k.toNumber() / 4, recipient.address, @@ -219,8 +219,8 @@ describe('StreamUtilities - processWithdrawal', function () { ) ) .to.emit(zkAsset, 'DestroyNote') - .withArgs(streamUtilitiesMock.address, streamNote.noteHash); + .withArgs(streamUtilitiesMock.address, streamNote.noteHash) + .and.emit(streamUtilitiesMock, 'ProcessWithdrawal') + .withArgs(refundNote.noteHash); }); - - it('returns the hash of the new stream note'); }); diff --git a/packages/contracts/test/StreamUtilities/validateRatioProof.js b/packages/contracts/test/StreamUtilities/validateRatioProof.js index 31df98c..382b4b5 100644 --- a/packages/contracts/test/StreamUtilities/validateRatioProof.js +++ b/packages/contracts/test/StreamUtilities/validateRatioProof.js @@ -143,5 +143,62 @@ describe('StreamUtilities - validateRatioProof', function () { ).to.be.revertedWith('incorrect notional note in proof 1'); }); - it('returns withdrawal note hash'); + it('returns withdrawal note hash', async function () { + const inputNoteValue = 1000; + + const streamTotalDuration = 100; + const withdrawalDuration = 10; + + // We may then withdraw 1/10th of the note's value, i.e. 100 + const withdrawNoteValue = 100; + const remainderNoteValue = 0; + const ratioNumerator = 1; + const ratioDenominator = 10; + + const inputNote = await createNote( + inputNoteValue, + streamUtilitiesMock.address, + [sender.address] + ); + + const withdrawNote = await createNote( + withdrawNoteValue, + sender.address, + [sender.address] + ); + + const remainderNote = await createNote( + remainderNoteValue, + streamUtilitiesMock.address, + [sender.address] + ); + + const proofData = new DividendProof( + inputNote, + remainderNote, + withdrawNote, + streamUtilitiesMock.address, + ratioDenominator, + ratioNumerator + ).encodeABI(); + + const streamObject = { + ...streamObjectTemplate, + noteHash: inputNote.noteHash, + startTime: 0, + lastWithdrawTime: 0, + stopTime: streamTotalDuration, + }; + + await expect( + streamUtilitiesMock.validateRatioProof( + ace.address, + proofData, + withdrawalDuration, + streamObject + ) + ) + .to.emit(streamUtilitiesMock, 'ValidateRatioProof') + .withArgs(withdrawNote.noteHash); + }); });