0%

eth | web3.js 转账主网币

转账有很多种,这里直接放代码了。

另外,方法应该有很多种,所以,这里列出一些经过我检测的可执行代码。

这里举的例子是虎符的智能链。


环境


  • ethereumjs-tx: ^1.3.7
  • web3: ^1.3.6

版本一


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import Web3 from "web3"
import Tx from "ethereumjs-tx"

let w3 = new Web3(new Web3.providers.HttpProvider("https://http-mainnet.hoosmartchain.com"));

const send_hoo = () => {
w3.eth.getBalance("0x046DFD38e4eeB06073782e3DADFB8e56cAC360cE", ((error, balance) => {
if (error) {
return
}
console.log(balance);

w3.eth.getTransactionCount("0x046DFD38e4eeB06073782e3DADFB8e56cAC360cE", ((error, count) => {
if (error) {
return
}
console.log(count);

let rawTx = {
from: '0x046DFD38e4eeB06073782e3DADFB8e56cAC360cE',
to: '0x26b1724D9F6CA5DEe5DAA308f197dF7eafe4EB93',
value: w3.utils.toHex(w3.utils.toWei('0.001', 'ether')),
nonce: w3.utils.toHex(count),
gasLimit: w3.utils.toHex(8000000),
gasPrice: w3.utils.toHex(w3.utils.toWei('1', 'gwei')),
}

let tx = new Tx(rawTx);
tx.sign(new Buffer('985f728fccaf6****私钥', 'hex'));
let serializedTx = tx.serialize();
let raw = '0x' + serializedTx.toString('hex')
w3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('txHash:', txHash)
console.log(err)
})
}))
}))
}

send_hoo();
请我喝杯咖啡吧~