In order to provide easy access to all the features of Via network, the via-ethers JavaScript SDK was created,
which is made in a way that has an interface very similar to those of ethers. In
fact, ethers is a peer dependency of our library and most of the objects exported by via-ethers (
e.g. Wallet, Provider etc.) inherit from the corresponding ethers objects and override only the fields that need
to be changed.
While most of the existing SDKs should work out of the box, deploying smart contracts or using unique Via features, like account abstraction, requires providing additional fields to those that Ethereum transactions have by default.
The library is made in such a way that after replacing ethers with via-ethers most client apps will work out of
box.
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:
Providerprovides connection to the ZKsync 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.Walletwraps 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.
node: >= 18(installation guide)ethers: ^6.7.1
yarn add @vianetwork/via-ethers
yarn add ethers@6 # ethers is a peer dependency of via-ethersimport {Provider, utils, types} from '@vianetwork/via-ethers';
import BitcoinClient from 'bitcoin-core';
const provider = Provider.getDefaultProvider(types.Network.Testnet); // Via testnet (L2)
const providerL1 = new BitcoinClient({ // Bitcoin testnet (L1)
host: 'http://127.0.0.1:18332',
username: 'rpcuser',
password: 'rpcpassword',
wallet: 'personal',
});const blockNumber = await provider.getBlockNumber();const block = await provider.getBlock('latest');import {Wallet} from '@vianetwork/via-ethers';
import {TEST_NETWORK} from "@scure/btc-signer/src/utils";
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const L1_PRIVATE_KEY = process.env.L1_PRIVATE_KEY; // in WIF format
const L1_ADDRESS = process.env.L1_ADDRESS;
const wallet = new Wallet(
PRIVATE_KEY,
provider,
L1_PRIVATE_KEY,
L1_ADDRESS,
providerL1,
TEST_NETWORK
);const balance = await wallet.getBalance();Transfer funds among accounts on L2 network.
const receiver = Wallet.createRandom();
const tx = await wallet.transfer({
to: receiver.address,
amount: 7_000_000_000n,
});
const receipt = await tx.wait();Transfer funds from L1 to L2 network.
const receiver = Wallet.createRandom();
const tx = await wallet.deposit({
to: receiver.address,
amount: 70_000_000n,
});Transfer funds from L2 to L1 network.
const withdrawal = await wallet.withdraw({
to: L1_ADDRESS,
amount: 70_000_000n,
});
const receipt = await tx.wait();We welcome contributions from the community! If you're interested in contributing to the via-ethers JavaScript SDK,
please take a look at our CONTRIBUTING.md for guidelines and details on the process.
Thank you for making via-ethers JavaScript SDK better! π
