Skip to content

Commit 0d0bc1d

Browse files
committed
refactor: use TokenAmount in erc20 transfer
1 parent 4bd35cd commit 0d0bc1d

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

packages/contracts/noir/erc20/src/lib.nr

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,17 @@ pub mod Token {
5252
from_secret_key: Field,
5353
from_note_inputs: common::Erc20NoteConsumptionInputs,
5454
to: common::WaAddress,
55-
amount: common::U256,
55+
amount: common::TokenAmount,
5656
to_randomness: Field,
5757
change_randomness: Field,
5858
) {
5959
common::Erc20Note::sub_and_emit_change(
6060
context,
6161
[from_note_inputs],
62-
common::TokenAmount { token: from_note_inputs.note.amount.token, amount },
62+
amount,
6363
change_randomness,
6464
from_secret_key,
6565
);
66-
common::Erc20Note {
67-
owner: to,
68-
amount: common::TokenAmount { token: from_note_inputs.note.amount.token, amount },
69-
randomness: to_randomness,
70-
}
71-
.emit(context);
66+
common::Erc20Note { owner: to, amount, randomness: to_randomness }.emit(context);
7267
}
7368
}

packages/contracts/noir/erc20_transfer/src/main.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main(
55
from_note_inputs: erc20::Erc20NoteConsumptionInputs,
66
// transfer params
77
to: common::WaAddress,
8-
amount: common::U256,
8+
amount: common::TokenAmount,
99
to_randomness: Field,
1010
// change
1111
change_randomness: Field,

packages/contracts/sdk/RollupService.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class PoolErc20Service {
218218
secretKey: string;
219219
fromNote: Erc20Note;
220220
to: CompleteWaAddress;
221-
amount: bigint;
221+
amount: TokenAmount;
222222
}) {
223223
const { Fr } = await import("@aztec/aztec.js");
224224

@@ -231,25 +231,19 @@ export class PoolErc20Service {
231231
from_note_inputs: await this.toNoteConsumptionInputs(secretKey, fromNote),
232232
from_secret_key: secretKey,
233233
to: { inner: to.address },
234-
amount: toNoirU256(amount),
234+
amount: await amount.toNoir(),
235235
to_randomness,
236236
change_randomness,
237237
};
238238
const changeNote = await Erc20Note.from({
239239
owner: fromNote.owner,
240-
amount: await TokenAmount.from({
241-
token: fromNote.amount.token,
242-
amount: fromNote.amount.amount - amount,
243-
}),
240+
amount: fromNote.amount.sub(amount),
244241
randomness: change_randomness,
245242
});
246243
assert(changeNote.amount.amount >= 0n, "invalid change note");
247244
const toNote = await Erc20Note.from({
248245
owner: to,
249-
amount: await TokenAmount.from({
250-
token: fromNote.amount.token,
251-
amount,
252-
}),
246+
amount,
253247
randomness: to_randomness,
254248
});
255249
// console.log("input\n", JSON.stringify(input));
@@ -496,6 +490,12 @@ export class TokenAmount {
496490
amount: toNoirU256(this.amount),
497491
};
498492
}
493+
494+
sub(other: TokenAmount): TokenAmount {
495+
const result = this.amount - other.amount;
496+
assert(result >= 0n, "TokenAmount.sub: underflow");
497+
return new TokenAmount(this.token, result);
498+
}
499499
}
500500

501501
export type WaAddress = string;

packages/contracts/test/PoolERC20.test.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe("PoolERC20", () => {
2828
let sdk: ReturnType<typeof interfaceSdk.createInterfaceSdk>;
2929
let backendSdk: ReturnType<typeof createBackendSdk>;
3030
let CompleteWaAddress: typeof import("../sdk").sdk.CompleteWaAddress;
31+
let TokenAmount: typeof import("../sdk").sdk.TokenAmount;
3132
snapshottedBeforeEach(async () => {
3233
[alice, bob, charlie] = await ethers.getSigners();
3334
await typedDeployments.fixture();
@@ -42,8 +43,9 @@ describe("PoolERC20", () => {
4243
await usdc.mintForTests(alice, await parseUnits(usdc, "1000000"));
4344
await usdc.connect(alice).approve(pool, ethers.MaxUint256);
4445

45-
CompleteWaAddress = (await tsImport("../sdk", __filename)).sdk
46-
.CompleteWaAddress;
46+
({ CompleteWaAddress, TokenAmount } = (
47+
await tsImport("../sdk", __filename)
48+
).sdk);
4749
});
4850

4951
before(async () => {
@@ -203,7 +205,10 @@ describe("PoolERC20", () => {
203205

204206
// interact
205207
const [note] = await sdk.poolErc20.getBalanceNotesOf(usdc, aliceSecretKey);
206-
const transferAmount = 123n;
208+
const transferAmount = await TokenAmount.from({
209+
token: await usdc.getAddress(),
210+
amount: 123n,
211+
});
207212
const { nullifier, changeNote, toNote } = await sdk.poolErc20.transfer({
208213
secretKey: aliceSecretKey,
209214
fromNote: note,
@@ -230,10 +235,10 @@ describe("PoolERC20", () => {
230235
await backendSdk.rollup.rollup();
231236

232237
expect(await sdk.poolErc20.balanceOf(usdc, aliceSecretKey)).to.equal(
233-
amount - transferAmount,
238+
amount - transferAmount.amount,
234239
);
235240
expect(await sdk.poolErc20.balanceOf(usdc, bobSecretKey)).to.equal(
236-
transferAmount,
241+
transferAmount.amount,
237242
);
238243
});
239244

@@ -254,7 +259,10 @@ describe("PoolERC20", () => {
254259
secretKey: aliceSecretKey,
255260
fromNote: shieldedNote,
256261
to: await CompleteWaAddress.fromSecretKey(bobSecretKey),
257-
amount: 30n,
262+
amount: await TokenAmount.from({
263+
token: await usdc.getAddress(),
264+
amount: 30n,
265+
}),
258266
});
259267
// TODO: split notes even if they are not rolled up
260268
// const { } = await sdk.poolErc20.transfer({
@@ -270,7 +278,10 @@ describe("PoolERC20", () => {
270278
secretKey: bobSecretKey,
271279
fromNote: bobNote,
272280
to: await CompleteWaAddress.fromSecretKey(charlieSecretKey),
273-
amount: 10n,
281+
amount: await TokenAmount.from({
282+
token: await usdc.getAddress(),
283+
amount: 10n,
284+
}),
274285
});
275286
await backendSdk.rollup.rollup();
276287

@@ -284,11 +295,14 @@ describe("PoolERC20", () => {
284295
});
285296

286297
it("can't double spend a note", async () => {
287-
const amount = 100n;
298+
const amount = await TokenAmount.from({
299+
token: await usdc.getAddress(),
300+
amount: 100n,
301+
});
288302
await sdk.poolErc20.shield({
289303
account: alice,
290304
token: usdc,
291-
amount,
305+
amount: amount.amount,
292306
secretKey: aliceSecretKey,
293307
});
294308
await backendSdk.rollup.rollup();
@@ -338,7 +352,10 @@ describe("PoolERC20", () => {
338352
secretKey: aliceSecretKey,
339353
fromNote: note,
340354
to: await CompleteWaAddress.fromSecretKey(bobSecretKey),
341-
amount: 100n,
355+
amount: await TokenAmount.from({
356+
token: await usdc.getAddress(),
357+
amount: 100n,
358+
}),
342359
});
343360
expect(
344361
await sdk.poolErc20.getBalanceNotesOf(usdc, aliceSecretKey),

0 commit comments

Comments
 (0)