From d690b1c493bde11347e655dd30ff26e1b4d64316 Mon Sep 17 00:00:00 2001 From: Danijel Radakovic <129277218+danijelTxFusion@users.noreply.github.com> Date: Fri, 27 Oct 2023 12:19:52 +0200 Subject: [PATCH] fix: updates for v0.2.1 version (#763) Co-authored-by: Dennis <10233439+idea404@users.noreply.github.com> --- docs/.vuepress/sidebar/en.ts | 4 + docs/api/js/zksync2-js/accounts.md | 23 +++ docs/api/js/zksync2-js/examples/create.md | 125 +++++++++++++++++ docs/api/js/zksync2-js/examples/create2.md | 131 ++++++++++++++++++ .../custom-paymaster/deploy-account.md | 72 ++++++++++ .../examples/custom-paymaster/deploy-token.md | 78 +++++++++++ docs/api/js/zksync2-js/paymaster-utils.md | 7 +- docs/api/js/zksync2-js/utils.md | 16 +++ 8 files changed, 455 insertions(+), 1 deletion(-) create mode 100644 docs/api/js/zksync2-js/examples/create.md create mode 100644 docs/api/js/zksync2-js/examples/create2.md create mode 100644 docs/api/js/zksync2-js/examples/custom-paymaster/deploy-account.md create mode 100644 docs/api/js/zksync2-js/examples/custom-paymaster/deploy-token.md diff --git a/docs/.vuepress/sidebar/en.ts b/docs/.vuepress/sidebar/en.ts index f4a530d1cb..9c888b9a63 100644 --- a/docs/.vuepress/sidebar/en.ts +++ b/docs/.vuepress/sidebar/en.ts @@ -127,6 +127,10 @@ export const enSidebar = sidebar({ "/api/js/zksync2-js/examples/transfer", "/api/js/zksync2-js/examples/withdraw", "/api/js/zksync2-js/examples/get-confirmed-tokens", + "/api/js/zksync2-js/examples/create", + "/api/js/zksync2-js/examples/create2", + "/api/js/zksync2-js/examples/custom-paymaster/deploy-token", + "/api/js/zksync2-js/examples/custom-paymaster/deploy-account", "/api/js/zksync2-js/examples/custom-paymaster/use-paymaster", ], }, diff --git a/docs/api/js/zksync2-js/accounts.md b/docs/api/js/zksync2-js/accounts.md index b8f42d9409..af71692ea2 100644 --- a/docs/api/js/zksync2-js/accounts.md +++ b/docs/api/js/zksync2-js/accounts.md @@ -395,6 +395,29 @@ const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider); console.log(`Nonce: ${await wallet.getNonce()}`); ``` +### getDeploymentNonce + +Returns account's deployment nonce number. + +```ts +async getDeploymentNonce(): Promise +``` + +#### Example + +```ts +import { Wallet, Provider, utils } from "zksync2-js"; +import { ethers } from "ethers"; + +const PRIVATE_KEY = ""; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const ethProvider = ethers.getDefaultProvider("goerli"); +const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider); + +console.log(`Nonce: ${await wallet.getDeploymentNonce()}`); +``` + ### ethWallet You can get an `ethers.Wallet` object with the same private key with `ethWallet()` method. diff --git a/docs/api/js/zksync2-js/examples/create.md b/docs/api/js/zksync2-js/examples/create.md new file mode 100644 index 0000000000..439174ba01 --- /dev/null +++ b/docs/api/js/zksync2-js/examples/create.md @@ -0,0 +1,125 @@ +--- +head: + - - meta + - name: "twitter:title" + content: JS SDK Create Example | zkSync Era Docs +--- + +# Deploy contract with CREATE opcode + +With zkSync Era contract can be deployed using the CREATE by simply building the contract into a binary format and deploying it to the +zkSync Era network. + +- [Storage](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/storage/Storage.sol): Contract without constructor. +- [Incrementer](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/incrementer/Incrementer.sol): Contract with constructor. +- [Demo](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/demo/Demo.sol): Contract that has a dependency on + [Foo](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/demo/Foo.sol) contract. + +There is a [user guide](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/README.md) on how to compile Solidity smart contracts using `zksolc` +compiler. `zksolc` compiler generates a `*.zbin` and a `combined.json` file that contains the bytecode and ABI of a smart contract. +Those files are used in the following examples. + +## Deploy contract + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; +import { Contract, Typed } from "ethers"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +async function main() { + const conf = require("../../solidity/storage/build/combined.json"); + const abi = conf.contracts["Storage.sol:Storage"].abi; + const bytecode: string = conf.contracts["Storage.sol:Storage"].bin; + + const factory = new ContractFactory(abi, bytecode, wallet); + const contract = await factory.deploy(); + const contractAddress = await contract.getAddress(); + console.log(`Contract address: ${contractAddress}`); + + const storage = new Contract(contractAddress, abi, wallet); + console.log(`Value: ${await storage.get()}`); + + const tx = await storage.set(Typed.uint256(200)); + await tx.wait(); + + console.log(`Value: ${await storage.get()}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` + +## Deploy contract with constructor + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; +import { Contract } from "ethers"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +async function main() { + const conf = require("../../solidity/incrementer/build/combined.json"); + const abi = conf.contracts["Incrementer.sol:Incrementer"].abi; + const bytecode: string = conf.contracts["Incrementer.sol:Incrementer"].bin; + + const factory = new ContractFactory(abi, bytecode, wallet); + const contract = await factory.deploy(2); + const contractAddress = await contract.getAddress(); + console.log(`Contract address: ${contractAddress}`); + + const incrementer = new Contract(contractAddress, abi, wallet); + console.log(`Value before Increment method execution: ${await incrementer.get()}`); + + const tx = await incrementer.increment(); + await tx.wait(); + + console.log(`Value after Increment method execution: ${await incrementer.get()}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` + +## Deploy contract with dependencies + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; +import { Contract } from "ethers"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +async function main() { + const conf = require("../../solidity/demo/build/combined.json"); + const abi = conf.contracts["Demo.sol:Demo"].abi; + const bytecode: string = conf.contracts["Demo.sol:Demo"].bin; + + const factory = new ContractFactory(abi, bytecode, wallet); + const contract = await factory.deploy({ + customData: { factoryDeps: [conf.contracts["Foo.sol:Foo"].bin] }, + }); + const contractAddress = await contract.getAddress(); + console.log(`Contract address: ${contractAddress}`); + + const demo = new Contract(contractAddress, abi, wallet); + console.log(`Value: ${await demo.getFooName()}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` diff --git a/docs/api/js/zksync2-js/examples/create2.md b/docs/api/js/zksync2-js/examples/create2.md new file mode 100644 index 0000000000..45bc907fcd --- /dev/null +++ b/docs/api/js/zksync2-js/examples/create2.md @@ -0,0 +1,131 @@ +--- +head: + - - meta + - name: "twitter:title" + content: JS SDK Create2 Example | zkSync Era Docs +--- + +# Deploy contract with CREATE2 opcode + +With zkSync Era contract can be deployed using the CREATE2 by simply building the contract into a binary format and deploying it to the +zkSync Era network. + +- [Storage](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/storage/Storage.sol): Contract without constructor. +- [Incrementer](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/incrementer/Incrementer.sol): Contract with constructor. +- [Demo](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/demo/Demo.sol): Contract that has a dependency on + [Foo](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/demo/Foo.sol) contract. + +There is a [user guide](https://github.com/zksync-sdk/zksync2-examples/blob/main/solidity/README.md) on how to compile Solidity smart contracts using `zksolc` +compiler. `zksolc` compiler generates a `*.zbin` and a `combined.json` file that contains the bytecode and ABI of a smart contract. + +## Deploy contract + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; +import { ethers, Contract, Typed } from "ethers"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +async function main() { + const conf = require("../../solidity/storage/build/combined.json"); + const abi = conf.contracts["Storage.sol:Storage"].abi; + const bytecode: string = conf.contracts["Storage.sol:Storage"].bin; + + const factory = new ContractFactory(abi, bytecode, wallet, "create2"); + const contract = await factory.deploy({ + customData: { salt: ethers.hexlify(ethers.randomBytes(32)) }, + }); + const contractAddress = await contract.getAddress(); + console.log(`Contract address: ${contractAddress}`); + + const storage = new Contract(contractAddress, abi, wallet); + console.log(`Value: ${await storage.get()}`); + + const tx = await storage.set(Typed.uint256(200)); + await tx.wait(); + + console.log(`Value: ${await storage.get()}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` + +## Deploy contract with constructor + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; +import { Contract, ethers } from "ethers"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +async function main() { + const conf = require("../../solidity/incrementer/build/combined.json"); + const abi = conf.contracts["Incrementer.sol:Incrementer"].abi; + const bytecode: string = conf.contracts["Incrementer.sol:Incrementer"].bin; + + const factory = new ContractFactory(abi, bytecode, wallet, "create2"); + const contract = await factory.deploy(2, { + customData: { salt: ethers.hexlify(ethers.randomBytes(32)) }, + }); + const contractAddress = await contract.getAddress(); + console.log(`Contract address: ${contractAddress}`); + + const incrementer = new Contract(contractAddress, abi, wallet); + console.log(`Value before Increment method execution: ${await incrementer.get()}`); + + const tx = await incrementer.increment(); + await tx.wait(); + + console.log(`Value after Increment method execution: ${await incrementer.get()}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` + +## Deploy contract with dependencies + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; +import { Contract, ethers } from "ethers"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +async function main() { + const conf = require("../../solidity/demo/build/combined.json"); + const abi = conf.contracts["Demo.sol:Demo"].abi; + const bytecode: string = conf.contracts["Demo.sol:Demo"].bin; + + const factory = new ContractFactory(abi, bytecode, wallet, "create2"); + const contract = await factory.deploy({ + customData: { + salt: ethers.hexlify(ethers.randomBytes(32)), + factoryDeps: [conf.contracts["Foo.sol:Foo"].bin], + }, + }); + const contractAddress = await contract.getAddress(); + console.log(`Contract address: ${contractAddress}`); + + const demo = new Contract(contractAddress, abi, wallet); + console.log(`Value: ${await demo.getFooName()}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` diff --git a/docs/api/js/zksync2-js/examples/custom-paymaster/deploy-account.md b/docs/api/js/zksync2-js/examples/custom-paymaster/deploy-account.md new file mode 100644 index 0000000000..d1e4700a02 --- /dev/null +++ b/docs/api/js/zksync2-js/examples/custom-paymaster/deploy-account.md @@ -0,0 +1,72 @@ +--- +head: + - - meta + - name: "twitter:title" + content: JS SDK Deploy Paymaster Example | zkSync Era Docs +--- + +# Deploy paymaster + +Deploy the paymaster account, which will receive the token deployed in the [previous](deploy-token.md) step and pay the fee in ETH for the account that +sends the token. The token address on testnet that is deployed in previous step is `0xCd9BDa1d0FC539043D4C80103bdF4f9cb108931B` + +## Deploy paymaster with CREATE opcode + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +const tokenAddress = "0xCd9BDa1d0FC539043D4C80103bdF4f9cb108931B"; + +async function main() { + const conf = require("../../solidity/custom_paymaster/paymaster/build/Paymaster.json"); + const abi = conf.abi; + const bytecode: string = conf.bytecode; + + const factory = new ContractFactory(abi, bytecode, wallet, "createAccount"); + const account = await factory.deploy(tokenAddress); + const accountAddress = await account.getAddress(); + console.log(`Account address: ${accountAddress}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` + +## Deploy paymaster with CREATE2 opcode + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; +import { ethers } from "ethers"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +const tokenAddress = "0xCd9BDa1d0FC539043D4C80103bdF4f9cb108931B"; + +async function main() { + const conf = require("../../solidity/custom_paymaster/paymaster/build/Paymaster.json"); + const abi = conf.abi; + const bytecode: string = conf.bytecode; + + const factory = new ContractFactory(abi, bytecode, wallet, "create2Account"); + const account = await factory.deploy(tokenAddress, { + customData: { salt: ethers.hexlify(ethers.randomBytes(32)) }, + }); + const accountAddress = await account.getAddress(); + console.log(`Account address: ${accountAddress}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` diff --git a/docs/api/js/zksync2-js/examples/custom-paymaster/deploy-token.md b/docs/api/js/zksync2-js/examples/custom-paymaster/deploy-token.md new file mode 100644 index 0000000000..83a77fafa2 --- /dev/null +++ b/docs/api/js/zksync2-js/examples/custom-paymaster/deploy-token.md @@ -0,0 +1,78 @@ +--- +head: + - - meta + - name: "twitter:title" + content: JS SDK Deploy Token Example | zkSync Era Docs +--- + +# Deploy token + +Deploy the token which will be used to pay transaction fee. + +## Deploy token with CREATE opcode + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; +import { Contract, Typed } from "ethers"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +async function main() { + const conf = require("../../solidity/custom_paymaster/token/build/Token.json"); + const abi = conf.abi; + const bytecode: string = conf.bytecode; + + const factory = new ContractFactory(abi, bytecode, wallet); + const contract = await factory.deploy("Crown", "Crown", 18); + const tokenAddress = await contract.getAddress(); + console.log(`Contract address: ${tokenAddress}`); + + const token = new Contract(tokenAddress, abi, wallet); + const tx = await token.mint(Typed.address(await wallet.getAddress()), Typed.uint256(10)); + await tx.wait(); + console.log(`Crown tokens: ${await wallet.getBalance(tokenAddress)}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` + +## Deploy token with CREATE2 opcode + +```ts +import { Provider, types, Wallet, ContractFactory } from "zksync2-js"; +import { Contract, ethers, Typed } from "ethers"; + +const provider = Provider.getDefaultProvider(types.Network.Goerli); +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const wallet = new Wallet(PRIVATE_KEY, provider); + +async function main() { + const conf = require("../../solidity/custom_paymaster/token/build/Token.json"); + const abi = conf.abi; + const bytecode: string = conf.bytecode; + + const factory = new ContractFactory(abi, bytecode, wallet, "create2"); + const contract = await factory.deploy("Crown", "Crown", 18, { + customData: { salt: ethers.hexlify(ethers.randomBytes(32)) }, + }); + const tokenAddress = await contract.getAddress(); + console.log(`Contract address: ${tokenAddress}`); + + const token = new Contract(tokenAddress, abi, wallet); + const tx = await token.mint(Typed.address(await wallet.getAddress()), Typed.uint256(10)); + await tx.wait(); + console.log(`Crown tokens: ${await wallet.getBalance(tokenAddress)}`); +} + +main() + .then() + .catch((error) => { + console.log(`Error: ${error}`); + }); +``` diff --git a/docs/api/js/zksync2-js/paymaster-utils.md b/docs/api/js/zksync2-js/paymaster-utils.md index f6582f5639..f47c53a13b 100644 --- a/docs/api/js/zksync2-js/paymaster-utils.md +++ b/docs/api/js/zksync2-js/paymaster-utils.md @@ -11,7 +11,12 @@ The [paymaster utilities library](https://github.com/zksync-sdk/zksync2-js/blob/ ## Contract interfaces -### `IPaymasterFlow` +### `IPaymasterFlow` Deprecated + +::: warning Deprecated + +- This method is deprecated in favor of [utils.PAYMASTER_FLOW_ABI](./utils.md#paymasterflow). + ::: Constant ABI definition for the [Paymaster Flow Interface](https://github.com/matter-labs/era-contracts/blob/36fe0fd11aeb2cfe88139e7e09d59a25366668d6/zksync/contracts/interfaces/IPaymasterFlow.sol). diff --git a/docs/api/js/zksync2-js/utils.md b/docs/api/js/zksync2-js/utils.md index 58213f8720..6ed98cfd47 100644 --- a/docs/api/js/zksync2-js/utils.md +++ b/docs/api/js/zksync2-js/utils.md @@ -73,6 +73,22 @@ export const L1_BRIDGE_ABI = new utils.Interface(require("../abi/IL1Bridge.json" export const L2_BRIDGE_ABI = new utils.Interface(require("../abi/IL2Bridge.json").abi); ``` +#### NonceHolder + +Used for managing deployment nonce. + +```ts +export const NONCE_HOLDER_ABI = new ethers.Interface(require("../abi/INonceHolder.json").abi); +``` + +#### PaymasterFlow + +Used for encoding paymaster flows. + +```ts +export const PAYMASTER_FLOW_ABI = new ethers.Interface(require("../abi/IPaymasterFlow.json").abi); +``` + #### L1 to L2 alias offset Used for applying and undoing aliases on addresses during bridging from L1 to L2.