上一节我们主要是环境搭建,主要是为了能够快速得去开发,有些地方只是简单的介绍,比如ETH 、web3j等等这些。
这一节我们来用代码来实现BNB转账、BEP20转账、链上交易监控
话不多说,我们直接用代码实现吧
1. BNB转账
/**
* BNB转账
* @param toAddress 接收地址地址
* @param amount 金额
* @return
*/
@Override
public String transBscBnbJson(String toAddress, String amount) throws Exception {
Web3j web3j = Web3j.build(new HttpService(tronServiceConfig.getBscUrl()));
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(tronServiceConfig.getBscFromAddress(), DefaultBlockParameterName.LATEST).sendAsync().get();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();
BigInteger gasLimit = BigInteger.valueOf(60000);
BigInteger functionAmount = Convert.toWei(new BigDecimal(amount), Convert.Unit.ETHER).toBigInteger();
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, functionAmount);
// 私钥
Credentials credentials = Credentials.create(tronServiceConfig.getBscFromPrivateKey());
//进行签名操作
byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValues = Numeric.toHexString(signMessage);
//发起交易
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValues).sendAsync().get();