From 977304c0aa24a2b8b51479bfe631b47dcb944632 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Wed, 27 Sep 2023 10:46:39 +0100 Subject: [PATCH] feat: TokenContract takes admin in constructor --- yarn-project/end-to-end/src/canary/browser.ts | 5 +++-- .../end-to-end/src/e2e_2_rpc_servers.test.ts | 3 +-- .../end-to-end/src/e2e_escrow_contract.test.ts | 4 +--- .../end-to-end/src/e2e_lending_contract.test.ts | 6 ++---- .../src/e2e_multiple_accounts_1_enc_key.test.ts | 4 +--- .../end-to-end/src/e2e_sandbox_example.test.ts | 5 ++--- .../end-to-end/src/e2e_token_contract.test.ts | 3 +-- yarn-project/end-to-end/src/fixtures/utils.ts | 9 +-------- .../end-to-end/src/guides/dapp_testing.test.ts | 12 ++++-------- .../src/guides/writing_an_account_contract.test.ts | 3 +-- yarn-project/end-to-end/src/sample-dapp/deploy.mjs | 3 +-- .../end-to-end/src/sample-dapp/index.test.mjs | 3 +-- .../src/contracts/token_contract/src/main.nr | 4 ++-- 13 files changed, 21 insertions(+), 43 deletions(-) diff --git a/yarn-project/end-to-end/src/canary/browser.ts b/yarn-project/end-to-end/src/canary/browser.ts index c3f16ff98611..8ac6306314d3 100644 --- a/yarn-project/end-to-end/src/canary/browser.ts +++ b/yarn-project/end-to-end/src/canary/browser.ts @@ -174,13 +174,14 @@ export const browserTestSuite = (setup: () => Server, pageLogger: AztecJs.DebugL accounts = await client.getRegisteredAccounts(); } const [owner] = await getSandboxAccountsWallets(client); - const tx = new DeployMethod(accounts[0].publicKey, client, TokenContractAbi).send(); + const tx = new DeployMethod(accounts[0].publicKey, client, TokenContractAbi, [ + owner.getCompleteAddress(), + ]).send(); await tx.wait(); const receipt = await tx.getReceipt(); console.log(`Contract Deployed: ${receipt.contractAddress}`); const token = await Contract.at(receipt.contractAddress!, TokenContractAbi, owner); - await token.methods._initialize(owner.getAddress()).send().wait(); const secret = Fr.random(); const secretHash = await computeMessageSecretHash(secret); await token.methods.mint_private(initialBalance, secretHash).send().wait(); diff --git a/yarn-project/end-to-end/src/e2e_2_rpc_servers.test.ts b/yarn-project/end-to-end/src/e2e_2_rpc_servers.test.ts index 88be20741da9..460dce4f5f17 100644 --- a/yarn-project/end-to-end/src/e2e_2_rpc_servers.test.ts +++ b/yarn-project/end-to-end/src/e2e_2_rpc_servers.test.ts @@ -85,8 +85,7 @@ describe('e2e_2_rpc_servers', () => { const deployTokenContract = async (initialAdminBalance: bigint, admin: AztecAddress) => { logger(`Deploying Token contract...`); - const contract = await TokenContract.deploy(walletA).send().deployed(); - expect((await contract.methods._initialize(admin).send().wait()).status).toBe(TxStatus.MINED); + const contract = await TokenContract.deploy(walletA, walletA.getCompleteAddress()).send().deployed(); if (initialAdminBalance > 0n) { await mintTokens(contract, admin, initialAdminBalance); diff --git a/yarn-project/end-to-end/src/e2e_escrow_contract.test.ts b/yarn-project/end-to-end/src/e2e_escrow_contract.test.ts index 1a661fe563dc..acc5cb51185e 100644 --- a/yarn-project/end-to-end/src/e2e_escrow_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_escrow_contract.test.ts @@ -49,9 +49,7 @@ describe('e2e_escrow_contract', () => { logger(`Escrow contract deployed at ${escrowContract.address}`); // Deploy Private Token contract and mint funds for the escrow contract - token = await TokenContract.deploy(wallet).send().deployed(); - - expect((await token.methods._initialize(owner).send().wait()).status).toBe(TxStatus.MINED); + token = await TokenContract.deploy(wallet, wallet.getCompleteAddress()).send().deployed(); const secret = Fr.random(); const secretHash = await computeMessageSecretHash(secret); diff --git a/yarn-project/end-to-end/src/e2e_lending_contract.test.ts b/yarn-project/end-to-end/src/e2e_lending_contract.test.ts index 4f8dfe54b8e4..5baed9482848 100644 --- a/yarn-project/end-to-end/src/e2e_lending_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_lending_contract.test.ts @@ -50,14 +50,14 @@ describe('e2e_lending_contract', () => { { logger(`Deploying collateral asset feed contract...`); - const receipt = await waitForSuccess(TokenContract.deploy(wallet).send()); + const receipt = await waitForSuccess(TokenContract.deploy(wallet, wallet.getCompleteAddress()).send()); logger(`Collateral asset deployed to ${receipt.contractAddress}`); collateralAsset = await TokenContract.at(receipt.contractAddress!, wallet); } { logger(`Deploying stable coin contract...`); - const receipt = await waitForSuccess(TokenContract.deploy(wallet).send()); + const receipt = await waitForSuccess(TokenContract.deploy(wallet, wallet.getCompleteAddress()).send()); logger(`Stable coin asset deployed to ${receipt.contractAddress}`); stableCoin = await TokenContract.at(receipt.contractAddress!, wallet); } @@ -69,9 +69,7 @@ describe('e2e_lending_contract', () => { lendingContract = await LendingContract.at(receipt.contractAddress!, wallet); } - await waitForSuccess(collateralAsset.methods._initialize(accounts[0]).send()); await waitForSuccess(collateralAsset.methods.set_minter(lendingContract.address, true).send()); - await waitForSuccess(stableCoin.methods._initialize(accounts[0]).send()); await waitForSuccess(stableCoin.methods.set_minter(lendingContract.address, true).send()); return { priceFeedContract, lendingContract, collateralAsset, stableCoin }; diff --git a/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts b/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts index 688d5c4cd168..a2818f2a4d91 100644 --- a/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts +++ b/yarn-project/end-to-end/src/e2e_multiple_accounts_1_enc_key.test.ts @@ -43,12 +43,10 @@ describe('e2e_multiple_accounts_1_enc_key', () => { } logger(`Deploying Token...`); - const token = await TokenContract.deploy(wallets[0]).send().deployed(); + const token = await TokenContract.deploy(wallets[0], wallets[0].getCompleteAddress()).send().deployed(); tokenAddress = token.address; logger(`Token deployed at ${tokenAddress}`); - expect((await token.methods._initialize(accounts[0]).send().wait()).status).toBe(TxStatus.MINED); - const secret = Fr.random(); const secretHash = await computeMessageSecretHash(secret); diff --git a/yarn-project/end-to-end/src/e2e_sandbox_example.test.ts b/yarn-project/end-to-end/src/e2e_sandbox_example.test.ts index 84f72c805ca7..c5d108b3c241 100644 --- a/yarn-project/end-to-end/src/e2e_sandbox_example.test.ts +++ b/yarn-project/end-to-end/src/e2e_sandbox_example.test.ts @@ -55,13 +55,12 @@ describe('e2e_sandbox_example', () => { const initialSupply = 1_000_000n; logger(`Deploying token contract minting an initial ${initialSupply} tokens to Alice...`); - const contract = await TokenContract.deploy(aztecRpc).send().deployed(); + const contract = await TokenContract.deploy(aztecRpc, alice).send().deployed(); // Create the contract abstraction and link to Alice's wallet for future signing const tokenContractAlice = await TokenContract.at(contract.address, accounts[0]); - // Initialize the contract and add Bob as a minter - await tokenContractAlice.methods._initialize(alice).send().wait(); + // add Bob as a minter await tokenContractAlice.methods.set_minter(bob, true).send().wait(); logger(`Contract successfully deployed at address ${contract.address.toShortString()}`); diff --git a/yarn-project/end-to-end/src/e2e_token_contract.test.ts b/yarn-project/end-to-end/src/e2e_token_contract.test.ts index 53536987de88..a0969a881e59 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract.test.ts @@ -35,7 +35,7 @@ describe('e2e_token_contract', () => { beforeAll(async () => { ({ teardown, logger, wallets, accounts } = await setup(3)); - asset = await TokenContract.deploy(wallets[0]).send().deployed(); + asset = await TokenContract.deploy(wallets[0], wallets[0].getCompleteAddress()).send().deployed(); logger(`Token deployed to ${asset.address}`); tokenSim = new TokenSimulator( asset, @@ -43,7 +43,6 @@ describe('e2e_token_contract', () => { accounts.map(a => a.address), ); - await asset.methods._initialize(accounts[0].address).send().wait(); expect(await asset.methods.admin().view()).toBe(accounts[0].address.toBigInt()); asset.abi.functions.forEach(fn => { diff --git a/yarn-project/end-to-end/src/fixtures/utils.ts b/yarn-project/end-to-end/src/fixtures/utils.ts index 34725dfc973a..8b84f18d8b97 100644 --- a/yarn-project/end-to-end/src/fixtures/utils.ts +++ b/yarn-project/end-to-end/src/fixtures/utils.ts @@ -400,7 +400,7 @@ export async function deployAndInitializeStandardizedTokenAndBridgeContracts( }); // deploy l2 token - const deployTx = TokenContract.deploy(wallet).send(); + const deployTx = TokenContract.deploy(wallet, wallet.getCompleteAddress()).send(); // deploy l2 token bridge and attach to the portal const bridgeTx = TokenBridgeContract.deploy(wallet).send({ @@ -419,16 +419,9 @@ export async function deployAndInitializeStandardizedTokenAndBridgeContracts( await bridge.attach(tokenPortalAddress); const bridgeAddress = bridge.address.toString() as `0x${string}`; - // initialize l2 token - const initializeTx = token.methods._initialize(owner).send(); - // initialize bridge const initializeBridgeTx = bridge.methods._initialize(token.address).send(); - // now we wait for the txs to be mined. This way we send all tx in the same rollup. - const initializeReceipt = await initializeTx.wait(); - if (initializeReceipt.status !== TxStatus.MINED) - throw new Error(`Initialize token tx status is ${initializeReceipt.status}`); if ((await token.methods.admin().view()) !== owner.toBigInt()) throw new Error(`Token admin is not ${owner}`); const initializeBridgeReceipt = await initializeBridgeTx.wait(); diff --git a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts index 161454e4afa7..63dccfaf942e 100644 --- a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts +++ b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts @@ -31,8 +31,7 @@ describe('guides/dapp/testing', () => { // docs:end:in-proc-sandbox owner = await createAccount(rpc); recipient = await createAccount(rpc); - token = await TokenContract.deploy(owner).send().deployed(); - await token.methods._initialize(owner.getAddress()).send().wait(); + token = await TokenContract.deploy(owner, owner.getCompleteAddress()).send().deployed(); }, 60_000); // docs:start:stop-in-proc-sandbox @@ -67,8 +66,7 @@ describe('guides/dapp/testing', () => { rpc = createAztecRpcClient(SANDBOX_URL); owner = await createAccount(rpc); recipient = await createAccount(rpc); - token = await TokenContract.deploy(owner).send().deployed(); - await token.methods._initialize(owner.getAddress()).send().wait(); + token = await TokenContract.deploy(owner, owner.getCompleteAddress()).send().deployed(); }, 30_000); it('increases recipient funds on mint', async () => { @@ -92,8 +90,7 @@ describe('guides/dapp/testing', () => { // docs:start:use-existing-wallets rpc = createAztecRpcClient(SANDBOX_URL); [owner, recipient] = await getSandboxAccountsWallets(rpc); - token = await TokenContract.deploy(owner).send().deployed(); - await token.methods._initialize(owner.getAddress()).send().wait(); + token = await TokenContract.deploy(owner, owner.getCompleteAddress()).send().deployed(); // docs:end:use-existing-wallets }, 30_000); @@ -143,8 +140,7 @@ describe('guides/dapp/testing', () => { owner = await createAccount(rpc); recipient = await createAccount(rpc); testContract = await TestContract.deploy(owner).send().deployed(); - token = await TokenContract.deploy(owner).send().deployed(); - await token.methods._initialize(owner.getAddress()).send().wait(); + token = await TokenContract.deploy(owner, owner.getCompleteAddress()).send().deployed(); const secret = Fr.random(); const secretHash = await computeMessageSecretHash(secret); await token.methods.mint_private(100n, secretHash).send().wait(); diff --git a/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts b/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts index 8930f604edad..9e1afbf30d99 100644 --- a/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts +++ b/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts @@ -60,9 +60,8 @@ describe('guides/writing_an_account_contract', () => { logger(`Deployed account contract at ${address}`); // docs:start:account-contract-works - const token = await TokenContract.deploy(wallet).send().deployed(); + const token = await TokenContract.deploy(wallet, wallet.getCompleteAddress()).send().deployed(); logger(`Deployed token contract at ${token.address}`); - await token.methods._initialize({ address }).send().wait(); const secret = Fr.random(); const secretHash = await computeMessageSecretHash(secret); diff --git a/yarn-project/end-to-end/src/sample-dapp/deploy.mjs b/yarn-project/end-to-end/src/sample-dapp/deploy.mjs index b620819eb331..7f69391775a2 100644 --- a/yarn-project/end-to-end/src/sample-dapp/deploy.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/deploy.mjs @@ -11,8 +11,7 @@ async function main() { const client = createAztecRpcClient(SANDBOX_URL); const [owner] = await getSandboxAccountsWallets(client); - const token = await Contract.deploy(client, TokenContractAbi, []).send().deployed(); - await token.withWallet(owner).methods._initialize(owner.getAddress()).send().wait(); + const token = await Contract.deploy(client, TokenContractAbi, [owner.getCompleteAddress()]).send().deployed(); console.log(`Token deployed at ${token.address.toString()}`); diff --git a/yarn-project/end-to-end/src/sample-dapp/index.test.mjs b/yarn-project/end-to-end/src/sample-dapp/index.test.mjs index 1f6955497872..d4f17874cb93 100644 --- a/yarn-project/end-to-end/src/sample-dapp/index.test.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/index.test.mjs @@ -10,8 +10,7 @@ describe('token', () => { owner = await createAccount(rpc); recipient = await createAccount(rpc); - token = await Contract.deploy(owner, TokenContractAbi, []).send().deployed(); - await token.methods._initialize(owner.getAddress()).send().wait(); + token = await Contract.deploy(owner, TokenContractAbi, [owner.getCompleteAddress()]).send().deployed(); const initialBalance = 20n; const secret = Fr.random(); diff --git a/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr index 3c0c669bb683..ecb398d3b9de 100644 --- a/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr @@ -118,9 +118,9 @@ contract Token { // docs:start:constructor #[aztec(private)] - fn constructor() { + fn constructor(admin: AztecAddress) { let selector = compute_selector("_initialize((Field))"); - let _callStackItem = context.call_public_function(context.this_address(), selector, [context.msg_sender()]); + let _callStackItem = context.call_public_function(context.this_address(), selector, admin.serialize()); } // docs:end:constructor