Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(docs): Simple e2e tests to use in docs #4596

Merged
merged 16 commits into from
Feb 15, 2024
14 changes: 13 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,6 @@ jobs:
- run:
name: "Test"
command: AVM_ENABLED=1 cond_spot_run_compose end-to-end 4 ./scripts/docker-compose.yml TEST=e2e_avm_simulator.test.ts

pxe:
docker:
- image: aztecprotocol/alpine-build-image
Expand All @@ -906,6 +905,17 @@ jobs:
- run:
name: "Test"
command: cond_spot_run_compose end-to-end 4 ./scripts/docker-compose.yml TEST=cli_docs_sandbox.test.ts

e2e-docs-examples:
docker:
- image: aztecprotocol/alpine-build-image
resource_class: small
steps:
- *checkout
- *setup_env
- run:
name: "Test"
command: AVM_ENABLED=1 cond_spot_run_compose end-to-end 4 ./scripts/docker-compose.yml TEST=docs_examples_test.ts

guides-writing-an-account-contract:
docker:
Expand Down Expand Up @@ -1342,6 +1352,7 @@ workflows:
- e2e-avm-simulator: *e2e_test
- pxe: *e2e_test
- cli-docs-sandbox: *e2e_test
- e2e-docs-examples: *e2e_test
- guides-writing-an-account-contract: *e2e_test
- guides-dapp-testing: *e2e_test
- guides-sample-dapp: *e2e_test
Expand Down Expand Up @@ -1387,6 +1398,7 @@ workflows:
- boxes-blank-react
- boxes-token
- cli-docs-sandbox
- e2e-docs-examples
- guides-writing-an-account-contract
- guides-dapp-testing
- guides-sample-dapp
Expand Down
31 changes: 25 additions & 6 deletions docs/docs/developers/aztecjs/guides/call_view_function.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,29 @@ This guide explains how to call a `view` function using [Aztec.js](../main.md).

