Skip to content

Commit

Permalink
test: add tests for return values on different StreamUtilities functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jul 21, 2020
1 parent 9b845a3 commit 6dba1bb
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 35 deletions.
56 changes: 31 additions & 25 deletions packages/contracts/contracts/mocks/StreamUtilitiesMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -39,7 +44,7 @@ contract StreamUtilitiesMock {
Types.AztecStream memory _stream
) public returns (bytes memory) {
stream = _stream;
return
bytes memory proofOutputs =
StreamUtilities._validateJoinSplitProof(
_aceContractAddress,
_proof2,
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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);
}
}
37 changes: 37 additions & 0 deletions packages/contracts/test/StreamUtilities/processCancellation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
10 changes: 5 additions & 5 deletions packages/contracts/test/StreamUtilities/processDeposit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
});
8 changes: 4 additions & 4 deletions packages/contracts/test/StreamUtilities/processWithdrawal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
});
59 changes: 58 additions & 1 deletion packages/contracts/test/StreamUtilities/validateRatioProof.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 6dba1bb

Please sign in to comment.