NEWS

Integrazione Java con Web3j

Integrazione Java con Web3j

Un esempio di integrazione

Questo un piccolo esempio di integrazione utilizzando il linguaggio di programmazione Java e la libreria Web3j.

Con questo codice vediamo come interagire con lo smart contract creato.

 

package com.cryptox.ethereum;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.ExecutionException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.StaticGasProvider;
import org.web3j.utils.Convert;

import com.cryptox.ethereum.contracts.TokenERC20;

public class EthereumHelper {
   
   private static final Logger logger = LoggerFactory.getLogger(EthereumHelper.class);
   
   private final static BigInteger GAS_LIMIT = new BigInteger("200000");
   private final static BigInteger GAS_PRICE = Convert.toWei("5", Convert.Unit.GWEI).toBigInteger();

   //Gwei is a unit of Ethereum (ETH) cryptocurrency. 1 ETH = 1000000000 Gwei
   private final static String CONTRACT_RECIPIENT   = "0x..."; //Contract Address Ethereum Wallet PP
   
   private final static String PRIVATE_KEY   = "xxxxxx"; //Metamask PP
   
   //External Address 
   private final static String USER_RECIPIENT   = "0x..."; //Metamask PP
   private final static String ADMIN_RECIPIENT = "0x...";

 
   public static void main(String[] args) {
       Web3j web3j = Web3j.build(new HttpService("https://ropsten.infura.io/v3/..."));
       //Web3j web3j = Web3j.build(new HttpService("https://mainnet.infura.io/v3/..."));
       
 try {
           System.out.println("Connected to Ethereum client version: " + web3j.web3ClientVersion().send().getWeb3ClientVersion());
       } catch (IOException e) {
           logger.error("Error {}", e.getMessage(), e);
       }
       
       //ETH USER BALANCE
       try {
           BigInteger wei = web3j.ethGetBalance(USER_RECIPIENT, DefaultBlockParameterName.LATEST).sendAsync().get().getBalance();
           System.out.println("User Address " + USER_RECIPIENT + " balance " + new BigDecimal(wei).divide(new BigDecimal("1000000000000000000")));
       } catch (InterruptedException | ExecutionException e) {
           logger.error("Error {}", e.getMessage(), e);
       }
       
       //ETH ADMIN USER BALANCE
       try {
           BigInteger wei = web3j.ethGetBalance(ADMIN_RECIPIENT, DefaultBlockParameterName.LATEST).sendAsync().get().getBalance();
           System.out.println("Admin Address " + USER_RECIPIENT + " balance " + new BigDecimal(wei).divide(new BigDecimal("1000000000000000000")));
       } catch (InterruptedException | ExecutionException e) {
           logger.error("Error {}", e.getMessage(), e);
       }
       
       //TOKEN ACT AS USER
       try {
           ContractGasProvider contractGasProvider = new StaticGasProvider(GAS_PRICE, GAS_LIMIT);
           TokenERC20 tokenUser = TokenERC20.load(CONTRACT_RECIPIENT, web3j, getCredentialsFromPrivateKey(), contractGasProvider);
           BigInteger wei = tokenUser.balanceOf(USER_RECIPIENT).sendAsync().get();
           System.out.println("Address " + USER_RECIPIENT + " balance " + new BigDecimal(wei).divide(new BigDecimal("1000000000000000000")));
       } catch (InterruptedException | ExecutionException e) {
           logger.error("Error {}", e.getMessage(), e);
       } 
       
       //TOKEN ACT AS ADMIN
       try {
           ContractGasProvider contractGasProvider = new StaticGasProvider(GAS_PRICE, GAS_LIMIT);
           TokenERC20 tokenUser = TokenERC20.load(CONTRACT_RECIPIENT, web3j, getCredentialsFromPrivateKey(), contractGasProvider);
           BigInteger wei = tokenUser.balanceOf(ADMIN_RECIPIENT).sendAsync().get();
           System.out.println("Address " + ADMIN_RECIPIENT + " balance " + new BigDecimal(wei).divide(new BigDecimal("1000000000000000000")));
       } catch (InterruptedException | ExecutionException e) {
           logger.error("Error {}", e.getMessage(), e);
       } 
       
       //TOKEN ACT AS ADMIN
       try {
           ContractGasProvider contractGasProvider = new StaticGasProvider(GAS_PRICE, GAS_LIMIT);
           TokenERC20 tokenAdmin = TokenERC20.load(CONTRACT_RECIPIENT, web3j, getCredentialsFromPrivateKeyAdmin(), contractGasProvider);
           TransactionReceipt receipt = tokenAdmin.transfer(USER_RECIPIENT, new BigInteger("50").multiply(new BigInteger("1000000000000000000"))).sendAsync().get();
           System.out.println("Block " + receipt.getBlockHash());
           BigInteger wei = tokenAdmin.balanceOf(USER_RECIPIENT).sendAsync().get();
           System.out.println("Address " + USER_RECIPIENT + " balance " + new BigDecimal(wei).divide(new BigDecimal("1000000000000000000")));
       } catch (InterruptedException | ExecutionException | IOException | CipherException e) {
           logger.error("Error {}", e.getMessage(), e);
       }
       
       web3j.shutdown();

 }
 
