波场技术对接官网:对接官网
TRC20代币官方教程:TRC20发币
这里用到的是java+maven,这些依赖也是搞了很久,话不多说直接上代码,有问题麻烦加我qq284466315告诉一下~
一:离线生成地址(copy的这位大佬冷少哥)
public TrxApi newAddress() {
try {
String pwd = "随便输个";
ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
ECKeyPair ecKeyPair = Keys.createEcKeyPair();
WalletFile walletFile = Wallet.createStandard(pwd, ecKeyPair);
String keystore = objectMapper.writeValueAsString(walletFile);
WalletFile walletFile2 = objectMapper.readValue(keystore, WalletFile.class);
ECKeyPair ecKeyPair1 = Wallet.decrypt(pwd, walletFile2);
String addressT = fromHexAddress("41" + walletFile.getAddress());
// 创个类接受一下就行
return new TrxApi("41" + walletFile.getAddress(), addressT, ecKeyPair1.getPrivateKey().toString(16));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 41 ---- > T
*
* @param address
* @return
*/
public static String fromHexAddress(String address) {
return TronWalletApi.encode58Check(ByteArray.fromHexString(address));
}
/**
* T ----> 41
*
* @param address
* @return
*/
public static String toHexAddress(String address) {
return ByteArray.toHexString(TronWalletApi.decodeFromBase58Check(address));
}
二 充值(这个我不是很确定,我的data解析方式有点low,希望知道咋解析的评论告诉我一下)
JSONObject jsonObject = new JSONObject();
Long trxBlock = ethDao.getTrxBlock();
jsonObject.put("num", trxBlock);
JSONObject trans = HttpsUtil.sendPost(TRON_NODE_URL + "/walletsolidity/getblockbynum", jsonObject);
if (trans.size() > 0
) {
List TransactionNewList = JSON.parseArray(trans.getString("transactions"), TransactionNew.class);
if (TransactionNewList != null && TransactionNewList.size() > 0) {
for (TransactionNew TransactionNew :
TransactionNewList) {
String txID = TransactionNew.getTxID();
if (TransactionNew.getRawData() != null && TransactionNew.getRawData().getContract() != null && TransactionNew.getRawData().getContract().size() > 0) {
String contractRet = TransactionNew.getRet().get(0).getContractRet();
if (contractRet.equalsIgnoreCase("SUCCESS")) {
Contract contract = TransactionNew.getRawData().getContract().get(0);
Value value = contract.getParameter().getValue();
// 判断是否是一个合约地址
String contractAddress = fromHexAddress(value.getContractAddress());
// USDT_CONTRACT你的合约地址
String from = fromHexAddress(value.getOwnerAddress());
String data = value.getData();
if (data == null || data.length() < 73) {
continue;
}
// 解析普通的Trc20
String to = fromHexAddress("41" + data.substring(32, 72));
BigDecimal amount = BigDecimal.ZERO;
Integer coinType = 0;
BigDecimal amountDecimal = new BigDecimal(new BigInteger(data.substring(72), 16));
if (contractAddress.equalsIgnoreCase(USDT_CONTRACT)) {
amount = amountDecimal.divide(new BigDecimal("1000000"), 8, BigDecimal.ROUND_DOWN);
coinType = CoinTypeEnum.USDT_COIN.getCode();
updUserAsset.setUsdtCoin(amount);
} else {
continue;
}
}
}
}
}
}
ethDao.updateTrxBlock(trxBlock + 1);
}
三:TRC20转账
public Map Trc20Transation2(String toAddress, String contract, BigDecimal amount) {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("contract_address", toHexAddress(contract));
jsonObject.put("function_selector", "transfer(address,uint256)");
// KBTC_CONTRACT 合约,根据精度乘一下
if (contract.equalsIgnoreCase(KBTC_CONTRACT)) {
amount = amount.multiply(new BigDecimal("1000000000000000000"));
} else {
amount = amount.multiply(new BigDecimal("1000000"));
}
BigInteger l = amount.toBigInteger();
String parameter = encoderAbi(toHexAddress(toAddress).substring(2), l);
System.out.println(parameter);
jsonObject.put("parameter", parameter);
jsonObject.put("owner_address", toHexAddress(TRX_CENTER_ADDRESS));
jsonObject.put("call_value", 0);
jsonObject.put("fee_limit", 100000000);
JSONObject trans = HttpsUtil.sendPost(TRON_NODE_URL + "/wallet/triggersmartcontract", jsonObject);
// System.out.println("签名前========" + trans);
Transaction transaction = Util.packTransaction(trans.getJSONObject("transaction").toJSONString(), false);
byte[] bytes = signTransaction2Byte(transaction.toByteArray(), ByteArray.fromHexString(TRX_CENTER_PRIKEY));
String signTransation = ByteArray.toHexString(bytes);
// System.out.println("签名后========" + signTransation);
// // 广播交易
JSONObject jsonObjectGB = new JSONObject();
jsonObjectGB.put("transaction", signTransation);
JSONObject transationCompelet = HttpsUtil.sendPost(TRON_NODE_URL + "/wallet/broadcasthex", jsonObjectGB);
System.out.println("广播交易后========" + transationCompelet);
// 判断交易是否成功
String txid = transationCompelet.getString("txid");
Map map = new HashMap<>();
map.put("txId",txid);
map.put("result",transationCompelet.getBoolean("result"));
return map;
}catch (Exception e){
return null;
}
}
private static byte[] signTransaction2Byte(byte[] transaction, byte[] privateKey)
throws InvalidProtocolBufferException {
ECKey ecKey = ECKey.fromPrivate(privateKey);
Transaction transaction1 = Transaction.parseFrom(transaction);
byte[] rawdata = transaction1.getRawData().toByteArray();
byte[] hash = Sha256Hash.hash(rawdata);
byte[] sign = ecKey.sign(hash).toByteArray();
return transaction1.toBuilder().addSignature(ByteString.copyFrom(sign)).build().toByteArray();
}
public static String encoderAbi(String Address, BigInteger amount) {
List inputParameters = new ArrayList<>();
inputParameters.add(new Address(Address));
inputParameters.add(new Uint256(amount));
return FunctionEncoder.encodeConstructor(inputParameters);
}
四:TRX转账
/**
* TRx转账
*
* @param toAddress
* @param
*/
public Map TrxTransation2(String toAddress, BigDecimal amount) {
try {
amount = amount.multiply(new BigDecimal("1000000"));
JSONObject jsonObject = new JSONObject();
jsonObject.put("to_address",toHexAddress(toAddress));
jsonObject.put("owner_address", toHexAddress(TRX_CENTER_ADDRESS));
jsonObject.put("amount", amount.toBigInteger());
JSONObject trans = HttpsUtil.sendPost(TRON_NODE_URL + "/wallet/createtransaction", jsonObject);
Transaction transaction = Util.packTransaction2(trans.toJSONString());
byte[] bytes = signTransaction2Byte(transaction.toByteArray(), ByteArray.fromHexString(TRX_CENTER_PRIKEY));
String signTransation = ByteArray.toHexString(bytes);
// // 广播交易
JSONObject jsonObjectGB = new JSONObject();
jsonObjectGB.put("transaction", signTransation);
JSONObject transationCompelet = HttpsUtil.sendPost(TRON_NODE_URL + "/wallet/broadcasthex", jsonObjectGB);
// 判断交易是否成功
String txid = transationCompelet.getString("txid");
Map map = new HashMap<>();
map.put("txId",txid);
map.put("result",transationCompelet.getBoolean("result"));
return map;
}catch (Exception e){
return null;
}
}
五: 查询trc20余额或trx余额
/**
* 查询Trx余额
*/
public BigDecimal getTrxBalance(String address) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("address", toHexAddress(address));
JSONObject trans = HttpsUtil.sendPost(TRON_NODE_URL + "/wallet/getaccount", jsonObject);
if (trans.isEmpty()) {
return BigDecimal.ZERO;
}
BigDecimal balance = trans.getBigDecimal("balance");
BigDecimal amount = balance.divide(new BigDecimal("1000000"), 8, BigDecimal.ROUND_DOWN);
return amount;
}
/**
* 查询TRC20合约余额
*/
public BigDecimal getTrc20Balance(String address, String contract) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("contract_address", toHexAddress(contract));
jsonObject.put("function_selector", "balanceOf(address)");
jsonObject.put("parameter", "0000000000000000000000" + toHexAddress(address));
jsonObject.put("owner_address", toHexAddress(address));
JSONObject trans = HttpsUtil.sendPost(TRON_NODE_URL + "/wallet/triggersmartcontract", jsonObject);
// System.out.println(trans);
String constant_result = trans.getJSONArray("constant_result").getString(0);
String constantResult = constant_result.replaceAll("^(0+)", "");
if (StringUtils.isBlank(constantResult)){
return BigDecimal.ZERO;
}
BigDecimal amountDecimal = new BigDecimal(new BigInteger(constantResult
, 16));
BigDecimal amount = BigDecimal.ZERO;
// 根据合约精度 除以对应0
if (contract.equalsIgnoreCase(KBTC_CONTRACT)) {
amount = amountDecimal.divide(new BigDecimal("1000000000000000000"), 8, BigDecimal.ROUND_DOWN);
} else if (contract.equalsIgnoreCase(USDT_CONTRACT)) {
amount = amountDecimal.divide(new BigDecimal("1000000"), 8, BigDecimal.ROUND_DOWN);
}
return amount;
}
]有错误希望朋友们指出来一下,我纠正一下,谢谢~。微信a284466315