Skip to content

Commit

Permalink
chore(contracts): Use autogenerated Noir interfaces where possible (#…
Browse files Browse the repository at this point in the history
…2073)

Fixes #1604
  • Loading branch information
spalladino authored Sep 7, 2023
1 parent f0aa3ca commit bd6368b
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,10 @@ async function main() {
// 4. Send L2 to L1 message to withdraw funds and another message to swap assets.
logger('Send L2 tx to withdraw WETH to uniswap portal and send message to swap assets on L1');
// recipient is the uniswap portal
const selector = wethL2Contract.methods.withdraw.selector.toField();
const minimumOutputAmount = 0n;

const withdrawTx = uniswapL2Contract.methods
.swap(
selector,
wethL2Contract.address.toField(),
wethAmountToBridge,
new Fr(3000),
Expand Down
2 changes: 0 additions & 2 deletions yarn-project/canary/src/uniswap_trade_on_l1_from_l2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,10 @@ describe('uniswap_trade_on_l1_from_l2', () => {
// 4. Send L2 to L1 message to withdraw funds and another message to swap assets.
logger('Send L2 tx to withdraw WETH to uniswap portal and send message to swap assets on L1');
// recipient is the uniswap portal
const selector = wethL2Contract.methods.withdraw.selector.toField();
const minimumOutputAmount = 0n;

const withdrawTx = uniswapL2Contract.methods
.swap(
selector,
wethL2Contract.address.toField(),
wethAmountToBridge,
new Fr(3000),
Expand Down
48 changes: 13 additions & 35 deletions yarn-project/end-to-end/src/e2e_multi_transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ describe('multi-transfer payments', () => {
it('12 transfers per transactions should work', async () => {
// Transaction 1
logger(`self batchTransfer()`);
const batchTransferTx = zkTokenContract.methods
await zkTokenContract.methods
.batchTransfer(ownerAddress, [200n, 300n, 400n], [ownerAddress, ownerAddress, ownerAddress], 0)
.send({ origin: ownerAddress });
await batchTransferTx.isMined();
const batchTransferTxReceipt = await batchTransferTx.getReceipt();
logger(`consumption Receipt status: ${batchTransferTxReceipt.status}`);
.send({ origin: ownerAddress })
.wait();

await expectBalance(zkTokenContract, ownerAddress, initialBalance);
await expectsNumOfEncryptedLogsInTheLastBlockToBe(aztecNode, 4);

Expand All @@ -115,19 +114,10 @@ describe('multi-transfer payments', () => {

// Transaction 2
logger(`multiTransfer()...`);
const multiTransferTx = multiTransferContract.methods
.multiTransfer(
zkTokenContract.address.toField(),
recipients,
amounts,
ownerAddress,
zkTokenContract.methods.batchTransfer.selector.toField(),
noteOffsets,
)
.send({ origin: ownerAddress });
await multiTransferTx.isMined({ timeout: 1000 }); // mining timeout ≥ time needed for the test to finish.
const multiTransferTxReceipt = await multiTransferTx.getReceipt();
logger(`Consumption Receipt status: ${multiTransferTxReceipt.status}`);
await multiTransferContract.methods
.multiTransfer(zkTokenContract.address.toField(), recipients, amounts, ownerAddress, noteOffsets)
.send({ origin: ownerAddress })
.wait({ timeout: 1000 }); // mining timeout ≥ time needed for the test to finish.

await expectBalance(zkTokenContract, ownerAddress, initialBalance - amountSum);
for (let index = 0; index < numberOfAccounts; index++) {
Expand Down Expand Up @@ -178,19 +168,10 @@ describe('multi-transfer payments', () => {
const noteOffsets: bigint[] = [0n, 0n, 3n, 6n];
const repeatedSelfAdddress: AztecAddress[] = Array(12).fill(ownerAddress);

const multiTransferTx = multiTransferContract.methods
.multiTransfer(
zkTokenContract.address.toField(),
repeatedSelfAdddress,
amounts,
ownerAddress,
zkTokenContract.methods.batchTransfer.selector.toField(),
noteOffsets,
)
.send({ origin: ownerAddress });
await multiTransferTx.isMined({ timeout: 100 }); // mining timeout ≥ time needed for the test to finish.
const multiTransferTxReceipt = await multiTransferTx.getReceipt();
logger(`Consumption Receipt status: ${multiTransferTxReceipt.status}`);
await multiTransferContract.methods
.multiTransfer(zkTokenContract.address.toField(), repeatedSelfAdddress, amounts, ownerAddress, noteOffsets)
.send({ origin: ownerAddress })
.wait({ timeout: 100 }); // mining timeout ≥ time needed for the test to finish.

await expectBalance(zkTokenContract, ownerAddress, initialBalance);
await expectsNumOfEncryptedLogsInTheLastBlockToBe(aztecNode, 16);
Expand All @@ -203,10 +184,7 @@ describe('multi-transfer payments', () => {
const recipient = recipients[0];
await expectBalance(zkTokenContract, recipient, 0n);

const transferTx = zkTokenContract.methods.transfer(transferAmount, recipient).send({ origin: ownerAddress });
await transferTx.isMined();
const txReceipt = await transferTx.getReceipt();
logger(`consumption Receipt status: ${txReceipt.status}`);
await zkTokenContract.methods.transfer(transferAmount, recipient).send({ origin: ownerAddress }).wait();

await expectBalance(zkTokenContract, ownerAddress, initialBalance - transferAmount);
await expectBalance(zkTokenContract, recipient, transferAmount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,10 @@ describe('uniswap_trade_on_l1_from_l2', () => {

// 4. Send L2 to L1 message to withdraw funds and another message to swap assets.
logger('Send L2 tx to withdraw WETH to uniswap portal and send message to swap assets on L1');
const selector = wethCrossChainHarness.l2Contract.methods.withdraw.selector.toField();
const minimumOutputAmount = 0;

const withdrawTx = uniswapL2Contract.methods
.swap(
selector,
wethCrossChainHarness.l2Contract.address.toField(),
wethAmountToBridge,
new Fr(3000),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod private_token_airdrop_interface;

// Demonstrates how to perform 4 x 4 = 16 transfers in one transaction. Uses the private airdrop contract in the backend.
contract MultiTransfer {
use dep::aztec::oracle::public_call;
Expand All @@ -14,6 +16,9 @@ contract MultiTransfer {
utils as note_utils,
};

// Interfaces
use crate::private_token_airdrop_interface::PrivateTokenAirdropPrivateContextInterface;

#[aztec(private)]
fn constructor() {}

Expand All @@ -28,60 +33,45 @@ contract MultiTransfer {
addresses: [Field; 12], // Addresses to distribute to
amounts: [Field; 12], // Amounts to distribute
owner: Field, // Owner of the asset
batch_transfer_selector: Field, // Function selector for transfer
note_offsets: [Field; 4], // Offsets from which 4 notes of the owner would be read.
) -> [Field; 4] {
let token = PrivateTokenAirdropPrivateContextInterface::at(asset);

// First batch transfer call
let return_values_1 = context.call_private_function(asset, batch_transfer_selector, [
let result1 = token.batchTransfer(
&mut context,
owner,
amounts[0],
amounts[1],
amounts[2],
addresses[0],
addresses[1],
addresses[2],
note_offsets[0],
]);
let result1 = return_values_1[0];
[amounts[0], amounts[1], amounts[2]],
[addresses[0], addresses[1], addresses[2]],
note_offsets[0] as u32,
)[0];

// Second batch transfer call
let return_values_2 = context.call_private_function(asset, batch_transfer_selector, [
let result2 = token.batchTransfer(
&mut context,
owner,
amounts[3],
amounts[4],
amounts[5],
addresses[3],
addresses[4],
addresses[5],
note_offsets[1],
]);
let result2 = return_values_2[0];
[amounts[3], amounts[4], amounts[5]],
[addresses[3], addresses[4], addresses[5]],
note_offsets[1] as u32,
)[0];

// Third batch transfer call
let return_values_3 = context.call_private_function(asset, batch_transfer_selector, [
let result3 = token.batchTransfer(
&mut context,
owner,
amounts[6],
amounts[7],
amounts[8],
addresses[6],
addresses[7],
addresses[8],
note_offsets[2],
]);
let result3 = return_values_3[0];
[amounts[6], amounts[7], amounts[8]],
[addresses[6], addresses[7], addresses[8]],
note_offsets[2] as u32,
)[0];

// Fourth batch transfer call
let return_values_4 = context.call_private_function(asset, batch_transfer_selector, [
let result4 = token.batchTransfer(
&mut context,
owner,
amounts[9],
amounts[10],
amounts[11],
addresses[9],
addresses[10],
addresses[11],
note_offsets[3],
]);
let result4 = return_values_4[0];
[amounts[9], amounts[10], amounts[11]],
[addresses[9], addresses[10], addresses[11]],
note_offsets[3] as u32,
)[0];

[result1, result2, result3, result4]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ contract NativeToken {
compute_selector::compute_selector
},
public_call_stack_item::PublicCallStackItem,
types::point::Point
types::point::Point,
auth::assert_valid_message_for,
};

#[aztec(private)]
Expand Down Expand Up @@ -321,9 +322,8 @@ contract NativeToken {
],
GENERATOR_INDEX__SIGNATURE_PAYLOAD
)[0];
let is_valid_selector = compute_selector("is_valid(Field)");
let _callStackItem0 = context.call_private_function(from, is_valid_selector, [message_field]);
assert(_callStackItem0[0] == is_valid_selector);

assert_valid_message_for(&mut context, from, message_field);
}

// Reduce user balance
Expand Down
Loading

0 comments on commit bd6368b

Please sign in to comment.