Skip to content

Commit 6dba1bb

Browse files
committed
test: add tests for return values on different StreamUtilities functions
1 parent 9b845a3 commit 6dba1bb

File tree

5 files changed

+135
-35
lines changed

5 files changed

+135
-35
lines changed

packages/contracts/contracts/mocks/StreamUtilitiesMock.sol

+31-25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import '../StreamUtilities.sol';
77
contract StreamUtilitiesMock {
88
// The provided struct object is stored here as StreamUtilities expects a storage variable.
99
Types.AztecStream public stream;
10+
event ValidateRatioProof(bytes32 withdrawalNoteHash);
11+
event ValidateJoinSplitProof(bytes32 withdrawalNoteHash);
12+
event ProcessDeposit(bytes32 streamNoteHash);
13+
event ProcessWithdrawal(bytes32 newStreamNoteHash);
14+
event ProcessCancellation(bool cancellationSuccess);
1015

1116
function getRatio(bytes memory _proofData)
1217
public
@@ -23,13 +28,13 @@ contract StreamUtilitiesMock {
2328
Types.AztecStream memory _stream
2429
) public returns (bytes32) {
2530
stream = _stream;
26-
return
27-
StreamUtilities._validateRatioProof(
28-
_aceContractAddress,
29-
_proof1,
30-
_withdrawDuration,
31-
stream
32-
);
31+
bytes32 withdrawalNoteHash = StreamUtilities._validateRatioProof(
32+
_aceContractAddress,
33+
_proof1,
34+
_withdrawDuration,
35+
stream
36+
);
37+
emit ValidateRatioProof(withdrawalNoteHash);
3338
}
3439

3540
function validateJoinSplitProof(
@@ -39,7 +44,7 @@ contract StreamUtilitiesMock {
3944
Types.AztecStream memory _stream
4045
) public returns (bytes memory) {
4146
stream = _stream;
42-
return
47+
bytes memory proofOutputs =
4348
StreamUtilities._validateJoinSplitProof(
4449
_aceContractAddress,
4550
_proof2,
@@ -56,15 +61,15 @@ contract StreamUtilitiesMock {
5661
address _recipient,
5762
address _tokenAddress
5863
) public returns (bytes32) {
59-
return
60-
StreamUtilities._processDeposit(
61-
_proof,
62-
_proofSignature,
63-
_aceContractAddress,
64-
_sender,
65-
_recipient,
66-
_tokenAddress
67-
);
64+
bytes32 newStreamNoteHash = StreamUtilities._processDeposit(
65+
_proof,
66+
_proofSignature,
67+
_aceContractAddress,
68+
_sender,
69+
_recipient,
70+
_tokenAddress
71+
);
72+
emit ProcessDeposit(newStreamNoteHash);
6873
}
6974

7075
function processWithdrawal(
@@ -74,13 +79,13 @@ contract StreamUtilitiesMock {
7479
Types.AztecStream memory _stream
7580
) public returns (bytes32) {
7681
stream = _stream;
77-
return
78-
StreamUtilities._processWithdrawal(
79-
_aceContractAddress,
80-
_proof2,
81-
_withdrawalNoteHash,
82-
stream
83-
);
82+
bytes32 newStreamNoteHash = StreamUtilities._processWithdrawal(
83+
_aceContractAddress,
84+
_proof2,
85+
_withdrawalNoteHash,
86+
stream
87+
);
88+
emit ProcessWithdrawal(newStreamNoteHash);
8489
}
8590

8691
function processCancellation(
@@ -90,12 +95,13 @@ contract StreamUtilitiesMock {
9095
Types.AztecStream memory _stream
9196
) public returns (bool) {
9297
stream = _stream;
93-
return
98+
bool cancellationSuccess =
9499
StreamUtilities._processCancellation(
95100
_aceContractAddress,
96101
_proof2,
97102
_proof1OutputNotes,
98103
stream
99104
);
105+
emit ProcessCancellation(cancellationSuccess);
100106
}
101107
}

packages/contracts/test/StreamUtilities/processCancellation.js

+37
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,41 @@ describe('StreamUtilities - processCancellation', function () {
159159
.to.emit(zkAsset, 'DestroyNote')
160160
.withArgs(streamUtilitiesMock.address, streamNote.noteHash);
161161
});
162+
163+
it('transfers the zkAssets to the sender and recipient and returns true', async function () {
164+
const withdrawalNote = await createNote(
165+
streamNote.k.toNumber() / 4,
166+
recipient.address,
167+
[recipient.address]
168+
);
169+
170+
const refundNote = await createNote(
171+
(streamNote.k.toNumber() * 3) / 4,
172+
sender.address,
173+
[sender.address]
174+
);
175+
176+
const proof = new JoinSplitProof(
177+
[streamNote],
178+
[withdrawalNote, refundNote],
179+
streamUtilitiesMock.address,
180+
0,
181+
sender.address
182+
);
183+
184+
const proofData = proof.encodeABI(zkAsset.address);
185+
186+
await expect(
187+
streamUtilitiesMock.processCancellation(
188+
ace.address,
189+
proofData,
190+
withdrawalNote.noteHash,
191+
streamObject
192+
)
193+
)
194+
.to.emit(zkAsset, 'DestroyNote')
195+
.withArgs(streamUtilitiesMock.address, streamNote.noteHash)
196+
.and.emit(streamUtilitiesMock, 'ProcessCancellation')
197+
.withArgs(true);
198+
});
162199
});

packages/contracts/test/StreamUtilities/processDeposit.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ describe('StreamUtilities - processDeposit', function () {
209209
)
210210
).to.be.revertedWith("stream recipient can't view stream note");
211211
});
212-
it('transfers the zkAssets to the contract', async function () {
213-
const { depositProof } = await createStreamDepositProof(
212+
it('transfers the zkAssets to the contract and returns the new stream note hash', async function () {
213+
const { depositProof, streamNote } = await createStreamDepositProof(
214214
[depositOutputNote],
215215
streamUtilitiesMock.address,
216216
sender.address,
@@ -235,8 +235,8 @@ describe('StreamUtilities - processDeposit', function () {
235235
)
236236
)
237237
.to.emit(zkAsset, 'DestroyNote')
238-
.withArgs(sender.address, depositOutputNote.noteHash);
238+
.withArgs(sender.address, depositOutputNote.noteHash)
239+
.and.emit(streamUtilitiesMock, 'ProcessDeposit')
240+
.withArgs(streamNote.noteHash);
239241
});
240-
241-
it('returns the hash of the new stream note');
242242
});

packages/contracts/test/StreamUtilities/processWithdrawal.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe('StreamUtilities - processWithdrawal', function () {
187187
).to.be.revertedWith("stream recipient can't view new stream note");
188188
});
189189

190-
it('transfers the zkAssets to the sender and recipient', async function () {
190+
it('transfers the zkAssets to the sender and recipient and returns the hash of the new stream note', async function () {
191191
const withdrawalNote = await createNote(
192192
streamNote.k.toNumber() / 4,
193193
recipient.address,
@@ -219,8 +219,8 @@ describe('StreamUtilities - processWithdrawal', function () {
219219
)
220220
)
221221
.to.emit(zkAsset, 'DestroyNote')
222-
.withArgs(streamUtilitiesMock.address, streamNote.noteHash);
222+
.withArgs(streamUtilitiesMock.address, streamNote.noteHash)
223+
.and.emit(streamUtilitiesMock, 'ProcessWithdrawal')
224+
.withArgs(refundNote.noteHash);
223225
});
224-
225-
it('returns the hash of the new stream note');
226226
});

packages/contracts/test/StreamUtilities/validateRatioProof.js

+58-1
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,62 @@ describe('StreamUtilities - validateRatioProof', function () {
143143
).to.be.revertedWith('incorrect notional note in proof 1');
144144
});
145145

146-
it('returns withdrawal note hash');
146+
it('returns withdrawal note hash', async function () {
147+
const inputNoteValue = 1000;
148+
149+
const streamTotalDuration = 100;
150+
const withdrawalDuration = 10;
151+
152+
// We may then withdraw 1/10th of the note's value, i.e. 100
153+
const withdrawNoteValue = 100;
154+
const remainderNoteValue = 0;
155+
const ratioNumerator = 1;
156+
const ratioDenominator = 10;
157+
158+
const inputNote = await createNote(
159+
inputNoteValue,
160+
streamUtilitiesMock.address,
161+
[sender.address]
162+
);
163+
164+
const withdrawNote = await createNote(
165+
withdrawNoteValue,
166+
sender.address,
167+
[sender.address]
168+
);
169+
170+
const remainderNote = await createNote(
171+
remainderNoteValue,
172+
streamUtilitiesMock.address,
173+
[sender.address]
174+
);
175+
176+
const proofData = new DividendProof(
177+
inputNote,
178+
remainderNote,
179+
withdrawNote,
180+
streamUtilitiesMock.address,
181+
ratioDenominator,
182+
ratioNumerator
183+
).encodeABI();
184+
185+
const streamObject = {
186+
...streamObjectTemplate,
187+
noteHash: inputNote.noteHash,
188+
startTime: 0,
189+
lastWithdrawTime: 0,
190+
stopTime: streamTotalDuration,
191+
};
192+
193+
await expect(
194+
streamUtilitiesMock.validateRatioProof(
195+
ace.address,
196+
proofData,
197+
withdrawalDuration,
198+
streamObject
199+
)
200+
)
201+
.to.emit(streamUtilitiesMock, 'ValidateRatioProof')
202+
.withArgs(withdrawNote.noteHash);
203+
});
147204
});

0 commit comments

Comments
 (0)