btc、usdt 根据交易hash如何得到交易明细和手续费
V刘晨曦
发表于 2022-12-20 00:43:20
139
0
0
1.在自己的钱包节点上根据txId获取交易明细0 u2 N, k3 i7 r+ f2 B0 ]3 D
2.通过原始交易递归计算手续费: 公式[ 手续费 sum(vin)- sum(vout) ]# m2 W! F" h# |# i8 ]
要求:# A/ [- B/ \9 [6 x" \1 p5 Y0 y
需要对utxo有一定的了解~~~
直接low代码:
//交易信息/ g0 y$ {4 Z# e `* J# i: k) h
public class BtcTransactionInfo implements Serializable {0 a9 z$ J4 s. G) v R: G
0 S. a t7 C0 e q# n
private static final long serialVersionUID = 1L;! h# G7 n; e7 Z" [$ W
; g; T( i2 w/ d! v% X" A" c
private String txId;
private String hash;+ b9 j$ C( Y4 f% G' I! B
private String blockheight;! Y; v5 R; w* |
private String blockweight;6 B+ J# C. q2 M% z8 q' x! Y: f
private String blockconfirmations;) e# o* `) U) i X
private String blockversion;
private String previousblockhash;3 U3 A; L) F& t+ Y# P0 d1 U+ S
private String nextblockhash;3 x* Q8 E0 a& M) l- h/ T
private String version;7 m+ U0 c; c' I8 E h
private String size;. {& ]% t1 R, v. N# f. ]
private String vsize;: O* _+ y4 Q* [& a+ N7 p
private String locktime;
private String hex;$ T" |- D8 s. _7 l% e
private String blockhash;
private String confirmations;/ j" P; s5 D8 P7 O
private String time;
private String blocktime;, u1 O, u' F' \( D
private BigDecimal amount;
private BigDecimal free;+ @. t) P' B# W$ J) Y
private String from;
private String to;
: g9 t: ?1 ]1 O/ D+ e5 u- D z$ a
}
//根据交易的txid调用钱包api获取相应信息
public Object getrawTransactionByTxIdFromWallet(String txId) throws CoinException {7 m S8 r! v( L; M
BtcTransactionInfo tranc = new BtcTransactionInfo();
try {( F$ \ v2 ?6 M0 P# z
//调用原始交易接口5 r' c% q9 N! e( U! e8 J
Map transaction = (Map) btService.getTrawtransaction(txId, 1);# h# J; U+ c/ w
JSONObject info = JSONObject.parseObject(JSON.toJSONString(transaction));
String blockHash = info.getString("blockhash");
/*block*/
//调用根据blockhash 获取块信息接口
Map blockInfo = (Map)btService.getblock(blockHash);
JSONObject block = JSONObject.parseObject(JSON.toJSONString(blockInfo));
tranc.setBlockconfirmations(block.getString("confirmations")); M2 Q; c0 ~9 m/ v* b7 \) D7 L
tranc.setBlocktime(block.getString("time"));" @' P d0 @9 T% N! m% g9 O# F
tranc.setBlockversion(block.getString("version"));: N. G- k: b2 i
tranc.setBlockheight(block.getString("height"));
tranc.setBlockweight(block.getString("weight"));- c2 O" W9 H" m" k% m, \
tranc.setPreviousblockhash(block.getString("previousblockhash"));: @* p& W& T5 K2 Q$ r
tranc.setNextblockhash(block.getString("nextblockhash"));
! h5 ]2 n/ i" w/ W
tranc.setTxId(info.getString("txid"));6 |6 J, ^# T0 Z6 L
tranc.setHash(info.getString("hash"));+ A/ B- A7 {4 e3 G( b+ x' p) l
tranc.setVersion(info.getString("version"));& t8 [$ x) r: e
tranc.setSize(info.getString("size"));) H; m# n& O9 J9 M
tranc.setVsize(info.getString("vsize"));0 S" t. \! V6 ?) t
tranc.setLocktime(info.getString("locktime"));2 `1 T" j$ \2 I1 {- J y7 x7 z
tranc.setHex(info.getString("hex"));( a( e/ o ~% T# m- o, p' P
tranc.setBlockhash(blockHash);
tranc.setConfirmations(info.getString("confirmations"));+ b, J& L P) F. y( {
tranc.setTime(info.getString("time"));3 P$ f8 R' N4 w+ V; g1 {! O4 v) a/ S& s
tranc.setBlocktime(info.getString("blocktime"));' s8 z0 k1 L7 Q' e, Z3 |# f
JSONArray vins = JSONArray.parseArray(info.getString("vin"));
double sumvin = 0 ;
List formAddress = new ArrayList();0 M l7 t" ?; G0 t' K+ [
for (int i = 0,len = vins.size(); i
成为第一个吐槽的人