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_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 = sdk.rpc.send_neo_vm_transaction(hex_contract_address, None, acct1, gas_limit, gas_price, notify_args, False) time.sleep(randint(6, 10)) 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 query_balance(self, asset: str, b58_address: str) -> int: """ This interface is used to query the account's ONT or ONG balance. :param asset: a string which is used to indicate which asset we want to check the balance. :param b58_address: a base58 encode account address. :return: account balance. """ raw_address = Address.b58decode(b58_address).to_bytes() contract_address = self.get_asset_address(asset) invoke_code = build_native_invoke_code(contract_address, b'\x00', "balanceOf", raw_address) unix_time_now = int(time()) version = 0 tx_type = 0xd1 gas_price = 0 gas_limit = 0 attributes = bytearray() signers = list() tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, None, invoke_code, attributes, signers) response = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) try: balance = ContractDataParser.to_int(response['Result']) return balance except SDKException: return 0
def query_allowance(self, asset: str, b58_from_address: str, b58_to_address: str) -> int: """ :param asset: a string which is used to indicate which asset's allowance we want to get. :param b58_from_address: a base58 encode address which indicate where the allowance from. :param b58_to_address: a base58 encode address which indicate where the allowance to. :return: the amount of allowance in the from of int. """ contract_address = self.get_asset_address(asset) raw_from = Address.b58decode(b58_from_address).to_bytes() raw_to = Address.b58decode(b58_to_address).to_bytes() args = {"from": raw_from, "to": raw_to} invoke_code = build_native_invoke_code(contract_address, b'\x00', "allowance", args) unix_time_now = int(time()) version = 0 tx_type = 0xd1 gas_price = 0 gas_limit = 0 attributes = bytearray() signers = list() tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, None, invoke_code, attributes, signers) response = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) try: allowance = ContractDataParser.to_int(response['Result']) return allowance except SDKException: return 0
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) try: 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)) except SDKException as e: self.assertIn('already in the tx pool', e.args[1]) return time.sleep(randint(6, 10)) 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 test_test_hello(self): bool_msg = True int_msg = 1 bytes_msg = b'Hello' str_msg = 'Hello' address_msg = acct.get_address().to_bytes() tx_hash = hello_ontology.test_hello(bool_msg, int_msg, bytes_msg, str_msg, address_msg, acct, gas_limit, gas_price) time.sleep(6) event = ontology.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(acct.get_address_base58(), states[5])
def test_test_struct_list_and_str(self): bool_msg = True int_msg = 10 bytes_msg = b'Hello' str_msg = 'Hello' list_msg = [1, 10, 1024, [1, 10, 1024, [1, 10, 1024]]] struct_list = [bool_msg, int_msg, bytes_msg, str_msg, list_msg] tx_hash = hello_ontology.test_struct_list_and_str( struct_list, str_msg, acct, gas_limit, gas_price) time.sleep(6) event = ontology.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]) states[1][0] = ContractDataParser.to_bool(states[1][0]) self.assertEqual(bool_msg, states[1][0]) states[1][1] = ContractDataParser.to_int(states[1][1]) self.assertEqual(int_msg, states[1][1]) states[1][2] = ContractDataParser.to_bytes(states[1][2]) self.assertEqual(bytes_msg, states[1][2]) states[1][3] = ContractDataParser.to_utf8_str(states[1][3]) self.assertEqual(str_msg, states[1][3]) states[1][4] = ContractDataParser.to_int_list(states[1][4]) self.assertEqual(list_msg, states[1][4]) states[2] = ContractDataParser.to_utf8_str(states[2])
def test_oep4_decimal(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' func = InvokeFunction('decimals') result = sdk.rpc.send_neo_vm_transaction(hex_contract_address, None, None, 0, 0, func, True) 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(hex_contract_address, None, None, 0, 0, func, True) total_supply = result['Result'] total_supply = ContractDataParser.to_int(total_supply) self.assertEqual(10000000000000000000, total_supply)
def test_get_storage(self): sdk = OntologySdk() sdk.rpc.connect_to_test_net() contract_address = "0100000000000000000000000000000000000000" key = "746f74616c537570706c79" value = sdk.rpc.get_storage(contract_address, key) value = ContractDataParser.to_int(value) self.assertEqual(1000000000, value)
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_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(6, 10)) 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 test_oep4_balance_of(self): hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' func = InvokeFunction('balanceOf') bytes_address = acct1.get_address().to_bytes() func.set_params_value(bytes_address) result = sdk.rpc.send_neo_vm_transaction(hex_contract_address, None, None, 0, 0, func, True) balance = result['Result'] balance = ContractDataParser.to_int(balance) self.assertGreater(balance, 100)
def test_transfer_multi(self): sdk = OntologySdk() sdk.rpc.connect_to_test_net() contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9' oep4 = sdk.neo_vm.oep4() oep4.hex_contract_address = contract_address transfer_list = list() b58_from_address1 = acct1.get_address_base58() b58_from_address2 = acct2.get_address_base58() hex_from_address1 = acct1.get_address_hex() hex_from_address2 = acct2.get_address_hex() from_address_list = [hex_from_address1, hex_from_address2] b58_to_address1 = acct2.get_address_base58() b58_to_address2 = acct3.get_address_base58() hex_to_address1 = acct2.get_address_hex() hex_to_address2 = acct3.get_address_hex() to_address_list = [hex_to_address1, hex_to_address2] value_list = [1, 2] transfer1 = [b58_from_address1, b58_to_address1, value_list[0]] transfer2 = [b58_from_address2, b58_to_address2, value_list[1]] signers = [acct1, acct2, acct3] transfer_list.append(transfer1) transfer_list.append(transfer2) gas_limit = 20000000 gas_price = 500 tx_hash = oep4.transfer_multi(transfer_list, signers[0], signers, gas_limit, gas_price) self.assertEqual(64, len(tx_hash)) sdk = OntologySdk() sdk.rpc.connect_to_test_net() time.sleep(randint(6, 10)) try: event = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash) notify_list = event['Notify'][:-1] self.assertEqual(len(transfer_list), len(notify_list)) for index, notify in enumerate(notify_list): self.assertEqual( 'transfer', ContractDataParser.to_utf8_str(notify['States'][0])) self.assertEqual(from_address_list[index], notify['States'][1]) self.assertEqual(to_address_list[index], notify['States'][2]) notify_value = ContractDataParser.to_int(notify['States'][3]) self.assertEqual(value_list[index], notify_value) except SDKException as e: raised = False self.assertTrue(raised, e)
def query_transfer_event(self, tx_hash): 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['States'][0] = ContractDataParser.to_utf8_str( notify['States'][0]) notify['States'][1] = ContractDataParser.to_b58_address( notify['States'][1]) notify['States'][2] = ContractDataParser.to_b58_address( notify['States'][2]) notify['States'][3] = ContractDataParser.to_int(notify['States'][3]) return notify
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) try: 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)) except SDKException as e: self.assertIn('already in the tx pool', e.args[1]) return time.sleep(randint(6, 10)) 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 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( self.__hex_contract_address, None, None, 0, 0, func, True) try: total_supply = ContractDataParser.to_int(response['Result']) except SDKException: total_supply = 0 return total_supply
def query_decimals(self, asset: str) -> int: """ This interface is used to query the asset's decimals of ONT or ONG. :param asset: a string which is used to indicate which asset's decimals we want to get :return: asset's decimals in the form of int """ contract_address = self.get_asset_address(asset) invoke_code = build_native_invoke_code(contract_address, b'\x00', 'decimals', bytearray()) tx = Transaction(0, 0xd1, int(time()), 0, 0, None, invoke_code, bytearray(), list()) response = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) try: decimal = ContractDataParser.to_int(response['Result']) return decimal except SDKException: return 0
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( self.__hex_contract_address, None, None, 0, 0, func, True) try: balance = ContractDataParser.to_int(result['Result']) except SDKException: balance = 0 return balance
def test_test_struct_list_and_str_pre_exec(self): bool_msg = True int_msg = 10 bytes_msg = b'Hello' str_msg = 'Hello' list_msg = [1, 10, 1024, [1, 10, 1024, [1, 10, 1024]]] struct_list = [bool_msg, int_msg, bytes_msg, str_msg, list_msg] value = hello_ontology.test_struct_list_and_str_pre_exec( struct_list, str_msg) value[0][0] = ContractDataParser.to_bool(value[0][0]) self.assertEqual(bool_msg, value[0][0]) value[0][1] = ContractDataParser.to_int(value[0][1]) self.assertEqual(int_msg, value[0][1]) value[0][2] = ContractDataParser.to_bytes(value[0][2]) self.assertEqual(bytes_msg, value[0][2]) value[0][3] = ContractDataParser.to_utf8_str(value[0][3]) self.assertEqual(str_msg, value[0][3]) value[0][4] = ContractDataParser.to_int_list(value[0][4]) self.assertEqual(list_msg, value[0][4]) value[1] = ContractDataParser.to_utf8_str(value[1]) self.assertEqual(str_msg, value[1])
def allowance(self, b58_owner_address: str, b58_spender_address: str): """ This interface is used to call the Allowance method in ope4 that query the amount of spender still allowed to withdraw from owner account. :param b58_owner_address: a base58 encode address that represent owner's account. :param b58_spender_address: a base58 encode address that represent spender's account. :return: the amount of oep4 token that owner allow spender to transfer from the owner account. """ func = InvokeFunction('allowance') Oep4.__b58_address_check(b58_owner_address) owner = Address.b58decode(b58_owner_address).to_bytes() Oep4.__b58_address_check(b58_spender_address) spender = Address.b58decode(b58_spender_address).to_bytes() func.set_params_value(owner, spender) result = self.__sdk.get_network().send_neo_vm_transaction( self.__hex_contract_address, None, None, 0, 0, func, True) try: allowance = ContractDataParser.to_int(result['Result']) except SDKException: allowance = 0 return allowance
def test_notify_pre_exec(self): bool_msg = True int_msg = 1024 list_msg = [1, 1024, 2048] str_msg = 'Hello' bytes_address_msg = acct1.get_address().to_bytes() hex_contract_address = '4855735ffadad50e7000d73e1c4e96f38d225f70' notify_args = InvokeFunction('notify_args') notify_args.set_params_value(bool_msg, int_msg, list_msg, str_msg, bytes_address_msg) rpc_address = 'http://polaris5.ont.io:20336' sdk.set_rpc_address(rpc_address) try: response = sdk.rpc.send_neo_vm_transaction(hex_contract_address, None, None, 0, 0, notify_args, True) except SDKException as e: self.assertIn('already in the tx pool', e.args[1]) return sdk.rpc.connect_to_test_net() response['Result'] = ContractDataParser.to_bool(response['Result']) self.assertEqual(1, response['State']) self.assertEqual(20000, response['Gas']) self.assertEqual(True, response['Result']) notify = response['Notify'][0] self.assertEqual(hex_contract_address, notify['ContractAddress']) states = notify['States'] states[0] = ContractDataParser.to_utf8_str(states[0]) self.assertEqual('notify args', states[0]) states[1] = ContractDataParser.to_bool(states[1]) self.assertEqual(True, states[1]) states[2] = ContractDataParser.to_int(states[2]) self.assertEqual(int_msg, states[2]) states[3] = ContractDataParser.to_int_list(states[3]) self.assertEqual(list_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_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(6, 10)) 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_get_storage(self): contract_address = "0100000000000000000000000000000000000000" key = "746f74616c537570706c79" value = restful_client.get_storage(contract_address, key) value = ContractDataParser.to_int(value) self.assertEqual(1000000000, value)