faucet.py (3223B)
1 # Author: Louis Holbrook <dev@holbrook.no> 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 2 # SPDX-License-Identifier: GPL-3.0-or-later 3 # File-version: 1 4 # Description: Python interface to abi and bin files for faucet contracts 5 6 # standard imports 7 import logging 8 import json 9 import os 10 11 # external imports 12 from chainlib.eth.tx import TxFactory 13 from chainlib.eth.constant import ZERO_ADDRESS 14 from chainlib.eth.contract import ( 15 abi_decode_single, 16 ABIContractEncoder, 17 ABIContractType, 18 ) 19 from chainlib.eth.tx import TxFormat 20 from chainlib.jsonrpc import JSONRPCRequest 21 from erc20_faucet import Faucet 22 from hexathon import add_0x 23 24 logg = logging.getLogger().getChild(__name__) 25 26 moddir = os.path.dirname(__file__) 27 datadir = os.path.join(moddir, 'data') 28 29 30 class EthFaucet(Faucet): 31 32 __abi = None 33 __bytecode = None 34 __address = None 35 36 @staticmethod 37 def abi(): 38 if EthFaucet.__abi == None: 39 f = open(os.path.join(datadir, 'EthFaucet.json'), 'r') 40 EthFaucet.__abi = json.load(f) 41 f.close() 42 return EthFaucet.__abi 43 44 45 @staticmethod 46 def bytecode(): 47 if EthFaucet.__bytecode == None: 48 f = open(os.path.join(datadir, 'EthFaucet.bin')) 49 EthFaucet.__bytecode = f.read() 50 f.close() 51 return EthFaucet.__bytecode 52 53 @staticmethod 54 def gas(code=None): 55 return 2000000 56 57 58 # TODO: allow multiple overriders 59 def constructor(self, sender_address): 60 code = EthFaucet.bytecode() 61 enc = ABIContractEncoder() 62 code += enc.get() 63 tx = self.template(sender_address, None, use_nonce=True) 64 tx = self.set_code(tx, code) 65 return self.build(tx) 66 67 68 def set_period_checker(self, contract_address, sender_address, checker_address, tx_format=TxFormat.JSONRPC): 69 enc = ABIContractEncoder() 70 enc.method('setPeriodChecker') 71 enc.typ(ABIContractType.ADDRESS) 72 enc.address(checker_address) 73 data = enc.get() 74 tx = self.template(sender_address, contract_address, use_nonce=True) 75 tx = self.set_code(tx, data) 76 tx = self.finalize(tx, tx_format) 77 return tx 78 79 80 def set_registry(self, contract_address, sender_address, checker_address, tx_format=TxFormat.JSONRPC): 81 enc = ABIContractEncoder() 82 enc.method('setRegistry') 83 enc.typ(ABIContractType.ADDRESS) 84 enc.address(checker_address) 85 data = enc.get() 86 tx = self.template(sender_address, contract_address, use_nonce=True) 87 tx = self.set_code(tx, data) 88 tx = self.finalize(tx, tx_format) 89 return tx 90 91 92 def check(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None): 93 j = JSONRPCRequest(id_generator) 94 o = j.template() 95 o['method'] = 'eth_call' 96 enc = ABIContractEncoder() 97 enc.method('check') 98 enc.typ(ABIContractType.ADDRESS) 99 enc.address(address) 100 data = add_0x(enc.get()) 101 tx = self.template(sender_address, contract_address) 102 tx = self.set_code(tx, data) 103 o['params'].append(self.normalize(tx)) 104 o['params'].append('latest') 105 o = j.finalize(o) 106 return o