前面做过波场相关的转账交易(搭建节点跟不搭建节点的),现在对这些内容做以下整合。
TRX转账功能
/**
* 发起trx交易
*
* @param privateKey 发起地址私钥
* @param to 收款地址
* @param sun trx数量(sun单位 1000000表示1trx)
* @param remark 备注信息
* @return
*/
@Override
public String transfer(String privateKey, String to, BigDecimal trx, String remark) {
requireNotEmpty(privateKey, "private-key");
requireNotEmpty(to, "to-address");
if (trx.compareTo(BigDecimal.ZERO) <= 0) {
throw new IllegalArgumentException("amount is not illegal:" + trx.toString());
}
String from = TronUtils.getAddressByPrivateKey(privateKey);
try {
String url = tronUrl + "/wallet/createtransaction";
JSONObject param = new JSONObject();
param.put("owner_address", TronUtils.toHexAddress(from));
param.put("to_address", TronUtils.toHexAddress(to));
param.put("amount", trx.multiply(DECIMAL).toBigInteger());
String _result = HttpClientUtils.postJson(url, param.toJSONString());
String txid = null;
if (StringUtils.isNotEmpty(_result)) {
if(_result.toLowerCase().contains("error")){
logger.info(_result);
}
JSONObject transaction = JSONObject.parseObject(_result);
if (StringUtils.isNotEmpty(remark)) {
transaction.getJSONObject("raw_data").put("data", Hex.toHexString(remark.getBytes()));
}
txid = TronUtils.signAndBroadcast(tronUrl, privateKey, transaction);
}
if (txid != null) {
logger.info(String.format("send trx|success:txid=%s from=%s to=%s amount=%s", txid, from, to, trx.toString()));
} else {
logger.error(String.format("send trx|fail:from=%s to=%s amount=%s", from, to, trx.toString()));
}
return txid;
} catch (