Exemplo n.º 1
0
 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, self.gas_price,
                                               self.gas_limit, func, False)
     time.sleep(randint(10, 15))
     event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash)
     states = Event.get_states_by_contract_address(event,
                                                   hex_contract_address)
     states[0] = Data.to_utf8_str(states[0])
     self.assertEqual('mapInfo', states[0])
     states[1] = Data.to_dict(states[1])
     self.assertTrue(isinstance(states[1], dict))
Exemplo n.º 2
0
def echo_cli_exception(e: Union[PunicaException, SDKException]):
    msg = e.args[1].replace('Other Error, ', '')
    if 'transactor' in msg:
        if isinstance(msg, str):
            words = msg.split(' ')
            words[0] = 'Payer'
            words[1] = Data.to_b58_address(Data.to_reserve_hex_str(words[1]))
            msg = ' '.join(words)
    echo(red(f'\n{msg}\n', bold=True))
    async def test_add_and_remove_attribute(self):
        ont_id = sdk.native_vm.aio_ont_id()
        identity = sdk.wallet_manager.create_identity(password)
        ctrl_acct = sdk.wallet_manager.get_control_account_by_index(
            identity.ont_id, 0, password)
        tx_hash = await ont_id.registry_ont_id(identity.ont_id, ctrl_acct,
                                               acct3, self.gas_price,
                                               self.gas_limit)
        self.assertEqual(64, len(tx_hash))
        await asyncio.sleep(randint(10, 15))
        event = await sdk.default_aio_network.get_contract_event_by_tx_hash(
            tx_hash)
        hex_contract_address = ont_id.contract_address
        notify = Event.get_notify_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')
        tx_hash = await ont_id.add_attribute(identity.ont_id, ctrl_acct,
                                             attribute, acct2, self.gas_price,
                                             self.gas_limit)
        await asyncio.sleep(randint(10, 15))
        event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash)
        notify = Event.get_notify_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', Data.to_utf8_str(notify['States'][3][0]))

        attrib_key = 'hello'
        tx_hash = await ont_id.remove_attribute(identity.ont_id, ctrl_acct,
                                                attrib_key, acct3,
                                                self.gas_price, self.gas_limit)
        await asyncio.sleep(randint(10, 15))
        event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash)
        notify = Event.get_notify_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', Data.to_utf8_str(notify['States'][3]))
        try:
            await ont_id.remove_attribute(identity.ont_id, ctrl_acct,
                                          attrib_key, acct3, self.gas_price,
                                          self.gas_limit)
        except SDKException as e:
            self.assertIn('attribute not exist', e.args[1])
        attrib_key = 'key'
        try:
            await ont_id.remove_attribute(identity.ont_id, ctrl_acct,
                                          attrib_key, acct3, self.gas_price,
                                          self.gas_limit)
        except SDKException as e:
            self.assertIn('attribute not exist', e.args[1])
Exemplo n.º 4
0
 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 = Data.big_int_to_neo_bytearray(value)
         self.assertTrue(isinstance(neo_bytearray, bytearray))
         neo_value = Data.neo_bytearray_to_big_int(neo_bytearray)
         self.assertEqual(value, neo_value)
Exemplo n.º 5
0
 def test_test_list_and_str(self):
     list_msg = [1, 2, 3]
     tx_hash = self.hello_ontology.test_list(list_msg, self.acct, gas_limit, gas_price)
     time.sleep(6)
     event = self.ontology.rpc.get_contract_event_by_tx_hash(tx_hash)
     states = Event.get_states_by_contract_address(event, self.hex_contract_address)
     states[0] = Data.to_utf8_str(states[0])
     self.assertEqual('testMsgList', states[0])
     states[1] = Data.to_int_list(states[1])
     self.assertEqual(list_msg, states[1])
Exemplo n.º 6
0
    def test_invoke_transaction(self):
        """
        from ontology.interop.System.Runtime import Notify

        def main(operation, args):
            if operation == 'hello':
                return hello(args[0])
            return False


        def hello(msg):
            Notify(["hello", msg])
            return msg
        """
        avm_code = '51c56b6c58c56b6a00527ac46a51527ac46a52527ac46a51c30568656c6c6f7d9c7c756427' \
                   '00006a53527ac46a52c300c3516a53c3936a53527ac46a53c36a00c365f2006c7566620300' \
                   '006c75660111c56b6a00527ac46a51527ac46a51c300947600a0640c00c16a52527ac4620e' \
                   '007562030000c56a52527ac46a52c3c0517d9c7c75641c00006a53527ac46a52c300c36a54' \
                   '527ac4516a55527ac4625c006a52c3c0527d9c7c756421006a52c300c36a53527ac46a52c3' \
                   '51c36a54527ac4516a55527ac4616232006a52c3c0537d9c7c756424006a52c300c36a5352' \
                   '7ac46a52c351c36a54527ac46a52c352c36a55527ac462050000f100c176c96a56527ac46a' \
                   '53c36a57527ac46a57c36a54c37d9f7c756419006a56c36a57c3c86a57c36a55c3936a5752' \
                   '7ac462e0ff6a56c36c756656c56b6a00527ac46a51527ac46a52527ac46203000568656c6c' \
                   '6f6a52c352c176c9681553797374656d2e52756e74696d652e4e6f746966796a52c36c7566'
        contract_address = sdk.neo_vm.address_from_avm_code(avm_code).hex()
        self.assertEqual('f7b9970fd6def5229c1f30ad15372bd1c20bb260',
                         contract_address)
        hello = InvokeFunction('hello')
        hello.set_params_value('ontology')
        tx = sdk.neo_vm.make_invoke_transaction(contract_address, hello,
                                                acct1.get_address_base58(),
                                                500, 20000)
        response = sdk.rpc.send_raw_transaction_pre_exec(tx)
        self.assertEqual(1, response['State'])
        response['Result'] = Data.to_utf8_str(response['Result'])
        self.assertEqual('ontology', response['Result'])
        tx.sign_transaction(acct1)
        tx_hash = sdk.rpc.send_raw_transaction(tx)
        sleep(10)
        for _ in range(5):
            try:
                event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash)
                if isinstance(event, dict) and event.get('Notify', '') != '':
                    notify = Event.get_notify_by_contract_address(
                        event, contract_address)
                    self.assertEqual(contract_address,
                                     notify['ContractAddress'])
                    self.assertEqual('hello',
                                     Data.to_utf8_str(notify['States'][0]))
                    self.assertEqual('ontology',
                                     Data.to_utf8_str(notify['States'][1]))
                    break
            except SDKException:
                continue
            sleep(2)
Exemplo n.º 7
0
 def query_revoke_event(self, tx_hash: str):
     event = self._sdk.default_network.get_contract_event_by_tx_hash(
         tx_hash)
     notify = Event.get_notify_by_contract_address(
         event, self.__hex_contract_address)
     if len(notify['States']) == 4:
         notify['States'][0] = Data.to_utf8_str(notify['States'][0])
         notify['States'][1] = Data.to_b58_address(notify['States'][1])
         notify['States'][2] = Data.to_utf8_str(notify['States'][2])
         notify['States'][3] = Data.to_hex_str(notify['States'][3])
     return notify
Exemplo n.º 8
0
 def test_test_dict_in_ctx(self):
     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}
     tx_hash = self.hello_ontology.test_dict_in_ctx(dict_msg, self.acct, gas_limit, gas_price)
     time.sleep(6)
     event = self.ontology.rpc.get_contract_event_by_tx_hash(tx_hash)
     states = Event.get_states_by_contract_address(event, self.hex_contract_address)
     states[0] = Data.to_utf8_str(states[0])
     self.assertEqual('mapInfo', states[0])
     states[1] = Data.to_dict(states[1])
     self.assertTrue(isinstance(states[1], dict))
Exemplo n.º 9
0
 def symbol(self) -> str:
     """
     This interface is used to query the asset's symbol of ONT or ONG.
     """
     tx = self.new_symbol_tx()
     response = self._sdk.default_network.send_raw_transaction_pre_exec(tx)
     return Data.to_utf8_str(response['Result'])
Exemplo n.º 10
0
 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(10, 15))
     event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash)
     states = Event.get_states_by_contract_address(event,
                                                   hex_contract_address)
     states[0] = Data.to_utf8_str(states[0])
     self.assertEqual('testMsgList', states[0])
     states[1] = Data.to_int_list(states[1])
     self.assertEqual(list_msg, states[1])
Exemplo n.º 11
0
 def name(self):
     """
     This interface is used to get the name of the token synchronously. E.g. "DXToken".
     """
     tx = self.new_name_tx()
     response = self._sdk.default_network.send_raw_transaction_pre_exec(tx)
     return Data.to_utf8_str(response.get('Result', ''))
