diff --git a/src/code_examples/sandbox_example.ts b/src/code_examples/sandbox_example.ts index f9be30a..cf6dcff 100644 --- a/src/code_examples/sandbox_example.ts +++ b/src/code_examples/sandbox_example.ts @@ -1,5 +1,4 @@ // docs:start:index -import { PrivateTokenContract } from "@aztec/noir-contracts/types"; import { AztecRPC, L2BlockL2Logs, @@ -8,41 +7,39 @@ import { createDebugLogger, getSchnorrAccount, mustSucceedFetch, -} from "@aztec/aztec.js"; +} from '@aztec/aztec.js'; +import { PrivateTokenContract } from '@aztec/noir-contracts/types'; ////////////// CREATE THE CLIENT INTERFACE AND CONTACT THE SANDBOX ////////////// -const logger = createDebugLogger("private-token"); -const sandboxUrl = "http://localhost:8080"; +const logger = createDebugLogger('private-token'); +const sandboxUrl = 'http://localhost:8080'; const aztecRpc = createAztecRpcClient(sandboxUrl, mustSucceedFetch); const nodeInfo = await aztecRpc.getNodeInfo(); -logger("Aztec Sandbox Info ", nodeInfo); +logger('Aztec Sandbox Info ', nodeInfo); // docs:end:index // docs:start:Accounts ////////////// CREATE SOME ACCOUNTS WITH SCHNORR SIGNERS ////////////// // Creates new accounts using an account contract that verifies schnorr signatures // Returns once the deployment transactions have settled -const createSchnorrAccounts = async ( - numAccounts: number, - aztecRpc: AztecRPC -) => { +const createSchnorrAccounts = async (numAccounts: number, aztecRpc: AztecRPC) => { const accountManagers = Array(numAccounts) .fill(0) - .map((x) => + .map(x => getSchnorrAccount( aztecRpc, PrivateKey.random(), // encryption private key - PrivateKey.random() // signing private key - ) + PrivateKey.random(), // signing private key + ), ); return await Promise.all( - accountManagers.map(async (x) => { + accountManagers.map(async x => { await x.waitDeploy({}); return x; - }) + }), ); }; @@ -52,17 +49,15 @@ const accounts = await createSchnorrAccounts(2, aztecRpc); ////////////// VERIFY THE ACCOUNTS WERE CREATED SUCCESSFULLY ////////////// -const [alice, bob] = ( - await Promise.all(accounts.map((x) => x.getCompleteAddress())) -).map((x) => x.address); +const [alice, bob] = (await Promise.all(accounts.map(x => x.getCompleteAddress()))).map(x => x.address); // Verify that the accounts were deployed -const registeredAccounts = await aztecRpc.getAccounts(); +const registeredAccounts = (await aztecRpc.getAccounts()).map(x => x.address); for (const [account, name] of [ - [alice, "Alice"], - [bob, "Bob"], + [alice, 'Alice'], + [bob, 'Bob'], ] as const) { - if (registeredAccounts.find((acc) => acc.equals(account))) { + if (registeredAccounts.find(acc => acc.equals(account))) { logger(`Created ${name}'s account at ${account.toShortString()}`); continue; } @@ -76,23 +71,19 @@ for (const [account, name] of [ // Deploy a private token contract, create a contract abstraction object and link it to the owner's wallet // The contract's constructor takes 2 arguments, the initial supply and the owner of that initial supply const initialSupply = 1_000_000; -logger( - `Deploying private token contract minting an initial ${initialSupply} tokens to Alice...` -); +logger(`Deploying private token contract minting an initial ${initialSupply} tokens to Alice...`); const tokenContractTx = PrivateTokenContract.deploy( aztecRpc, initialSupply, // the initial supply - alice // the owner of the initial supply + alice, // the owner of the initial supply ).send(); // wait for the tx to settle await tokenContractTx.isMined(); const receipt = await tokenContractTx.getReceipt(); logger(`Transaction status is ${receipt.status}`); -const contractInfo = await aztecRpc.getContractInfo(receipt.contractAddress!); -if (contractInfo) { - logger( - `Contract successfully deployed at address ${receipt.contractAddress!.toShortString()}` - ); +const contractData = await aztecRpc.getContractData(receipt.contractAddress!); +if (contractData) { + logger(`Contract successfully deployed at address ${receipt.contractAddress!.toShortString()}`); } // docs:end:Deployment // docs:start:Logs @@ -105,7 +96,7 @@ const viewUnencryptedLogs = async () => { logger(`Retrieving unencrypted logs for block ${lastBlock}`); const logs = await aztecRpc.getUnencryptedLogs(lastBlock, 1); const unrolledLogs = L2BlockL2Logs.unrollLogs(logs); - const asciiLogs = unrolledLogs.map((log) => log.toString("ascii")); + const asciiLogs = unrolledLogs.map(log => log.toString('ascii')); logger(`Emitted logs: `, asciiLogs); }; await viewUnencryptedLogs(); @@ -116,28 +107,16 @@ await viewUnencryptedLogs(); ////////////// QUERYING THE TOKEN BALANCE FOR EACH ACCOUNT ////////////// // Create the contract abstraction and link to Alice's wallet for future signing -const tokenContractAlice = await PrivateTokenContract.create( - receipt.contractAddress!, - await accounts[0].getWallet() -); +const tokenContractAlice = await PrivateTokenContract.create(receipt.contractAddress!, await accounts[0].getWallet()); // Bob wants to mint some funds, the contract is already deployed, create an abstraction and link it his wallet -const tokenContractBob = await PrivateTokenContract.create( - receipt.contractAddress!, - await accounts[1].getWallet() -); +const tokenContractBob = await PrivateTokenContract.create(receipt.contractAddress!, await accounts[1].getWallet()); const checkBalances = async () => { // Check Alice's balance - logger( - `Alice's balance ${await tokenContractAlice.methods - .getBalance(alice) - .view()}` - ); + logger(`Alice's balance ${await tokenContractAlice.methods.getBalance(alice).view()}`); // Check Bob's balance - logger( - `Bob's balance ${await tokenContractBob.methods.getBalance(bob).view()}` - ); + logger(`Bob's balance ${await tokenContractBob.methods.getBalance(bob).view()}`); }; // Check the initial balances await checkBalances(); @@ -148,10 +127,7 @@ await checkBalances(); // We will now transfer tokens from ALice to Bob const transferQuantity = 543; logger(`Transferring ${transferQuantity} tokens from Alice to Bob...`); -await tokenContractAlice.methods - .transfer(transferQuantity, alice, bob) - .send() - .wait(); +await tokenContractAlice.methods.transfer(transferQuantity, alice, bob).send().wait(); // See if any logs were emitted await viewUnencryptedLogs();