我是靠谱客的博主 雪白大叔,最近开发中收集的这篇文章主要介绍TRC20 创建账户,转账,余额查询 java实现,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


public static void createAddress() {
Map<String, String> map = TronUtils.createAddress();
String address = TronUtils.toViewAddress(map.get("address"));
String privateKey = map.get("privateKey");
System.out.print("address:" + address);
System.out.print("privateKey:" + privateKey);
}

public static void getTrxBalance(String queryAddress) throws Exception {
// 精度
BigDecimal decimal = new BigDecimal("1000000");
String url = tronUrl + "/wallet/getaccount";
JSONObject param = new JSONObject();
param.put("address", TronUtils.toHexAddress(queryAddress));
String result = HttpClientUtils.postJson(url, param.toJSONString());
BigInteger balance = BigInteger.ZERO;
if (!StringUtils.isEmpty(result)) {
JSONObject obj = JSONObject.parseObject(result);
BigInteger b = obj.getBigInteger("balance");
if (b != null) {
balance = b;
}
}
System.out.println("trx:" + new BigDecimal(balance).divide(decimal, 6, RoundingMode.FLOOR));
}

public static void getTrc20Balance(String contractAddress, String queryAddress) throws Exception {
// 合约精度
BigDecimal decimal = new BigDecimal("1000000");
String url = tronUrl + "/wallet/triggerconstantcontract";
JSONObject param = new JSONObject();
param.put("owner_address", TronUtils.toHexAddress(queryAddress));
param.put("contract_address", TronUtils.toHexAddress(contractAddress));
param.put("function_selector", "balanceOf(address)");
List<Type> inputParameters = new ArrayList<>();
inputParameters.add(new Address(TronUtils.toHexAddress(queryAddress).substring(2)));
param.put("parameter", FunctionEncoder.encodeConstructor(inputParameters));
String result = HttpClientUtils.postJson(url, param.toJSONString());
BigDecimal amount = BigDecimal.ZERO;
if (StringUtils.isNotEmpty(result)) {
JSONObject obj = JSONObject.parseObject(result);
JSONArray results = obj.getJSONArray("constant_result");
if (results != null && results.size() > 0) {
BigInteger _amount = new BigInteger(results.getString(0), 16);
amount = new BigDecimal(_amount).divide(decimal, 6, RoundingMode.FLOOR);
}
}
System.out.printf("账号%s的balance=%s%n", queryAddress, amount);
}

public static String transferTrc20(String contractAddress, String ownerAddress, String fromPrivateKey, String toAddress, BigDecimal amount) throws Throwable {
JSONObject jsonObject = new JSONObject();
jsonObject.put("contract_address", TronUtils.toHexAddress(contractAddress));
jsonObject.put("function_selector", "transfer(address,uint256)");
List<Type> inputParameters = new ArrayList<>();
inputParameters.add(new Address(TronUtils.toHexAddress(toAddress).substring(2)));
inputParameters.add(new Uint256(amount.multiply(BigDecimal.TEN.pow(6)).toBigInteger()));
String parameter = FunctionEncoder.encodeConstructor(inputParameters);
jsonObject.put("parameter", parameter);
jsonObject.put("owner_address", TronUtils.toHexAddress(ownerAddress));
jsonObject.put("call_value", 0);
jsonObject.put("fee_limit", 100000000L);
String trans1 = HttpClientUtils.postJson(tronUrl + "/wallet/triggersmartcontract", jsonObject.toString());
JSONObject result = JSONObject.parseObject(trans1);
if (result.containsKey("Error")) {
System.out.println("send error==========");
return "";
}
JSONObject tx = result.getJSONObject("transaction");
String txid = TronUtils.signAndBroadcast(tronUrl, fromPrivateKey, tx);
if (txid != null) {
return txid;
} else {
return "";
}
}

public static String transferTrx(String ownerAddress, String fromPrivateKey, String toAddress, BigDecimal trxAmount) throws Throwable {
String url = tronUrl + "/wallet/createtransaction";
JSONObject param = new JSONObject();
param.put("owner_address", TronUtils.toHexAddress(ownerAddress));
param.put("to_address", TronUtils.toHexAddress(toAddress));
// 将金额转为sun单位
param.put("amount", trxAmount.multiply(BigDecimal.TEN.pow(6)).toBigInteger());
String _result = HttpClientUtils.postJson(url, param.toJSONString());
if (StringUtils.isNotEmpty(_result)) {
JSONObject transaction = JSONObject.parseObject(_result);
return TronUtils.signAndBroadcast(tronUrl, fromPrivateKey, transaction);
}
return "";
}

最近公司新接了一个项目,涉及到波场的对接,研究了一下,将基础实现demo整理了下,全部demo源码已上传github,

源码链接:https://github.com/tron639/trx-api-demo

最后

以上就是雪白大叔为你收集整理的TRC20 创建账户,转账,余额查询 java实现的全部内容,希望文章能够帮你解决TRC20 创建账户,转账,余额查询 java实现所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部