From 51aecb3e9bef4052acad7b09de799838c9601881 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 16 Oct 2023 12:09:15 +0000 Subject: [PATCH] nuking --- .circleci/config.yml | 28 --- .../end-to-end/src/e2e_multi_transfer.test.ts | 188 -------------- .../src/e2e_private_airdrop.test.ts | 152 ----------- .../src/e2e_public_token_contract.test.ts | 80 ------ yarn-project/noir-contracts/Nargo.toml | 3 - .../multi_transfer_contract/Nargo.toml | 8 - .../multi_transfer_contract/src/main.nr | 64 ----- .../src/private_token_airdrop_interface.nr | 1 - .../private_token_airdrop_contract/Nargo.toml | 9 - .../src/claim_note.nr | 90 ------- .../src/interface.nr | 161 ------------ .../src/main.nr | 237 ------------------ .../public_token_contract/Nargo.toml | 8 - .../public_token_contract/src/main.nr | 102 -------- 14 files changed, 1131 deletions(-) delete mode 100644 yarn-project/end-to-end/src/e2e_multi_transfer.test.ts delete mode 100644 yarn-project/end-to-end/src/e2e_private_airdrop.test.ts delete mode 100644 yarn-project/end-to-end/src/e2e_public_token_contract.test.ts delete mode 100644 yarn-project/noir-contracts/src/contracts/multi_transfer_contract/Nargo.toml delete mode 100644 yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/main.nr delete mode 120000 yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/private_token_airdrop_interface.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/Nargo.toml delete mode 100644 yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/claim_note.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/interface.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/main.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/public_token_contract/Nargo.toml delete mode 100644 yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr diff --git a/.circleci/config.yml b/.circleci/config.yml index 2abd7304e4ff..0ab2f6fc6a60 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -643,18 +643,6 @@ jobs: command: cond_run_script end-to-end ./scripts/run_tests_local e2e_sandbox_example.test.ts ./scripts/docker-compose-e2e-sandbox.yml environment: { DEBUG: "aztec:*" } - e2e-multi-transfer-contract: - machine: - image: ubuntu-2204:2023.07.2 - resource_class: large - steps: - - *checkout - - *setup_env - - run: - name: "Test" - command: cond_run_script end-to-end ./scripts/run_tests_local e2e_multi_transfer.test.ts - environment: { DEBUG: "aztec:*" } - e2e-block-building: machine: image: ubuntu-2204:2023.07.2 @@ -811,18 +799,6 @@ jobs: command: cond_run_script end-to-end ./scripts/run_tests_local integration_l1_publisher.test.ts environment: { DEBUG: "aztec:*" } - e2e-public-token-contract: - machine: - image: ubuntu-2204:2023.07.2 - resource_class: large - steps: - - *checkout - - *setup_env - - run: - name: "Test" - command: cond_run_script end-to-end ./scripts/run_tests_local e2e_public_token_contract.test.ts - environment: { DEBUG: "aztec:*" } - e2e-cli: machine: image: ubuntu-2204:2023.07.2 @@ -1344,12 +1320,10 @@ workflows: - e2e-token-contract: *e2e_test - e2e-private-airdrop: *e2e_test - e2e-sandbox-example: *e2e_test - - e2e-multi-transfer-contract: *e2e_test - e2e-block-building: *e2e_test - e2e-nested-contract: *e2e_test - e2e-non-contract-account: *e2e_test - e2e-multiple-accounts-1-enc-key: *e2e_test - - e2e-public-token-contract: *e2e_test - e2e-cli: *e2e_test - e2e-cross-chain-messaging: *e2e_test - e2e-public-cross-chain-messaging: *e2e_test @@ -1382,12 +1356,10 @@ workflows: - e2e-token-contract - e2e-private-airdrop - e2e-sandbox-example - - e2e-multi-transfer-contract - e2e-block-building - e2e-nested-contract - e2e-non-contract-account - e2e-multiple-accounts-1-enc-key - - e2e-public-token-contract - e2e-cli - e2e-cross-chain-messaging - e2e-public-cross-chain-messaging diff --git a/yarn-project/end-to-end/src/e2e_multi_transfer.test.ts b/yarn-project/end-to-end/src/e2e_multi_transfer.test.ts deleted file mode 100644 index 80b7259a3560..000000000000 --- a/yarn-project/end-to-end/src/e2e_multi_transfer.test.ts +++ /dev/null @@ -1,188 +0,0 @@ -import { AztecAddress, Contract, Wallet } from '@aztec/aztec.js'; -import { DebugLogger } from '@aztec/foundation/log'; -import { MultiTransferContract, PrivateTokenAirdropContract } from '@aztec/noir-contracts/types'; -import { AztecNode, CompleteAddress } from '@aztec/types'; - -import { expectsNumOfEncryptedLogsInTheLastBlockToBe, setup } from './fixtures/utils.js'; - -/** - * Multi-transfer payments is an example application to demonstrate how a payroll application could be built using aztec. - * In the current version of aztec, each multi-transfer can support only 12 recipients per transaction. The sender - * can decide which note can be spent. - */ -describe('multi-transfer payments', () => { - const numberOfAccounts = 12; - - let aztecNode: AztecNode | undefined; - let wallet: Wallet; - let logger: DebugLogger; - let teardown: () => Promise; - - let ownerAddress: AztecAddress; - let recipients: AztecAddress[]; - let initialBalance: bigint; - - let zkTokenContract: PrivateTokenAirdropContract; - let multiTransferContract: MultiTransferContract; - - beforeEach(async () => { - let accounts: CompleteAddress[]; - ({ teardown, aztecNode, accounts, logger, wallet } = await setup(numberOfAccounts + 1)); // 1st being the `owner` - ownerAddress = accounts[0].address; - recipients = accounts.slice(1).map(a => a.address); - - logger(`Deploying zk token contract...`); - initialBalance = 1000n; - await deployZkTokenContract(initialBalance, ownerAddress); - - logger(`Deploying multi-transfer contract...`); - await deployMultiTransferContract(); - }, 100_000); - - afterEach(() => teardown(), 30_000); - - const deployZkTokenContract = async (initialBalance: bigint, owner: AztecAddress) => { - logger(`Deploying zk token contract...`); - zkTokenContract = await PrivateTokenAirdropContract.deploy(wallet, initialBalance, owner).send().deployed(); - logger(`zk token contract deployed at ${zkTokenContract.address}`); - }; - - const deployMultiTransferContract = async () => { - logger(`Deploying multi-transfer contract...`); - multiTransferContract = await MultiTransferContract.deploy(wallet).send().deployed(); - logger(`multi-transfer contract deployed at ${multiTransferContract.address}`); - }; - - const expectBalance = async (tokenContract: Contract, owner: AztecAddress, expectedBalance: bigint) => { - const balance = await tokenContract.methods.getBalance(owner).view({ from: owner }); - logger(`Account ${owner} balance: ${balance}`); - expect(balance).toBe(expectedBalance); - }; - - /** - * Payroll example - * - * Transaction 1: - * The sender first splits 1000 to create new notes (for himself) with values 100, 200, 300, 400: - * 0: sender: [1000] - * | - * +-- [100 (change), 200, 300, 400] - * - * Transaction 2: - * In the next transaction, the sender wants to spend all four notes created in the previous transaction: - * index: [0 1 2 3 4 5 6 7] - * 0: sender: [100, 200, 300, 400] - * | - * +-- [25 (change), 20, 25, 30] // first batchTx call - * - * index: [0 1 2 3 4 5 6 7] - * 1: sender: [200, 300, 400, 25] - * | - * +-- [50 (change), 40, 50, 60] // second batchTx call - * - * index: [0 1 2 3 4 5 6 7] - * 2: sender: [300, 400, 25, 50] - * | - * +-- [60 (change), 75, 80, 85] // third batchTx call - * - * index: [0 1 2 3 4 5 6 7] - * 3: sender: [400, 25, 50, 60] - * | - * +-- [50 (change), 100, 120, 130] // fourth batchTx call - * - */ - it('12 transfers per transactions should work', async () => { - // Transaction 1 - logger(`self batchTransfer()`); - await zkTokenContract.methods - .batchTransfer(ownerAddress, [200n, 300n, 400n], [ownerAddress, ownerAddress, ownerAddress], 0) - .send() - .wait(); - - await expectBalance(zkTokenContract, ownerAddress, initialBalance); - await expectsNumOfEncryptedLogsInTheLastBlockToBe(aztecNode, 4); - - const amounts: bigint[] = [20n, 25n, 30n, 40n, 50n, 60n, 75n, 80n, 85n, 100n, 120n, 130n]; - const amountSum = amounts.reduce((a, b) => a + b, 0n); - const noteOffsets: bigint[] = [0n, 0n, 0n, 0n]; - - // Transaction 2 - logger(`multiTransfer()...`); - await multiTransferContract.methods - .multiTransfer(zkTokenContract.address.toField(), recipients, amounts, ownerAddress, noteOffsets) - .send() - .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++) { - await expectBalance(zkTokenContract, recipients[index], amounts[index]); - } - await expectsNumOfEncryptedLogsInTheLastBlockToBe(aztecNode, 16); - }, 100_000); - - /** - * Creating change notes for self. - * - * Transaction 1: Splits the 1000 note to create 12 notes x 50 each. - * index: [0 1 2 3 4 5 6 7] - * 0: sender: [1000] - * | - * +-- [850, 50, 50, 50] - * - * index: [0 1 2 3 4 5 6 7] - * 1: sender: [850, 50, 50, 50] - * | - * +-- [700, 50, 50, 50] - * - * index: [0 1 2 3 4 5 6 7] - * 2: sender: [50, 50, 50, 700, 50, 50, 50] - * | - * +-- [550, 50, 50, 50] - * - * index: [0 1 2 3 4 5 6 7] - * 3: sender: [50, 50, 50, 50, 50, 50, 550, 50, 50, 50] - * | - * +-- [400, 50, 50, 50] - * - * End state: - * sender: [50, 50, 50, 50, 50, 50, 50, 50, 50, 400, 50, 50, 50] - * - * Transaction 2: Spend more notes than it's allowed in a single call, to transfer some amount to a recipient. - * It will destroy the largest note (400n) plus 8 small notes (50n * 8n). - * 4 notes will be destroyed first: [400n, 50n, 50n, 50n] - * And another 4 + 1 notes will be burnt in two function calls: [50n, 50n, 50n, 50n] and [50n] - * One change note (10n) will be created for the sender. - * One note will be created for the recipient. - */ - it('create 12 small notes out of 1 large note and transfer to a recipient', async () => { - // Transaction 1 - logger(`split multiTransfer()...`); - { - const amounts: bigint[] = [50n, 50n, 50n, 50n, 50n, 50n, 50n, 50n, 50n, 50n, 50n, 50n]; - const noteOffsets: bigint[] = [0n, 0n, 3n, 6n]; - const repeatedSelfAddress: AztecAddress[] = Array(12).fill(ownerAddress); - - await multiTransferContract.methods - .multiTransfer(zkTokenContract.address.toField(), repeatedSelfAddress, amounts, ownerAddress, noteOffsets) - .send() - .wait({ timeout: 100 }); // mining timeout ≥ time needed for the test to finish. - - await expectBalance(zkTokenContract, ownerAddress, initialBalance); - await expectsNumOfEncryptedLogsInTheLastBlockToBe(aztecNode, 16); - } - - // Transaction 2 - logger(`transfer()`); - { - const transferAmount = 400n + 50n * 7n + 40n; - const recipient = recipients[0]; - await expectBalance(zkTokenContract, recipient, 0n); - - await zkTokenContract.methods.transfer(transferAmount, recipient).send().wait(); - - await expectBalance(zkTokenContract, ownerAddress, initialBalance - transferAmount); - await expectBalance(zkTokenContract, recipient, transferAmount); - await expectsNumOfEncryptedLogsInTheLastBlockToBe(aztecNode, 2); - } - }, 100_000); -}); diff --git a/yarn-project/end-to-end/src/e2e_private_airdrop.test.ts b/yarn-project/end-to-end/src/e2e_private_airdrop.test.ts deleted file mode 100644 index 2a81c41ddf3b..000000000000 --- a/yarn-project/end-to-end/src/e2e_private_airdrop.test.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { CompleteAddress, NotePreimage, TxHash, Wallet } from '@aztec/aztec.js'; -import { MAX_NEW_COMMITMENTS_PER_CALL } from '@aztec/circuits.js'; -import { Fr } from '@aztec/foundation/fields'; -import { DebugLogger } from '@aztec/foundation/log'; -import { PrivateTokenAirdropContract } from '@aztec/noir-contracts/types'; - -import { setup } from './fixtures/utils.js'; - -class Claim { - static EMPTY = new Claim(0n, Fr.ZERO); - - constructor(public readonly amount: bigint, public readonly secret: Fr) {} - - get preimage() { - return new NotePreimage([new Fr(this.amount), this.secret]); - } -} - -describe('private airdrop', () => { - const numberOfAccounts = 3; - const initialSupply = 1000n; - const claimsStorageSlot = new Fr(2n); - - let wallets: Wallet[]; - let contracts: PrivateTokenAirdropContract[]; - let accounts: CompleteAddress[]; - let claims: Claim[]; - let logger: DebugLogger; - let teardown: () => Promise; - - beforeEach(async () => { - ({ teardown, accounts, wallets, logger } = await setup(numberOfAccounts)); - - logger(`Deploying zk token contract...`); - const owner = accounts[0].address; - const contract = await PrivateTokenAirdropContract.deploy(wallets[0], initialSupply, owner).send().deployed(); - logger(`zk token contract deployed at ${contract.address}`); - - contracts = [contract]; - for (let i = 1; i < accounts.length; ++i) { - contracts.push(await PrivateTokenAirdropContract.at(contract.address, wallets[i])); - } - }, 100_000); - - afterEach(() => teardown()); - - const expectBalance = async (accountIndex: number, expectedBalance: bigint) => { - const account = accounts[accountIndex].address; - const balance = await contracts[accountIndex].methods.getBalance(account).view({ from: account }); - logger(`Account ${accountIndex} balance: ${balance}`); - expect(balance).toBe(expectedBalance); - }; - - const createClaims = (amounts: bigint[]) => { - claims = amounts.map(amount => new Claim(amount, Fr.random())); - claims.push( - ...Array(MAX_NEW_COMMITMENTS_PER_CALL - amounts.length) - .fill(0) - .map(() => Claim.EMPTY), - ); - }; - - const claimToken = async (accountIndex: number, claim: Claim, txHash: TxHash, nonceIndex = 0) => { - const contract = contracts[accountIndex]; - const account = accounts[accountIndex].address; - const wallet = wallets[accountIndex]; - const nonces = await wallet.getNoteNonces(contract.address, claimsStorageSlot, claim.preimage, txHash); - - const preimageBuf = claim.preimage.toBuffer(); - const numNonces = claims.reduce((count, c) => count + (c.preimage.toBuffer().equals(preimageBuf) ? 1 : 0), 0); - expect(nonces.length).toBe(numNonces); - expect(nonces[nonceIndex]).not.toEqual(Fr.ZERO); - - const nonce = nonces[nonceIndex]; - await wallet.addNote(account, contract.address, claimsStorageSlot, claim.preimage, txHash, nonce); - - return contract.methods.claim(claim.amount, claim.secret).send().wait(); - }; - - it('should create claim notes for any accounts to claim', async () => { - let txHash: TxHash; - - // Transaction 1 - { - logger(`Create claims...`); - const accountIndex = 0; - await expectBalance(accountIndex, initialSupply); - - createClaims([12n, 345n]); - // Create a claim that has the exact same preimage as another claim. - claims[2] = claims[0]; - - const amounts = claims.map(c => c.amount); - const secrets = claims.map(c => c.secret); - ({ txHash } = await contracts[accountIndex].methods.createClaims(amounts, secrets).send().wait()); - - const amountSum = amounts.reduce((sum, a) => sum + a, 0n); - await expectBalance(accountIndex, initialSupply - amountSum); - } - - // Transaction 2 - { - logger(`Account 1 claims note 0...`); - const accountIndex = 1; - const claim = claims[0]; - await expectBalance(accountIndex, 0n); - - await claimToken(accountIndex, claim, txHash); - - await expectBalance(accountIndex, claim.amount); - - logger(`Fails to claim note 0 again...`); - await expect(claimToken(accountIndex, claim, txHash)).rejects.toThrow(); - } - - // Transaction 3 - { - logger(`Account 2 claims note 1...`); - const accountIndex = 2; - const claim = claims[1]; - await expectBalance(accountIndex, 0n); - - await claimToken(accountIndex, claim, txHash); - - await expectBalance(accountIndex, claim.amount); - - logger(`Fails to claim note 1 again...`); - await expect(claimToken(accountIndex, claim, txHash)).rejects.toThrow(); - - logger(`Fails to claim note 0...`); - await expect(claimToken(accountIndex, claims[0], txHash)).rejects.toThrow(); - } - - // Transaction 4 - { - logger(`Account 1 claims note 2...`); - const accountIndex = 1; - const claim0 = claims[0]; - const claim2 = claims[2]; - expect(claim2.preimage).toEqual(claim0.preimage); - - await expectBalance(accountIndex, claim0.amount); - - // Claim 2 has the same preimage as claim 0. - // `getNoteNonces` will return 2 nonces. And we need to use nonce 1 to spend the duplicated claim. - const nonceIndex = 1; - await claimToken(accountIndex, claim2, txHash, nonceIndex); - - await expectBalance(accountIndex, claim0.amount + claim2.amount); - } - }, 100_000); -}); diff --git a/yarn-project/end-to-end/src/e2e_public_token_contract.test.ts b/yarn-project/end-to-end/src/e2e_public_token_contract.test.ts deleted file mode 100644 index 2707988a2fc2..000000000000 --- a/yarn-project/end-to-end/src/e2e_public_token_contract.test.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { AztecAddress, Wallet } from '@aztec/aztec.js'; -import { DebugLogger } from '@aztec/foundation/log'; -import { PublicTokenContract } from '@aztec/noir-contracts/types'; -import { CompleteAddress, PXE, TxStatus } from '@aztec/types'; - -import times from 'lodash.times'; - -import { expectUnencryptedLogsFromLastBlockToBe, expectUnencryptedLogsInTxToBe, setup } from './fixtures/utils.js'; - -describe('e2e_public_token_contract', () => { - let pxe: PXE; - let wallet: Wallet; - let logger: DebugLogger; - let recipient: AztecAddress; - let teardown: () => Promise; - - let contract: PublicTokenContract; - - const deployContract = async () => { - logger(`Deploying L2 public contract...`); - const txReceipt = await PublicTokenContract.deploy(wallet).send().wait(); - contract = txReceipt.contract; - logger(`L2 contract deployed at ${txReceipt.contractAddress}`); - return { contract, txReceipt }; - }; - - beforeEach(async () => { - let accounts: CompleteAddress[]; - ({ teardown, pxe, accounts, wallet, logger } = await setup()); - recipient = accounts[0].address; - }, 100_000); - - afterEach(() => teardown()); - - it('should deploy a public token contract', async () => { - const { txReceipt } = await deployContract(); - expect(txReceipt.status).toEqual(TxStatus.MINED); - }, 30_000); - - it('should deploy a public token contract and mint tokens to a recipient', async () => { - const mintAmount = 359n; - - await deployContract(); - - const tx = contract.methods.mint(mintAmount, recipient).send(); - - await tx.isMined({ interval: 0.1 }); - const receipt = await tx.getReceipt(); - - expect(receipt.status).toBe(TxStatus.MINED); - - const balance = await contract.methods.publicBalanceOf(recipient.toField()).view({ from: recipient }); - expect(balance).toBe(mintAmount); - - await expectUnencryptedLogsInTxToBe(tx, ['Coins minted']); - }, 45_000); - - // Regression for https://github.com/AztecProtocol/aztec-packages/issues/640 - it('should mint tokens thrice to a recipient within the same block', async () => { - const mintAmount = 42n; - - await deployContract(); - - // Assemble two mint txs sequentially (no parallel calls to circuits!) and send them simultaneously - const methods = times(3, () => contract.methods.mint(mintAmount, recipient)); - for (const method of methods) await method.simulate(); - const txs = await Promise.all(methods.map(method => method.send())); - - // Check that all txs got mined in the same block - await Promise.all(txs.map(tx => tx.isMined())); - const receipts = await Promise.all(txs.map(tx => tx.getReceipt())); - expect(receipts.map(r => r.status)).toEqual(times(3, () => TxStatus.MINED)); - expect(receipts.map(r => r.blockNumber)).toEqual(times(3, () => receipts[0].blockNumber)); - - const balance = await contract.methods.publicBalanceOf(recipient.toField()).view({ from: recipient }); - expect(balance).toBe(mintAmount * 3n); - - await expectUnencryptedLogsFromLastBlockToBe(pxe, ['Coins minted', 'Coins minted', 'Coins minted']); - }, 60_000); -}); diff --git a/yarn-project/noir-contracts/Nargo.toml b/yarn-project/noir-contracts/Nargo.toml index 1097b1ae1183..74708cbe79de 100644 --- a/yarn-project/noir-contracts/Nargo.toml +++ b/yarn-project/noir-contracts/Nargo.toml @@ -9,13 +9,10 @@ members = [ "src/contracts/escrow_contract", "src/contracts/import_test_contract", "src/contracts/lending_contract", - "src/contracts/multi_transfer_contract", "src/contracts/parent_contract", "src/contracts/pending_commitments_contract", "src/contracts/pokeable_token_contract", "src/contracts/price_feed_contract", - "src/contracts/private_token_airdrop_contract", - "src/contracts/public_token_contract", "src/contracts/schnorr_account_contract", "src/contracts/schnorr_hardcoded_account_contract", "src/contracts/schnorr_single_key_account_contract", diff --git a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/Nargo.toml b/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/Nargo.toml deleted file mode 100644 index cccba64b2ea0..000000000000 --- a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/Nargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "multi_transfer_contract" -authors = [""] -compiler_version = "0.1" -type = "contract" - -[dependencies] -aztec = { path = "../../../../aztec-nr/aztec" } \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/main.nr deleted file mode 100644 index 05e173019c5f..000000000000 --- a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/main.nr +++ /dev/null @@ -1,64 +0,0 @@ -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 { - // Interfaces - use crate::private_token_airdrop_interface::PrivateTokenAirdropPrivateContextInterface; - - #[aztec(private)] - fn constructor() {} - - // Transfers 12 amounts to 12 recipients. - // multiTransfer() => 4 calls to batchTransfer() on the private airdrop contract. - // Each batchTransfer() call allows sending new notes to 3 recipients, so 3 x 4 = 12 recipients in total. - // Note that all the notes stay on the airdrop contract, the multi transfer contract must interact with - // methods in the private airdrop contract to initiate multiple transfers in one transaction. - #[aztec(private)] - fn multiTransfer( - asset: Field, // Asset to distribute - addresses: [Field; 12], // Addresses to distribute to - amounts: [Field; 12], // Amounts to distribute - owner: Field, // Owner of the asset - 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 result1 = token.batchTransfer( - &mut context, - owner, - [amounts[0], amounts[1], amounts[2]], - [addresses[0], addresses[1], addresses[2]], - note_offsets[0] as u32, - )[0]; - - // Second batch transfer call - let result2 = token.batchTransfer( - &mut context, - owner, - [amounts[3], amounts[4], amounts[5]], - [addresses[3], addresses[4], addresses[5]], - note_offsets[1] as u32, - )[0]; - - // Third batch transfer call - let result3 = token.batchTransfer( - &mut context, - owner, - [amounts[6], amounts[7], amounts[8]], - [addresses[6], addresses[7], addresses[8]], - note_offsets[2] as u32, - )[0]; - - // Fourth batch transfer call - let result4 = token.batchTransfer( - &mut context, - owner, - [amounts[9], amounts[10], amounts[11]], - [addresses[9], addresses[10], addresses[11]], - note_offsets[3] as u32, - )[0]; - - [result1, result2, result3, result4] - } -} \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/private_token_airdrop_interface.nr b/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/private_token_airdrop_interface.nr deleted file mode 120000 index 3fe452c3079e..000000000000 --- a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/private_token_airdrop_interface.nr +++ /dev/null @@ -1 +0,0 @@ -../../private_token_airdrop_contract/src/interface.nr \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/Nargo.toml b/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/Nargo.toml deleted file mode 100644 index c41c12b43510..000000000000 --- a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/Nargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "private_token_airdrop_contract" -authors = [""] -compiler_version = "0.1" -type = "contract" - -[dependencies] -aztec = { path = "../../../../aztec-nr/aztec" } -value_note = { path = "../../../../aztec-nr/value-note"} \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/claim_note.nr b/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/claim_note.nr deleted file mode 100644 index 2fe272999691..000000000000 --- a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/claim_note.nr +++ /dev/null @@ -1,90 +0,0 @@ -use dep::std::hash::pedersen; -use dep::aztec::note::{ - note_header::NoteHeader, - note_interface::NoteInterface, - utils::compute_note_hash_for_read_or_nullify, -}; - -global CLAIM_NOTE_LEN: Field = 2; - -struct ClaimNote { - value: Field, - secret_hash: Field, - header: NoteHeader, -} - -impl ClaimNote { - pub fn new(value: Field, secret_hash: Field) -> Self { - ClaimNote { - value, - secret_hash, - header: NoteHeader::empty(), - } - } - - pub fn serialize(self) -> [Field; CLAIM_NOTE_LEN] { - [self.value, self.secret_hash] - } - - pub fn deserialize(preimage: [Field; CLAIM_NOTE_LEN]) -> Self { - ClaimNote { - value: preimage[0], - secret_hash: preimage[1], - header: NoteHeader::empty(), - } - } - - pub fn compute_note_hash(self) -> Field { - // TODO(#1205) Should use a non-zero generator index. - dep::std::hash::pedersen([ - self.value, - self.secret_hash, - ])[0] - } - - pub fn compute_nullifier(self) -> Field { - let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(ClaimNoteMethods, self); - // TODO(#1205) Should use a non-zero generator index. - dep::std::hash::pedersen([ - note_hash_for_nullify, - self.secret_hash, // Include the secret_hash again so that the public won't know the note has been claimed. - ])[0] - } - - pub fn set_header(&mut self, header: NoteHeader) { - self.header = header; - } -} - -fn deserialize(preimage: [Field; CLAIM_NOTE_LEN]) -> ClaimNote { - ClaimNote::deserialize(preimage) -} - -fn serialize(note: ClaimNote) -> [Field; CLAIM_NOTE_LEN] { - note.serialize() -} - -fn compute_note_hash(note: ClaimNote) -> Field { - note.compute_note_hash() -} - -fn compute_nullifier(note: ClaimNote) -> Field { - note.compute_nullifier() -} - -fn get_header(note: ClaimNote) -> NoteHeader { - note.header -} - -fn set_header(note: &mut ClaimNote, header: NoteHeader) { - note.set_header(header) -} - -global ClaimNoteMethods = NoteInterface { - deserialize, - serialize, - compute_note_hash, - compute_nullifier, - get_header, - set_header, -}; diff --git a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/interface.nr b/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/interface.nr deleted file mode 100644 index 1c6bc62fb5a3..000000000000 --- a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/interface.nr +++ /dev/null @@ -1,161 +0,0 @@ -/* Autogenerated file, do not edit! */ - -use dep::std; -use dep::aztec::context::{ PrivateContext, PublicContext }; -use dep::aztec::constants_gen::RETURN_VALUES_LENGTH; - - - -// Interface for calling PrivateTokenAirdrop functions from a private context -struct PrivateTokenAirdropPrivateContextInterface { - address: Field, -} - -impl PrivateTokenAirdropPrivateContextInterface { - pub fn at(address: Field) -> Self { - Self { - address, - } - } - - pub fn batchTransfer( - self, - context: &mut PrivateContext, - sender: Field, - amounts: [Field;3], - recipients: [Field;3], - spend_note_offset: u32 - ) -> [Field; RETURN_VALUES_LENGTH] { - let mut serialized_args = [0; 8]; - serialized_args[0] = sender; - serialized_args[1] = amounts[0]; - serialized_args[2] = amounts[1]; - serialized_args[3] = amounts[2]; - serialized_args[4] = recipients[0]; - serialized_args[5] = recipients[1]; - serialized_args[6] = recipients[2]; - serialized_args[7] = spend_note_offset as Field; - - context.call_private_function(self.address, 0xbf748730, serialized_args) - } - - - pub fn burn( - self, - context: &mut PrivateContext, - amount: Field, - owner: Field - ) -> [Field; RETURN_VALUES_LENGTH] { - let mut serialized_args = [0; 2]; - serialized_args[0] = amount; - serialized_args[1] = owner; - - context.call_private_function(self.address, 0xa4fa3a6f, serialized_args) - } - - - pub fn claim( - self, - context: &mut PrivateContext, - amount: Field, - secret: Field - ) -> [Field; RETURN_VALUES_LENGTH] { - let mut serialized_args = [0; 2]; - serialized_args[0] = amount; - serialized_args[1] = secret; - - context.call_private_function(self.address, 0xe642f6a0, serialized_args) - } - - - pub fn createClaims( - self, - context: &mut PrivateContext, - amounts: [Field;16], - secrets: [Field;16] - ) -> [Field; RETURN_VALUES_LENGTH] { - let mut serialized_args = [0; 32]; - serialized_args[0] = amounts[0]; - serialized_args[1] = amounts[1]; - serialized_args[2] = amounts[2]; - serialized_args[3] = amounts[3]; - serialized_args[4] = amounts[4]; - serialized_args[5] = amounts[5]; - serialized_args[6] = amounts[6]; - serialized_args[7] = amounts[7]; - serialized_args[8] = amounts[8]; - serialized_args[9] = amounts[9]; - serialized_args[10] = amounts[10]; - serialized_args[11] = amounts[11]; - serialized_args[12] = amounts[12]; - serialized_args[13] = amounts[13]; - serialized_args[14] = amounts[14]; - serialized_args[15] = amounts[15]; - serialized_args[16] = secrets[0]; - serialized_args[17] = secrets[1]; - serialized_args[18] = secrets[2]; - serialized_args[19] = secrets[3]; - serialized_args[20] = secrets[4]; - serialized_args[21] = secrets[5]; - serialized_args[22] = secrets[6]; - serialized_args[23] = secrets[7]; - serialized_args[24] = secrets[8]; - serialized_args[25] = secrets[9]; - serialized_args[26] = secrets[10]; - serialized_args[27] = secrets[11]; - serialized_args[28] = secrets[12]; - serialized_args[29] = secrets[13]; - serialized_args[30] = secrets[14]; - serialized_args[31] = secrets[15]; - - context.call_private_function(self.address, 0x2eebe7ab, serialized_args) - } - - - pub fn mint( - self, - context: &mut PrivateContext, - amount: Field, - owner: Field - ) -> [Field; RETURN_VALUES_LENGTH] { - let mut serialized_args = [0; 2]; - serialized_args[0] = amount; - serialized_args[1] = owner; - - context.call_private_function(self.address, 0x1535439c, serialized_args) - } - - - pub fn transfer( - self, - context: &mut PrivateContext, - amount: Field, - recipient: Field - ) -> [Field; RETURN_VALUES_LENGTH] { - let mut serialized_args = [0; 2]; - serialized_args[0] = amount; - serialized_args[1] = recipient; - - context.call_private_function(self.address, 0xc0888d22, serialized_args) - } - -} - - - - -// Interface for calling PrivateTokenAirdrop functions from a public context -struct PrivateTokenAirdropPublicContextInterface { - address: Field, -} - -impl PrivateTokenAirdropPublicContextInterface { - pub fn at(address: Field) -> Self { - Self { - address, - } - } - -} - - diff --git a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/main.nr deleted file mode 100644 index 125a5333dd23..000000000000 --- a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/main.nr +++ /dev/null @@ -1,237 +0,0 @@ -mod claim_note; -mod interface; - -contract PrivateTokenAirdrop { - // Libs - use dep::std::option::Option; - use dep::value_note::{ - balance_utils, - utils::{increment, decrement, decrement_by_at_most}, - value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods}, - }; - use dep::aztec::{ - constants_gen::MAX_NEW_COMMITMENTS_PER_CALL, - context::{PrivateContext, PublicContext, Context}, - state_vars::{map::Map, set::Set}, - note::{ - note_getter_options::NoteGetterOptions, - note_header::NoteHeader, - utils as note_utils, - }, - log::emit_unencrypted_log_from_private, - }; - - use crate::claim_note::{ClaimNote, ClaimNoteMethods, CLAIM_NOTE_LEN}; - use crate::interface::PrivateTokenAirdropPrivateContextInterface; - - - struct Storage { - balances: Map>, - claims: Set, - } - - impl Storage { - fn init(context: Context) -> pub Self { - Storage { - balances: Map::new( - context, - 1, // Storage slot - |context, slot| { - Set::new(context, slot, ValueNoteMethods) - }, - ), - claims: Set::new(context, 2, ClaimNoteMethods), - } - } - } - - // Constructs the contract and sets `initial_supply` which is fully owned by `owner`. - #[aztec(private)] - fn constructor( - initial_supply: Field, - owner: Field - ) { - // Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call. - let owner_balance = storage.balances.at(owner); - if (initial_supply != 0) { - increment(owner_balance, initial_supply, owner); - } - } - - // Mints `amount` of tokens to `owner`. - #[aztec(private)] - fn mint( - amount: Field, - owner: Field - ) { - // Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call. - let owner_balance = storage.balances.at(owner); - increment(owner_balance, amount, owner); - } - - // Burn `amount` of tokens from `owner`'s balance. - // This is an example of a recursive function. It calls itself until enough notes have been burned to burn the `amount`. - #[aztec(private)] - fn burn( - amount: Field, - owner: Field, - ) { - let msg_sender = context.msg_sender(); - let this_address = context.this_address(); - - // If this function is not called by another function in the same contract, the owner must be msg_sender. - if msg_sender != this_address { - assert(owner == msg_sender); - } - - let balance = storage.balances.at(owner); - let sum = decrement_by_at_most(balance, amount, owner); - - // If sum is 0, there are no notes to be burned. - assert(sum != 0); - - if sum != amount { - // The destroyed notes' sum is not enough. Keep burning. - let amount_to_burn = amount - sum; - let this = PrivateTokenAirdropPrivateContextInterface::at(this_address); - let _res = this.burn(&mut context, amount_to_burn, owner); - } - } - - // Transfers `amount` of tokens from `sender` to a `recipient`. - #[aztec(private)] - fn transfer( - amount: Field, - recipient: Field, - ) { - let sender = context.msg_sender(); - let sender_balance = storage.balances.at(sender); - - let sum = decrement_by_at_most(sender_balance, amount, sender); - - // If sum is 0, there are no notes to be used. - assert(sum != 0); - - if sum != amount { - // The destroyed notes' sum is not enough for the transfer. - // Burn the remaining amount. - // We only call burn() when decrement_by_at_most() didn't destroy enough notes. - let amount_to_burn = amount - sum; - let this_address = context.this_address(); - let this = PrivateTokenAirdropPrivateContextInterface::at(this_address); - let _res = this.burn(&mut context, amount_to_burn, sender); - } - - // Create a new note for the recipient. - let recipient_balance = storage.balances.at(recipient); - increment(recipient_balance, amount, recipient); - } - - #[aztec(private)] - fn createClaims( - amounts: [Field; MAX_NEW_COMMITMENTS_PER_CALL], - secrets: [Field; MAX_NEW_COMMITMENTS_PER_CALL], - ) { - let sender = context.msg_sender(); - - // Pick from the set of sender's notes to spend amount. - let sender_balance = storage.balances.at(sender); - let total = amounts.fold(0, |sum, a| sum + a); - decrement(sender_balance, total, sender); - - // Create claim notes. - let claims = storage.claims; - for i in 0..amounts.len() { - let amount = amounts[i]; - if amount != 0 { - let mut note = ClaimNote::new(amount, secrets[i]); - claims.insert(&mut note); - } - } - } - - #[aztec(private)] - fn claim(amount: Field, secret: Field) { - let owner = context.msg_sender(); - - // Find a claim note with the exact amount (field_index = 0) and secret (field_index = 1). - let options = NoteGetterOptions::new().select(0, amount).select(1, secret).set_limit(1); - let opt_notes = storage.claims.get_notes(options); - let note = opt_notes[0].unwrap_unchecked(); - - // Remove the claim note from the set. - storage.claims.remove(note); - - // Send the value note. - let balance = storage.balances.at(owner); - increment(balance, note.value, owner); - } - - // Transfers `amounts` of tokens from `sender` to 3 `recipients`. - // Aztec only allows `MAX_NEW_COMMITMENTS_PER_CALL = 4` notes per tx => - // 1 new note for sender's new balance and 1 note each for 3 recipients (for their new balance) - #[aztec(private)] - fn batchTransfer( - sender: Field, - amounts: [Field; 3], - recipients: [Field; 3], - spend_note_offset: u32, - ) { - // Gets the set of sender's notes and picks 4 of those based on the offset. - // Spends the first of those 4 notes. - let sender_balance = storage.balances.at(sender); - let total = amounts[0] + amounts[1] + amounts[2]; - - let options = NoteGetterOptions::new().set_limit(1).set_offset(spend_note_offset); - let opt_notes = sender_balance.get_notes(options); - - // The note should always exist. - let note = opt_notes[0].unwrap_unchecked(); - - assert(note.owner == sender); - - sender_balance.remove(note); - - // Assert that the note chosen to spend has enough funds. - assert(note.value as u64 >= total as u64); - - // Add the change value back to the owner's balance. - let change_value = note.value - total; - increment(sender_balance, change_value, sender); - - // Creates new note for the recipient. - for i in 0..3 { - let recipient_balance = storage.balances.at(recipients[i]); - increment(recipient_balance, amounts[i], recipients[i]); - } - - // Also emit an unencrypted log, eg. "Coins transferred" - // In this example, we emit the first output note's commitment to ensure that the unencrypted log - // for each call to this function is distinct. This is done to detect any issues while collecting - // logs when building a transaction. See: https://github.com/AztecProtocol/aztec-packages/issues/987 - emit_unencrypted_log_from_private(&mut context, context.new_commitments.storage[0]); - } - - // Helper function to get the balance of a user ("unconstrained" is a Noir alternative of Solidity's "view" function). - unconstrained fn getBalance( - owner: Field, - ) -> Field { - // Get the set of notes owned by the user. - let owner_balance = storage.balances.at(owner); - - // Return the sum of all notes in the set. - balance_utils::get_balance(owner_balance) - } - - // Computes note hash and nullifier. - // Note 1: Needs to be defined by every contract producing logs. - // Note 2: Having it in all the contracts gives us the ability to compute the note hash and nullifier differently for different kind of notes. - unconstrained fn compute_note_hash_and_nullifier(contract_address: Field, nonce: Field, storage_slot: Field, preimage: [Field; VALUE_NOTE_LEN]) -> [Field; 4] { - let note_header = NoteHeader::new(contract_address, nonce, storage_slot); - if (storage_slot == 2) { - note_utils::compute_note_hash_and_nullifier(ClaimNoteMethods, note_header, preimage) - } else { - note_utils::compute_note_hash_and_nullifier(ValueNoteMethods, note_header, preimage) - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/public_token_contract/Nargo.toml b/yarn-project/noir-contracts/src/contracts/public_token_contract/Nargo.toml deleted file mode 100644 index c3098dda9ee1..000000000000 --- a/yarn-project/noir-contracts/src/contracts/public_token_contract/Nargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "public_token_contract" -authors = [""] -compiler_version = "0.1" -type = "contract" - -[dependencies] -aztec = { path = "../../../../aztec-nr/aztec" } \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr deleted file mode 100644 index 23815b7c7404..000000000000 --- a/yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr +++ /dev/null @@ -1,102 +0,0 @@ -// docs:start:all -contract PublicToken { - use dep::std::option::Option; - - // docs:start:unencrypted_import - use dep::aztec::log::emit_unencrypted_log; - // docs:end:unencrypted_import - - use dep::aztec::{ - context::{PrivateContext, PublicContext, Context}, - state_vars::{ - map::Map, - public_state::PublicState, - }, - types::type_serialization::field_serialization::{ - FieldSerializationMethods, FIELD_SERIALIZED_LEN, - }, - }; - - struct Storage { - balances: Map>, - } - - impl Storage { - fn init(context: Context) -> pub Self { - Storage { - balances: Map::new( - context, - 1, - |context, slot| { - PublicState::new(context, slot, FieldSerializationMethods) - }, - ), - } - } - } - - // Constructs the contract. - // docs:start:empty-constructor - #[aztec(private)] - fn constructor() {} - // docs:end:empty-constructor - - // Mints `amount` of tokens to a `recipient`. - #[aztec(public)] - fn mint( - amount: Field, - recipient: Field, - ) -> Field { - - - let recipient_balance = storage.balances.at(recipient); - let new_amount = recipient_balance.read() + amount; - // TODO: Remove return value. - // docs:start:unencrypted_log - emit_unencrypted_log(&mut context, "Coins minted"); - // docs:end:unencrypted_log - recipient_balance.write(new_amount); - - new_amount - } - - // Transfers `amount` of tokens from `msg_sender` to `recipient`. - #[aztec(public)] - fn transfer( - amount: Field, - recipient: Field, - ) -> Field { - - let sender = context.msg_sender(); - - let sender_balance = storage.balances.at(sender); - let recipient_balance = storage.balances.at(recipient); - - let current_sender_balance: Field = sender_balance.read(); - let current_recipient_balance = recipient_balance.read(); - - // TODO: Should support larger numbers. - let mut return_value = 0; - - if (current_sender_balance as u126 > amount as u126) { - sender_balance.write(current_sender_balance - amount); - // TODO: Compiler complains if we don't assign the result of the write to anything - emit_unencrypted_log(&mut context, "Coins transferred"); - let amount = current_recipient_balance + amount; - recipient_balance.write(amount); - return_value = amount; - } else { - // TODO: Revert if there is not enough balance - return_value = current_recipient_balance; - } - return_value - } - - unconstrained fn publicBalanceOf( - owner: Field, - ) -> Field { - - storage.balances.at(owner).read() - } -} -// docs:end:all \ No newline at end of file