Web3JS
Wallet
Wallet capabilities from the Web3.js plugin for ZKsync
The Web3.js plugin for ZKsync exposes a wallet that allows developers to create, manage, and use ZKsync accounts.
Create a wallet
Once the Web3.js plugin for ZKsync has been registered, it can be used to create a wallet.
import { Web3 } from "web3";
import {
types,
Web3ZKsyncL2,
ZKsyncPlugin,
ZKsyncWallet,
} from "web3-plugin-zksync";
async function main() {
const web3: Web3 = new Web3(/* optional L1 provider */);
web3.registerPlugin(
new ZKsyncPlugin(
Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia),
),
);
const zksync: ZKsyncPlugin = web3.ZKsync;
const PRIVATE_KEY: string = "<PRIVATE_KEY>";
const wallet: ZKsyncWallet = new zksync.Wallet(PRIVATE_KEY);
console.log("Wallet address:", await wallet.getAddress());
}
main()
.then(() => console.log("✅ Script executed successfully"))
.catch((error) => console.error(`❌ Error executing script: ${error}`));
Deposit
The ZKsyncWallet.deposit
method is
used to transfer ETH or any ERC-20 token from the wallet's L1 account to the target account on the ZKsync Era network.
import { TransactionReceipt, utils, Web3 } from "web3";
import {
constants,
types,
Web3ZKsyncL2,
ZKsyncPlugin,
ZKsyncWallet,
} from "web3-plugin-zksync";
async function main() {
const web3: Web3 = new Web3("https://rpc.sepolia.org");
web3.registerPlugin(
new ZKsyncPlugin(
Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia),
),
);
const zksync: ZKsyncPlugin = web3.ZKsync;
const PRIVATE_KEY: string = "<PRIVATE_KEY>";
const wallet: ZKsyncWallet = new zksync.Wallet(PRIVATE_KEY);
const senderL1BeginningBalance: bigint = await wallet.getBalanceL1();
const receiver: string = "<RECEIVER_ADDRESS>";
const receiverL2BeginningBalance: bigint =
await zksync.L2.getBalance(receiver);
const tx: types.PriorityOpResponse = await wallet.deposit({
token: constants.ETH_ADDRESS,
to: receiver,
amount: utils.toWei("0.00020", "ether"),
refundRecipient: wallet.getAddress(),
});
const receipt: TransactionReceipt = await tx.waitFinalize();
console.log("Transaction Hash:", receipt.transactionHash);
console.log(
"Sender Change In L1 Balance:",
(await wallet.getBalanceL1()) - senderL1BeginningBalance,
);
console.log(
"Receiver Change In L2 Balance:",
(await zksync.L2.getBalance(receiver)) - receiverL2BeginningBalance,
);
}
main()
.then(() => console.log("✅ Script executed successfully"))
.catch((error) => console.error(`❌ Error executing script: ${error}`));
Withdraw
The ZKsyncWallet.withdraw
method
and
ZKsyncWallet.finalizeWithdrawal
method
are used to transfer ETH or any ERC-20 token from the wallet's account on the ZKsync Era network to the target account
on the L1 network.
import { TransactionReceipt, utils, Web3 } from "web3";
import {
constants,
types,
Web3ZKsyncL2,
ZKsyncPlugin,
ZKsyncWallet,
} from "web3-plugin-zksync";
async function main() {
const web3: Web3 = new Web3("https://rpc.sepolia.org");
web3.registerPlugin(
new ZKsyncPlugin(
Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia),
),
);
const zksync: ZKsyncPlugin = web3.ZKsync;
const PRIVATE_KEY: string = "<PRIVATE_KEY>";
const wallet: ZKsyncWallet = new zksync.Wallet(PRIVATE_KEY);
const senderL2BeginningBalance: bigint = await wallet.getBalance();
const receiver: string = "<RECEIVER_ADDRESS>";
const receiverL1BeginningBalance: bigint =
await web3.eth.getBalance(receiver);
const withdrawTx: types.PriorityOpResponse = await wallet.withdraw({
token: constants.ETH_ADDRESS,
to: receiver,
amount: utils.toWei("0.00020", "ether"),
});
const receipt: TransactionReceipt = await withdrawTx.waitFinalize();
console.log("Transaction Hash:", receipt.transactionHash);
console.log(
"[Before Finalize Withdrawal] Sender Change In L2 Balance:",
(await wallet.getBalance()) - senderL2BeginningBalance,
);
console.log(
"[Before Finalize Withdrawal] Receiver Change In L1 Balance:",
(await web3.eth.getBalance(receiver)) - receiverL1BeginningBalance,
);
await wallet.finalizeWithdrawal(receipt.transactionHash);
console.log(
"[After Finalize Withdrawal] Sender Change In L2 Balance:",
(await wallet.getBalance()) - senderL2BeginningBalance,
);
console.log(
"[After Finalize Withdrawal] Receiver Change In L1 Balance:",
(await web3.eth.getBalance(receiver)) - receiverL1BeginningBalance,
);
}
main()
.then(() => console.log("✅ Script executed successfully"))
.catch((error) => console.error(`❌ Error executing script: ${error}`));