上一节我们具体讲到Java实现Tron波场链的开发部署:通过trident源码编译组件,使用RPC的方式链接Tron波场链,并进行了代码实现,还有如何创建离线钱包。
这一节我们通过部署好的项目环境,具体如何使用代码来实现波场链的交易监控和交易转账.
1. 所有的开端就是获取链的接通(ApiWrapper)
private ApiWrapper getApiWrapper(String hexPrivateKey) {
//配置是否是测试链还是正式链
if (tronServiceConfig.getTronDomainOnline()) {
// 正式环境需要到Tron申请一个apiKey进行使用
// APIKEY获取往后会讲如何获取
return ApiWrapper.ofMainnet(hexPrivateKey, tronServiceConfig.getApiKey());
} else {
// 测试环境不需要apikey
return new ApiWrapper("grpc.nile.trongrid.io:50051", "grpc.nile.trongrid.io:50061", hexPrivateKey);
}
}
2. 查看Trc20(如USDT)余额
/**
* TRC20余额
* @param address 我的钱包地址
* @return 余额
*/
public BigDecimal getTrc20Balance(String address) {
ApiWrapper client = getApiWrapper(tronServiceConfig.getHexPrivateKey());
// Trc20合约地址
Contract contract = client.getContract(tronServiceConfig.getTrc20Address());
Trc20Contract token = new Trc20Contract(contract, address, client);
BigInteger balanceOf = token.balanceOf(address);
BigDecimal divisor = new BigDecimal(tronServiceConfig.getTrc20Decimals());
BigDecimal divide = new BigDecimal(balanceOf).divide(divisor, 4, RoundingMode.HALF_UP);
client.close();
return divide;
}
3. 查看Trx余额
/**
* TRx余额
* @param address
* @return
*/
@Override
public BigDecimal getTRxBalance(String address) {
ApiWrapper wrapper = getApiWrapper(tronServiceConfig.getHexPrivateKey());
Long balance = wrapper.getAccountBalance(address);
BigDecimal divisor = new BigDecimal(tronServiceConfig.getTrc20Decimals());
BigDecimal divide = new BigDecimal(balance).divide(divisor, 4, RoundingMode.HALF_UP)