账户:概述


账户:概述

zksync-web3输出四个可以在zkSync上签署交易的类。

  • Wallet类是ethers.Wallet的扩容,具有额外的zkSync功能。
  • EIP712Signer类,用于签署EIP712类型的zkSync交易。
  • SignerL1Signer类,应该用于浏览器集成。

Wallet.

从一个私钥创建钱包

就像ethers.Wallet一样,zksync-web3Wallet对象可以由Ethereum私钥创建。

constructor Wallet(
  privateKey: ethers.utils.BytesLike | ethers.utils.SigningKey,
  providerL2?: Provider,
  providerL1?: ethers.providers.Provider): Wallet

输入和输出

名称说明
privateKey以太坊账户的私钥。
providerL2?一个zkSync节点提供者。需要与zkSync进行互动。
providerL1?一个Ethereum节点提供者。需要与L1进行交互。
returns新的 "钱包 "对象。

Example

import * as zksync from "zksync-web3";
import { ethers } from "ethers";

const PRIVATE_KEY = "0xc8acb475bb76a4b8ee36ea4d0e516a755a17fad2e84427d5559b37b544d9ba5a";

const zkSyncProvider = new zksync.Provider("https://zksync2-testnet.zksync.dev");
const ethereumProvider = ethers.getDefaultProvider("goerli");
const wallet = new zksync.Wallet(PRIVATE_KEY, zkSyncProvider, ethereumProvider);

创建钱包实例的其他方法

Wallet类支持ethers.Wallet中所有用于创建钱包的方法,例如,从助记符创建,从加密的JSON创建,创建一个随机钱包,等等。所有这些方法的参数与ethers.Wallet相同,所以你应该参考它的文档来了解如何使用它们。

连接到zkSync提供者

为了与zkSync网络进行交互,Wallet对象应该连接到Provider,方法是将其传递给构造函数或使用connect方法。

Wallet.connect(provider: Provider): Wallet

输入和输出

名称说明
provider一个zkSync节点提供者。
returns一个新的zkSync "钱包 "实例。

Example

import { Wallet, Provider } from "zksync-web3";

const unconnectedWallet = new Wallet(PRIVATE_KEY);

const provider = new Provider("https://zksync2-testnet.zksync.dev");
const wallet = unconnectedWallet.connect(provider);

连接到Ethereum提供者

为了执行L1操作,Wallet对象需要连接到一个ethers.providers.Provider对象。

Wallet.connectToL1(provider: ethers.providers.Provider): Wallet

Inputs and outputs

NameDescription
provider一个Ethereum节点提供者。
returns一个新的zkSync "钱包 "实例,与L1 "供应商 "相连。

Example

import { Wallet } from "zksync-web3";
import { ethers } from "ethers";

const unconnectedWallet = new Wallet(PRIVATE_KEY);

const ethProvider = ethers.getDefaultProvider("goerli");
const wallet = unconnectedWallet.connectToL1(ethProvider);

可以将 "connect "和 "connectToL1 "方法进行连锁。

const wallet = unconnectedWallet.connect(provider).connectToL1(ethProvider);

获取zkSync L1智能合约

async getMainContract(): Promise<IZkSync>

输入和输出

NameDescription
returnszkSync智能合约的合约封装器。

Example

import * as zksync from "zksync-web3";
import { ethers } from "ethers";
const PRIVATE_KEY = "0xc8acb475bb76a4b8ee36ea4d0e516a755a17fad2e84427d5559b37b544d9ba5a";
const zkSyncProvider = new zksync.Provider("https://zksync2-testnet.zksync.dev");
const ethereumProvider = ethers.getDefaultProvider("goerli");
const wallet = new Wallet(PRIVATE_KEY, zkSyncProvider, ethereumProvider);

const contract = await wallet.getMainContract();
console.log(contract.address);

获得代币余额

async getBalance(token?: Address, blockTag: BlockTag = 'committed'): Promise<BigNumber>

Inputs and outputs

NameDescription
token?代币的地址。默认为ETH.
blockTag?承诺,即最近处理的一个是默认选项。
returns钱包拥有的代币的数量。

Example

import * as zksync from "zksync-web3";
import { ethers } from "ethers";

const PRIVATE_KEY = "0xc8acb475bb76a4b8ee36ea4d0e516a755a17fad2e84427d5559b37b544d9ba5a";

const zkSyncProvider = new zksync.Provider("https://zksync2-testnet.zksync.dev");
const ethereumProvider = ethers.getDefaultProvider("goerli");
const wallet = new Wallet(PRIVATE_KEY, zkSyncProvider);

const USDC_L2_ADDRESS = "0x852a4599217e76aa725f0ada8bf832a1f57a8a91";

// Getting balance in USDC
console.log(await wallet.getBalance(USDC_L2_ADDRESS));

// Getting balance in ETH
console.log(await wallet.getBalance());

在L1上获得代币平衡

