Setup
Setting up the SDK.
This explainer will frequently reference code snippets from the CLI reference repository. You can pull this repo and follow along to run the examples yourself.
If you are building a web app, check out the create react app reference repo.
The following examples are written in Typescript and assumes users will be creating keys using Metamask in a browser.
Install
yarn add @aztec/sdk
Once the SDK is installed, import it into your project. Using Typescript is highly recommended.
import { createAztecSdk } from "@aztec/sdk";
Provider Setup
You need to set up a connection to an Ethereum network and import a private key. Since this is assuming a Nodejs context there is no direct access to an Ethereum wallet.
Import EthersAdapter
from @aztec/sdk
and JsonRpcSigner
from @ethersproject/providers
.
const ethersProvider = new ethers.providers.JsonRpcProvider("http://localhost:24012/rpc") // local connection to metamask
const ethereumProvider = new EthersAdapter(ethersProvider);
SDK setup
Once the provider is set up you can create an instance of the Aztec SDK, specifying the rollup host. When working on the mainnet fork testnet, the corresponding sequencer endpoint is:
https://api.aztec.network/aztec-connect-testnet/falafel
You can connect to the Aztec sequencer that is connected to Ethereum at:
https://api.aztec.network/aztec-connect-prod/falafel
const setupSdk = async () => {
sdk = await createAztecSdk(ethereumProvider, {
serverUrl: "https://api.aztec.network/aztec-connect-testnet/falafel", // testnet
pollInterval: 1000,
memoryDb: true, // set to false to save chain data
debug: "bb:*", // print debug logs
flavour: SdkFlavour.PLAIN, // Use PLAIN with Nodejs
minConfirmation: 1, // ETH block confirmations
});
await sdk.run();
};
Debug
Run export DEBUG=bb:*
in your terminal before running a Nodejs script to also turn on the debug logs.
Browser debugging
If you have the debug
flag set to bb:*
to log output during development, make sure the browser console log level includes "verbose" to be able to see all of the output.
You can enable debugging when you create the SDK instance.
const sdk = await createAztecSdk(ethereumProvider, {
serverUrl: "https://aztec-connect-testnet-sdk.aztec.network", // mainnet fork testnet
pollInterval: 1000,
memoryDb: true, // set to false to save DB in a project file rather than memory
debug: "bb:*",
minConfirmation: 1, // ETH block confirmations
});