def parse_single_item(item: Union[Dict, List]): if 'iterator' in item: item = item['iterator'] if item: if type(item[0]['value']) is not list: return [parse_single_item(i) for i in item] else: return { parse_single_item(i['value'][0]): parse_single_item(i['value'][1]) for i in item } _type = item['type'] if _type == 'Any' and 'value' not in item: return None else: value = item['value'] if _type == 'Integer': return int(value) elif _type == 'Boolean': return value elif _type == 'ByteString' or _type == 'Buffer': byte_value = base64.b64decode(value) try: return byte_value.decode() except UnicodeDecodeError: try: len_bytes = len(byte_value) if len_bytes == 20: return Hash160Str.from_UInt160(UInt160(byte_value)) if len_bytes == 32: return Hash256Str.from_UInt256(UInt256(byte_value)) except Exception: pass # may be an N3 address starting with 'N' # TODO: decode to N3 address return byte_value elif _type == 'Array': return [parse_single_item(i) for i in value] elif _type == 'Struct': return tuple([parse_single_item(i) for i in value]) elif _type == 'Map': return { parse_single_item(i['key']): parse_single_item(i['value']) for i in value } elif _type == 'Pointer': return int(value) else: raise ValueError(f'Unknown type {_type}')
except ValueError as e: if 'ASSERT is executed with false result.' in e.args[0]: print(e, end=' '); print('Maybe you have already added the Pair') else: raise e dev_client = TestClient(target_url, contract_hash, dev_wallet_hash, dev_wallet_address, 'dev.json', '1') dev_client.openwallet() sleep_for_next_block() neo_balance, gas_balance = dev_client.get_neo_balance(), dev_client.get_gas_balance() if neo_balance < 10 or gas_balance < 100e8: input(f'Warning: only f{neo_balance} NEOs and {gas_balance/1e8} left. \npress ENTER to continue') dev_client.invokefunction("getCollaterals", result_interpreted_as_iterator=True) collaterals = ClientResultInterpreter.interpret_getCollaterals(dev_client.previous_result) print('collaterals:', collaterals) try: assert Hash160Str.from_UInt160(neo.hash) in collaterals except AssertionError as e: print('getCollaterals failed. Maybe you need to wait 15 seconds before running this test again') raise e dev_client.invokefunction("getPairsMap", params=[collaterals[0]], result_interpreted_as_iterator=True) pairs = ClientResultInterpreter.interpret_getPairsMap(dev_client.previous_result) print('pairs:', pairs) selected_pair = max(pairs.keys()) dev_client.invokefunction("getPairAttributes", params=[selected_pair], result_interpreted_as_iterator=True) attributes = ClientResultInterpreter.interpret_getPairAttribtutes(dev_client.previous_result) print('attributes:', attributes) print() print('check rcToken and rrToken balance before deposit:') rcToken_before_deposit = dev_client.get_rToken_balance(attributes['rcToken']) print('rcToken balance:', rcToken_before_deposit) rrToken_before_deposit = dev_client.get_rToken_balance(attributes['rrToken'])
def bytes_to_UInt160(bytestring: bytes): return Hash160Str.from_UInt160( UInt160.deserialize_from_bytes(bytestring))
def get_gas_balance(self) -> int: return self.getwalletbalance(Hash160Str.from_UInt160(GasToken().hash))
def get_neo_balance(self) -> int: return self.getwalletbalance(Hash160Str.from_UInt160(NeoToken().hash))
def send_gas_to_address(self, to_address: Hash160Str, value: int): return self.sendtoaddress(Hash160Str.from_UInt160(gas.hash), to_address, value)
def parse_params( param: Union[str, int, dict, Hash160Str, UInt160, UInt256, bytes] ) -> Dict[str, str]: type_param = type(param) if type_param is UInt160: return { 'type': 'Hash160', 'value': str(Hash160Str.from_UInt160(param)), } elif type_param is Hash160Str: return { 'type': 'Hash160', 'value': str(param), } elif type_param is UInt256: return { 'type': 'Hash256', 'value': str(Hash256Str.from_UInt256(param)), } elif type_param is Hash256Str: return { 'type': 'Hash256', 'value': str(param), } elif type_param is PublicKeyStr: return { 'type': 'PublicKey', 'value': str(param), } elif type_param is bool: return { 'type': 'Boolean', 'value': param, } elif type_param is int: return { 'type': 'Integer', 'value': str(param), } elif type_param is str: return { 'type': 'String', 'value': param, } elif type_param is bytes: # not the best way to judge, but maybe no better method try: return { 'type': 'String', 'value': param.decode(), } except UnicodeDecodeError: return { 'type': 'ByteArray', 'value': base64.b64encode(param).decode() } elif type_param is list: return { 'type': 'Array', 'value': [parse_params(param_) for param_ in param] } elif type_param is dict: return { 'type': 'Map', 'value': [{ 'key': parse_params(k), 'value': parse_params(v) } for k, v in param.items()] } elif param is None: return { 'type': 'Any', } raise ValueError( f'Unable to handle param {param} with type {type_param}')