period.py (3049B)
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 erc20_faucet import Faucet 21 from hexathon import add_0x 22 23 logg = logging.getLogger().getChild(__name__) 24 25 moddir = os.path.dirname(__file__) 26 datadir = os.path.join(moddir, 'data') 27 28 29 class PeriodSimple(Faucet): 30 31 __abi = None 32 __bytecode = None 33 __address = None 34 35 @staticmethod 36 def abi(): 37 if PeriodSimple.__abi == None: 38 f = open(os.path.join(datadir, 'PeriodSimple.json'), 'r') 39 PeriodSimple.__abi = json.load(f) 40 f.close() 41 return PeriodSimple.__abi 42 43 44 @staticmethod 45 def bytecode(): 46 if PeriodSimple.__bytecode == None: 47 f = open(os.path.join(datadir, 'PeriodSimple.bin')) 48 PeriodSimple.__bytecode = f.read() 49 f.close() 50 return PeriodSimple.__bytecode 51 52 @staticmethod 53 def gas(code=None): 54 return 2000000 55 56 57 # TODO: allow multiple overriders 58 def constructor(self, sender_address): 59 code = PeriodSimple.bytecode() 60 enc = ABIContractEncoder() 61 code += enc.get() 62 tx = self.template(sender_address, None, use_nonce=True) 63 tx = self.set_code(tx, code) 64 return self.build(tx) 65 66 67 def set_poker(self, contract_address, sender_address, poker_address, tx_format=TxFormat.JSONRPC): 68 enc = ABIContractEncoder() 69 enc.method('setPeriodChecker') 70 enc.typ(ABIContractType.ADDRESS) 71 enc.address(poker_address) 72 data = enc.get() 73 tx = self.template(sender_address, contract_address, use_nonce=True) 74 tx = self.set_code(tx, data) 75 tx = self.finalize(tx, tx_format) 76 return tx 77 78 79 def set_period(self, contract_address, sender_address, period, tx_format=TxFormat.JSONRPC): 80 enc = ABIContractEncoder() 81 enc.method('setPeriod') 82 enc.typ(ABIContractType.UINT256) 83 enc.uint256(threshold) 84 data = enc.get() 85 tx = self.template(sender_address, contract_address, use_nonce=True) 86 tx = self.set_code(tx, data) 87 tx = self.finalize(tx, tx_format) 88 return tx 89 90 91 def set_balance_threshold(self, contract_address, sender_address, threshold, tx_format=TxFormat.JSONRPC): 92 enc = ABIContractEncoder() 93 enc.method('setBalanceThreshold') 94 enc.typ(ABIContractType.UINT256) 95 enc.uint256(threshold) 96 data = enc.get() 97 tx = self.template(sender_address, contract_address, use_nonce=True) 98 tx = self.set_code(tx, data) 99 tx = self.finalize(tx, tx_format) 100 return tx