web3js 進行轉帳

 一、準備階段javascript

部署以太坊gethjava

安裝nodejsnode

npm install web3web

npm install npm

npm install ethereumjs-tx服務器

其中, web3是1.0.0.beta-36版本網絡

 

二、在部署有geth的機器上,執行node命令, 就入控制檯模式,而後逐行執行下面程序: ide

const Web3 = require('web3')
const EthereumTx = require('ethereumjs-tx')
const web3 = new Web3( new Web3.providers.HttpProvider('http://47.254.215.233:8145') )                // 服務器RPC地址                                                            
web3.eth.defaultAccount = 'af7574a21729d22e119df7456bf913194d73c209'                                  // 發送方地址                                                               
                                                                                                                                                                                  
let details = {                                                                                                                                                                   
    "to": '0x5cf83df52a32165a7f392168ac009b168c9e8915',                                               // 接收方地址                                                               
    "value": 4052500000,                                                                              // 轉帳多少wei                                                              
    "gas": 51000,                                                                                     // GAS                                                                      
    "gasPrice": 2 * 1000000000,                                                                      // GAS PRICE 須要一個比較合理的價格, 否則可能會報錯(replacement transaction underpriced)                                                                               
    "gasLimit": '0x420710',                                                                        
    "nonce": 40,                                                                                     // 序號ID, 重要, 須要一個帳號的交易序號,能夠經過web3.eth.getTransactionCount(web3.eth.defaultAccount)得到
    "chainId": 11243575,                                                                              
    "data":'0x40aabbccdd000011223344'                                                                                                                                             
  }                                                                                                    // 網絡ID, 這是區別於具體在哪一個網絡上                                     
                                                                                                                                                                                  
var tx = new EthereumTx(details)
tx.sign( Buffer.from('FC1A5C66932BD7C93E05661A58FD534967739CE517D8695EBBE89135CEE78DA4', 'hex') )
var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', console.log);

  

  

輸出輸出:ui

Promise {
<pending>,
_events: { receipt: EE { fn: [Function: bound ], context: [Circular], once: false } },
emit: [Function: emit],
on: [Function: on],
once: [Function: once],
off: [Function: removeListener],
listeners: [Function: listeners],
addListener: [Function: on],
removeListener: [Function: removeListener],
removeAllListeners: [Function: removeAllListeners] }3d

 

最終成功,輸出(這裏等待了很長時間):

{ blockHash: '0x7a0f412ba0b09dfcfacb826712f4b8e5089e8d1c6d35385d2e29d5dc4400ab37', blockNumber: 155425, contractAddress: null, cumulativeGasUsed: 63000, from: '0xaf7574a21729d22e119df7456bf913194d73c209', gasUsed: 21000, logs: [], logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', status: true, to: '0x5cf83df52a32165a7f392168ac009b168c9e8915', transactionHash: '0x6030b35f4d948a83370d0a759be98820eb72d94623e9d9063fabaab4a9543aed', transactionIndex: 2 }

相關文章
相關標籤/搜索