def test_invoke_transaction(self): avm_code = '58c56b6a00527ac46a51527ac46a00c30548656c6c6f9c6416006a51c300c36a52527ac46a52c3650b006c756' \ '661006c756655c56b6a00527ac46a00c3681553797374656d2e52756e74696d652e4e6f7469667961516c7566' hex_contract_address = sdk.neo_vm.avm_code_to_hex_contract_address( avm_code) self.assertEqual('39f3fb644842c808828817bd73da0946d99f237f', hex_contract_address) hello = InvokeFunction('Hello') hello.set_params_value('Ontology') response = sdk.rpc.send_neo_vm_transaction_pre_exec( hex_contract_address, None, hello) self.assertEqual(1, response['State']) result = response['Result'] result = ContractDataParser.to_bool(result) self.assertEqual(True, result) gas_limit = 20000 gas_price = 500 tx_hash = sdk.rpc.send_neo_vm_transaction(hex_contract_address, None, acct1, gas_limit, gas_price, hello) sleep(6) response = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) notify = response['Notify'][0] self.assertEqual(hex_contract_address, notify['ContractAddress']) notify['States'] = ContractDataParser.to_utf8_str(notify['States']) self.assertEqual('Ontology', notify['States'])
def test_dict_in_ctx(self): hex_contract_address = '6690b6638251be951dded8c537678200a470c679' bool_value = True int_value = 100 str_value = 'value3' dict_value = {'key': 'value'} list_value = [1, 10, 1024, [1, 10, 1024, [1, 10, 1024]]] dict_msg = { 'key': dict_value, 'key1': int_value, 'key2': str_value, 'key3': bool_value, 'key4': list_value } func = InvokeFunction('testMapInMap') func.set_params_value(dict_msg) tx_hash = sdk.rpc.send_neo_vm_transaction(hex_contract_address, None, acct1, gas_limit, gas_price, func, False) time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) states = ContractEventParser.get_states_by_contract_address( event, hex_contract_address) states[0] = ContractDataParser.to_utf8_str(states[0]) self.assertEqual('mapInfo', states[0]) states[1] = ContractDataParser.to_dict(states[1]) self.assertTrue(isinstance(states[1], dict))
def validate_proof(proof: List[dict], hex_target_hash: str, hex_merkle_root: str, is_big_endian: bool = False): if is_big_endian: hex_merkle_root = ContractDataParser.to_reserve_hex_str( hex_merkle_root) hex_target_hash = ContractDataParser.to_reserve_hex_str( hex_target_hash) if len(proof) == 0: return hex_target_hash == hex_merkle_root else: hex_proof_hash = hex_target_hash for node in proof: if is_big_endian: sibling = ContractDataParser.to_reserve_hex_str( node['TargetHash']) else: sibling = node['TargetHash'] try: direction = node['Direction'].lower() except KeyError: raise SDKException(ErrorCode.other_error('Invalid proof')) if direction == 'left': value = bytes.fromhex('01' + sibling + hex_proof_hash) hex_proof_hash = Digest.sha256(value, is_hex=True) elif direction == 'right': value = bytes.fromhex('01' + hex_proof_hash + sibling) hex_proof_hash = Digest.sha256(value, is_hex=True) else: raise SDKException(ErrorCode.other_error('Invalid proof.')) return hex_proof_hash == hex_merkle_root
def test_bigint_to_neo_bytes(self): value_list = [ 9175052165852779861, -9175052165852779861, 9199634313818843819, -9199634313818843819, 8380656, -8380656, 8446192, -8446192, 0 ] for value in value_list: neo_bytearray = ContractDataParser.big_int_to_neo_bytearray(value) self.assertTrue(isinstance(neo_bytearray, bytearray)) neo_value = ContractDataParser.neo_bytearray_to_big_int( neo_bytearray) self.assertEqual(value, neo_value)
def query_revoke_event(self, tx_hash: str): event = self.__sdk.get_network().get_smart_contract_event_by_tx_hash( tx_hash) notify = ContractEventParser.get_notify_list_by_contract_address( event, self.__hex_contract_address) if len(notify['States']) == 4: notify['States'][0] = ContractDataParser.to_utf8_str( notify['States'][0]) notify['States'][1] = ContractDataParser.to_b58_address( notify['States'][1]) notify['States'][2] = ContractDataParser.to_utf8_str( notify['States'][2]) notify['States'][3] = ContractDataParser.to_hex_str( notify['States'][3]) return notify
def query_approve_event(self, tx_hash: str): event = self.__sdk.get_network().get_smart_contract_event_by_tx_hash( tx_hash) notify = ContractEventParser.get_notify_list_by_contract_address( event, self.__hex_contract_address) notify = ContractDataParser.parse_addr_addr_int_notify(notify) return notify
def test_list(self): hex_contract_address = '6690b6638251be951dded8c537678200a470c679' list_msg = [1, 10, 1024, [1, 10, 1024, [1, 10, 1024]]] func = InvokeFunction('testList') func.set_params_value(list_msg) tx_hash = self.send_tx(hex_contract_address, None, acct1, func) if len(tx_hash) == 0: return time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) states = ContractEventParser.get_states_by_contract_address( event, hex_contract_address) states[0] = ContractDataParser.to_utf8_str(states[0]) self.assertEqual('testMsgList', states[0]) states[1] = ContractDataParser.to_int_list(states[1]) self.assertEqual(list_msg, states[1])
def test_oep4_decimal(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' func = InvokeFunction('decimals') result = sdk.rpc.send_neo_vm_transaction_pre_exec( hex_contract_address, None, func) decimals = result['Result'] decimals = ContractDataParser.to_int(decimals) self.assertEqual(10, decimals)
def test_oep4_total_supply(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' func = InvokeFunction('totalSupply') result = sdk.rpc.send_neo_vm_transaction_pre_exec( hex_contract_address, None, func) total_supply = result['Result'] total_supply = ContractDataParser.to_int(total_supply) self.assertEqual(10000000000000000000, total_supply)
def test_add_and_remove_attribute(self): ont_id = sdk.native_vm.ont_id() identity = sdk.wallet_manager.create_identity(password) ctrl_acct = sdk.wallet_manager.get_control_account_by_index(identity.ont_id, 0, password) gas_limit = 20000 gas_price = 500 tx_hash = sdk.native_vm.ont_id().registry_ont_id(identity.ont_id, ctrl_acct, acct3, gas_limit, gas_price) self.assertEqual(64, len(tx_hash)) time.sleep(randint(7, 12)) event = sdk.restful.get_smart_contract_event_by_tx_hash(tx_hash) hex_contract_address = sdk.native_vm.ont_id().contract_address notify = ContractEventParser.get_notify_list_by_contract_address(event, hex_contract_address) self.assertEqual(hex_contract_address, notify['ContractAddress']) self.assertEqual('Register', notify['States'][0]) self.assertEqual(identity.ont_id, notify['States'][1]) attribute = Attribute('hello', 'string', 'attribute') gas_limit = 20000 gas_price = 500 tx_hash = ont_id.add_attribute(identity.ont_id, ctrl_acct, attribute, acct2, gas_limit, gas_price) time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) notify = ContractEventParser.get_notify_list_by_contract_address(event, hex_contract_address) self.assertEqual('Attribute', notify['States'][0]) self.assertEqual('add', notify['States'][1]) self.assertEqual(identity.ont_id, notify['States'][2]) self.assertEqual('hello', ContractDataParser.to_utf8_str(notify['States'][3][0])) attrib_key = 'hello' tx_hash = ont_id.remove_attribute(identity.ont_id, ctrl_acct, attrib_key, acct3, gas_limit, gas_price) time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) notify = ContractEventParser.get_notify_list_by_contract_address(event, hex_contract_address) self.assertEqual('Attribute', notify['States'][0]) self.assertEqual('remove', notify['States'][1]) self.assertEqual(identity.ont_id, notify['States'][2]) self.assertEqual('hello', ContractDataParser.to_utf8_str(notify['States'][3])) try: ont_id.remove_attribute(identity.ont_id, ctrl_acct, attrib_key, acct3, gas_limit, gas_price) except SDKException as e: self.assertIn('attribute not exist', e.args[1]) attrib_key = 'key' try: ont_id.remove_attribute(identity.ont_id, ctrl_acct, attrib_key, acct3, gas_limit, gas_price) except SDKException as e: self.assertIn('attribute not exist', e.args[1])
def test_op_code_to_int(self): builder = ParamsBuilder() for num in range(100000): builder.emit_push_int(num) op_code = builder.to_bytes().hex() builder.clear_up() value = ContractDataParser.op_code_to_int(op_code) self.assertEqual(num, value)
def query_multi_transfer_event(self, tx_hash: str) -> list: event = self.__sdk.get_network().get_smart_contract_event_by_tx_hash( tx_hash) notify_list = ContractEventParser.get_notify_list_by_contract_address( event, self.__hex_contract_address) for index, notify in enumerate(notify_list): if notify.get('ContractAddress', '') == self.__hex_contract_address: notify_list[index]['States'][ 0] = ContractDataParser.to_utf8_str(notify['States'][0]) notify_list[index]['States'][ 1] = ContractDataParser.to_b58_address(notify['States'][1]) notify_list[index]['States'][ 2] = ContractDataParser.to_b58_address(notify['States'][2]) notify_list[index]['States'][3] = ContractDataParser.to_int( notify['States'][3]) return notify_list
def test_get_storage(self): hex_contract_address = '0100000000000000000000000000000000000000' key = '746f74616c537570706c79' event_loop = asyncio.get_event_loop() value = event_loop.run_until_complete( TestWebsocketClient.get_storage(hex_contract_address, key)) value = ContractDataParser.to_int(value) self.assertEqual(1000000000, value)
def test_transfer_multi_args(self): transfer_1 = [ acct1.get_address().to_bytes(), acct2.get_address().to_bytes(), 10 ] transfer_2 = [ acct2.get_address().to_bytes(), acct3.get_address().to_bytes(), 100 ] hex_contract_address = 'ca91a73433c016fbcbcf98051d385785a6a5d9be' func = InvokeFunction('transfer_multi_args') func.set_params_value(transfer_1, transfer_2) tx_hash = self.send_tx(hex_contract_address, acct1, acct2, func) if len(tx_hash) == 0: return time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) states = ContractEventParser.get_states_by_contract_address( event, hex_contract_address) states[0] = ContractDataParser.to_utf8_str(states[0]) self.assertEqual('transfer_multi_args', states[0]) states[1][0][0] = ContractDataParser.to_b58_address(states[1][0][0]) self.assertEqual(acct1.get_address_base58(), states[1][0][0]) states[1][0][1] = ContractDataParser.to_b58_address(states[1][0][1]) self.assertEqual(acct2.get_address_base58(), states[1][0][1]) states[1][0][2] = ContractDataParser.to_int(states[1][0][2]) self.assertEqual(10, states[1][0][2]) states[1][1][0] = ContractDataParser.to_b58_address(states[1][1][0]) self.assertEqual(acct2.get_address_base58(), states[1][1][0]) states[1][1][1] = ContractDataParser.to_b58_address(states[1][1][1]) self.assertEqual(acct3.get_address_base58(), states[1][1][1]) states[1][1][2] = ContractDataParser.to_int(states[1][1][2]) self.assertEqual(100, states[1][1][2])
def test_get_storage(self): hex_contract_address = "0100000000000000000000000000000000000000" key = "746f74616c537570706c79" try: value = sdk.rpc.get_storage(hex_contract_address, key) value = ContractDataParser.to_int(value) self.assertEqual(1000000000, value) except SDKException as e: self.assertTrue('ConnectTimeout' in e.args[1])
def get_symbol(self) -> str: """ This interface is used to call the Symbol method in ope4 that return the symbol of an oep4 token. :return: a short string symbol of the oep4 token """ symbol = self.__get_token_setting('symbol') return ContractDataParser.to_utf8_str(symbol)
def get_name(self) -> str: """ This interface is used to call the Name method in ope4 that return the name of an oep4 token. :return: the string name of the oep4 token. """ name = self.__get_token_setting('name') return ContractDataParser.to_utf8_str(name)
def get_decimal(self) -> int: """ This interface is used to call the Decimal method in ope4 that return the number of decimals used by the oep4 token. :return: the number of decimals used by the oep4 token. """ decimals = self.__get_token_setting('decimals') return ContractDataParser.to_int(decimals)
def test_get_dict_in_ctx(self): hex_contract_address = '6690b6638251be951dded8c537678200a470c679' key = 'key' func = InvokeFunction('testGetMapInMap') func.set_params_value(key) result = sdk.rpc.send_neo_vm_transaction_pre_exec( hex_contract_address, None, func) value = result['Result'] value = ContractDataParser.to_utf8_str(value) self.assertEqual('value', value)
def test_dict(self): hex_contract_address = '6690b6638251be951dded8c537678200a470c679' dict_msg = {'key': 'value'} func = InvokeFunction('testMap') func.set_params_value(dict_msg) result = sdk.rpc.send_neo_vm_transaction_pre_exec( hex_contract_address, None, func) dict_value = result['Result'] dict_value = ContractDataParser.to_utf8_str(dict_value) self.assertEqual('value', dict_value) list_value = [1, 10, 1024, [1, 10, 1024, [1, 10, 1024]]] dict_msg = {'key': list_value} func = InvokeFunction('testMap') func.set_params_value(dict_msg) result = sdk.rpc.send_neo_vm_transaction_pre_exec( hex_contract_address, None, func) dict_value = result['Result'] dict_value = ContractDataParser.to_int_list(dict_value) self.assertEqual(list_value, dict_value)
def test_oep4_symbol(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' func = InvokeFunction('symbol') self.assertEqual(bytearray(b'\x00\xc1\x06symbol'), func.create_invoke_code()) result = sdk.rpc.send_neo_vm_transaction_pre_exec( hex_contract_address, None, func) symbol = result['Result'] symbol = ContractDataParser.to_utf8_str(symbol) self.assertEqual('DX', symbol)
def test_oep4_name(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' func = InvokeFunction('name') self.assertEqual(bytearray(b'\x00\xc1\x04name'), func.create_invoke_code()) result = sdk.rpc.send_neo_vm_transaction_pre_exec( hex_contract_address, None, func) name = result['Result'] name = ContractDataParser.to_utf8_str(name) self.assertEqual('DXToken', name)
def get_status(self, claim_id: str): func = InvokeFunction('GetStatus') func.set_params_value(claim_id) result = self.__sdk.get_network().send_neo_vm_transaction_pre_exec( self.__hex_contract_address, None, func) status = result['Result'] if status == '': status = False else: status = ContractDataParser.to_dict(status) status = bool(status[3]) return status
def test_notify(self): hex_contract_address = '6690b6638251be951dded8c537678200a470c679' notify_args = InvokeFunction('testHello') bool_msg = True int_msg = 1 bytes_msg = b'Hello' str_msg = 'Hello' bytes_address_msg = acct1.get_address().to_bytes() notify_args.set_params_value(bool_msg, int_msg, bytes_msg, str_msg, bytes_address_msg) tx_hash = self.send_tx(hex_contract_address, None, acct1, notify_args) if len(tx_hash) == 0: return time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) states = ContractEventParser.get_states_by_contract_address( event, hex_contract_address) states[0] = ContractDataParser.to_utf8_str(states[0]) self.assertEqual('testHello', states[0]) states[1] = ContractDataParser.to_bool(states[1]) self.assertEqual(bool_msg, states[1]) states[2] = ContractDataParser.to_int(states[2]) self.assertEqual(int_msg, states[2]) states[3] = ContractDataParser.to_bytes(states[3]) self.assertEqual(bytes_msg, states[3]) states[4] = ContractDataParser.to_utf8_str(states[4]) self.assertEqual(str_msg, states[4]) states[5] = ContractDataParser.to_b58_address(states[5]) self.assertEqual(acct1.get_address_base58(), states[5])
def test_oep4_transfer(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' func = InvokeFunction('transfer') bytes_from_address = acct1.get_address().to_bytes() bytes_to_address = acct2.get_address().to_bytes() value = 1 func.set_params_value(bytes_from_address, bytes_to_address, value) tx_hash = self.send_tx(hex_contract_address, acct1, acct2, func) if len(tx_hash) == 0: return time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) states = ContractEventParser.get_states_by_contract_address( event, hex_contract_address) states[0] = ContractDataParser.to_utf8_str(states[0]) self.assertEqual('transfer', states[0]) states[1] = ContractDataParser.to_b58_address(states[1]) self.assertEqual(acct1.get_address().b58encode(), states[1]) states[2] = ContractDataParser.to_b58_address(states[2]) self.assertEqual(acct2.get_address().b58encode(), states[2]) states[3] = ContractDataParser.to_int(states[3]) self.assertEqual(value, states[3])
def get_total_supply(self) -> int: """ This interface is used to call the TotalSupply method in ope4 that return the total supply of the oep4 token. :return: the total supply of the oep4 token. """ func = InvokeFunction('totalSupply') response = self.__sdk.get_network().send_neo_vm_transaction_pre_exec( self.__hex_contract_address, None, func) try: total_supply = ContractDataParser.to_int(response['Result']) except SDKException: total_supply = 0 return total_supply
def test_oep4_approve(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' func = InvokeFunction('approve') bytes_owner_address = acct1.get_address().to_bytes() bytes_spender_address = acct2.get_address().to_bytes() amount = 10 func.set_params_value(bytes_owner_address, bytes_spender_address, amount) tx_hash = sdk.rpc.send_neo_vm_transaction(hex_contract_address, acct1, acct2, gas_limit, gas_price, func, False) self.assertEqual(64, len(tx_hash)) time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) states = ContractEventParser.get_states_by_contract_address( event, hex_contract_address) states[0] = ContractDataParser.to_utf8_str(states[0]) self.assertEqual('approval', states[0]) states[1] = ContractDataParser.to_b58_address(states[1]) self.assertEqual(acct1.get_address_base58(), states[1]) states[2] = ContractDataParser.to_b58_address(states[2]) self.assertEqual(acct2.get_address_base58(), states[2]) states[3] = ContractDataParser.to_int(states[3]) self.assertEqual(amount, states[3])
def test_oep4_balance_of(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' func = InvokeFunction('balanceOf') self.assertEqual(bytearray(b'\x00\xc1\tbalanceOf'), func.create_invoke_code()) bytes_address = acct1.get_address().to_bytes() func.set_params_value(bytes_address) target = bytearray( b'\x14F\xb1\xa1\x8a\xf6\xb7\xc9\xf8\xa4`/\x9fs\xee\xb3\x03\x0f\x0c)\xb7Q\xc1\tbalanceOf' ) self.assertEqual(target, func.create_invoke_code()) result = sdk.rpc.send_neo_vm_transaction_pre_exec( hex_contract_address, None, func) balance = result['Result'] balance = ContractDataParser.to_int(balance) self.assertGreater(balance, 100)
def test_oep4_transfer_multi(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' bytes_from_address1 = acct1.get_address().to_bytes() bytes_to_address1 = acct2.get_address().to_bytes() value1 = 2 transfer1 = [bytes_from_address1, bytes_to_address1, value1] bytes_from_address2 = acct2.get_address().to_bytes() bytes_to_address2 = acct3.get_address().to_bytes() value2 = 1 transfer2 = [bytes_from_address2, bytes_to_address2, value2] func = InvokeFunction('transferMulti') func.set_params_value(transfer1, transfer2) try: tx_hash = sdk.rpc.send_neo_vm_transaction(hex_contract_address, acct1, acct2, gas_limit, gas_price, func, False) except SDKException as e: self.assertIn('already in the tx pool', e.args[1]) return time.sleep(randint(7, 12)) event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) states_list = ContractEventParser.get_states_by_contract_address( event, hex_contract_address) states_list[0][0] = ContractDataParser.to_utf8_str(states_list[0][0]) self.assertEqual('transfer', states_list[0][0]) states_list[0][1] = ContractDataParser.to_b58_address( states_list[0][1]) self.assertEqual(acct1.get_address().b58encode(), states_list[0][1]) states_list[0][2] = ContractDataParser.to_b58_address( states_list[0][2]) self.assertEqual(acct2.get_address().b58encode(), states_list[0][2]) states_list[0][3] = ContractDataParser.to_int(states_list[0][3]) self.assertEqual(value1, states_list[0][3]) states_list[1][0] = ContractDataParser.to_utf8_str(states_list[1][0]) self.assertEqual('transfer', states_list[1][0]) states_list[1][1] = ContractDataParser.to_b58_address( states_list[1][1]) self.assertEqual(acct2.get_address().b58encode(), states_list[1][1]) states_list[1][2] = ContractDataParser.to_b58_address( states_list[1][2]) self.assertEqual(acct3.get_address().b58encode(), states_list[1][2]) states_list[1][3] = ContractDataParser.to_int(states_list[1][3]) self.assertEqual(value2, states_list[1][3])
def balance_of(self, b58_address: str) -> int: """ This interface is used to call the BalanceOf method in ope4 that query the ope4 token balance of the given base58 encode address. :param b58_address: the base58 encode address. :return: the oep4 token balance of the base58 encode address. """ func = InvokeFunction('balanceOf') Oep4.__b58_address_check(b58_address) address = Address.b58decode(b58_address).to_bytes() func.set_params_value(address) result = self.__sdk.get_network().send_neo_vm_transaction_pre_exec( self.__hex_contract_address, None, func) try: balance = ContractDataParser.to_int(result['Result']) except SDKException: balance = 0 return balance