Web3JS文档速读 (3)啃最硬的骨头 Eth

账户

eth.defaultAccount 获取或者设置默认 from 账户.

1
2
3
4
web3.eth.sendTransaction()
web3.eth.call()
new web3.eth.Contract() -> myContract.methods.myMethod().call()
new web3.eth.Contract() -> myContract.methods.myMethod().send()

eth.getAccounts().then(accounts => {}); 节点中的账户列表

eth.getBalance(addr [,defaultBlock]).then(bigNum => {}); 账户在某节点的余额

节点状态

eth.defaultBlock 默认区块, 值为 Number | Genesis | latest(默认) | pending 几种情况.

1
2
3
4
5
6
web3.eth.getBalance()
web3.eth.getCode()
web3.eth.getTransactionCount()
web3.eth.getStorageAt()
web3.eth.call()
new web3.eth.Contract() -> myContract.methods.myMethod().call()

上面几种方法会使用默认区块进行查询.

eth.getProtocolVersion(version => {}); 协议版本

eth.isSyncing(obj|false => {}); 是否处于同步状态

1
2
3
4
5
6
7
{
startingBlock: 100,
currentBlock: 312,
highestBlock: 512,
knownStates: 234566,
pulledStates: 123455
}

eth.getBlockNumber().then(num => {}); 当前区块号

合约编译

eth.getCompilers().then(arr => {}); 获取支持的编译器 ['lll', 'solidity', 'serpent']

eth.compile.solidity(sourceCode).then(code => {}); 编译合约

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"test": {
"code": "0x605280600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b60376004356041565b8060005260206000f35b6000600782029050604d565b91905056",
"info": {
"source": "contract test {\n\tfunction multiply(uint a) returns(uint d) {\n\t\treturn a * 7;\n\t}\n}\n",
"language": "Solidity",
"languageVersion": "0",
"compilerVersion": "0.8.2",
"abiDefinition": [{"constant": false,"inputs": [{"name": "a","type": "uint256"}],"name": "multiply","outputs": [{"name": "d","type": "uint256"}],"type": "function"}],
"userDoc": {"methods": {}},
"developerDoc": {"methods": {}}
}
}
}

eth.compile.lll(sourceCode).then(code => {}); 编译合约

eth.compile.serpent(sourceCode).then(code => {}); 编译合约

1
"0x605280600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b60376004356041565b8060005260206000f35b6000600782029050604d565b91905056"

挖矿信息

eth.getCoinbase().then(addr=>{}); 挖矿收益账户

eth.isMining().then(bool => {}); 是否处于挖矿状态

eth.getHashrate().then(num => {}); 挖矿每秒 hash 计算速度

eth.getGasPrice().then(bigPrice => {}); 最近几个区块的中值

eth.getWork().then(arr => {}); 获取挖矿信息, 返回3个 bytes32

eth.submitWork(nonce, powHash, digest).then(bool => {}); 提交挖矿证明

区块和交易信息

1
eth.getStorageAt(addr, pos, [,defaultBlock]).then(str => {});  // Get the storage at a specific position of an address.
1
eth.getCode(addr [,defaultBlock]).then(data => {}); // 在指定地址的数据.

blockId: blockHash, blockNumber, 或者 “genesis”, “latest”, “pending” 字符串

1
eth.getBlockTransactionCount(blockId).then(amount => {});  // 区块交易数
1
eth.getTransactionCount(addr [,defaultBlock]).then(amount => {});  // addr 在区块的交易数.

区块信息

1
eth.getBlock(blockId [,withTxsObj]).then(obj => {});
1
eth.getUncle(blockId, uncleIndex [,withTxsObj]).then(obj => {});  获取区块 uncle 信息.

withTxsObj 表示是否包含交易详情, 默认 false, 只包含 transaction hash 列表.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"number": 3,
"hash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
"parentHash": "0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88",
"nonce": "0xfb6e1a62d119228b",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"transactionsRoot": "0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee",
"stateRoot": "0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb",
"miner": "0x8888f1f195afa192cfee860698584c030f4c9db1",
"difficulty": '21345678965432',
"totalDifficulty": '324567845321',
"size": 616,
"extraData": "0x",
"gasLimit": 3141592,
"gasUsed": 21662,
"timestamp": 1429287689,
"transactions": [
"0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b"
],
"uncles": []
}

获取交易信息

1
eth.getTransaction(txHash).then(txObj => {});
1
eth.getTransactionFromBlock(blockId, idx).then(txObj => {});
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"hash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
"nonce": 2,
"blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
"blockNumber": 3,
"transactionIndex": 0,
"from": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"to": "0x6295ee1b4f6dd65047762f924ecd367c17eabf8f",
"value": '123450000000000000',
"gas": 314159,
"gasPrice": '2000000000000',
"input": "0x57cb2fc4"
}
1
eth.getTransactionReceipt(txHash).then(obj => {}); // 获取交易详情 Logs, 如果是 pending transaction 则返回 null.
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"status": true,
"transactionHash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
"transactionIndex": 0,
"blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
"blockNumber": 3,
"contractAddress": "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
"cumulativeGasUsed": 314159,
"gasUsed": 30234,
"logs": [{
// logs as returned by getPastLogs, etc.
}, ...]
}
1
2
3
4
5
eth.getPastLogs({
fromBlock: blockId, toBlock: blockId,
address: str|arr,
topics: arr|null
}).then(logs => {}); // 获取区块中(仅包含指定地址发起)的交易
1
2
3
4
5
6
7
8
9
10
[{
data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385']
logIndex: 0,
transactionIndex: 0,
transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
blockNumber: 1234,
address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
},{...}]

交易

1. 发起交易(写方法/合约部署)

eth.sendTransaction({
from: 交易发起账户, 默认 eth.defaultAccount,
to: 交易目标账户, 如果是部署合约不设置为 undefined,
value: Number|str|BN|BigNumber 转账 wei 数,
gas: 为交易提供的 gas 最大值, 多付的会退还,
gasPrice: Number|str|BN|BigNumber 价格 wei 数, 默认 eth.getPrice,
data: 部署合约的初始化二进制, 或者调用合约方法的二进制代码,
nonce: 用于覆盖自己 pending 种状态的交易
})
.on(‘transactionHash’, hash => {})
.on(‘receipt’, receipt => {})
.on(‘confirmation’, (confirmationNumber, receipt) => {})
.on(‘error’, (err, receipt) => {}); // If a out of gas error, the second parameter is the receipt.

如果 from 是钱包中的地址, 那么将会使用 private key 在本地进行签名, 然后使用 sendSignedTransaction 发送交易.

2. 调用写方法

sign(str,addr)/signTransaction(txObj,addr) + sendSignedTransaction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const Tx = require('ethereumjs-tx');
const privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')

const rawTx = {
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}

const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize();

// console.log(serializedTx.toString('hex'));
// 0xf889808609184e72a00082271094000000000000000000000000000000000000000080a47f74657374320000000000000000000000000000000000000000000000000000006000571ca08a8bbf888cfa37bbf0bb965423625641fc956967b81d12e23709cead01446075a01ce999b56a8a88504be365442ea61239198e23d1fce7d00fcfc5cd3b44b7215f

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', receipt => {});

3. 调用只读方法

eth.call({to:, data:,}).then(returns => {});

4. gas 预估

eth.estimateGas({to:, data:,}).then(returns => {});

Donate - Support to make this site better.
捐助 - 支持我让我做得更好.