Provider

在ethers里面,Provider 是访问区块链数据的只读抽象。

使用过 Web3.js吗?

如果你用过Web3.js,以下是与ethers.js最大不同点。

ethers library 在 ProviderSigner可执行的操作之间创建了一个强大的划分,而Web3.js将二者结合在一起。

这种划分的关注点在于Provider严格的操作子集允许更多的后端、更一致的API,并确保其他库可以在不依赖任何潜在假设的情况下运行。

账户的方法

provider.getBalance( address [ , blockTag = latest ] ) Promise< 大数(BigNumber) >

返回给定的blockTag 区块高度下地址的余额。

await provider.getBalance("ricmoo.eth"); // { BigNumber: "36426320346873870455" }
provider.getCode( address [ , blockTag = latest ] ) Promise< string< DataHexString > >

返回给定的blockTag 区块高度下的合约源代码,如果当前没有合约被部署, 将返回 0x

await provider.getCode("registrar.firefly.eth"); // '0x606060405236156100885763ffffffff60e060020a60003504166369fe0e2d81146100fa578063704b6c021461010f57806379502c551461012d578063bed866f614610179578063c37067fa1461019e578063c66485b2146101ab578063d80528ae146101c9578063ddca3f43146101f7578063f2c298be14610219578063f3fef3a314610269575b6100f85b6000808052600760209081527f6d5257204ebe7d88fd91ae87941cb2dd9d8062b64ae5a2bd2d28ec40b9fbf6df80543490810190915560408051918252517fdb7750418f9fa390aaf85d881770065aa4adbe46343bcff4ae573754c829d9af929181900390910190a25b565b005b341561010257fe5b6100f860043561028a565b005b341561011757fe5b6100f8600160a060020a03600435166102ec565b005b341561013557fe5b61013d610558565b60408051600160a060020a0396871681526020810195909552928516848401526060840191909152909216608082015290519081900360a00190f35b341561018157fe5b61018c600435610580565b60408051918252519081900360200190f35b6100f8600435610595565b005b34156101b357fe5b6100f8600160a060020a03600435166105e6565b005b34156101d157fe5b6101d9610676565b60408051938452602084019290925282820152519081900360600190f35b34156101ff57fe5b61018c61068d565b60408051918252519081900360200190f35b6100f8600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284375094965061069495505050505050565b005b341561027157fe5b6100f8600160a060020a0360043516602435610ab2565b005b60025433600160a060020a039081169116146102a65760006000fd5b600454604080519182526020820183905280517f854231545a00e13c316c82155f2b8610d638e9ff6ebc4930676f84a5af08a49a9281900390910190a160048190555b50565b60025433600160a060020a039081169116146103085760006000fd5b60025460408051600160a060020a039283168152918316602083015280517fbadc9a52979e89f78b7c58309537410c5e51d0f63a0a455efe8d61d2b474e6989281900390910190a16002805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a038381169190911790915560008054604080516020908101849052815160e060020a6302571be30281527f91d1777781884d03a6757a803996e38de2a42967fb37eeaca72729271025a9e26004820152915192909416936302571be39360248084019492938390030190829087803b15156103e957fe5b60325a03f115156103f657fe5b50505060405180519050600160a060020a0316631e83409a826000604051602001526040518263ffffffff1660e060020a0281526004018082600160a060020a0316600160a060020a03168152602001915050602060405180830381600087803b151561045f57fe5b60325a03f1151561046c57fe5b50506040805160008054600354602093840183905284517f0178b8bf00000000000000000000000000000000000000000000000000000000815260048101919091529351600160a060020a039091169450630178b8bf9360248082019493918390030190829087803b15156104dd57fe5b60325a03f115156104ea57fe5b505060408051805160035460025460e860020a62d5fa2b0284526004840191909152600160a060020a03908116602484015292519216925063d5fa2b0091604480830192600092919082900301818387803b151561054457fe5b60325a03f1151561055157fe5b5050505b50565b600054600354600254600454600154600160a060020a039485169492831692165b9091929394565b6000818152600760205260409020545b919050565b6000818152600760209081526040918290208054349081019091558251908152915183927fdb7750418f9fa390aaf85d881770065aa4adbe46343bcff4ae573754c829d9af92908290030190a25b50565b60025433600160a060020a039081169116146106025760006000fd5b60015460408051600160a060020a039283168152918316602083015280517f279875333405c968e401e3bc4e71d5f8e48728c90f4e8180ce28f74efb5669209281900390910190a16001805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0383161790555b50565b600654600554600160a060020a033016315b909192565b6004545b90565b80516001820190600080808060048510806106af5750601485115b156106ba5760006000fd5b600093505b8484101561072a57855160ff16925060618310806106e05750607a8360ff16115b80156106fc575060308360ff1610806106fc575060398360ff16115b5b801561070d57508260ff16602d14155b156107185760006000fd5b6001909501945b6001909301926106bf565b60045434101561073a5760006000fd5b866040518082805190602001908083835b6020831061076a5780518252601f19909201916020918201910161074b565b51815160209384036101000a60001901801990921691161790526040805192909401829003822060035483528282018190528451928390038501832060008054948401819052865160e060020a6302571be3028152600481018390529651929a509098509650600160a060020a0390921694506302571be393602480820194509192919082900301818787803b15156107ff57fe5b60325a03f1151561080c57fe5b505060405151600160a060020a031691909114905061082b5760006000fd5b60008054600354604080517f06ab5923000000000000000000000000000000000000000000000000000000008152600481019290925260248201869052600160a060020a03308116604484015290519216926306ab59239260648084019382900301818387803b151561089a57fe5b60325a03f115156108a757fe5b505060008054600154604080517f1896f70a00000000000000000000000000000000000000000000000000000000815260048101879052600160a060020a0392831660248201529051919092169350631896f70a9260448084019391929182900301818387803b151561091657fe5b60325a03f1151561092357fe5b50506001546040805160e860020a62d5fa2b02815260048101859052600160a060020a033381166024830152915191909216925063d5fa2b009160448082019260009290919082900301818387803b151561097a57fe5b60325a03f1151561098757fe5b505060008054604080517f5b0fc9c300000000000000000000000000000000000000000000000000000000815260048101869052600160a060020a0333811660248301529151919092169350635b0fc9c39260448084019391929182900301818387803b15156109f357fe5b60325a03f11515610a0057fe5b505060058054349081019091556006805460010190556000838152600760209081526040918290208054840190558151600160a060020a03331681529081019290925280518493507f179ef3319e6587f6efd3157b34c8b357141528074bcb03f9903589876168fa149281900390910190a260408051348152905182917fdb7750418f9fa390aaf85d881770065aa4adbe46343bcff4ae573754c829d9af919081900360200190a25b50505050505050565b60025433600160a060020a03908116911614610ace5760006000fd5b604051600160a060020a0383169082156108fc029083906000818181858888f193505050501515610aff5760006000fd5b60408051600160a060020a03841681526020810183905281517fac375770417e1cb46c89436efcf586a74d0298fee9838f66a38d40c65959ffda929181900390910190a15b50505600a165627a7a723058205c3628c01dc80233f51979d91a76cec2a25d84e86c9838d34672734ca2232b640029'
provider.getStorageAt( addr , pos [ , blockTag = latest ] ) Promise< string< DataHexString > >

在当前的blockTag下,输入address参数addr和position参数pos,返回类型为Bytes32的值。

await provider.getStorageAt("registrar.firefly.eth", 0) // '0x000000000000000000000000314159265dd8dbb310642f98f50c066173c1259b'
provider.getTransactionCount( address [ , blockTag = latest ] ) Promise< number >

在当前的blockTag下,返回已经发送的交易的数量,这个值是用来提供给发送到网络的下一个交易的nonce值。

await provider.getTransactionCount("ricmoo.eth"); // 47

区块的方法

provider.getBlock( block ) Promise< Block >

从网络中得到区块result.transactions 是一串交易集合的哈希值。

await provider.getBlock(100004) // { // _difficulty: { BigNumber: "3849295379889" }, // difficulty: 3849295379889, // extraData: '0x476574682f76312e302e312d39383130306634372f6c696e75782f676f312e34', // gasLimit: { BigNumber: "3141592" }, // gasUsed: { BigNumber: "21000" }, // hash: '0xf93283571ae16dcecbe1816adc126954a739350cd1523a1559eabeae155fbb63', // miner: '0x909755D480A27911cB7EeeB5edB918fae50883c0', // nonce: '0x1a455280001cc3f8', // number: 100004, // parentHash: '0x73d88d376f6b4d232d70dc950d9515fad3b5aa241937e362fdbfd74d1c901781', // timestamp: 1439799168, // transactions: [ // '0x6f12399cc2cb42bed5b267899b08a847552e8c42a64f5eb128c1bcbd1974fb0c' // ] // }
provider.getBlockWithTransactions( block ) Promise< BlockWithTransactions >

从网络中得到区块result.transactions 是 一个TransactionResponse 对象的数组集合。

await provider.getBlockWithTransactions(100004) // { // _difficulty: { BigNumber: "3849295379889" }, // difficulty: 3849295379889, // extraData: '0x476574682f76312e302e312d39383130306634372f6c696e75782f676f312e34', // gasLimit: { BigNumber: "3141592" }, // gasUsed: { BigNumber: "21000" }, // hash: '0xf93283571ae16dcecbe1816adc126954a739350cd1523a1559eabeae155fbb63', // miner: '0x909755D480A27911cB7EeeB5edB918fae50883c0', // nonce: '0x1a455280001cc3f8', // number: 100004, // parentHash: '0x73d88d376f6b4d232d70dc950d9515fad3b5aa241937e362fdbfd74d1c901781', // timestamp: 1439799168, // transactions: [ // { // accessList: null, // blockHash: '0xf93283571ae16dcecbe1816adc126954a739350cd1523a1559eabeae155fbb63', // blockNumber: 100004, // chainId: 0, // confirmations: 16283837, // creates: null, // data: '0x', // from: '0xcf00A85f3826941e7A25BFcF9Aac575d40410852', // gasLimit: { BigNumber: "90000" }, // gasPrice: { BigNumber: "54588778004" }, // hash: '0x6f12399cc2cb42bed5b267899b08a847552e8c42a64f5eb128c1bcbd1974fb0c', // nonce: 25, // r: '0xb23adc880d3735e4389698dddc953fb02f1fa9b57e84d3510a2a4b3597ac2486', // s: '0x4e856f95c4e2828933246fb4765a5bfd2ca5959840643bef0e80b4e3a243d064', // to: '0xD9666150A9dA92d9108198a4072970805a8B3428', // transactionIndex: 0, // type: 0, // v: 27, // value: { BigNumber: "5000000000000000000" }, // wait: [Function] // } // ] // }

以太坊域名服务 (ENS) 方法

以太坊域名服务 (ENS) 允许一个简短且易于记忆的ENS名称附加到任何一组键和值。

最常见的用法之一是使用一个简单的命名来引用以太坊地址

在ethers的 API中,几乎任何接受地址的地方都可以使用ENS命名,这可以简化代码,使读取和调试更加简单。

provider 提供了一些基本操作,以帮助解析和处理ENS命名。

provider.getResolver( name ) Promise< EnsResolver >

返回一个EnsResolver实例,该实例可用于进一步查询ENS命名的特定实体。

// 有关使用此对象的示例,请参见下面(Resolver) const resolver = await provider.getResolver("ricmoo.eth");
provider.lookupAddress( address ) Promise< string >

使用反向注册器对ENS中的地址进行反向查找。如果名称不存在,或者正向查找不匹配,则返回null

ENS名称需要额外的配置来设置反向记录,它们不会自动设置。

await provider.lookupAddress("0x5555763613a12D8F3e73be831DFf8598089d3dCa"); // 'ricmoo.eth'
provider.resolveName( name ) Promise< string< Address > >

查找一个名称的地址。如果这个名称没有被拥有,或者没有配置一个解析器,或者解析器没有配置一个地址,则返回null

await provider.resolveName("ricmoo.eth"); // '0x5555763613a12D8F3e73be831DFf8598089d3dCa'

EnsResolver

resolver.name string

Resolver 的名称

resolver.address string< 地址(Address) >

Resolve 的地址

resolver.getAddress( [ cointType = 60 ] ) Promise< string >

返回一个 解析的类型为EIP-2304 多币地址的 Promise。 默认情况下,返回一个以太坊的地址(Address)(coinType = 60)。

// 默认情况下,查找以太坊地址 // (这个会 match provider.resolveName) await resolver.getAddress(); // '0x5555763613a12D8F3e73be831DFf8598089d3dCa' // 为其他coin地址指定coinType (0 = Bitcoin) await resolver.getAddress(0); // '1RicMooMWxqKczuRCa5D2dnJaUEn9ZJyn'
resolver.getContentHash( ) Promise< string >

返回一个Promise,解析为任何存储的EIP-1577内容哈希。 EIP-1577

await resolver.getContentHash(); // 'ipfs://QmdTPkMMBWQvL8t7yXogo7jq5pAcWg8J7RkLrDsWZHT82y'
resolver.getText( key ) Promise< string >

返回一个Promise,解析为任何存储的EIP-634作为key的文本实体。

await resolver.getText("email"); // 'me@ricmoo.com' await resolver.getText("url"); // 'https://www.ricmoo.com/' await resolver.getText("com.twitter"); // '@ricmoo'

Logs 方法

provider.getLogs( filter ) Promise< Array< Log > >

返回匹配筛选器的 Log数组。

请记住,许多后端会丢弃旧的事件,并且请求范围太广可能也会被丢弃,因为它们需要太多的资源来执行查询。

Network Status Methods

provider.getNetwork( ) Promise< Network >

返回这个 Provider 所连接的 Network

await provider.getNetwork() // { // chainId: 1, // ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e', // name: 'homestead' // }
provider.getBlockNumber( ) Promise< number >

返回最近挖出的区块的序号(或高度)。

await provider.getBlockNumber() // 16383842
provider.getGasPrice( ) Promise< 大数(BigNumber) >

返回一个 关于这个交易中的 gas price最准预测

// 燃料价格 (单位: wei)... gasPrice = await provider.getGasPrice() // { BigNumber: "16674318809" } // 通常来说燃料的价格用 gwei 会更好理解 utils.formatUnits(gasPrice, "gwei") // '16.674318809'
provider.getFeeData( ) Promise< FeeData >

返回在一笔交易中当前的recommended FeeData

对于 EIP-1559 的交易, 应该使用 maxFeePerGasmaxPriorityFeePerGas

对于不支持 EIP-1559 中被遗留的交易和网络,应该使用 gasPrice

// 燃料价格 (单位: wei)... feeData = await provider.getFeeData() // { // gasPrice: { BigNumber: "16674318809" }, // lastBaseFeePerGas: { BigNumber: "16492712329" }, // maxFeePerGas: { BigNumber: "34485424658" }, // maxPriorityFeePerGas: { BigNumber: "1500000000" } // } // 通常来说燃料的价格用 gwei 会更好理解 utils.formatUnits(feeData.maxFeePerGas, "gwei") // '34.485424658'
provider.ready Promise< Network >

返回一个 Promise ,直到网络建立就失效,,忽略由于目标节点还未激活而出现的错误。 这可以用于测试或附上脚本,以等待节点启动并顺利运行。

Transactions 方法

provider.call( transaction [ , blockTag = latest ] ) Promise< string< DataHexString > >

使用call返回执行交易的结果。调用不需要任何的以太,但不能改变任何状态。这在合约上调用getter方法是非常有用的。

await provider.call({ // ENS public resovler address to: "0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41", // `function addr(namehash("ricmoo.eth")) view returns (address)` data: "0x3b3b57debf074faa138b72c65adbdcfb329847e4f2c04bde7f7dd7fcad5a52d2f395a558" }); // '0x0000000000000000000000005555763613a12d8f3e73be831dff8598089d3dca'
provider.estimateGas( transaction ) Promise< 大数(BigNumber) >

返回向网络提交交易所需的预估gas值。

估计的gas值可能不准确,因为网络上可能有另一个交易没有被计算在内,但在被挖出来之后就会影响相关状态。

await provider.estimateGas({ // Wrapped ETH address to: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // `function deposit() payable` data: "0xd0e30db0", // 1 ether value: parseEther("1.0") }); // { BigNumber: "27938" }
provider.getTransaction( hash ) Promise< TransactionResponse >

返回带有哈希值的交易,如果交易未知,则返回null。

如果一个交易还没有被挖出,这个方法将搜索交易池。各种后端可能有更多的限制交易池访问(例如,燃料价格太低或交易最近才发送,还没有索引),在这种情况下,这个方法也可能返回null。

await provider.getTransaction("0x5b73e239c55d790e3c9c3bbb84092652db01bb8dbf49ccc9e4a318470419d9a0"); // { // accessList: null, // blockHash: '0x8a179bc6cb299f936c4fd614995e62d597ec6108b579c23034fb220967ceaa94', // blockNumber: 12598244, // chainId: 1, // confirmations: 3785600, // creates: '0x733aF852514e910E2f8af40d61E00530377889E9', // data: '0x608060405234801561001057600080fd5b5060405161062438038061062483398101604081905261002f916100cd565b60405163c47f002760e01b815260206004820152600d60248201526c0daead8e8d2c6c2d8d85ccae8d609b1b60448201526001600160a01b0382169063c47f002790606401602060405180830381600087803b15801561008e57600080fd5b505af11580156100a2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100c691906100fb565b5050610113565b6000602082840312156100de578081fd5b81516001600160a01b03811681146100f4578182fd5b9392505050565b60006020828403121561010c578081fd5b5051919050565b610502806101226000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c80634c0770b914610030575b600080fd5b61004361003e366004610309565b61005b565b60405161005293929190610389565b60405180910390f35b600060608085841461006c57600080fd5b8567ffffffffffffffff81111561009357634e487b7160e01b600052604160045260246000fd5b6040519080825280602002602001820160405280156100bc578160200160208202803683370190505b5091508567ffffffffffffffff8111156100e657634e487b7160e01b600052604160045260246000fd5b60405190808252806020026020018201604052801561011957816020015b60608152602001906001900390816101045790505b50905060005b86811015610235576101cd8a8a8a8a8581811061014c57634e487b7160e01b600052603260045260246000fd5b905060200201602081019061016191906102db565b89898681811061018157634e487b7160e01b600052603260045260246000fd5b90506020028101906101939190610460565b8080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525061024592505050565b8483815181106101ed57634e487b7160e01b600052603260045260246000fd5b6020026020010184848151811061021457634e487b7160e01b600052603260045260246000fd5b6020908102919091010191909152528061022d816104a5565b91505061011f565b5043925096509650969350505050565b6000606060405190506000815260208101604052600080845160208601878afa9150843d101561028857603f3d01601f191681016040523d81523d6000602083013e5b94509492505050565b60008083601f8401126102a2578182fd5b50813567ffffffffffffffff8111156102b9578182fd5b6020830191508360208260051b85010111156102d457600080fd5b9250929050565b6000602082840312156102ec578081fd5b81356001600160a01b0381168114610302578182fd5b9392505050565b60008060008060008060808789031215610321578182fd5b8635955060208701359450604087013567ffffffffffffffff80821115610346578384fd5b6103528a838b01610291565b9096509450606089013591508082111561036a578384fd5b5061037789828a01610291565b979a9699509497509295939492505050565b60006060820185835260206060818501528186518084526080860191508288019350845b818110156103c9578451835293830193918301916001016103ad565b5050848103604086015285518082528282019350600581901b82018301838801865b8381101561045057601f1980868503018852825180518086528a5b81811015610421578281018a01518782018b01528901610406565b81811115610431578b8a83890101525b5098880198601f019091169390930186019250908501906001016103eb565b50909a9950505050505050505050565b6000808335601e19843603018112610476578283fd5b83018035915067ffffffffffffffff821115610490578283fd5b6020019150368190038213156102d457600080fd5b60006000198214156104c557634e487b7160e01b81526011600452602481fd5b506001019056fea264697066735822122083b5dc25b3c9256aa4244eddaf9e4b5fccd09a45ec4e0174f2c900de7144602d64736f6c63430008040033000000000000000000000000084b1c3c81545d370f3634392de611caabff8148', // from: '0x8ba1f109551bD432803012645Ac136ddd64DBA72', // gasLimit: { BigNumber: "443560" }, // gasPrice: { BigNumber: "10100000000" }, // hash: '0x5b73e239c55d790e3c9c3bbb84092652db01bb8dbf49ccc9e4a318470419d9a0', // nonce: 745, // r: '0xaf2b969de6dfb234fb8843f47a029636abb1ef52f26bb8bb615bbabcf23808e9', // s: '0x3dd61cd8df015e0af5689a249dd3224ee71f2b04917b7b4c14f7e68bb3a4ec17', // to: null, // transactionIndex: 315, // type: 0, // v: 38, // value: { BigNumber: "0" }, // wait: [Function] // }
provider.getTransactionReceipt( hash ) Promise< TransactionReceipt >

返回交易收据的哈希值,如果交易还没有被挖出则返回null。

如果需要等待交易被挖出,请考虑下面的waitForTransaction方法。

await provider.getTransactionReceipt("0x5b73e239c55d790e3c9c3bbb84092652db01bb8dbf49ccc9e4a318470419d9a0"); // { // blockHash: '0x8a179bc6cb299f936c4fd614995e62d597ec6108b579c23034fb220967ceaa94', // blockNumber: 12598244, // byzantium: true, // confirmations: 3785600, // contractAddress: '0x733aF852514e910E2f8af40d61E00530377889E9', // cumulativeGasUsed: { BigNumber: "12102324" }, // effectiveGasPrice: { BigNumber: "10100000000" }, // from: '0x8ba1f109551bD432803012645Ac136ddd64DBA72', // gasUsed: { BigNumber: "443560" }, // logs: [ // { // address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e', // blockHash: '0x8a179bc6cb299f936c4fd614995e62d597ec6108b579c23034fb220967ceaa94', // blockNumber: 12598244, // data: '0x000000000000000000000000084b1c3c81545d370f3634392de611caabff8148', // logIndex: 160, // topics: [ // '0xce0457fe73731f824cc272376169235128c118b49d344817417c6d108d155e82', // '0x91d1777781884d03a6757a803996e38de2a42967fb37eeaca72729271025a9e2', // '0x4774f6d3b3d08b5ec00115f0e2fddb92604b39e52b0dc908c6f8fcb7aa5d2a9a' // ], // transactionHash: '0x5b73e239c55d790e3c9c3bbb84092652db01bb8dbf49ccc9e4a318470419d9a0', // transactionIndex: 315 // }, // { // address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e', // blockHash: '0x8a179bc6cb299f936c4fd614995e62d597ec6108b579c23034fb220967ceaa94', // blockNumber: 12598244, // data: '0x000000000000000000000000a2c122be93b0074270ebee7f6b7292c7deb45047', // logIndex: 161, // topics: [ // '0x335721b01866dc23fbee8b6b2c7b1e14d6f05c28cd35a2c934239f94095602a0', // '0x790fdce97f7b2b1c4c5a709fb6a49bf878feffcaa85ed0245f6dff09abcefda7' // ], // transactionHash: '0x5b73e239c55d790e3c9c3bbb84092652db01bb8dbf49ccc9e4a318470419d9a0', // transactionIndex: 315 // } // ], // logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000004000000000000010000000000000020000000000000000000040000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000040000000000000000100004000000000000008000040000000000000000000000000000000000005000000000041000000000000000000000000000000000000000000000000100000000000000001000000000000000000000', // status: 1, // to: null, // transactionHash: '0x5b73e239c55d790e3c9c3bbb84092652db01bb8dbf49ccc9e4a318470419d9a0', // transactionIndex: 315, // type: 0 // }
provider.sendTransaction( transaction ) Promise< TransactionResponse >

向在挖区块的网络提交交易交易 必须 经过签名,并且需要合法(例如,nonce值需要正确并且账户要有足够的余额来进行该笔交易的支付)。

const signedTx = "0x02f874827a69048459682f00845e558eb4825209945555763613a12d8f3e73be831dff8598089d3dca882b992b75cbeb600080c080a0c8a8f1bed41823367b739625e724e8ff89bd0caff38e5747cf76384c3ae3e26da03893b6aaefa8337cd67fbd4ceaf4aec44dfc928d78c040118a6cc8f9ea94367e"; await provider.sendTransaction(signedTx); // { // accessList: [], // chainId: 31337, // confirmations: 0, // data: '0x', // from: '0x894ed91B666FacCe5a4D2FF8261924b4754A5759', // gasLimit: { BigNumber: "21001" }, // gasPrice: null, // hash: '0xabee48ad39a96ee6e018afd801d34b23eae384cbdc7304b5089f2ebc0c126b18', // maxFeePerGas: { BigNumber: "1582665396" }, // maxPriorityFeePerGas: { BigNumber: "1500000000" }, // nonce: 4, // r: '0xc8a8f1bed41823367b739625e724e8ff89bd0caff38e5747cf76384c3ae3e26d', // s: '0x3893b6aaefa8337cd67fbd4ceaf4aec44dfc928d78c040118a6cc8f9ea94367e', // to: '0x5555763613a12D8F3e73be831DFf8598089d3dCa', // type: 2, // v: 0, // value: { BigNumber: "3141590000000000000" }, // wait: [Function] // }
provider.waitForTransaction( hash [ , confirms = 1 [ , timeout ] ] ) Promise< TxReceipt >

返回一个Promise,直到transactionHash被挖出来,才能够被解析。

如果confirms参数为0,这个方法是非阻塞的,如果交易还没有被挖出,则返回null。 否则,该方法是阻塞的,直到这个包含该交易的、被confirms标识的区块被挖出。

Event Emitter 方法

EventEmitter API允许应用程序使用Obeserver Pattern来注册各种事件发生时的回调函数。

这与其他JavaScript库提供的Event Emitter密切相关,除开那些事件名称支持一些更复杂的对象,而不仅仅是字符串。对象在内部是标准化的。

provider.on( eventName , listener ) this

为每一个参数为eventName事件添加监听器

provider.once( eventName , listener ) this

为参数为eventName事件添加监听器,监听使用过后将会被移除。

provider.emit( eventName , ...args ) boolean

通知所有的eventName event监听器,并把参数传递给它们。这通常只在内部使用。

provider.off( eventName [ , listener ] ) this

移除一个参数为eventName事件监听器,如果没有提供listener参数,则移除所有关于eventName的监听器。

provider.removeAllListeners( [ eventName ] ) this

移除所有参数为eventName事件监听器,如果没有提供eventName参数,则移除所有事件。

provider.listenerCount( [ eventName ] ) number

返回所有参数为eventName事件的监听器数量。如果没有提供eventName参数,返回所有监听器的数量。

provider.listeners( eventName ) Array< Listener >

返回参数为eventName事件监听器的list集合。

事件

以下任何一个方法都可以作为上述方法中的eventName参数。

Log Filter

一个过滤器是一个对象,表示合约log Filter它具有可选的address (合约地址) 和 topics (一个要匹配的主题集)。

如果参数address未被指定,则过滤器匹配任何合约地址。

有关于过滤事件的更多信息,请参阅 EventFilters

Topic-Set Filter

主题集过滤器的值是一个主题集数组。

此事件与日志过滤器相同,但参数address可以不用填(匹配任何合约)。

有关于过滤事件的更多信息,请参阅 EventFilters

Transaction Filter

交易过滤器的值是交易的哈希值。

这个事件会在任何给定的交易挖出的链上区块中触发。 使用once方法比使用on方法要普遍地多。

除了交易和过滤事件外,还有几个命名事件。

Event NameArgumentsDescription 
"block"blockNumber当一个区块被挖出时触发 
"error"error只要有错误就触发 
"pending"pendingTransaction当一个新交易进入内存池时触发;只有特定的providers提供此事件,从而在运行在自己的节点上获得可靠的数据 
"willPoll"pollId在一个polling loop开始之前触发;(大多数开发者很少使用) 
"poll"pollId, blockNumber在每个poll cycle中,`blockNumber`更新之后(如果改变了),以及与在poll loop中任何其他的事件(如果有)之前触发; (大多数开发者很少使用) 
"didPoll"pollId在polling loop中的所有事件被触发后触发;(大多数开发者很少使用) 
"debug"provider dependent每个Provider可以使用它来发出有用的调试信息,格式由开发者决定;(大多数开发者很少使用) (very rarely used by most developers) 
Named Provider Events 
事件例子
provider.on("block", (blockNumber) => { // 只有有一个区块改变就会触发 }) provider.once(txHash, (transaction) => { // 当含有交易的区块被挖出就会触发 }) // 这个过滤器也可以通过Contract或接口API生成。 // 如果不指定address,则默认匹配任何地址, // 如果不指定topics,则默认匹配任何日志。 filter = { address: "dai.tokens.ethers.eth", topics: [ utils.id("Transfer(address,address,uint256)") ] } provider.on(filter, (log, event) => { // 当 DAI token 发生转账时触发 }) // 注意,这是一个主题集数组,与使用没有address参数的过滤器相同(即匹配任何地址) topicSets = [ utils.id("Transfer(address,address,uint256)"), null, [ hexZeroPad(myAddress, 32), hexZeroPad(myOtherAddress, 32) ] ] provider.on(topicSets, (log, event) => { // 当任何一个token 发送到其他地址时触发 }) provider.on("pending", (tx) => { // 当有一笔pending交易被捕获时触发 }); provider.on("error", (tx) => { // 只要有错误就会触发 });

校验方法

Provider.isProvider( object ) boolean

当且仅当参数object是Provider返回 true 。