This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 809
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: updates for v0.2.1 version (#763)
Co-authored-by: Dennis <[email protected]>
- Loading branch information
1 parent
ab88300
commit d690b1c
Showing
8 changed files
with
455 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
72
docs/api/js/zksync2-js/examples/custom-paymaster/deploy-account.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
``` |
Oops, something went wrong.