Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web3J for Java #42

Closed
Tracked by #13
manuelbarbas opened this issue Nov 30, 2023 · 1 comment
Closed
Tracked by #13

Web3J for Java #42

manuelbarbas opened this issue Nov 30, 2023 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@manuelbarbas
Copy link
Collaborator

manuelbarbas commented Nov 30, 2023

Web3j

Web3j is a modular, reactive, type safe Java and Android library for working with Smart Contracts and integrating with clients (nodes) on the Ethereum network.

Some of the features are:

  • Complete implementation of Ethereum's JSON-RPC client API over HTTP and IPC
  • Ethereum wallet support
  • Auto-generation of Java smart contract wrappers to create, deploy, transact with and call smart contracts from native Java code
  • Support for ERC20 and ERC721 token standards

For more information check the web3j documentation.

Implementation Example

Install Web3j CLI

curl -L get.web3j.io | sh && source ~/.web3j/source.sh

Create a project

web3j new

Generate a Wallet

web3j wallet create

ERC721 Mint Example Java script

package org.web3j;

import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.generated.contracts.NFT_721;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.StaticGasProvider;
import java.math.BigInteger;


public class Web3App {

   //Can use this as the Node_URL
   //public static String SKALE_Chaos_rpc = "https://staging-v3.skalenodes.com/v1/staging-fast-active-bellatrix";

   private static final String nodeUrl = System.getenv().getOrDefault("WEB3J_NODE_URL", "<node_url>");
   private static final String walletPassword = System.getenv().getOrDefault("WEB3J_WALLET_PASSWORD", "<wallet_password>");
   private static final String walletPath = System.getenv().getOrDefault("WEB3J_WALLET_PATH", "<wallet_path>");

   public static final BigInteger gasLimit = BigInteger.valueOf(5_000_000);
   public static final BigInteger gasPrice = BigInteger.valueOf(1_000_00);

   public static String address = "some address";

   public static void main(String[] args) throws Exception {

      Credentials credentials = WalletUtils.loadCredentials(walletPassword, walletPath);
      Web3j web3j = Web3j.build(new HttpService(nodeUrl));

      NFT_721 nft = NFT_721.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
      System.out.println("Contract address: " + nft.getContractAddress());

      TransactionReceipt mint_receipt = nft._mintTest(address).send();
      System.out.println("Tx has "+ mint_receipt.getTransactionHash());
     }
}

To run the Java script call the following command
Pick the SKALE Chain RPC that suits you

web3j run https://staging-v3.skalenodes.com/v1/staging-fast-active-bellatrix <wallet_path> <wallet_password>
@manuelbarbas
Copy link
Collaborator Author

manuelbarbas commented Jan 2, 2024

What is: Library
Language: Java
Target: Backend and Android

@manuelbarbas manuelbarbas added the documentation Improvements or additions to documentation label Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant