本文代码应用场景:
每个用户在交易所充值时,每个人都分配了独立的USDT地址,而交易所需要将所有独立的地址中USDT汇集到一个钱包地址(一般是冷钱包),从而实现资产归集与安全保障。
注意:
理解以下代码最好先搭建好一个USDT的节点,再在命令行中体验以下RPC的调用效果。
package com.bizzan.col;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import org.apache.commons.codec.binary.Base64;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class UsdtCollect { private String url = "http://127.0.0.1:18332";
private String username = "u";
private String password = "p";
//outValue是总钱数,这个一般在充值记录里面计算。我就不调用钱包去查询了
public String collectionUsdt( BigDecimal outValue) {
JsonRpcHttpClient client = null;
try {
Map[] argsOne = new Map[1];
Map[] args1 = new Map[1];
String creb = Base64.encodeBase64String((username+":"+password).getBytes());
Map<String,String> headers = new HashMap<>(2);
headers.put("Authorization","Basic "+creb);
headers.put("server", "1");
client = new JsonRpcHttpClient(new URL(url), headers);
List<Map> listunspent = (List<Map>) client.invoke("listunspent", new Object[]{}, Object.class);
for (Map map : listunspent) {
Double amount=(Double) map.get("amount");
//这里是找一笔uxto的btc交易做桥梁
if(amount>1){
String txId =(String) map.get("txid");
String scriptPubKey =(String) map.get("scriptPubKey");
int vout =(int) map.get("vout");
Map input = new HashMap<>();
input.put("txid", txId);
input.put("vout", vout);
args1[0] = input;
Map inputT = new HashMap<>();
inputT.put("txid", txId);
inputT.put("vout",vout);
inputT.put("scriptPubKey", scriptPubKey);
inputT.put("value", amount);
argsOne[0] = inputT;
break;
}
}
//计算字节大小和费用(因为是归集USDT 所以我用最小的输入来降低手续费,如果你是BTC和USDT一起归总那就要根据归集的输入来计算了)
BigDecimal keyCount = calculationFee(1);
//将聪换算成BTC
BigDecimal transferFee = keyCount.divide(new BigDecimal("100000000"), 8, RoundingMode.HALF_UP);
if (transferFee.compareTo(BigDecimal.ZERO) <= 0 || outValue.compareTo(transferFee) <= 0) {
return null;
}
/**
* 通过全节点构造原生交易
*/
//创建BTC交易
Map args2 = new HashMap<>();
Object result = (Object) client.invoke("createrawtransaction", new Object[]{args1,args2 }, Object.class);
String transaction = String.valueOf(result);
/*//解锁钱包
client.invoke("walletpassphrase", new Object[]{"xxxx", 100}, Object.class);*/
//创建Usdt交易
String simplesendResult = (String) client.invoke("omni_createpayload_simplesend", new Object[]{ 1,outValue.toString() }, Object.class);
//usdt交易附加到BTC交易上
String opreturnResult = (String) client.invoke("omni_createrawtx_opreturn", new Object[]{transaction,simplesendResult}, Object.class);
//设置归总地址
String reference = (String) client.invoke("omni_createrawtx_reference", new Object[]{opreturnResult,"归总地址"}, Object.class);
//填写手续费及找零地址
String changeResult = (String) client.invoke("omni_createrawtx_change", new Object[]{reference,argsOne,"找零地址",transferFee.toString()}, Object.class);
//获取原生交易hex
Map signrawtransaction = (Map) client.invoke("signrawtransaction", new Object[]{changeResult}, Object.class);
//广播交易
String txId = (String) client.invoke("sendrawtransaction", new Object[]{signrawtransaction.get("hex")}, Object.class);
return txId;
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable e1) {
e1.printStackTrace();
}
return null;
} /**
* 计算手续费
*
* @param inputCount
* @return
*/
private BigDecimal calculationFee( int inputCount) {
//计算手续费获取每个字节的手续费
String url="bitcoinfees.earn.com";
//计算字节大小和费用
String resut=sendGet(url,null);
//=====resut===>转对象Model省略了,其实http请求都有公用的方法所以我随便写了。。
BigDecimal keyCount = BigDecimal.valueOf((inputCount * 148 + 44) * Model.getHalfHourFee());
//
return keyCount; }
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
BIZZAN(币严) 数字货币交易所官方网址:
www.bizzan.com