Exemplo n.º 12
0
 def symbol(self):
     """
     This interface is used to get the symbol of the token synchronously. E.g. “DX”.
     """
     tx = self.new_symbol_tx()
     response = self._sdk.default_network.send_raw_transaction_pre_exec(tx)
     return Data.to_utf8_str(response.get('Result', ''))
Exemplo n.º 13
0
 async def query_transfer_from_event(self, tx_hash: str):
     event = await self._sdk.default_aio_network.get_contract_event_by_tx_hash(
         tx_hash)
     notify = Event.get_notify_by_contract_address(event,
                                                   self._contract_address)
     notify = Data.parse_addr_addr_int_notify(notify)
     return notify
Exemplo n.º 14
0
 async def test_get_storage(self):
     hex_contract_address = '0100000000000000000000000000000000000000'
     key = '746f74616c537570706c79'
     storage = await sdk.websocket.get_storage(hex_contract_address, key)
     await sdk.websocket.close_connect()
     value = Data.to_int(storage)
     self.assertEqual(1000000000, value)
Exemplo n.º 15
0
 async def test_subscribe(self):
     hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9'
     oep4 = sdk.neo_vm.aio_oep4(hex_contract_address)
     response = await sdk.websocket.subscribe(hex_contract_address, True, False, False, False)
     self.assertEqual([hex_contract_address], response['ContractsFilter'])
     self.assertEqual(True, response['SubscribeEvent'])
     self.assertEqual(False, response['SubscribeJsonBlock'])
     self.assertEqual(False, response['SubscribeRawBlock'])
     b58_to_address = acct2.get_address_base58()
     value = 10
     tx_hash = await oep4.transfer(acct1, b58_to_address, value, acct3, 500, 20000)
     self.assertEqual(64, len(tx_hash))
     try:
         event = await asyncio.wait_for(sdk.websocket.recv_subscribe_info(), timeout=10)
         self.assertEqual(False, response['SubscribeBlockTxHashs'])
         self.assertEqual(64, len(event['TxHash']))
         notify = Event.get_notify_by_contract_address(event, hex_contract_address)
         notify = Data.parse_addr_addr_int_notify(notify)
         self.assertEqual(hex_contract_address, notify['ContractAddress'])
         self.assertEqual('transfer', notify['States'][0])
         self.assertEqual(acct1.get_address_base58(), notify['States'][1])
         self.assertEqual(b58_to_address, notify['States'][2])
         self.assertEqual(value, notify['States'][3])
     except asyncio.TimeoutError:
         pass
     finally:
         await sdk.websocket.close_connect()
Exemplo n.º 16
0
 def test_oep4_decimal(self):
     hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9'
     func = InvokeFunction('decimals')
     result = sdk.rpc.send_neo_vm_tx_pre_exec(hex_contract_address, func)
     decimals = result['Result']
     decimals = Data.to_int(decimals)
     self.assertEqual(10, decimals)
Exemplo n.º 17
0
 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_tx_pre_exec(hex_contract_address, func)
     dict_value = result['Result']
     dict_value = Data.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_tx_pre_exec(hex_contract_address, func)
     dict_value = result['Result']
     dict_value = Data.to_int_list(dict_value)
     self.assertEqual(list_value, dict_value)
Exemplo n.º 18
0
 def test_send_raw_transaction_pre_exec(self):
     b58_address = acct1.get_address_base58()
     tx = sdk.native_vm.ong().new_transfer_tx(b58_address, acct2.get_address(), 2, b58_address, 500, 20000)
     tx.sign_transaction(acct1)
     result = sdk.rpc.send_raw_transaction_pre_exec(tx)
     self.assertTrue(Data.to_bool(result['Result']))
     self.assertEqual(result['Gas'], 20000)
     self.assertEqual(result['State'], 1)
Exemplo n.º 19
0
 def parse_status(result: dict):
     status = result['Result']
     if status == '':
         status = False
     else:
         status = Data.to_dict(status)
         status = bool(status[3])
     return status
Exemplo n.º 20
0
 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 = Data.op_code_to_int(op_code)
         self.assertEqual(num, value)
Exemplo n.º 21
0
 async def name(self) -> str:
     """
     This interface is used to query the asset's name of ONT or ONG.
     """
     tx = self.new_name_tx()
     response = await self._sdk.default_aio_network.send_raw_transaction_pre_exec(
         tx)
     return Data.to_utf8_str(response['Result'])
