Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
fix: updates for v0.2.1 version (#763)
Browse files Browse the repository at this point in the history
Co-authored-by: Dennis <[email protected]>
  • Loading branch information
danijelTxFusion and idea404 authored Oct 27, 2023
1 parent ab88300 commit d690b1c
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/.vuepress/sidebar/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
},
Expand Down
23 changes: 23 additions & 0 deletions docs/api/js/zksync2-js/accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<bigint>
```

#### Example

```ts
import { Wallet, Provider, utils } from "zksync2-js";
import { ethers } from "ethers";

const PRIVATE_KEY = "<WALLET_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.
Expand Down
125 changes: 125 additions & 0 deletions docs/api/js/zksync2-js/examples/create.md
Original file line number Diff line number Diff line change
@@ -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}`);
});
```
131 changes: 131 additions & 0 deletions docs/api/js/zksync2-js/examples/create2.md
Original file line number Diff line number Diff line change
@@ -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}`);
});
```
72 changes: 72 additions & 0 deletions docs/api/js/zksync2-js/examples/custom-paymaster/deploy-account.md
Original file line number Diff line number Diff line change
@@ -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}`);
});
```
Loading

0 comments on commit d690b1c

Please sign in to comment.