2021-02-11 10:16:05 +01:00
|
|
|
|
# third-party imports
|
2021-04-04 14:55:27 +02:00
|
|
|
|
from chainlib.jsonrpc import jsonrpc_template
|
2021-02-11 10:16:05 +01:00
|
|
|
|
from chainlib.eth.tx import Tx
|
|
|
|
|
from hexathon import (
|
|
|
|
|
add_0x,
|
|
|
|
|
strip_0x,
|
|
|
|
|
even,
|
|
|
|
|
)
|
2021-02-11 08:45:26 +01:00
|
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
|
|
2021-02-12 00:24:10 +01:00
|
|
|
|
def block_latest():
|
2021-02-11 08:45:26 +01:00
|
|
|
|
o = jsonrpc_template()
|
|
|
|
|
o['method'] = 'eth_blockNumber'
|
|
|
|
|
return o
|
|
|
|
|
|
|
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
|
def block_by_hash(hsh, include_tx=True):
|
2021-02-11 08:45:26 +01:00
|
|
|
|
o = jsonrpc_template()
|
2021-02-11 10:16:05 +01:00
|
|
|
|
o['method'] = 'eth_getBlockByHash'
|
2021-02-11 08:45:26 +01:00
|
|
|
|
o['params'].append(hsh)
|
2021-04-04 14:55:27 +02:00
|
|
|
|
o['params'].append(include_tx)
|
2021-02-11 08:45:26 +01:00
|
|
|
|
return o
|
|
|
|
|
|
|
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
|
def block_by_number(n, include_tx=True):
|
2021-02-11 10:16:05 +01:00
|
|
|
|
nhx = add_0x(even(hex(n)[2:]))
|
2021-02-11 08:45:26 +01:00
|
|
|
|
o = jsonrpc_template()
|
2021-02-11 10:16:05 +01:00
|
|
|
|
o['method'] = 'eth_getBlockByNumber'
|
|
|
|
|
o['params'].append(nhx)
|
2021-04-04 14:55:27 +02:00
|
|
|
|
o['params'].append(include_tx)
|
|
|
|
|
return o
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def transaction_count(block_hash):
|
|
|
|
|
o = jsonrpc_template()
|
|
|
|
|
o['method'] = 'eth_getBlockTransactionCountByHash'
|
|
|
|
|
o['params'].append(block_hash)
|
2021-02-11 08:45:26 +01:00
|
|
|
|
return o
|
2021-02-11 10:16:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Block:
|
|
|
|
|
|
|
|
|
|
def __init__(self, src):
|
|
|
|
|
self.hash = src['hash']
|
2021-04-08 17:39:32 +02:00
|
|
|
|
try:
|
|
|
|
|
self.number = int(strip_0x(src['number']), 16)
|
|
|
|
|
except TypeError:
|
|
|
|
|
self.number = int(src['number'])
|
2021-02-11 10:16:05 +01:00
|
|
|
|
self.txs = src['transactions']
|
|
|
|
|
self.block_src = src
|
2021-04-08 17:39:32 +02:00
|
|
|
|
try:
|
|
|
|
|
self.timestamp = int(strip_0x(src['timestamp']), 16)
|
|
|
|
|
except TypeError:
|
|
|
|
|
self.timestamp = int(src['timestamp'])
|
2021-02-11 10:16:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def src(self):
|
|
|
|
|
return self.block_src
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tx(self, i):
|
|
|
|
|
return Tx(self.txs[i], self)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tx_src(self, i):
|
|
|
|
|
return self.txs[i]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2021-02-17 21:20:21 +01:00
|
|
|
|
return 'block {} {} ({} txs)'.format(self.number, self.hash, len(self.txs))
|