2021-02-09 09:42:46 +01:00
|
|
|
# third-party imports
|
|
|
|
from hexathon import (
|
|
|
|
add_0x,
|
|
|
|
strip_0x,
|
|
|
|
)
|
|
|
|
|
|
|
|
# local imports
|
2021-04-04 14:55:27 +02:00
|
|
|
from chainlib.jsonrpc import jsonrpc_template
|
2021-02-09 09:42:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
def nonce(address):
|
|
|
|
o = jsonrpc_template()
|
|
|
|
o['method'] = 'eth_getTransactionCount'
|
|
|
|
o['params'].append(address)
|
|
|
|
o['params'].append('pending')
|
|
|
|
return o
|
|
|
|
|
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
class NonceOracle:
|
2021-02-09 09:42:46 +01:00
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
def __init__(self, address):
|
2021-02-09 09:42:46 +01:00
|
|
|
self.address = address
|
2021-04-04 14:55:27 +02:00
|
|
|
self.nonce = self.get_nonce()
|
2021-02-09 09:42:46 +01:00
|
|
|
|
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
def get_nonce(self):
|
|
|
|
raise NotImplementedError('Class must be extended')
|
2021-02-12 00:24:10 +01:00
|
|
|
|
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
def next_nonce(self):
|
2021-02-12 00:24:10 +01:00
|
|
|
n = self.nonce
|
|
|
|
self.nonce += 1
|
|
|
|
return n
|
2021-02-17 21:20:21 +01:00
|
|
|
|
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
class RPCNonceOracle(NonceOracle):
|
|
|
|
|
|
|
|
def __init__(self, address, conn):
|
|
|
|
self.conn = conn
|
|
|
|
super(RPCNonceOracle, self).__init__(address)
|
|
|
|
|
|
|
|
|
|
|
|
def get_nonce(self):
|
|
|
|
o = nonce(self.address)
|
|
|
|
r = self.conn.do(o)
|
|
|
|
n = strip_0x(r)
|
|
|
|
return int(n, 16)
|
|
|
|
|
|
|
|
|
|
|
|
class OverrideNonceOracle(NonceOracle):
|
2021-02-17 21:20:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, address, nonce):
|
|
|
|
self.nonce = nonce
|
2021-04-04 14:55:27 +02:00
|
|
|
super(OverrideNonceOracle, self).__init__(address)
|
2021-02-17 21:20:21 +01:00
|
|
|
|
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
def get_nonce(self):
|
2021-02-17 21:20:21 +01:00
|
|
|
return self.nonce
|
2021-04-04 14:55:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
DefaultNonceOracle = RPCNonceOracle
|