以太坊入門(三)用web3j進(jìn)行以太轉(zhuǎn)賬及代幣轉(zhuǎn)賬

上章講到賬戶的查詢,本章講述賬戶轉(zhuǎn)賬。

  1. 以太坊轉(zhuǎn)賬

         We3j web3j = Web3j.build(new HttpService(ConstantLibs.WEB3_ADDRESS));
    
         Credentials credentials = WalletTool.loadCredentials(fromAddress);
    
         EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
                 fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
    
         BigInteger nonce = ethGetTransactionCount.getTransactionCount();
    
         RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
                 nonce, Convert.toWei("18", Convert.Unit.GWEI).toBigInteger(),
                 Convert.toWei("45000", Convert.Unit.WEI).toBigInteger(), toAddress, new BigInteger(amount));
         byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
         String hexValue = Numeric.toHexString(signedMessage);
    
         EthSendTransaction ethSendTransaction = web3.ethSendRawTransaction(hexValue).sendAsync().get();
         if (ethSendTransaction.hasError()) {
             log.info("transfer error:", ethSendTransaction.getError().getMessage());
         } else {
             String transactionHash = ethSendTransaction.getTransactionHash();
             log.info("Transfer transactionHash:" + transactionHash);
         }
    
  2. 以太坊代幣轉(zhuǎn)賬

         Web3j web3j = Web3j.build(new HttpService(ConstantLibs.WEB3_ADDRESS));
    
         Credentials credentials = WalletTool.loadCredentials(fromAddress);
    
         EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
                 fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
    
         BigInteger nonce = ethGetTransactionCount.getTransactionCount();
    
         Function function = new Function(
                 "transfer",
                 Arrays.asList(new Address(toAddress), new Uint256(new BigInteger(amount))),  
                 Arrays.asList(new TypeReference<Type>() {
                 }));
    
         String encodedFunction = FunctionEncoder.encode(function);
    
         RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, Convert.toWei("18", Convert.Unit.GWEI).toBigInteger(),
                 Convert.toWei("100000", Convert.Unit.WEI).toBigInteger(), contractAddress, encodedFunction);
    
         byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
         String hexValue = Numeric.toHexString(signedMessage);
    
         log.debug("transfer hexValue:" + hexValue);
    
         EthSendTransaction ethSendTransaction = web3.ethSendRawTransaction(hexValue).sendAsync().get();
         if (ethSendTransaction.hasError()) {
             log.info("transfer error:", ethSendTransaction.getError().getMessage());
            
         } else {
             String transactionHash = ethSendTransaction.getTransactionHash();
             log.info("Transfer transactionHash:" + transactionHash);
         
         }
    

代幣轉(zhuǎn)賬和以太轉(zhuǎn)賬的區(qū)別在于,to地址是合約地址,而input是有三部分?jǐn)?shù)據(jù)構(gòu)成:transfer方法的哈希+收款人的地址+轉(zhuǎn)賬金額。此處比較難理解的正是Function部分,設(shè)置好參數(shù)以后,調(diào)用rawTransaction就可以了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 以ERC20轉(zhuǎn)賬為例 token轉(zhuǎn)賬參數(shù)即data字段String methodName = "transfer"...
    fengshunli閱讀 5,003評(píng)論 0 1
  • 以太坊的開發(fā),基本都是go語言和nodejs的天下,web3j出現(xiàn)給java開發(fā)人員提供了很大的便利。本文會(huì)對(duì)一些...
    酒趣琴音閱讀 1,799評(píng)論 0 2
  • 英文文檔,一開始我也是抗拒的,邊翻譯邊看,也就花費(fèi)了1個(gè)小時(shí)基本就閱讀過了,我的英文基礎(chǔ)其實(shí)很差。附上鏈接:鏈接:...
    lonecolonel閱讀 10,411評(píng)論 3 1
  • 鳥又犯了“不想去幼兒園”的病。。。 懶得勸。干脆跟她一起“不想去”。 我說明天起,你不去幼兒園,我不去上班。咱倆在...
    S貓閱讀 609評(píng)論 10 8
  • 驕傲的風(fēng)越過了地平線 迷途的羔羊找尋遠(yuǎn)方的呼喚 月亮離開靜靜的河岸 來到我垂老的窗前 鉆進(jìn)一只空酒杯 照亮了半生的...
    只有兩天閱讀 333評(píng)論 0 9

友情鏈接更多精彩內(nèi)容