美文网首页
以太坊入门(三)用web3j进行以太转账及代币转账

以太坊入门(三)用web3j进行以太转账及代币转账

作者: 酒趣琴音 | 来源:发表于2018-08-14 22:53 被阅读0次

    上章讲到账户的查询,本章讲述账户转账。

    1. 以太坊转账

           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. 以太坊代币转账

           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);
           
           }
      

    代币转账和以太转账的区别在于,to地址是合约地址,而input是有三部分数据构成:transfer方法的哈希+收款人的地址+转账金额。此处比较难理解的正是Function部分,设置好参数以后,调用rawTransaction就可以了。

    相关文章

      网友评论

          本文标题:以太坊入门(三)用web3j进行以太转账及代币转账

          本文链接:https://www.haomeiwen.com/subject/tkvbbftx.html