skalenetwork / docs.skale.space

https://docs.skale.space/
MIT License
0 stars 2 forks source link

Web3J for Java #42

Closed manuelbarbas closed 4 months ago

manuelbarbas commented 9 months ago

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:

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 commented 8 months ago

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