async getBalanceL1(token?: Address, blockTag?: ethers.providers.BlockTag): Promise<BigNumber>

输入和输出

NameDescription
token?代币的地址。默认为ETH。
blockTag?余额应该被检查的区块。最近处理的那个是默认选项。
returns钱包在以太坊上拥有的代币的数量。

Example

import * as zksync from "zksync-web3";
import { ethers } from "ethers";

const PRIVATE_KEY = "0xc8acb475bb76a4b8ee36ea4d0e516a755a17fad2e84427d5559b37b544d9ba5a";

const zkSyncProvider = new zksync.Provider("https://zksync2-testnet.zksync.dev");
const ethereumProvider = ethers.getDefaultProvider("goerli");
const wallet = new Wallet(PRIVATE_KEY, zkSyncProvider);

const USDC_ADDRESS = "0xd35CCeEAD182dcee0F148EbaC9447DA2c4D449c4";

// Getting balance in USDC
console.log(await wallet.getBalanceL1(USDC_ADDRESS));

// Getting balance in ETH
console.log(await wallet.getBalanceL1());

获得一个nonce

Wallet "还提供了 "getNonce "方法,它是getTransactionCount的一个别名。

async getNonce(blockTag?: BlockTag): Promise<number>

Inputs and outputs

NameDescription
blockTag?承诺的,即最新处理的,是默认选项。
returns账户的nonce号码。

Example

import * as zksync from "zksync-web3";
import { ethers } from "ethers";

const PRIVATE_KEY = "0xc8acb475bb76a4b8ee36ea4d0e516a755a17fad2e84427d5559b37b544d9ba5a";

const zkSyncProvider = new zksync.Provider("https://zksync2-testnet.zksync.dev");
// Note that we don't need ethereum provider to get the nonce
const wallet = new Wallet(PRIVATE_KEY, zkSyncProvider);

console.log(await wallet.getNonce());

在zkSync内部传输令牌

为了方便,"钱包 "类有 "转移 "方法,可以在同一界面内转移 "ETH "或任何 "ERC20 "令牌。

async transfer(tx: {
    to: Address;
    amount: BigNumberish;
    token?: Address;
    overrides?: ethers.CallOverrides;
}): Promise<TransactionResponse>

输入和输出

NameDescription
tx.toThe address of the recipient.
tx.amountThe amount of the token to transfer.
token?The address of the token. ETH by default.
overrides?zkSync transaction overrides. May be used to pass l2 gasLimit, gasPrice, value, etc.
returnsA TransactionResponse object

Example

import * as zksync from "zksync-web3";
import { ethers } from "ethers";

const PRIVATE_KEY = "0xc8acb475bb76a4b8ee36ea4d0e516a755a17fad2e84427d5559b37b544d9ba5a";

const zkSyncProvider = new zksync.Provider("https://zksync2-testnet.zksync.dev");
const ethereumProvider = ethers.getDefaultProvider("goerli");
const wallet = new zksync.Wallet(PRIVATE_KEY, zkSyncProvider, ethereumProvider);

const recipient = zksync.Wallet.createRandom();

// We transfer 0.01 ETH to the recipient and pay the fee in USDC
const transferHandle = wallet.transfer({
  to: recipient.address,
  amount: ethers.utils.parseEther("0.01"),
});

Initiating a withdrawal to L1

async withdraw(transaction: {
    token: Address;
    amount: BigNumberish;
    to?: Address;
    bridgeAddress?: Address;
    overrides?: ethers.CallOverrides;
}): Promise<TransactionResponse>
NameDescription
tx.toThe address of the recipient on L1.
tx.amountThe amount of the token to transfer.
token?The address of the token. ETH by default.
bridgeAddress?The address of the bridge contract to be used.
overrides?zkSync transaction overrides. May be used to pass gasLimit, gasPrice, etc.
returnsA TransactionResponse object

检索底层的 L1 钱包

你可以通过ethWallet()方法获得一个具有相同私钥的ethers.Wallet对象。

输入和输出

NameDescription
returnsAn ethers.Wallet object with the same private key.

Example

import * as zksync from "zksync-web3";
import { ethers } from "ethers";

const PRIVATE_KEY = "0xc8acb475bb76a4b8ee36ea4d0e516a755a17fad2e84427d5559b37b544d9ba5a";

const zkSyncProvider = new zksync.Provider("https://zksync2-testnet.zksync.dev");
const ethereumProvider = ethers.getDefaultProvider("goerli");
const wallet = new zksync.Wallet(PRIVATE_KEY, zkSyncProvider, ethereumProvider);

const ethWallet = wallet.ethWallet();

EIP712Signer

这个类的方法主要在内部使用。使用这个类的例子很快就会出现!

Signer

这个类将在浏览器环境中使用。构造它的最简单方法是使用Web3ProvidergetSigner方法。这个结构扩展了ethers.providers.JsonRpcSigner,所以支持它的所有可用方法。

import { Web3Provider } from "zksync-web3";

const provider = new Web3Provider(window.ethereum);
const signer = provider.getSigner();

获得代币余额

async getBalance(token?: Address, blockTag: BlockTag = 'committed'): Promise<BigNumber>

Inputs and outputs

NameDescription
token?代币的地址。默认为ETH。
blockTag?承诺的,即最近处理的,是默认选项。
returns签约人拥有的代币的数量。

Example

import { Web3Provider } from "zksync-web3";
import { ethers } from "ethers";

const provider = new Web3Provider(window.ethereum);
const signer = provider.getSigner();

const USDC_L2_ADDRESS = "0x852a4599217e76aa725f0ada8bf832a1f57a8a91";
// Getting balance in USDC
console.log(await signer.getBalance(USDC_L2_ADDRESS));

// Getting balance in ETH
console.log(await signer.getBalance());

获得一个nonce

Wallet "类也提供了 "getNonce "方法,它是getTransactionCountopen in new window的一个别名。

async getNonce(blockTag?: BlockTag): Promise<number>

Inputs and outputs

NameDescription
blockTag?The block the nonce should be got on. committed, i.e. the latest processed one is the default option.
returnsThe amount of the token the Wallet has.

Example

import { Web3Provider } from "zksync-web3";

const provider = new Web3Provider(window.ethereum);
const signer = provider.getSigner();

console.log(await signer.getNonce());

在zkSync内部转移代币

请注意,目前,与Ethereum不同,zkSync不支持原生转移,即所有交易的值域都等于0。

但是为了方便,钱包类有转移方法,可以转移任何ERC20代币。

async transfer(tx: {
    to: Address;
    amount: BigNumberish;
    token?: Address;
    overrides?: ethers.CallOverrides;
}): Promise<ethers.ContractTransaction>

输入和输出

NameDescription
tx.toThe address of the recipient.
tx.amountThe amount of the token to transfer.
token?The address of the token. ETH by default.
overrides?zkSync transaction overrides. May be used to pass L2 gasLimit, gasPrice, etc.
returnsAn ethers.ContractTransaction object.

Example

import { Wallet, Web3Provider } from "zksync-web3";
import { ethers } from "ethers";

const provider = new Web3Provider(window.ethereum);
const signer = provider.getSigner();

const recipient = Wallet.createRandom();

// We transfer 0.01 ETH to the recipient and pay the fee in USDC
const transferHandle = signer.transfer({
  to: recipient.address,
  amount: ethers.utils.parseEther("0.01"),
});

L1Signer

该类将在浏览器环境中用于在第1层进行zkSync相关的操作。该类扩展了ethers.providers.JsonRpcSigner,因此支持它的所有可用方法。

构造它的最简单的方法是来自Web3Provider对象。

import { Web3Provider, Provider, L1Signer } from "zksync-web3";

const provider = new ethers.Web3Provider(window.ethereum);
const zksyncProvider = new Provider("https://zksync2-testnet.zksync.dev");
const signer = L1Signer.from(provider.getSigner(), zksyncProvider);

Getting the zkSync L1 smart contract

async getMainContract(): Promise<Contract>

Getting bridge contracts

ERC-20桥 contract 对象。

async getL1BridgeContracts(): Promise<{
    erc20: IL1Bridge;
}>

Note

没有单独的以太桥合约,而是使用主合约。

Inputs and outputs

NameDescription
returnsContract wrapper of the zkSync smart contract.

Example

import { Web3Provider, Provider, L1Signer } from "zksync-web3";
import { ethers } from "ethers";

const provider = new ethers.Web3Provider(window.ethereum);
const zksyncProvider = new Provider("https://zksync2-testnet.zksync.dev");
const signer = L1Signer.from(provider.getSigner(), zksyncProvider);

const mainContract = await signer.getMainContract();
console.log(mainContract.address);

Getting token balance on L1

async getBalanceL1(token?: Address, blockTag?: ethers.providers.BlockTag): Promise<BigNumber>

Inputs and outputs

名称说明
token?代币的地址。默认为ETH。
blockTag?余额应该被检查的区块。最新处理的一个是默认选项。
returns`L1Signer'在Ethereum上拥有的代币的数量。

Example

import { Web3Provider, Provider, L1Signer } from "zksync-web3";
import { ethers } from "ethers";

const provider = new ethers.Web3Provider(window.ethereum);
const zksyncProvider = new Provider("https://zksync2-testnet.zksync.dev");
const signer = L1Signer.from(provider.getSigner(), zksyncProvider);

const USDC_ADDRESS = "0xd35CCeEAD182dcee0F148EbaC9447DA2c4D449c4";

// Getting balance in USDC
console.log(await signer.getBalanceL1(USDC_ADDRESS));

// Getting balance in ETH
console.log(await signer.getBalanceL1());
Last update:
Contributors: Newbee740,Stanislav Bezkorovainyi,Blessing Krofegha,AnastasiiaVashchuk,Dimitris Apostolou,Roman Brodetski,Antonio,botdad