惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 陈峰

yamux多路复用的使用例子 golang struct golang channel golang context - 陈峰 - 博客园 发布spring cloud + vue项目 以太坊上发行ERC20代币 [转]大白话讲解Promise(一) 比特币测试网络搭建 非对称加密, 助记词, PIN, WIF 搭建EOS未完 - 陈峰 - 博客园 解决 Unknown action buyram in contract eosio 错误 [转]EOS智能合约 & 私链激活 & 基本操作 [转]https://www.jianshu.com/p/06443248f4d8 [转]How to Send Ethereum with Web3.js and Node 【转】干货 | 【虚拟货币钱包】从 BIP32、BIP39、BIP44 到 Ethereum HD Wallet 钱包,卡 [转]vux使用教程 [转]CentOS7利用systemctl添加自定义系统服务 [转]重定向输出
web3js 进行转账
陈峰 · 2018-11-22 · via 博客园 - 陈峰

 1、准备阶段

部署以太坊geth

安装nodejs

npm install web3

npm install 

npm install ethereumjs-tx

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

2、在部署有geth的机器上,执行node命令, 就入控制台模式,然后逐行执行下面程序: 

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

输出输出:

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] }

最终成功,输出(这里等待了很长时间):

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