Implement Web3j Ethereum in Spring Boot Using Private Network

Posted By : Dipen Chawla | 09-Mar-2021

Setting up genesis block:

The primary piece (square zero) of the piece chain is called as the genesis block. This is the main piece on the system that doesn't point to the antecedent square. Of course the beginning square is hard coded into Ethereum customers. However as we are setting up our own particular test arrange, this progression is fundamental. Every resulting square will reference this beginning piece. On Geth customer, this is accomplished by setting up a json document (say mygenesis.json) with the accompanying esteems. Guarantee this record is added to both the cases that will run Geth (as Ethereum accord calculation guarantees no other hub will concur with your form of blockchain unless they have a similar beginning square). To comprehend what each of these params mean please allude to this answer on stack trade.

    HTTP CONFIGURATION:

    Some Ethereum operations take longer than the default HTTP timeout set by the OkHttp3 library used by web3j.

     

    To configure those timeouts set the web3j httpTimeoutSeconds property

     

    web3j.httpTimeoutSeconds = 600

     

    This sets all three OkHttp3 timeouts: connect, read, and write.

Create Spring Boot Application in web3j private network ethereum:

Create a new spring boot application https://spring.io/guides/gs/spring-boot/ and include the dependencies:

Maven Dependency

<dependency>
    <groupId>org.web3j</groupId>
    <artifactId>web3j-spring-boot-starter</artifactId>
    <version>1.5.0</version>
</dependency>

Create Wallet Controller:

RequestMapping(value ="/createwallet", method = RequestMethod.GET)
    ResponseEntity<Object> getcreateWallet() throws Exception {
        String s = pdfservice.walletCreation();
        return ResponseHandler.generateResponse(HttpStatus.OK, false,"SUCCESS", s);
}

Create Wallet Service:

public String walletCreation() throws Exception {
        File file = new File("/home/dipen/private-ethereum/keystore");
        String password = "[email protected]";
        String fileName = WalletUtils.generateFullNewWalletFile(password, file);
        System.out.println("File Name------>>> " + fileName);
        return fileName;

    }

Create Wallet Address Controller:

@RequestMapping(value ="/walletAddress", method = RequestMethod.GET)
    ResponseEntity<Object> getWalletAddress() throws Exception {
        String s = pdfservice.walletAddress();
        return ResponseHandler.generateResponse(HttpStatus.OK, false,"SUCCESS", s);
}

Create Wallet Address Service:

public String walletAddress() throws Exception {
        File file = new File("/home/dipen/private-ethereum/keystore/UTC--2018-01-01T13-36-56.423000000Z--ef873e67e8119e7135923010681fbec76e43d202.json");
        String password = "[email protected]";
        Credentials credentials = WalletUtils.loadCredentials(password, file);
        String s = credentials.getAddress();
        System.out.println("Get Wallet address===  " + credentials.getAddress());
        return s;
    } 

Create getbalanceTransaction  Controller:

@RequestMapping(value ="/getbalance", method = RequestMethod.GET)
    ResponseEntity<Object> getBalance() throws Exception {
        BigInteger s = pdfservice.getBalanceTransaction();
        return ResponseHandler.generateResponse(HttpStatus.OK, false,"SUCCESS", s);
}

Create  getbalanceTransaction  Service:

public BigInteger getBalanceTransaction() throws Exception{
        File file = new File("/home/dipen/private-ethereum/keystore/UTC--2017-12-30T20-37-42.204106266Z--e7e3543f0ad320fda4e6ab1383d56982d4f601b5");
        String password = "Testing";
        Credentials credentials = WalletUtils.loadCredentials(password, file);
        System.out.println("Get Wallet address===  "+credentials.getAddress());
            String address = credentials.getAddress();
            HttpService httpService = new HttpService("http://localhost:8000");
            Web3j web3 = Web3j.build(httpService);
        EthGetBalance balance = web3.ethGetBalance(address, DefaultBlockParameterName.LATEST).sendAsync().get();
        BigInteger s = balance.getBalance();
        System.out.println("---------- "+balance.getBalance());
        return s;
    }

Create TransactionSend Controller:

@RequestMapping(value ="/transactionsend", method = RequestMethod.GET)
    ResponseEntity<Object> transactionSend() throws Exception {
        String s = pdfservice.TransactionSend();
        return ResponseHandler.generateResponse(HttpStatus.OK, false,"SUCCESS", s);
}

Create TransactionSend  Service:

public String TransactionSend()throws Exception{
        File file = new File("/home/dipen/private-ethereum/keystore/UTC--2017-12-30T20-37-42.204106266Z--e7e3543f0ad320fda4e6ab1383d56982d4f601b5");
        String password = "Testing";
        Credentials credentials = WalletUtils.loadCredentials(password, file);
        System.out.println("Get Wallet address===  "+credentials.getAddress());
            HttpService httpService = new HttpService("http://localhost:8000");
            Web3j web3 = Web3j.build(httpService);

            TransactionReceipt transactionReceipt = Transfer.sendFunds(web3, credentials, "0x5c96bd628c3f8f8524f30836209abc6c24d67604", BigDecimal.valueOf(1.0), Convert.Unit.ETHER);
            String s = transactionReceipt.getTransactionHash();
            System.out.println(transactionReceipt.getTransactionHash());
            return s;
    }

About Author

Author Image
Dipen Chawla

Dipen is Java Developer and his keen interest is in Spring, Hibernate, Rest web-services, AngularJS and he is a self motivated person and loves to work in a team.

Request for Proposal

Name is required

Comment is required

Sending message..