简介
设计一个通过调用api创建对应的tron地址,当地址收到token的时候,进行归集&回调通知的。包括的功能有:
- 根据UID创建地址
- 归集(TRX归集 TRC10归集 TRC20归集)
- 回调通知(转出回调通知&接收回调通知)
- 发起转出订单
- 以及上述一些的异常处理
源码预览(后台代码)
- 创建地址
/**
* 离线创建地址
*
* @return
*/
public static CreateAddressResult createAddress() {
ECKey eCkey = new ECKey(random);
String privateKey = ByteArray.toHexString(eCkey.getPrivKeyBytes());
byte[] addressBytes = eCkey.getAddress();
String hexAddress = ByteArray.toHexString(addressBytes);
CreateAddressResult addressInfo = new CreateAddressResult();
addressInfo.setAddress(toViewAddress(hexAddress));
addressInfo.setHexAddress(hexAddress);
addressInfo.setPrivateKey(privateKey);
return addressInfo;
}
- 转出TRX
/**
* 转出TRX
*
* @param tronUrl
* @param signKey
* @param to
* @param trx
* @param headMap
* @return
* @throws Throwable
*/
public static String transfer(String tronUrl, String signKey, String to, BigDecimal trx, Map<String, String> headMap) throws Throwable {
if (trx.compareTo(BigDecimal.ZERO) <= 0) {
throw new IllegalArgumentException("trx cannot less than Zero");
}
String from = TronUtils.getAddressByPrivateKey(signKey);
logger.info(String.format("SendTrx|start|sender=%s receiver=%s amount=%s", from, to, trx));
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(TRX_DECIMAL).toBigInteger());
String _result = HttpClientUtils.postJson(url, param.toJSONString(), headMap);
String txid = null;
if (StringUtils.isNotEmpty(_result)) {
if (_result.toLowerCase().contains("error")) {
logger.info(_result);
}
JSONObject transaction = JSONObject.parseObject(_result);
txid = TronUtils.signAndBroadcast(tronUrl, signKey, transaction, headMap);
}
if (txid != null) {
logger.info(String.format("SendTrx|success|txid=%s sender=%s receiver=%s amount=%s", txid, from, to, trx.toString()));
} else {
logger.error(String.format("SendTrx|fail|sender=%s receiver=%s amount=%s", from, to, trx.toString()));
}
return txid;
}
- 触发智能合约
/**
* 执行合约函数
*
* @param tronUrl
* @param contract
* @param signKey
* @param owner
* @param function
* @param params
* @param feeLimit
* @param callValue
* @param headMap
* @return
* @throws Throwable
*/
public static String triggerSmartContract(String tronUrl, String contract, String signKey, String owner,
String function, List<Type> params, BigInteger feeLimit, BigDecimal callValue,
Map<String, String> headMap) throws Throwable {
logger.info(String.format("触发智能合约|START|%s|%s", contract, function));
if (StrUtil.isEmpty(owner)) {
owner = getAddressByPrivateKey(signKey);
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("contract_address", TronUtils.toHexAddress(contract));
jsonObject.put("function_selector", function);
String parameter = "";
if (CollUtil.isNotEmpty(params)) {
parameter = FunctionEncoder.encodeConstructor(params);
}
jsonObject.put("parameter", parameter);
jsonObject.put("owner_address", TronUtils.toHexAddress(owner));
if (callValue != null) {
jsonObject.put("call_value", callValue.multiply(new BigDecimal("1e6")).toBigInteger().longValue());
}
jsonObject.put("fee_limit", feeLimit.longValue());
String trans1 = HttpClientUtils.postJson(tronUrl + "/wallet/triggersmartcontract", jsonObject.toString());
JSONObject result = JSONObject.parseObject(trans1);
if (result.containsKey("Error")) {
logger.warn(String.format("触发智能合约|END|FAIL|%s|%s|%s", contract, function, trans1));
return null;
}
JSONObject tx = result.getJSONObject("transaction");
String txid = signAndBroadcast(tronUrl, signKey, tx, headMap);
if (txid != null) {
logger.info(String.format("触发智能合约|END|SUCCESS|%s|%s|%s", contract, function, txid));
} else {
logger.warn(String.format("触发智能合约|END|FAIL|%s|%s|%s", contract, function, "HashIsNull"));
}
return txid;
}
- 查询智能合约信息
/**
* 调用合约查询
*
* @param tronUrl
* @param contract
* @param function
* @param params
* @param headMap
* @return
* @throws Throwable
*/
public static String triggerConstantContract(String tronUrl, String contract, String function, List<Type> params, Map<String, String> headMap) throws Throwable {
String url = tronUrl + "/wallet/triggerconstantcontract";
JSONObject param = new JSONObject();
param.put("owner_address", TronUtils.toHexAddress("T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb"));
param.put("contract_address", TronUtils.toHexAddress(contract));
param.put("function_selector", function);
if (CollUtil.isNotEmpty(params)) {
param.put("parameter", FunctionEncoder.encodeConstructor(params));
}
String result = HttpClientUtils.postJson(url, param.toJSONString(), headMap);
if (StringUtils.isNotEmpty(result)) {
JSONObject obj = JSONObject.parseObject(result);
JSONArray results = obj.getJSONArray("constant_result");
if (results != null && results.size() > 0) {
return results.getString(0);
}
}
return "";
}
TronService
public interface TronService {
long getLogNowBlock();
/**
* trx数量
*
* @param address
* @return
*/
BigDecimal balanceOf(String address) throws IOException;
/**
* 发送trx
*
* @param signKey 签名私钥
* @param to 接受
* @param amount 数量
* @return
* @throws Throwable
*/
String sendTrx(String signKey, String to, BigDecimal amount) throws Throwable;
/**
* trc10数量
*
* @param tokenId
* @param address
* @return
* @throws Throwable
*/
BigInteger balanceOfTrc10(long tokenId, String address) throws Throwable;
/**
* 发送trc10
*
* @param signKey
* @param tokenId
* @param to
* @param amount
* @return
* @throws Throwable
*/
String sendTrc10(String signKey, long tokenId, String to, BigInteger amount) throws Throwable;
/**
* 发送trc20
*
* @param signKey
* @param contract
* @param to
* @param amount
* @return
* @throws Throwable
*/
String transferTrc20(String signKey, String contract, String to, BigInteger amount, BigInteger feeLimit) throws Throwable;
/**
* 获取trc20的数量
*
* @param contract
* @param address
* @return
*/
BigInteger balanceOfTrc20(String contract, String address) throws Throwable;
/**
* 获取trc20的数量
*
* @param contract
* @param address
* @param accuracy
* @return
*/
BigDecimal balanceOfTrc20(String contract, String address, int accuracy) throws Throwable;
/**
* 获取当前块高
*
* @return
*/
long getNowBlock();
/**
* 交易是否成功
*
* @param hash
* @return
*/
boolean isSuccess(String hash) throws Throwable;
}
系统预览(管理平台)
导航
订单管理
数据配置
重要配置加密存放(保证私钥不会因为数据导致泄露)
后台管理
维护管理
API文档
直接通过http/https相关调用,即可使用
调用API安全问题(通过API-KEY签名 以及配置的白名单IP地址)