-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf0f199
commit d6cccd1
Showing
70 changed files
with
23,210 additions
and
19 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
# zkSync Era SDKs | ||
|
||
zkSync Era SDKs empower developers to seamlessly interact with the zkSync Era network, | ||
a high-performance scaling solution for Ethereum. | ||
|
||
These SDKs, available in various programming languages, streamline the process of building decentralized applications | ||
(dApps) that leverage zkSync Era's benefits, including: | ||
|
||
- **Scalability**: Massively reduced transaction fees and faster confirmation times compared to the Ethereum. | ||
- **Security**: Inherits security from the underlying Ethereum blockchain. | ||
- **Enhanced User Experience**: Enables smoother and more cost-effective dApp interactions for users. | ||
- **Paymaster Support**: Enables flexible transaction fee payment strategies: | ||
- Third-party services can cover fees for users, fostering wider adoption. | ||
- Gasless transactions become possible, improving user experience. | ||
- **Account Abstraction**: | ||
- Enhances security and privacy by separating user accounts from keys. | ||
- Improves developer experience by simplifying account management and enabling features | ||
like multi-signature wallets and on-chain governance. | ||
|
||
![my image](/images/sdk/test.png) |
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
# Getting Started | ||
|
||
This is a short introduction to `zksync-ethers` SDK, but covers many of the most common operations that developers | ||
require and provides a starting point for those newer to zkSync Era. | ||
|
||
## Getting zksync-ethers | ||
|
||
### Prerequisites | ||
|
||
- Node: >=v18 ([installation guide](https://nodejs.org/en/download/package-manager)) | ||
|
||
In order to install SDK for zkSync Era, run the command below in your terminal. | ||
|
||
```bash | ||
yarn add zksync-ethers@5 | ||
yarn add ethers@5 # ethers is a peer dependency of zksync-ethers | ||
``` | ||
|
||
## Overview | ||
|
||
While most of the existing SDKs should work out of the box, deploying smart contracts or using unique zkSync features, | ||
like account abstraction, requires providing additional fields to those that Ethereum transactions have by default. | ||
|
||
To begin, it is useful to have a basic understanding of the types of objects available and what they are responsible | ||
for, at a high level: | ||
|
||
- `Provider` provides connection to the zkSync Era blockchain, which allows querying the blockchain state, such as | ||
account, block or transaction details, querying event logs or evaluating read-only code using call. Additionally, | ||
the client facilitates writing to the blockchain by sending transactions. | ||
- `Wallet` wraps all operations that interact with an account. An account generally has a private key, which can be | ||
used to sign a variety of types of payloads. It provides easy usage of the most common features. | ||
|
||
## Examples | ||
|
||
The following examples could be easily run by writing the code snippets in a file and executing them as follows: | ||
|
||
```shell | ||
npx ts-node src/zksync.ts | ||
``` | ||
|
||
Connect to the zkSync Era network: | ||
|
||
```ts | ||
import { Provider, utils, types } from "zksync-ethers"; | ||
import { ethers } from "ethers"; | ||
|
||
const provider = Provider.getDefaultProvider(types.Network.Sepolia); | ||
const ethProvider = ethers.getDefaultProvider("sepolia"); | ||
``` | ||
|
||
Get the network (helper function [toJSON](./providers.md#tojson)): | ||
|
||
```ts | ||
console.log(`Network: ${toJSON(await provider.getNetwork())}`); | ||
``` | ||
|
||
Get the latest block number: | ||
|
||
```ts | ||
console.log(`Block number: ${await provider.getBlockNumber()}`); | ||
``` | ||
|
||
Get the block by hash (helper function [toJSON](./providers.md#tojson)): | ||
|
||
```ts | ||
console.log(`Block: ${toJSON(await provider.getBlock("b472c070c9e121ba42702f6c322b7b266e287a4d8b5fa426ed265b105430c397", true))}`); | ||
``` | ||
|
||
Get the transaction by hash (helper function [toJSON](./providers.md#tojson)): | ||
|
||
```ts | ||
console.log(`Block: ${toJSON(await provider.getTransaction("0x9af27afed9a4dd018c0625ea1368afb8ba08e4cfb69b3e76dfb8521c8a87ecfc"))}`); | ||
``` |
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,121 @@ | ||
# zkSync Era Features | ||
|
||
While zkSync is mostly Web3-compatible, it has some differences compared to Ethereum. The major of those are: | ||
|
||
- Account abstraction support (accounts may have near-arbitrary validation logic, and also paymaster | ||
support is enabled). | ||
- Deployment transactions require the contracts' bytecode to be passed in a separate field. | ||
- The fee system is somewhat different. | ||
|
||
These require us to extend standard Ethereum transactions with new custom fields. Such extended transactions | ||
are called EIP712 transactions since [EIP712](https://eips.ethereum.org/EIPS/eip-712) is used to sign them. | ||
You can look at the internal structure of the EIP712 transactions [here](https://docs.zksync.io/zk-stack/concepts/transaction-lifecycle.md#eip-712-0x71). | ||
|
||
This document will focus solely on how to pass these arguments to the SDK. | ||
|
||
## Overrides | ||
|
||
`ethers.js` has a notion of overrides. For any on-chain transaction, `ethers.js` finds the optimal | ||
`gasPrice`, `gasLimit`, `nonce`, and other important fields under the hood. But sometimes, you may | ||
have a need to explicitly provide these values (you want to set a smaller `gasPrice` for instance, | ||
or sign a transaction with future `nonce`). | ||
|
||
In this case, you can provide an `Overrides` object as the last parameter. | ||
There you can supply fields like `gasPrice`, `gasLimit`, `nonce` etc. | ||
|
||
In order to make the SDK as flexible as possible, `zksync-ethers` uses `customData` object in the | ||
overrides to supply zkSync-specific fields. To supply zkSync-specific fields, you | ||
need to pass the following override: | ||
|
||
```typescript | ||
{ | ||
overrides: { | ||
customData: { | ||
gasPerPubdata?: BigNumberish; | ||
factoryDeps?: BytesLike[]; | ||
customSignature?: BytesLike; | ||
paymasterParams?: { | ||
paymaster: Address; | ||
paymasterInput: BytesLike; | ||
}; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Please note once again: everything that is inside `customData` in `overrides` is related to zkSync(L2 gas, etc.). | ||
|
||
Examples: | ||
|
||
Override to deploy a contract with bytecode `0xcde...12` and enforce that the operator will not charge more than | ||
`100` L2 gas per published bytes on Layer 1: | ||
|
||
```typescript | ||
{ | ||
customData: { | ||
gasPerPubdata: "100", | ||
factoryDeps: ["0xcde...12"], | ||
} | ||
} | ||
``` | ||
|
||
Use custom signature `0x123456` for account, while using paymaster with address | ||
`0x8e1DC7E4Bb15927E76a854a92Bf8053761501fdC` and paymaster input `0x8c5a3445`: | ||
|
||
```typescript | ||
{ | ||
customData: { | ||
customSignature: "0x123456", | ||
paymasterParams: { | ||
paymaster: "0x8e1DC7E4Bb15927E76a854a92Bf8053761501fdC", | ||
paymasterInput: "0x8c5a3445" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Encoding paymaster params | ||
|
||
While the paymaster feature by itself does not impose any limitations on values of the | ||
`paymasterInput`, the Matter Labs team endorses certain types of | ||
[paymaster flows](https://docs.zksync.io/build/developer-reference/account-abstraction.md#built-in-paymaster-flows) | ||
that are processable by EOAs. | ||
|
||
zkSync SDK provides a utility method that can be used to get the correctly formed `paymasterParams` object: | ||
[getPaymasterParams](./paymaster-utils.md#getpaymasterparams). | ||
|
||
## See in action | ||
|
||
If you want to call the method `setGreeting` of an ethers `Contract` object called `greeter`, | ||
this would look the following way, while paying fees with the | ||
[testnet paymaster](https://docs.zksync.io/build/developer-reference/account-abstraction.md#testnet-paymaster): | ||
|
||
```ts | ||
// The `setGreeting` method has a single parameter -- new greeting | ||
// We will set its value as `a new greeting`. | ||
const greeting = "a new greeting"; | ||
const tx = await greeter.populateTransaction.setGreeting(greeting); | ||
const gasPrice = await sender.provider.getGasPrice(); | ||
const gasLimit = await greeter.estimateGas.setGreeting(greeting); | ||
const fee = gasPrice.mul(gasLimit); | ||
|
||
const paymasterParams = utils.getPaymasterParams(testnetPaymaster, { | ||
type: "ApprovalBased", | ||
token, | ||
minimalAllowance: fee, | ||
innerInput: new Uint8Array(), | ||
}); | ||
const sentTx = await sender.sendTransaction({ | ||
...tx, | ||
maxFeePerGas: gasPrice, | ||
maxPriorityFeePerGas: BigNumber.from(0), | ||
gasLimit, | ||
customData: { | ||
gasPerPubdata: utils.DEFAULT_GAS_PER_PUBDATA_LIMIT, | ||
paymasterParams, | ||
}, | ||
}); | ||
``` | ||
|
||
You can also check out our [tutorial](https://docs.zksync.io/build/quick-start/hello-world.md) on the | ||
full-fledged mini-dApp, where users can choose token to pay the fee. |
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,16 @@ | ||
# Front-end Integration | ||
|
||
This section describes how to make the most of zkSync to provide the best UX. | ||
|
||
## Going to production right away | ||
|
||
If your front-end code does not deploy new smart contracts, then no changes to the codebase are required! | ||
All the existing SDKs/infrastructure will work out-of-box. | ||
|
||
## Enabling zkSync features | ||
|
||
If you want to deploy smart contracts or enable advanced zkSync features, like account abstraction, | ||
then you need to use the `zksync-ethers` library. You can read about the basics [here](./features.md). | ||
|
||
If you want to see some code, check out our basic [tutorial](https://docs.zksync.io/build/quick-start/hello-world.md) | ||
for full mini-dApp. |
Oops, something went wrong.