TRC20代码接入
Posted 村长
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TRC20代码接入相关的知识,希望对你有一定的参考价值。
查询余额
@Test
public void balanceOfTrc20() throws Throwable
String queryAddress = "TXoDY8b3upAAkPxaK5B845zd8x44bFh1nX";
String url = tronUrl + "/wallet/triggerconstantcontract";
JSONObject param = new JSONObject();
param.put("owner_address", TronUtils.toHexAddress(queryAddress));
param.put("contract_address", TronUtils.toHexAddress(contract));
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());
System.out.println(result);
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.println(String.format("账号%s的balance=%s", queryAddress, amount.toString()));
转账
@Test
public void sendTrc20() throws Throwable
String privateKey = "6d7bad55912c455795c399b70e5ec15e3a355c6b87c3358936";
String toAddress = "TNt2sv1K93HaGdMsdsYpJCorjw1yciE3wZ";
BigDecimal amount = new BigDecimal("400");
String ownerAddress = TronUtils.getAddressByPrivateKey(privateKey);
JSONObject jsonObject = new JSONObject();
jsonObject.put("contract_address", TronUtils.toHexAddress(contract));
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(decimal).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", 6000000L);
//调用智能合约,返回 TransactionExtention, 需要签名后广播.
String trans1 = HttpClientUtils.postJson(tronUrl + "/wallet/triggersmartcontract", jsonObject.toString());
JSONObject result = JSONObject.parseObject(trans1);
System.out.println(result);
if (result.containsKey("Error"))
System.out.println("send error==========");
return;
JSONObject tx = result.getJSONObject("transaction");
tx.getJSONObject("raw_data").put("data", Hex.toHexString("我是laoxiang".getBytes()));//填写备注
String txid = TronUtils.signAndBroadcast(tronUrl, privateKey, tx);
if (txid != null)
System.out.println("交易Id:" + txid);
广播
public static String signAndBroadcast(String tronUrl, String privateKey, JSONObject transaction) throws Throwable
System.out.println("广播:" + tronUrl + " privatekey:" + privateKey + " transation: " + transaction);
if (tronUrl.endsWith("/"))
tronUrl = tronUrl.substring(0, tronUrl.length() - 1);
Protocol.Transaction tx = packTransaction(transaction.toJSONString());
byte[] bytes = signTransactionByte(tx.toByteArray(), ByteArray.fromHexString(privateKey));
//签名之后的 Transaction 以 protobuf 格式序列化, HEX 格式
String signTransation = Hex.toHexString(bytes);
System.out.println("签名后的signTransation:" + signTransation);
JSONObject jsonObjectGB = new JSONObject();
jsonObjectGB.put("transaction", signTransation);
//直接广播 protobuf 序列化后的二进制 Transaction.
String url = tronUrl + "/wallet/broadcasthex";
String transationCompelet1 = HttpClientUtils.postJson(url, jsonObjectGB.toString());
System.out.println("广播结果:" + transationCompelet1);
JSONObject transationCompelet = JSONObject.parseObject(transationCompelet1);
if (transationCompelet.getBoolean("result"))
return transationCompelet.getString("txid");
else
log.error(String.format("签名交易失败:%s", transationCompelet1));
return null;
// return null;
其他的有时间再发出来
有需要帮助的联系:laoaxiang829
以上是关于TRC20代码接入的主要内容,如果未能解决你的问题,请参考以下文章
波场TRC20 TRX 以太坊 ETH ERC20 接口代码分享