erc20-demurrage-token/python/erc20_demurrage_token/seal.py

67 lines
1.8 KiB
Python
Raw Normal View History

2023-02-10 16:21:37 +01:00
# standard imports
import enum
2023-02-10 14:45:29 +01:00
# external imports
2023-02-10 16:21:37 +01:00
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.jsonrpc import JSONRPCRequest
2023-02-10 14:45:29 +01:00
from chainlib.eth.tx import (
TxFactory,
TxFormat,
)
from chainlib.eth.contract import (
ABIContractEncoder,
2023-02-10 16:21:37 +01:00
ABIContractType,
abi_decode_single,
)
from hexathon import (
add_0x,
2023-02-10 14:45:29 +01:00
)
2023-02-10 16:21:37 +01:00
class ContractState(enum.IntEnum):
MINTER_STATE = 1
SINK_STATE = 2
EXPIRY_STATE = 4
CAP_STATE = 8
CONTRACT_SEAL_STATE_MAX = 0
for v in dir(ContractState):
if len(v) > 6 and v[-6:] == '_STATE':
CONTRACT_SEAL_STATE_MAX += getattr(ContractState, v).value
2023-02-10 14:45:29 +01:00
class SealedContract(TxFactory):
2023-02-10 16:21:37 +01:00
def seal(self, contract_address, sender_address, seal, tx_format=TxFormat.JSONRPC):
2023-02-10 14:45:29 +01:00
enc = ABIContractEncoder()
enc.method('seal')
enc.typ(ABIContractType.UINT256)
enc.uint256(seal)
data = enc.get()
tx = self.template(sender_address, contract_address, use_nonce=True)
tx = self.set_code(tx, data)
tx = self.finalize(tx, tx_format)
return tx
2023-02-10 16:21:37 +01:00
def is_sealed(self, contract_address, v, sender_address=ZERO_ADDRESS, id_generator=None):
j = JSONRPCRequest(id_generator)
o = j.template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('isSealed')
enc.typ(ABIContractType.UINT256)
enc.uint256(v)
data = add_0x(enc.get())
tx = self.template(sender_address, contract_address)
tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx))
o['params'].append('latest')
o = j.finalize(o)
return o
@classmethod
def parse_is_sealed(self, v):
return abi_decode_single(ABIContractType.BOOLEAN, v)