To do this from the CLI, go [here](../../sandbox/references/cli-commands.md#calling-an-unconstrained-view-function).

```typescript
import { Contract } from "@aztec/aztec.js";
## Prerequisites

You should have a wallet to act as the caller, and a contract that has been deployed.

You can learn how to create wallets from [this guide](./create_account.md).

You can learn how to deploy a contract [here](./deploy_contract.md).

## Relevent imports

You will need to import this from Aztec.js:

#include_code import_contract yarn-project/end-to-end/src/docs_examples.test.ts typescript

## Define contract

Get a previously deployed contract like this:

#include_code get_contract yarn-project/end-to-end/src/docs_examples.test.ts typescript

## Call view function

Call the `view` function on the contract like this:

#include_code call_view_function yarn-project/end-to-end/src/docs_examples.test.ts typescript

const contract = await Contract.at(contractAddress, MyContractArtifact, wallet);
const balance = await contract.methods.getBalance(wallet.getAddress()).view();
console.log(`Account balance is ${balance}`);
```
29 changes: 16 additions & 13 deletions docs/docs/developers/aztecjs/guides/create_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ This guide explains how to create a new account using [Aztec.js](../main.md).

To do this from the CLI, go [here](../../sandbox/references/cli-commands.md#creating-accounts).

```typescript
import { getSchnorrAccount } from "@aztec/aztec.js";
import { GrumpkinPrivateKey } from "@aztec/circuit-types";

const encryptionPrivateKey = GrumpkinPrivateKey.random();
const signingPrivateKey = GrumpkinPrivateKey.random();
const wallet = getSchnorrAccount(
pxe,
encryptionPrivateKey,
signingPrivateKey
).waitDeploy();
console.log(`New account deployed at ${wallet.getAddress()}`);
```
## Relevant imports

You will need to import these libraries:

#include_code create_account_imports yarn-project/end-to-end/src/docs_examples.test.ts typescript

## Define arguments needed

#include_code define_account_vars yarn-project/end-to-end/src/docs_examples.test.ts typescript

## Create the wallet with these args

#include_code create_wallet yarn-project/end-to-end/src/docs_examples.test.ts typescript

Now you have a new wallet in your PXE! To learn how to use this wallet to deploy a contract, read [this guide](./deploy_contract.md).

29 changes: 19 additions & 10 deletions docs/docs/developers/aztecjs/guides/deploy_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ This guide explains how to deploy a smart contract using [Aztec.js](../main.md).

To do this from the CLI, go [here](../../sandbox/references/cli-commands.md#deploying-a-token-contract).

```typescript
import { Contract } from "@aztec/aztec.js";

const contract = await Contract.deploy(wallet, MyContractArtifact, [
...constructorArgs,
])
.send()
.deployed();
console.log(`Contract deployed at ${contract.address}`);
```
## Prerequisites

You should have a wallet to act as the deployer, and a contract artifact ready to be deployed.

You can learn how to create wallets from [this guide](./create_account.md).

You can read about contract artifacts [here](../../contracts/compiling_contracts/artifacts.md).

## Import the contract artifact

In this guide we are using a Token contract artifact. This comes from the [token contract tutorial](../../tutorials/writing_token_contract.md).

#include_code import_token_contract yarn-project/end-to-end/src/docs_examples.test.ts typescript

## Deploy contract

#include_code deploy_contract yarn-project/end-to-end/src/docs_examples.test.ts typescript

To learn how to send a transaction from Aztec.js read [this guide](./send_transaction.md). You can also call a `view` function from Aztec.js by reading [this guide](./call_view_function.md).
35 changes: 23 additions & 12 deletions docs/docs/developers/aztecjs/guides/send_transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ This guide explains how to send a transaction using [Aztec.js](../main.md).

To do this from the CLI, go [here](../../sandbox/references/cli-commands.md#sending-a-transaction).

```typescript
import { Contract } from "@aztec/aztec.js";

const contract = await Contract.at(contractAddress, MyContractArtifact, wallet);
const tx = await contract.methods
.transfer(amount, recipientAddress)
.send()
.wait();
console.log(
`Transferred ${amount} to ${recipientAddress} on block ${tx.blockNumber}`
);
```
## Prerequisites

You should have a wallet to act as the transaction sender, and a contract that has been deployed.

You can learn how to create wallets from [this guide](./create_account.md).

You can learn how to deploy a contract [here](./deploy_contract.md).

## Relevant imports

You will need to import this library:

#include_code import_contract yarn-project/end-to-end/src/docs_examples.test.ts typescript

## Define contract

Get a previously deployed contract like this:

#include_code get_contract yarn-project/end-to-end/src/docs_examples.test.ts typescript

## Call method

#include_code send_transaction yarn-project/end-to-end/src/docs_examples.test.ts typescript
46 changes: 46 additions & 0 deletions yarn-project/end-to-end/src/docs_examples.test.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't structured as a test, but I guess that's fine since it will cause CI to fail if there is an error or anything.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would still be ideal to have a test block here just saying 'tests aztec.js doc commands'

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// docs:start:create_account_imports
import { getSchnorrAccount } from '@aztec/accounts/schnorr';
import { GrumpkinScalar, createPXEClient } from '@aztec/aztec.js';
// docs:end:create_account_imports
// docs:start:import_contract
import { Contract } from '@aztec/aztec.js';
// docs:end:import_contract
// docs:start:import_token_contract
import { TokenContract, TokenContractArtifact } from '@aztec/noir-contracts.js/Token';

// docs:end:import_token_contract

// docs:start:define_account_vars
const PXE_URL = process.env.PXE_URL || 'http://localhost:8080';
const encryptionPrivateKey = GrumpkinScalar.random();
const signingPrivateKey = GrumpkinScalar.random();
const pxe = createPXEClient(PXE_URL);
// docs:end:define_account_vars

// docs:start:create_wallet
const wallet = await getSchnorrAccount(pxe, encryptionPrivateKey, signingPrivateKey).waitDeploy();
// docs:end:create_wallet

// docs:start:deploy_contract
const deployedContract = await TokenContract.deploy(
wallet, // wallet instance
wallet.getAddress(), // account
'TokenName', // constructor arg1
'TokenSymbol', // constructor arg2
18,
) // constructor arg3
.send()
.deployed();
// docs:end:deploy_contract

// docs:start:get_contract
const contract = await Contract.at(deployedContract.address, TokenContractArtifact, wallet);
// docs:end:get_contract

// docs:start:send_transaction
const _tx = await contract.methods.transfer(1, wallet).send().wait();
// docs:end:send_transaction

// docs:start:call_view_function
const _balance = await contract.methods.getBalance(wallet.getAddress()).view();
// docs:end:call_view_function
Loading