Skip to content

vianetwork/via-ethers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

22 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

πŸš€ via-ethers JavaScript SDK πŸš€

License License: Apache 2.0 Contributor Covenant Contributions Welcome Code Style: Google

Banner

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.

πŸ“Œ Overview

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 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.

πŸ›  Prerequisites

πŸ“₯ Installation & Setup

yarn add @vianetwork/via-ethers
yarn add ethers@6 # ethers is a peer dependency of via-ethers

πŸ“ Examples

Connect to the Via network:

import {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',
});

Get the latest block number

const blockNumber = await provider.getBlockNumber();

Get the latest block

const block = await provider.getBlock('latest');

Create a wallet

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
);

Check account balances

const balance = await wallet.getBalance();

Transfer funds

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();

Deposit funds

Transfer funds from L1 to L2 network.

const receiver = Wallet.createRandom();

const tx = await wallet.deposit({
  to: receiver.address,
  amount: 70_000_000n,
});

Withdraw funds

Transfer funds from L2 to L1 network.

const withdrawal = await wallet.withdraw({
  to: L1_ADDRESS,
  amount: 70_000_000n,
});
const receipt = await tx.wait();

🀝 Contributing

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! πŸ™Œ

About

Adapted ethers.js library to work with Via network

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages