11月22, 2019

web3py

web3.eth.call

Delegates to eth_call RPC Method Eth.call(transaction, block_identifier=web3.eth.defaultBlock)

Executes the given transaction locally without creating a new transaction on the blockchain. Returns the return value of the executed contract.

The transaction parameter is handled in the same manner as the sendTransaction() method

相当于模拟 send()

>>> myContract.functions.setVar(1).transact()
HexBytes('0x79af0c7688afba7588c32a61565fd488c422da7b5773f95b242ea66d3d20afda')
>>> myContract.functions.getVar().call()
1
# The above call equivalent to the raw call:
>>> we3.eth.call({'value': 0, 'gas': 21736, 'gasPrice': 1, 'to': '0xc305c901078781C232A2a521C2aF7980f8385ee9', 'data': '0x477a5c98'})
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000001')

In most cases it is better to make contract function call through the web3.contract.Contract interface.

Class web3.eth.Eth

通常,通过 property or method returns a mapping of keys to values, it will return an AttributeDict, can access the keys as attributes.

e.g. get the latest block number

>>>block = web3.heth.getBlock, getBlock('latest')
AttributeDict({
'hash': '0xe8ad537a 261e6fff80d551d8d087ee0f2202d9b09b64d17a5f45e818e8e472a',
'number': 4022281,
#... etc...
})

>>>block[/]'number']
4022281
>>>block.number
4022281

>>>block.number = 4022282
Traceback #... etc...
TypeError: This data is immutable -- create a copy instead of modifying
Properties
eth.defaultAccount
eth.defaultBlock # latest
eth.syncing #  Returns either false if the node is not syncing or a dictionary showing sync status.

  • 当然CurrentBlock大于等于HighestBlock时返回false,这也正是通常所说的同步完成之后,再执行eth.syncing()函数会返回false的原因。
  • startingBlock:开始同步的起始区块编号;
  • currentBlock:当前正在导入的区块编号;
  • highestBlock:通过所链接的节点获得的当前最高的区块高度;
  • pulledStates:当前已经拉取的状态条目数;
  • knownStates:当前已知的待拉取的总状态条目数;
// SyncProgress gives progress indications when the node is synchronising with
// the Ethereum network.
type SyncProgress struct {
    StartingBlock uint64 // Block number where sync began
    CurrentBlock  uint64 // Current block number where sync is at
    HighestBlock  uint64 // Highest alleged block number in the chain
    PulledStates  uint64 // Number of state trie entries already downloaded
    KnownStates   uint64 // Total number of state trie entries known about
}
Methods
eth.getBalance
>>> web3.eth.getBalance('0xd3CdA913deB6f67967B99D67aCDFa1712C293601')
77320681768999138915

>>> web3.eth.getStorageAt('0x6C8f2A135f6ed072DE4503Bd7C4999a1a17F824B', 0)
'0x00000000000000000000000000000000000000000000000000120a0b063499d4'
Contracts

本文链接:https://harry.uno/post/web3py.html

-- EOF --

Comments