Exemplo n.º 22
0
 def decimals(self):
     """
     This interface is used to the number of decimals the token uses synchronously.
     E.g. 8, means to divide the token amount by 100000000 to get its user representation.
     """
     tx = self.new_decimals_tx()
     response = self._sdk.default_network.send_raw_transaction_pre_exec(tx)
     return Data.to_int(response.get('Result', ''))
Exemplo n.º 23
0
 def test_oep4_total_supply(self):
     hex_contract_address = '1ddbb682743e9d9e2b71ff419e97a9358c5c4ee9'
     func = InvokeFunction('totalSupply')
     result = sdk.default_network.send_neo_vm_tx_pre_exec(
         hex_contract_address, func)
     total_supply = result['Result']
     total_supply = Data.to_int(total_supply)
     self.assertEqual(10000000000000000000, total_supply)
Exemplo n.º 24
0
 def test_transfer_multi_args_0(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
     ]
     transfer_list = [transfer_1, transfer_2]
     hex_contract_address = 'ca91a73433c016fbcbcf98051d385785a6a5d9be'
     func = InvokeFunction('transfer_multi')
     func.set_params_value(transfer_list)
     tx_hash = sdk.rpc.send_neo_vm_transaction(hex_contract_address, acct1,
                                               acct2, self.gas_price,
                                               self.gas_limit, func, False)
     self.assertEqual(64, len(tx_hash))
     time.sleep(randint(10, 15))
     event = sdk.rpc.get_contract_event_by_tx_hash(tx_hash)
     states = Event.get_states_by_contract_address(event,
                                                   hex_contract_address)
     states[0] = Data.to_utf8_str(states[0])
     states[1][0][0] = Data.to_b58_address(states[1][0][0])
     self.assertEqual(acct1.get_address_base58(), states[1][0][0])
     states[1][0][1] = Data.to_b58_address(states[1][0][1])
     self.assertEqual(acct2.get_address_base58(), states[1][0][1])
     states[1][0][2] = Data.to_int(states[1][0][2])
     self.assertEqual(10, states[1][0][2])
     states[1][1][0] = Data.to_b58_address(states[1][1][0])
     self.assertEqual(acct2.get_address_base58(), states[1][1][0])
     states[1][1][1] = Data.to_b58_address(states[1][1][1])
     self.assertEqual(acct3.get_address_base58(), states[1][1][1])
     states[1][1][2] = Data.to_int(states[1][1][2])
     self.assertEqual(100, states[1][1][2])
Exemplo n.º 25
0
 def hello(self, msg: str) -> str:
     if not isinstance(msg, str):
         raise RuntimeError('the type of msg should be str')
     func = InvokeFunction('hello')
     func.set_params_value(msg)
     tx = self.__sdk.neo_vm.make_invoke_transaction(
         self.__hex_contract_address, func)
     value = self.__sdk.rpc.send_raw_transaction_pre_exec(tx)
     return Data.to_utf8_str(value.get('Result', ''))
Exemplo n.º 26
0
 def allowance(self, from_address: Union[str, Address],
               to_address: Union[str, Address]) -> int:
     tx = self.new_allowance_tx(from_address, to_address)
     response = self._sdk.default_network.send_raw_transaction_pre_exec(tx)
     try:
         allowance = Data.to_int(response['Result'])
         return allowance
     except SDKException:
         return 0
Exemplo n.º 27
0
 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_tx_pre_exec(hex_contract_address, func)
     value = result['Result']
     value = Data.to_utf8_str(value)
     self.assertEqual('value', value)
Exemplo n.º 28
0
 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_tx_pre_exec(hex_contract_address, func)
     symbol = result['Result']
     symbol = Data.to_utf8_str(symbol)
     self.assertEqual('DX', symbol)
Exemplo n.º 29
0
 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_tx_pre_exec(hex_contract_address, func)
     name = result['Result']
     name = Data.to_utf8_str(name)
     self.assertEqual('DXToken', name)
Exemplo n.º 30
0
 def balance_of(self, owner: str) -> int:
     """
     This interface is used to get the account balance of another account with owner address synchronously.
     """
     tx = self.new_balance_of_tx(owner)
     result = self._sdk.default_network.send_raw_transaction_pre_exec(tx)
     try:
         balance = Data.to_int(result['Result'])
     except SDKException:
         balance = 0
     return balance