Metamask wallet connectivity

Posted By : Sagun Jain | 29-Jun-2022

1. Web3j

Web3j is a safe JAVA library that works with Ethereum

2. Create an account 
Use this link to create a Metamask account

Link:  https://chrome.google.com/webstore/detail/metamask/nkbihfbeogaeaoehlefnkodbefgpgknn?hl=en

3. Maven Dependency
First add maven dependency for Web3j.

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.0</version>
</dependency>

4. Networks
Different networks are used which we can connect our metamask wallet.

1. Ethereum Network - Mainnet
2. Ropsten Network for metamask
3. Goerli Network
4. Kovan Network 
5. Avalanche Network
6. Rinkeby Network
7. Polygon Network

Note: Each Network has its chainId

5. Metamask Connectivity Using Ropsten Network 

   final Web3j client = Web3j.build
(                
new HttpService
  ( url: "https://ropsten.infura.io/v3/...8a") //infura ropsten link      
 );
  final String ethAdress = "transactionHash"; // Please mention propoer transactionHash
  final EthGetBalance balance = client.ethGetBalance(ethAdress, DefaultBlockParameter.valueOf("latest")).sendAsync().get(10, TimeUnit.SECONDS);        
  final BigInteger result = balance.getBalance();        
  final BigDecimal resultset = new BigDecimal(result);        
  EthGetTransactionReceipt receipt = client.ethGetTransactionReceipt("transactionHash").send();

Note: With the above mentioned code you can connect with web3j and you'll get thr transaction receipt.

6. Api to Update Transaction Status

 if (Objects.isNull(txId)) {
                log.info("Transaction hash is not provided in request ");
            }
            final Web3j client = Web3j.build(new HttpService("https://ropsten.infura.io/v3/..6f"));

            final Transaction transactionRepositoryById = transactionRepository.findById(txId);
            if (transactionRepositoryById == null) {
                return null;
            }
            final EthGetTransactionReceipt receipt = client.ethGetTransactionReceipt(txId).send();
            final Optional<TransactionReceipt> transaction = receipt.getTransactionReceipt();
            if (transaction.isPresent()) {
                transactionRepositoryById.setBlockHash(transaction.get().getBlockHash());
                transactionRepository.save(transactionRepositoryById);
            }
            List<EthBlock.TransactionResult> txs = client.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, true).send().getBlock().getTransactions();
            int i = 0;
            while (!Objects.equals(transactionRepositoryById.getStatus(), "CONFIRMED")) {
                EthBlock.TransactionObject transactions = (EthBlock.TransactionObject) txs.get(i);
                BigInteger value = transactions.getValue();
                transactionRepositoryById.setValue(value);
                BigInteger bigInteger = new BigInteger("0");
                int testValue = value.compareTo(bigInteger);
                if (testValue == 0 && transactionRepositoryById.getBlockHash() != null) {
                    log.info("PENDING ");
                    transactionRepositoryById.setFlag(false);
                    transactionRepositoryById.setStatus(WalletStatus.CANCELLED.toString());
                    transactionRepository.save(transactionRepositoryById);
                    break;
                }
                if (transactionRepositoryById.getBlockHash() == null && testValue > 0) {
                    log.info("CANCELLED ");
                    transactionRepositoryById.setFlag(false);
                    transactionRepositoryById.setStatus(WalletStatus.PENDING.toString());
                    transactionRepository.save(transactionRepositoryById);
                    break;
                }
                if (transactionRepositoryById.getBlockHash() != null && testValue > 0) {
                    transactionRepositoryById.setFlag(true);
                   
                    log.info("SUCCESS ");
                    transactionRepositoryById.setStatus("CONFIRMED");
                    transactionRepository.save(transactionRepositoryById);
                    break;                }
                i++;
            }
            return transactionRepositoryById;
        } catch (Exception e) {
            log.info("Error in receipt find by id method ", e);
        }
        return null;
    }

 

About Author

Author Image
Sagun Jain

Sagun has good programming skills. She is a quick learner. During her free time she enjoys reading books.

Request for Proposal

Name is required

Comment is required

Sending message..