1. 程式人生 > >以太坊 錢包 轉賬 查詢指定代幣餘額

以太坊 錢包 轉賬 查詢指定代幣餘額

前一篇講了下怎麼生成錢包  現在再來說一下如何進行轉賬 查指定代幣餘額 

依舊是基於web3j的   好了直接上程式碼 

//建立連線

Admin web3j = AdminFactory.build(new HttpService("你自己站點的地址"));

//獲取指定錢包的比特幣餘額

BigInteger integer=web3j.ethGetBalance(“錢包地址”,DefaultBlockParameterName.LATEST).send().getBalance();

//獲取指定錢包的指定幣種餘額

value=web3j.ethCall(Transaction.createEthCallTransaction(“錢包地址”,”代幣地址”, “交易串”),DefaultBlockParameterName.PENDING).send().getValue();

//交易串的獲取應該是這麼做的 這麼做的話需要去閱讀以太坊原始碼 並找到方法的名稱

//所以我直接寫死了"0x70a08231000000000000000000000000cb1bf954b73031918a58f001c3c3e7fb66daaf7c"

//前邊幾位是固定寫死的 後邊的cb1bf954b73031918a58f001c3c3e7fb66daaf7c 是要查詢餘額的錢包地址   只需要替換下

//地址就可以了

Function function = new Function(
        "查詢餘額方法名稱",
        Arrays.asList(new Address(“錢包地址”)),
        Arrays.asList(new TypeReference<Address>(){})
);
交易串= FunctionEncoder.encode(function);

//獲取NONCE
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
        fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
//交易的發起者在之前進行過的交易數量
BigInteger nonce = ethGetTransactionCount.getTransactionCount();

//建立交易  注意金額 保留小數點後8位 要轉化為整數 比如0.00000001 轉化為1
Function function = new Function(
        "transfer",//交易的方法名稱  


        Arrays.asList(new Address("收款錢包地址"),new Uint256("金額")),
        Arrays.asList(new TypeReference<Address>(){},new TypeReference<Uint256>(){})
);
String encodedFunction = FunctionEncoder.encode(function);
//智慧合約事物
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, Constants.GAS_PRICE, Constants.GAS_LIMIT,"代幣地址",encodedFunction);
//通過私鑰獲取憑證  當然也可以根據其他的獲取 其他方式詳情請看web3j
Credentials credentials = Credentials.create("私鑰");

byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
//傳送事務
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
//事物的HASH
String transactionHash = ethSendTransaction.getTransactionHash();

到此 錢包的轉賬就完成了   好了 就這些  程式碼寫的比較渣 所以沒有傳github 請見諒

如果轉載請標明出處 謝謝合作