 private static Credentials getCredentialsFromPrivateKey() {
     return Credentials.create(PRIVATE_KEY);
 }
 
 private static Credentials getCredentialsFromPrivateKeyAdmin() throws IOException, CipherException {
     return  WalletUtils.loadCredentials("PASS", "/path/to/keystore/file");
 }
 
}

 

 


James Lombardi

Appassionato di criptovalute, blockchain e sistemi di pagamento. 

Donate

36mSZ9TkQRZPvqsu1ENUWC871XidejX6nT
0xE3D007E5Ef4A377D35a2C50317D01bc0B7Eae4F6
0xd11aebd251a7e2dd135c007eef3123f101b1ca79

GUADAGNA CRIPTOVALUTE CON CRYPTOX.IT

Se alla fine dell'articolo trovi "Guadagna cripto condividento questo contenuto" potrai guadagnare criptovalute condividendo il contenuto sui tuoi social o tra i tuoi amici, leggi il regolamento.

COMMENTS


PARTNER

CRYPTO RATES

24h 24h Vol Price
BTC +2,42 53.871,95M 48.321,42 EUR
ETH -4,42 33.706,87M 3.010,43 EUR
RPD +15.989.979,42 0,40M 18,45 EUR
BNB +0,71 2.923,22M 525,54 EUR
DOGE +19,55 22.386,53M 0,59 EUR
XRP -2,09 5.777,59M 1,31 EUR
USDT -0,70 130.484,81M 0,82 EUR
ADA -6,23 3.774,80M 1,31 EUR
DOT -3,02 1.868,44M 32,76 EUR
BCH -0,79 6.290,93M 1.147,65 EUR
aggiornato 4 minuti fa da coinmarketcap.com

POPULAR VIDEOS

POPULAR BOOKS

The Bitcoin Standard
The Bitcoin Standard
by Saifedean Ammous Dec 06, 2020
Compra su Amazon
The Mystery Of Banking
The Mystery Of Banking
by Murray N. Rothbard Dec 06, 2019
Compra su Amazon
Mastering Bitcoin
Mastering Bitcoin
by Andreas M. Antonopoulos Nov 12, 2020
Compra su Amazon
Flash Boys
Flash Boys
by Michael Lewis Nov 19, 2019
Compra su Amazon

POPULAR TWEETS

@AndrewNation13 @AndrewNation13 - 22 hours ago Latest Ripple XRP News @Kevin_Cage_ #XRP #cryptocurrency #bestcrypto https://t.co/VAGjl5MGGo

  • 17
  • 2

Agostino Mario Mela @agostinomela - 7 hours ago Si chiamano criptovalute perché per la stragrande maggioranza delle persone sono e saranno sempre un grande mistero.

  • 14
  • 0

FREE MCAFEE - Freedom Club @ostvest - 24 hours ago La mia microazienda, come sapranno i miei affezionatissimi lettori, accantona il 10% delle fatture in ingresso in criptovalute. Ecco il portafoglio ad oggi. NOT FINANCIAL ADVICE - YES LIFE ADVICE https://t.co/iiZU71sNaX

  • 8
  • 0

IOTA - The next generation of Crypto @IotaBullrun2020 - 22 hours ago #IOTA + #INTEL + #DELL = WORKS $iota $miota #miota #altcoins #altcoin #altseason #btc #bitcoin #usdt #binance #bnb https://t.co/p1tutJZfQa

  • 7
  • 2

ハデス @ripple_chan - 24 hours ago AI inside https://t.co/PY166U65br

  • 7
  • 1

CRYPTO NETWORK

Reward Block Difficulty
BTC 6 682449 20.608.845M
ETH 2 12389569 7.470.847.865M
BCH 6 686885 446.535M
UNO 0 1331510 368.211M
XMR 2 2355926 289.099M
DASH 1 1467121 199M
LTC 12 2048442 12M
ZEC 2 1241587 97M
ZEN 3 928985 33M
ETC 3 12697629 403.997.000M
aggiornato 17 ore fa da coinwarz.com

PODCAST


LINKS