web3.eth详解

                                              --1--

web3.eth
该web3-eth软件包允许您与以太坊区块链和以太坊智能合约进行交互。

var Eth = require(‘web3-eth’);

// 类库加载

var eth = new Eth(Eth.givenProvider || ‘ws://some.local-or-remote.node:8546’);

// 类库加载
var Web3 = require(‘web3’);

var web3 = new Web3(Web3.givenProvider || ‘ws://some.local-or-remote.node:8546’);
关于校验和地址的注意事项
此包的函数返回的所有以太坊地址都作为校验和地址返回。这意味着一些字母是大写的,一些是小写的。基于此,它将计算地址的校验和并证明其正确性。传递给函数时,错误的校验和地址将引发错误。如果要绕过校验和检查,可以将地址设置为小写或大写。

示例
web3.eth.getAccounts(console.log);//返回节点控制的帐户列表

[“0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe” ,“0x85F43D8a49eeB85d32Cf465507DD71d507100C1d”]
setProvider
web3.setProvider(myProvider)//设置连接的节点信息

web3.eth.setProvider(myProvider)

web3.shh.setProvider(myProvider)

web3.bzz.setProvider(myProvider)…
将更改其模块的提供程序。

注意

当伞包调用web3它也将设置供应商所有子模块web3.eth,web3.shh等除非web3.bzz它需要在任何时候都一个单独的供应商。

参数
Object- myProvider:有效的提供者。

返回
Boolean

示例
var Web3 = require(‘web3’);var web3 = new Web3(‘http://localhost:8545’);// 或

var web3 = new Web3(new Web3.providers.HttpProvider(‘http://localhost:8545’));// change provider

web3.setProvider(‘ws://localhost:8546’);// 或

web3.setProvider(new Web3.providers.WebsocketProvider(‘ws://localhost:8546’));// Using the IPC provider in node.js

var net = require(‘net’);

var web3 = new Web3(’/Users/myuser/Library/Ethereum/geth.ipc’, net); // mac os path// or

var web3 = new Web3(new Web3.providers.IpcProvider(’/Users/myuser/Library/Ethereum/geth.ipc’, net)); // mac os path// on windows the path is: “\\.\pipe\geth.ipc”// on linux the path is: “/users/myuser/.ethereum/geth.ipc”
提供者
web3.providersweb3.eth.providersweb3.shh.providersweb3.bzz.providers…
包含当前可用的提供程序。

价值
Object 与以下提供商:

Object- HttpProvider:不推荐使用 HTTP提供程序,因为它不适用于订阅。

Object- WebsocketProvider:Websocket提供程序是旧版浏览器中使用的标准。

Object- IpcProvider:IPC提供程序在运行本地节点时使用node.js dapps。提供最安全的连接。

示例
var Web3 = require(‘web3’);

//各种节点连接方法

var web3 = new Web3(Web3.givenProvider || ‘ws://remotenode.com:8546’);// 或

var web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider(‘ws://remotenode.com:8546’));// Using the IPC provider in node.js

var net = require(‘net’);var web3 = new Web3(’/Users/myuser/Library/Ethereum/geth.ipc’, net); // mac os path// or

var web3 = new Web3(new Web3.providers.IpcProvider(’/Users/myuser/Library/Ethereum/geth.ipc’, net)); // mac os path// on windows the path is: “\\.\pipe\geth.ipc”// on linux the path is: “/users/myuser/.ethereum/geth.ipc”
givenProvider
web3.givenProvider

web3.eth.givenProvider

web3.shh.givenProvider

web3.bzz.givenProvider…
在兼容以太坊的浏览器中使用web3.js时,它将使用该浏览器设置当前的本机提供程序。将通过(浏览器)环境返回给定的提供者,否则null。

返回
Object:给定的提供者集或null;

示例
currentProvider
web3.currentProvider

web3.eth.currentProvider

web3.shh.currentProvider

web3.bzz.currentProvider…
否则将返回当前提供者null。

返回
Object:当前提供商集或null;

示例
BatchRequest
new web3.BatchRequest()

new web3.eth.BatchRequest()

new web3.shh.BatchRequest()

new web3.bzz.BatchRequest()
用于创建和执行批处理请求的类。

参数
没有

返回
Object:使用以下方法:

add(request):向批处理调用添加请求对象。

execute():将执行批处理请求。

示例
var contract = new web3.eth.Contract(abi, address);

var batch = new web3.BatchRequest();

batch.add(

web3.eth.getBalance.request(‘0x0000000000000000000000000000000000000000’

, ‘latest’, callback));

batch.add(contract.methods.balance(address).call

.request({from: ‘0x0000000000000000000000000000000000000000’},
callback2));

batch.execute();
延伸
web3.extend(methods)

web3.eth.extend(methods)

web3.shh.extend(methods)

web3.bzz.extend(methods)…
允许扩展web3模块。

注意

您还*.extend.formatters可以使用其他格式化函数来输入和输出格式。有关功能详细信息,请参阅源文件。

参数

methods- Array:方法描述数组:

name- String:要添加的方法的名称。

call- String:RPC方法名称。

params- Number:(可选)该函数的参数数量。默认值为0。

inputFormatter- Array:(可选)inputformatter函数数组。每个数组项都响应一个函数参数,因此如果您希望某些参数不被格式化,请添加一个参数null。

outputFormatter - ``Function:(可选)可用于格式化方法的输出。

property- String:(可选)要添加到模块的属性的名称。如果未设置任何属性,则会直接将其添加到模块中。

methods- Object:具有方法描述对象数组的扩展对象,如下所示:

返回
Object:扩展模块。

示例
web3.extend({
property: ‘myModule’,
methods: [{
name: ‘getBalance’,
call: ‘eth_getBalance’,
params: 2,
inputFormatter: [web3.extend.formatters.inputAddressFormatter, web3.extend.formatters.inputDefaultBlockNumberFormatter],
outputFormatter: web3.utils.hexToNumberString
},{
name: ‘getGasPriceSuperFunction’,
call: ‘eth_gasPriceSuper’,
params: 2,
inputFormatter: [null, web3.utils.numberToHex]
}]});

web3.extend({
methods: [{
name: ‘directCall’,
call: ‘eth_callForFun’,
}]});
console.log(web3);

Web3 {
myModule: {
getBalance: function(){},
getGasPriceSuperFunction: function(){}
},
directCall: function(){},
eth: Eth {…},
bzz: Bzz {…},
…}
defaultAccount
web3.eth.defaultAccount
"from"如果没有"from"为以下方法指定属性,则此缺省地址将用作缺省属性:

web3.eth.sendTransaction()

web3.eth.call()

new web3.eth.Contract() - > myContract.methods.myMethod()。call()

new web3.eth.Contract() - > myContract.methods.myMethod()。send()

属性
String - 20字节:任何以太坊地址。您应该在节点或密钥库中拥有该地址的私钥。(默认是undefined)

示例
web3.eth.defaultAccount;> undefined// 设置默认账号

web3.eth.defaultAccount = ‘0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe’;

defaultBlock
web3.eth.defaultBlock
默认块用于某些方法。您可以通过传入defaultBlock作为最后一个参数来覆盖它。默认值为“latest”。

web3.eth.getBalance()

web3.eth.getCode()

web3.eth.getTransactionCount()

web3.eth.getStorageAt()

web3.eth.call()

new web3.eth.Contract() - > myContract.methods.myMethod()。call()

属性
默认块参数可以是以下之一:

Number:块号

“genesis”- String:创世块

“latest”- String:最新块(区块链的当前负责人)

“pending”- String:当前挖掘的块(包括待处理的事务)

默认是 “latest”

示例
web3.eth.defaultBlock;

“latest”// 设置默认块

web3.eth.defaultBlock = 231;
getProtocolVersion
web3.eth.getProtocolVersion([callback])
返回节点的以太坊协议版本。

返回
Promise返回String:协议版本。

示例
web3.eth.getProtocolVersion().then(console.log);

“63”
isSyncing
web3.eth.isSyncing([callback])
检查节点当前是否正在同步并返回同步对象,或false。

返回
Promise返回Object|Boolean- 节点当前正在同步时的同步对象或false:

startingBlock- Number:同步开始的块编号。

currentBlock- Number:节点当前同步到哪个块的块编号。

highestBlock- Number:要同步的估计块编号。

knownStates- Number:估计要下载的状态

pulledStates- Number:已下载的状态

示例
web3.eth.isSyncing().then(console.log);> {
startingBlock: 100,
currentBlock: 312,
highestBlock: 512,
knownStates: 234566,
pulledStates: 123455}
getCoinbase
getCoinbase([callback])
返回采矿奖励将进入的coinbase地址。

返回
Promise返回String- 字节20:在挖掘奖励的节点中设置的coinbase地址。

示例
web3.eth.getCoinbase().then(console.log);

“0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe”
isMining
web3.eth.isMining([callback])
检查节点是否正在挖掘。

返回
Promise返回Boolean:true如果节点正在挖掘,否则返回false。

示例
web3.eth.isMining().then(console.log);

true
getHashrate
web3.eth.getHashrate([callback])
返回节点挖掘的每秒哈希数。

返回
Promise返回Number:每秒的哈希数。

示例
web3.eth.getHashrate().then(console.log);

493736

getGasPrice
web3.eth.getGasPrice([callback])
返回当前的天然气价格oracle。天然气价格由最后几个区块中位数天然气价格决定。

返回
Promisereturns String- wei当前天然气价格的数字字符串。

请参阅有关处理JavaScript中大数字的说明。

示例
web3.eth.getGasPrice().then(console.log);

“20000000000”
getAccounts
web3.eth.getAccounts([callback])
返回节点控制的帐户列表。

返回
Promisereturns Array- 由节点控制的地址数组。

示例
web3.eth.getAccounts().then(console.log);

[“0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe”, “0xDCc6960376d6C6dEa93647383FfB245CfCed97Cf”]
getBlockNumber
web3.eth.getBlockNumber([callback])
返回当前块编号。

返回
Promisereturns Number- 最近块的编号。

示例
web3.eth.getBlockNumber().then(console.log);

2744
为getBalance
web3.eth.getBalance(address [, defaultBlock] [, callback])
获取给定块的地址余额。

参数
String - 获得平衡的地址。

Number|String- (可选)如果传递此参数,则不会使用web3.eth.defaultBlock设置的默认块。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回String- wei中给定地址的当前余额。

请参阅有关处理JavaScript中大数字的说明。

示例
web3.eth.getBalance(“0x407d73d8a49eeb85d32cf465507dd71d507100c1”).then(console.log);

“1000000000000”
getStorageAt
web3.eth.getStorageAt(address, position [, defaultBlock] [, callback])
将存储空间放在地址的特定位置。

参数
String - 从中获取存储的地址。

Number - 存储的索引位置。

Number|String- (可选)如果传递此参数,则不会使用web3.eth.defaultBlock设置的默认块。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回String- 给定位置的存储值。

示例
web3.eth.getStorageAt(“0x407d73d8a49eeb85d32cf465507dd71d507100c1”, 0)
.then(console.log);

“0x033456732123ffff2342342dd12342434324234234fd234fd23fd4f23d4234”
引用代码
web3.eth.getCode(address [, defaultBlock] [, callback])
获取特定地址的代码。

参数
String - 从中获取代码的地址。

Number|String- (可选)如果传递此参数,则不会使用web3.eth.defaultBlock设置的默认块。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回String- 给定地址的数据address。

示例
web3.eth.getCode(“0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8”)
.then(console.log);

“0x600160008035811a818181146012578301005b601b6001356025565b8060005260206000f25b600060078202905091905056”

getBlock
web3.eth.getBlock(blockHashOrBlockNumber [, returnTransactionObjects] [, callback])
返回与块编号或块哈希匹配的块。

参数
String|Number - 块编号或块哈希。或者字符串"genesis","latest"或者"pending"在默认的块参数中。

Boolean- (可选,默认false)如果true,返回的块将包含所有事务作为对象,如果false它只包含事务哈希值。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回Object- 块对象:

number- Number:块编号。null当它的挂起块。

hash32字节 - String:块的哈希。null当它的挂起块。

parentHash32字节 - String:父块的哈希值。

nonce8字节 - String:生成的工作证明的哈希值。null当它的挂起块。

sha3Uncles32字节 - String:块中的uncles数据的SHA3。

logsBloom256字节 - String:块的日志的bloom过滤器。null当它的挂起块。

transactionsRoot32字节 - String:块的事务trie的根

stateRoot32字节 - String:块的最终状态trie的根。

miner- String:获得采矿奖励的受益人的地址。

difficulty- String:此块的难度的整数。

totalDifficulty- String:直到此块的链的总难度的整数。

extraData- String:该块的“额外数据”字段。

size- Number:以字节为单位整数此块的大小。

gasLimit- Number:此区块允许的最大气体量。

gasUsed- Number:此块中所有事务的总使用气体。

timestamp- Number:整理块时的unix时间戳。

transactions- Array:事务对象数组,或32字节事务哈希值,具体取决于returnTransactionObjects参数。

uncles- Array:叔叔哈希数组。

示例
web3.eth.getBlock(3150).then(console.log);> {
“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”: []}
getBlockTransactionCount
web3.eth.getBlockTransactionCount(blockHashOrBlockNumber [, callback])
返回给定块中的事务数。

参数
String|Number - 块号或散列。或者字符串"genesis","latest"或者"pending"在默认的块参数中。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promisereturns Number- 给定块中的事务数。

示例
web3.eth.getBlockTransactionCount(“0x407d73d8a49eeb85d32cf465507dd71d507100c1”).then(console.log);

1
getUncle
web3.eth.getUncle(blockHashOrBlockNumber, uncleIndex [, returnTransactionObjects] [, callback])
按给定的叔叔索引位置返回一个块叔叔。

参数
String|Number - 块号或散列。或者字符串"genesis","latest"或者"pending"在默认的块参数中。

Number - 叔叔的指数位置。

Boolean- (可选,默认false)如果true,返回的块将包含所有事务作为对象,如果false它只包含事务哈希值。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回Object- 返回的叔叔。有关返回值,请参阅web3.eth.getBlock()。

注意

叔叔不包含个别交易。

示例
web3.eth.getUncle(500, 0).then(console.log);

// 功能同 web3.eth.getBlock
getTransaction
web3.eth.getTransaction(transactionHash [, callback])
返回与给定事务哈希匹配的事务。

参数
String - 事务哈希。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回Object- 一个事务对象的哈希transactionHash:

hash32字节 - String:交易的哈希值。

nonce- Number:发件人在此之前进行的交易次数。

blockHash32字节 - String:此事务所在的块的哈希值。null当它处于挂起状态时。

blockNumber- Number:此事务所在的块号。null当它处于挂起状态时。

transactionIndex- Number:块中事务索引位置的整数。null什么时候等待。

from- String:发件人的地址。

to- String:接收器的地址。null当它的合同创建交易。

value- String:在魏中转移的价值。

gasPrice- String:发送方在wei提供的燃气价格。

gas- Number:发件人提供的气体。

input- String:与事务一起发送的数据。

示例
web3.eth.getTransaction(‘0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b§234’).then(console.log);> {
“hash”: “0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b”,
“nonce”: 2,
“blockHash”: “0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46”,
“blockNumber”: 3,
“transactionIndex”: 0,
“from”: “0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b”,
“to”: “0x6295ee1b4f6dd65047762f924ecd367c17eabf8f”,
“value”: ‘123450000000000000’,
“gas”: 314159,
“gasPrice”: ‘2000000000000’,
“input”: “0x57cb2fc4”}
getTransactionFromBlock
getTransactionFromBlock(hashStringOrNumber, indexNumber [, callback])
返回基于块哈希或数字以及事务索引位置的事务。

参数
String - 块号或散列。或者字符串"genesis","latest"或者"pending"在默认的块参数中。

Number - 交易指数的位置。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回Object- 事务对象,请参阅web3.eth.getTransaction:

示例
var transaction = web3.eth.getTransactionFromBlock(‘0x4534534534’, 2)
.then(console.log);

// 功能同 web3.eth.getTransaction
getTransactionReceipt
web3.eth.getTransactionReceipt(hash [, callback])
按事务哈希返回事务的接收。

注意

收据不适用于待处理的交易和退货null。

参数
String - 事务哈希。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回Object- 事务接收对象,或未null找到收据时:

status- Boolean:TRUE如果交易成功FALSE,则EVM恢复交易。

blockHash32字节 - String:此事务所在块的哈希值。

blockNumber- Number:此交易所在的区号。

transactionHash32字节 - String:交易的哈希值。

transactionIndex- Number:块中事务索引位置的整数。

from- String:发件人的地址。

to- String:接收器的地址。null当它的合同创建交易。

contractAddress- String:如果交易是合同创建,则创建合同地址,否则null。

cumulativeGasUsed- Number:在块中执行此事务时使用的总气体量。

gasUsed- Number:仅此特定交易使用的天然气量。

logs- Array:此事务生成的日志对象数组。

示例
var receipt = web3.eth.getTransactionReceipt(‘0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b’).then(console.log);> {
“status”: true,
“transactionHash”: “0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b”,
“transactionIndex”: 0,
“blockHash”: “0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46”,
“blockNumber”: 3,
“contractAddress”: “0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe”,
“cumulativeGasUsed”: 314159,
“gasUsed”: 30234,
“logs”: [{
// logs as returned by getPastLogs, etc.
}, …]}

getTransactionCount
web3.eth.getTransactionCount(address [, defaultBlock] [, callback])
获取从此地址发送的交易数量。

参数
String - 从中获取交易数量的地址。

Number|String- (可选)如果传递此参数,则不会使用web3.eth.defaultBlock设置的默认块。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promisereturns Number- 从给定地址发送的事务数。

示例
web3.eth.getTransactionCount(“0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe”).
then(console.log);

1

sendTransaction
web3.eth.sendTransaction(transactionObject [, callback])
将事务发送到网络。

参数
Object - 要发送的事务对象:

from- String|Number:发送帐户的地址。如果未指定,则使用web3.eth.defaultAccount属性。或web3.eth.accounts.wallet中本地钱包的地址或索引。

to- String:(可选)消息的目标地址,未定义为合同创建事务。

value- Number|String|BN|BigNumber:(可选)为wei中的交易转移的价值,如果是合约创建交易,也是捐赠。

gas - Number:(可选,默认:待定)用于交易的天然气量(未使用的天然气退还)。

gasPrice- Number|String|BN|BigNumber:(可选)此交易的天然气价格在wei中,默认为web3.eth.gasPrice。

data- String:(可选)包含合同上函数调用数据的ABI字节字符串,或者在合同创建事务中包含初始化代码。

nonce- Number:(可选)随机数的整数。这允许覆盖使用相同nonce的自己的挂起事务。

callback- Function:(可选)可选回调,返回错误对象作为第一个参数,结果作为秒。

注意

该from属性也可以是web3.eth.accounts.wallet中的地址或索引。然后,它将使用该帐户的私钥在本地签名,并通过web3.eth.sendSignedTransaction()发送事务。

返回
该回调将返回32个字节的交易哈希值。

PromiEvent:一个组合事件发射器的承诺。将在交易收据可用时解决。此外,还提供以下活动:

"transactionHash"返回String:在发送事务并且事务哈希可用之后立即触发。

"receipt"返回Object:在事务收据可用时触发。

"confirmation"返回Number,Object:每次确认都会被解雇,直到第12次确认。接收确认编号作为第一个参数,将收据作为第二个参数。从确认0开始,这是它的头脑。

"error"返回Error:如果在发送过程中发生错误,则会触发。如果出现气体错误,则第二个参数是收货。

示例
//用remix 编译的代码

var code = “603d80600c6000396000f3007c01000000000000000000000000000000000000000000000000000000006000350463c6888fa18114602d57005b6007600435028060005260206000f3”;

// 调用

web3.eth.sendTransaction({
from: ‘0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe’,
data: code // 部署合约}
, function(error, hash){
…});// using the promiseweb3.eth.sendTransaction({
from: ‘0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe’,
to: ‘0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe’,
value: ‘1000000000000000’}).then(function(receipt){
…});// using the event emitterweb3.eth.sendTransaction({
from: ‘0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe’,
to: ‘0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe’,
value: ‘1000000000000000’}).on(‘transactionHash’, function(hash){
…}).on(‘receipt’, function(receipt){
…}).on(‘confirmation’, function(confirmationNumber, receipt){ … }).
on(‘error’, console.error);
// 如果gas错误,第二个参数接收

sendSignedTransaction
web3.eth.sendSignedTransaction(signedTransactionData [, callback])
发送已签名的事务,例如使用web3.eth.accounts.signTransaction生成。

参数
String - 以HEX格式签署的交易数据

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
PromiEvent:一个组合事件发射器的承诺。将在交易收据可用时解决。

有关详细信息,请参阅web3.eth.sendTransaction的返回值。

示例
var Tx = require(‘ethereumjs-tx’);

var privateKey = new Buffer(‘e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109’, ‘hex’)

var rawTx = {
nonce: ‘0x00’,
gasPrice: ‘0x09184e72a000’,
gasLimit: ‘0x2710’,
to: ‘0x0000000000000000000000000000000000000000’,
value: ‘0x00’,
data: ‘0x7f7465737432000000000000000000000000000000000000000000000000000000600057’}

var tx = new Tx(rawTx);

tx.sign(privateKey);

var serializedTx = tx.serialize();
//序列化
// console.log(serializedTx.toString(‘hex’));// 0xf889808609184e72a00082271094000000000000000000000000000000000000000080a47f74657374320000000000000000000000000000000000000000000000000000006000571ca08a8bbf888cfa37bbf0bb965423625641fc956967b81d12e23709cead01446075a01ce999b56a8a88504be365442ea61239198e23d1fce7d00fcfc5cd3b44b7215f

web3.eth.sendSignedTransaction(‘0x’ + serializedTx.toString(‘hex’)).on(‘receipt’, console.log);

// 查看交易详情eth.getTransactionReceipt()
签名
web3.eth.sign(dataToSign, address [, callback])
使用特定帐户签署数据。此帐户需要解锁。

参数
String - 要签名的数据。如果使用String,则使用web3.utils.utf8ToHex进行转换。

String|Number - 用于对数据进行签名的地址。或web3.eth.accounts.wallet中本地钱包的地址或索引。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

注意

  1. address参数也可以是web3.eth.accounts.wallet中的地址或索引。然后,它将使用此帐户的私钥在本地签名。

返回
Promise返回String- 签名。

示例
web3.eth.sign(“Hello world”, “0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe”).
then(console.log);

“0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400”// the below is the sameweb3.eth.sign(web3.utils.utf8ToHex(“Hello world”), “0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe”).then(console.log);> “0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400”
signTransaction
web3.eth.signTransaction(transactionObject, address [, callback])
签署交易。此帐户需要解锁。

参数
Object- 要更多地签署web3.eth.sendTransaction()的交易数据。

String - 用于签署交易的地址。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回Object- RLP编码的事务。该raw属性可用于使用web3.eth.sendSignedTransaction发送事务。

示例
web3.eth.signTransaction({
from: “0xEB014f8c8B418Db6b45774c326A0E64C78914dC0”,
gasPrice: “20000000000”,
gas: “21000”,
to: ‘0x3535353535353535353535353535353535353535’,
value: “1000000000000000000”,
data: “”}).then(console.log);

{
raw: ‘0xf86c808504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a04f4c17305743700648bc4f6cd3038ec6f6af0df73e31757007b7f59df7bee88da07e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0’,
tx: {
nonce: ‘0x0’,
gasPrice: ‘0x4a817c800’,
gas: ‘0x5208’,
to: ‘0x3535353535353535353535353535353535353535’,
value: ‘0xde0b6b3a7640000’,
input: ‘0x’,
v: ‘0x25’,
r: ‘0x4f4c17305743700648bc4f6cd3038ec6f6af0df73e31757007b7f59df7bee88d’,
s: ‘0x7e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0’,
hash: ‘0xda3be87732110de6c1354c83770aae630ede9ac308d9f7b399ecfba23d923384’
}}
调用
web3.eth.call(callObject [, defaultBlock] [, callback])
执行消息调用事务,该事务直接在节点的VM中执行,但从未开采到区块链中。

参数
Object- 事务对象参见web3.eth.sendTransaction,区别在于对于调用,from属性也是可选的。

Number|String- (可选)如果传递此参数,则不会使用web3.eth.defaultBlock设置的默认块。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回String:调用的返回数据,例如智能合约函数返回值。

示例
web3.eth.call({
to: “0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe”, // 合约地址
data: “0xc6888fa10000000000000000000000000000000000000000000000000000000000000003”}).then(console.log);> “0x000000000000000000000000000000000000000000000000000000000000000a”
估计
web3.eth.estimateGas(callObject [, callback])
执行消息调用或事务并返回使用的气体量。

参数
Object- 事务对象参见web3.eth.sendTransaction,区别在于对于调用,from属性也是可选的。

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回Number- 模拟呼叫/交易的已用气体。

示例
web3.eth.estimateGas({
to: “0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe”,
data: “0xc6888fa10000000000000000000000000000000000000000000000000000000000000003”}).then(console.log);> “0x0000000000000000000000000000000000000000000000000000000000000015”
getPastLogs
web3.eth.getPastLogs(options [, callback])
获取与给定选项匹配的过去日志。

参数
Object - 过滤器选项如下:

fromBlock- Number|String:最早的块的数量(“latest"可以表示最近和"pending"当前的挖掘块)。在默认情况下"latest”。

toBlock- Number|String:最新块的编号(“latest"可以表示最近和"pending"当前挖掘的块)。在默认情况下"latest”。

address- String|Array:仅从特定帐户获取日志的地址或地址列表。

topics- Array:必须各自出现在日志条目中的值数组。如果你想让主题不被使用null,顺序很重要,例如。您还可以为每个主题传递一个数组,其中包含该主题的选项,例如[null, ‘0x12…’][null, [‘option1’, ‘option2’]]

返回
Promisereturns Array- 日志对象的数组。

返回事件的结构Object在Array如下外观:

address- String:此事件源于此。

data- String:包含非索引日志参数的数据。

topics- Array:具有最多4个32字节主题的数组,主题1-3包含日志的索引参数。

logIndex- Number:块中事件索引位置的整数。

transactionIndex- Number:事务的索引位置的整数,事件是在中创建的。

transactionHash32字节 - String:此事件创建的事务的哈希值。

blockHash32字节 - String:创建此事件的块的哈希值。null当它仍然未决时。

blockNumber- Number:创建此日志的块编号。null当仍处于挂起状态时。

示例
web3.eth.getPastLogs({
address: “0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe”,
topics: [“0x033456732123ffff2342342dd12342434324234234fd234fd23fd4f23d4234”]}).then(console.log);> [{
data: ‘0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385’,
topics: [‘0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7’, ‘0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385’]
logIndex: 0,
transactionIndex: 0,
transactionHash: ‘0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385’,
blockHash: ‘0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7’,
blockNumber: 1234,
address: ‘0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe’},{…}]
getWork
web3.eth.getWork([callback])
获得矿工的工作。返回当前块的哈希值,seedHash和要满足的边界条件(“target”)。

参数
Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promise返回Array- 具有以下结构的挖掘工作:

String32字节 - 在索引0处:当前块标头pow-hash

String32字节 - 索引1:用于DAG的种子哈希。

String32字节 - 在索引2处:边界条件(“目标”),2 ^ 256 /难度。

示例
web3.eth.getWork().then(console.log);

[
“0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef”,
“0x5EED00000000000000000000000000005EED0000000000000000000000000000”,
“0xd1ff1c01710000000000000000000000d1ff1c01710000000000000000000000”]
submitWork
web3.eth.submitWork(nonce, powHash, digest, [callback])
用于提交工作量证明解决方案。

参数
String 8字节:发现的随机数(64位)

String 32字节:标头的pow-hash(256位)

String 32字节:混合摘要(256位)

Function - (可选)可选回调,将错误对象作为第一个参数返回,结果返回第二个参数。

返回
Promisereturns Boolean- TRUE如果提供的解决方案有效则返回,否则返回false。

示例
web3.eth.submitWork([
“0x0000000000000001”,
“0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef”,
“0xD1FE5700000000000000000000000000D1FE5700000000000000000000000000”]).then(console.log);>
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值