pool.py (5039B)
1 # standard imports 2 import logging 3 import os 4 import enum 5 6 # external imports 7 from chainlib.eth.constant import ZERO_ADDRESS 8 from chainlib.eth.contract import ( 9 ABIContractEncoder, 10 ABIContractDecoder, 11 ABIContractType, 12 abi_decode_single, 13 ) 14 from chainlib.eth.jsonrpc import to_blockheight_param 15 from chainlib.eth.error import RequestMismatchException 16 from chainlib.eth.tx import ( 17 TxFactory, 18 TxFormat, 19 ) 20 from chainlib.jsonrpc import JSONRPCRequest 21 from chainlib.block import BlockSpec 22 from hexathon import ( 23 add_0x, 24 strip_0x, 25 ) 26 from chainlib.eth.cli.encode import CLIEncoder 27 28 # local imports 29 from erc20_pool.data import data_dir 30 31 logg = logging.getLogger() 32 33 34 class Pool(TxFactory): 35 36 __abi = None 37 __bytecode = None 38 39 def constructor(self, sender_address, name, symbol, decimals, token_registry=None, token_limiter=None, tx_format=TxFormat.JSONRPC, version=None): 40 code = self.cargs(name, symbol, decimals, token_registry=token_registry, token_limiter=token_limiter, version=version) 41 tx = self.template(sender_address, None, use_nonce=True) 42 tx = self.set_code(tx, code) 43 return self.finalize(tx, tx_format) 44 45 46 @staticmethod 47 def cargs(name, symbol, decimals, token_registry=None, token_limiter=None, version=None): 48 if token_registry == None: 49 token_registry = ZERO_ADDRESS 50 if token_limiter == None: 51 token_limiter = ZERO_ADDRESS 52 code = Pool.bytecode(version=version) 53 enc = ABIContractEncoder() 54 enc.string(name) 55 enc.string(symbol) 56 enc.uint256(decimals) 57 enc.address(token_registry) 58 enc.address(token_limiter) 59 args = enc.get() 60 code += args 61 logg.debug('constructor code: ' + args) 62 return code 63 64 65 @staticmethod 66 def gas(code=None): 67 return 4000000 68 69 70 @staticmethod 71 def abi(): 72 if Pool.__abi == None: 73 f = open(os.path.join(data_dir, 'SwapPool.json'), 'r') 74 Pool.__abi = json.load(f) 75 f.close() 76 return Pool.__abi 77 78 79 @staticmethod 80 def bytecode(version=None): 81 if Pool.__bytecode == None: 82 f = open(os.path.join(data_dir, 'SwapPool.bin')) 83 Pool.__bytecode = f.read() 84 f.close() 85 return Pool.__bytecode 86 87 88 def deposit(self, contract_address, sender_address, token_address, value, tx_format=TxFormat.JSONRPC, id_generator=None): 89 enc = ABIContractEncoder() 90 enc.method('deposit') 91 enc.typ(ABIContractType.ADDRESS) 92 enc.typ(ABIContractType.UINT256) 93 enc.address(token_address) 94 enc.uint256(value) 95 data = add_0x(enc.get()) 96 tx = self.template(sender_address, contract_address, use_nonce=True) 97 tx = self.set_code(tx, data) 98 tx = self.finalize(tx, tx_format, id_generator=id_generator) 99 return tx 100 101 102 def swap(self, contract_address, sender_address, token_address_out, token_address_in, value, tx_format=TxFormat.JSONRPC, id_generator=None): 103 enc = ABIContractEncoder() 104 enc.method('withdraw') 105 enc.typ(ABIContractType.ADDRESS) 106 enc.typ(ABIContractType.ADDRESS) 107 enc.typ(ABIContractType.UINT256) 108 enc.address(token_address_out) 109 enc.address(token_address_in) 110 enc.uint256(value) 111 data = add_0x(enc.get()) 112 tx = self.template(sender_address, contract_address, use_nonce=True) 113 tx = self.set_code(tx, data) 114 tx = self.finalize(tx, tx_format, id_generator=id_generator) 115 return tx 116 117 118 def set_fee_address(self, contract_address, sender_address, fee_address, tx_format=TxFormat.JSONRPC, id_generator=None): 119 enc = ABIContractEncoder() 120 enc.method('setFeeAddress') 121 enc.typ(ABIContractType.ADDRESS) 122 enc.address(fee_address) 123 data = add_0x(enc.get()) 124 tx = self.template(sender_address, contract_address, use_nonce=True) 125 tx = self.set_code(tx, data) 126 tx = self.finalize(tx, tx_format, id_generator=id_generator) 127 return tx 128 129 130 def set_fee(self, contract_address, sender_address, fee, tx_format=TxFormat.JSONRPC, id_generator=None): 131 enc = ABIContractEncoder() 132 enc.method('setFee') 133 enc.typ(ABIContractType.UINT256) 134 enc.uint256(fee) 135 data = add_0x(enc.get()) 136 tx = self.template(sender_address, contract_address, use_nonce=True) 137 tx = self.set_code(tx, data) 138 tx = self.finalize(tx, tx_format, id_generator=id_generator) 139 return tx 140 141 142 def withdraw(self, contract_address, sender_address, token_address, tx_format=TxFormat.JSONRPC, id_generator=None): 143 enc = ABIContractEncoder() 144 enc.method('withdraw') 145 enc.typ(ABIContractType.ADDRESS) 146 enc.address(token_address) 147 data = add_0x(enc.get()) 148 tx = self.template(sender_address, contract_address, use_nonce=True) 149 tx = self.set_code(tx, data) 150 tx = self.finalize(tx, tx_format, id_generator=id_generator) 151 return tx