In order to provide easy access to all the features of zkSync Era, the zksync2-java
Java SDK was created,
which is made in a way that has an interface very similar to those of web3j. In
fact, web3j
is a peer dependency of our library and most of the objects exported by zksync2-java
inherit from the corresponding web3j
objects and override only the fields that need
to be changed.
While most of the existing SDKs functionalities 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.
The library is made in such a way that after replacing web3j
with zksync2-java
most client apps will work out of
box.
🔗 For a detailed walkthrough, refer to the official documentation.
For connecting ZkSync2 library just add the following dependency to your build file.
Maven pom.xml
<project>
...
<dependencies>
<dependency>
<groupId>io.zksync</groupId>
<artifactId>zksync2</artifactId>
<version>v0.2.0</version>
</dependency>
</dependencies>
</project>
Gradle build.gradle
dependencies {
implementation "io.zksync:zksync2:v0.2.0"
}
The complete examples with various use cases are available here.
import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
public class Main {
public static void main(String ...args) {
ZkSync zksync = ZkSync.build(new HttpService("http://127.0.0.1:3050"));
}
}
import com.sun.net.httpserver.HttpServer;
import io.zksync.crypto.signer.EthSigner;
import io.zksync.protocol.ZkSync;
import io.zksync.protocol.core.Token;
import io.zksync.protocol.account.wallet;
public class Main {
public static void main(String... args) {
ZkSync zksync = ZkSync.build(new HttpService("https://sepolia.era.zksync.dev"));
Credentials credentials = Credentials.create("PRIVATE_KEY");
Web3j l1Web3 = Web3j.build(new HttpService("https://rpc.ankr.com/eth_sepolia"));
Wallet wallet = new Wallet(l1Web3, zksync, credentials);
}
}
BigInteger ethBalance = wallet.getBalance().send(); // balance on zkSync Era network
BigInteger ethBalanceL1 = wallet.getBalanceL1().send(); // balance on Sepolia network
Transfer funds among accounts on L2 network.
TransferTransaction transaction = new TransferTransaction("RECEIVER", amount, signer.getAddress());
TransactionReceipt receipt = testWallet.transfer(transaction).sendAsync().join();
Transfer funds from L1 to L2 network.
DepositTransaction transaction = new DepositTransaction(ZkSyncAddresses.ETH_ADDRESS, amount);
String hash = testWallet.deposit(transaction).sendAsync().join().getResult();
Transfer funds from L2 to L1 network.
WithdrawTransaction transaction = new WithdrawTransaction(ZkSyncAddresses.ETH_ADDRESS, amount, testWallet.getAddress());
TransactionReceipt result = testWallet.withdraw(transaction).sendAsync().join();
In order to run test you need to run local-setup on your machine. For running tests, use:
gradle clean test
We welcome contributions from the community! If you're interested in contributing to the zksync2-java
Java SDK,
please take a look at our CONTRIBUTING.md for guidelines and details on the process.
Thank you for making zksync2-java
Java SDK better! 🙌