Example #1
0
 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(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('testHello', states[0])
     states[1] = Data.to_bool(states[1])
     self.assertEqual(bool_msg, states[1])
     states[2] = Data.to_int(states[2])
     self.assertEqual(int_msg, states[2])
     states[3] = Data.to_bytes(states[3])
     self.assertEqual(bytes_msg, states[3])
     states[4] = Data.to_utf8_str(states[4])
     self.assertEqual(str_msg, states[4])
     states[5] = Data.to_b58_address(states[5])
     self.assertEqual(acct1.get_address_base58(), states[5])
Example #2
0
 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'
     func = InvokeFunction('notify_args')
     func.set_params_value(bool_msg, int_msg, list_msg, str_msg,
                           bytes_address_msg)
     sdk.rpc.set_address('http://polaris5.ont.io:20336')
     response = sdk.rpc.send_neo_vm_tx_pre_exec(hex_contract_address, func)
     self.assertEqual(1, response['State'])
     self.assertEqual(20000, response['Gas'])
     notify = response['Notify'][0]
     self.assertEqual(hex_contract_address, notify['ContractAddress'])
     states = notify['States']
     states[0] = Data.to_utf8_str(states[0])
     self.assertEqual('notify args', states[0])
     states[1] = Data.to_bool(states[1])
     self.assertEqual(True, states[1])
     states[2] = Data.to_int(states[2])
     self.assertEqual(int_msg, states[2])
     states[3] = Data.to_int_list(states[3])
     self.assertEqual(list_msg, states[3])
     states[4] = Data.to_utf8_str(states[4])
     self.assertEqual(str_msg, states[4])
     states[5] = Data.to_b58_address(states[5])
     self.assertEqual(acct1.get_address_base58(), states[5])
Example #3
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))
Example #4
0
 def test_get_dict(self, key):
     func = InvokeFunction('testGetMap')
     func.set_params_value(key)
     tx = self.__sdk.neo_vm.make_invoke_transaction(
         self.__hex_contract_address, func)
     value = self.__sdk.rpc.send_raw_transaction_pre_exec(tx)
     return value
Example #5
0
 def test_struct_list_and_str_pre_exec(self, struct_list, str_msg):
     func = InvokeFunction('testStructListAndStr')
     func.set_params_value(struct_list, str_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 value.get('Result', list())
Example #6
0
 def new_allowance_tx(self, owner: Union[str, bytes, Address],
                      spender: Union[str, bytes, Address]) -> InvokeTransaction:
     func = InvokeFunction('allowance')
     func.set_params_value(Address.b58decode(owner), Address.b58decode(spender))
     tx = InvokeTransaction()
     tx.add_invoke_code(self._contract_address, func)
     return tx
Example #7
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])
Example #8
0
 def new_revoke_tx(self, claim_id: str, issuer: Union[str, bytes, Address],
                   payer: Union[str, bytes,
                                Address], gas_price: int, gas_limit: int):
     func = InvokeFunction('Revoke')
     func.set_params_value(claim_id, Address.b58decode(issuer))
     tx = InvokeTransaction(Address.b58decode(payer), gas_price, gas_limit)
     tx.add_invoke_code(self.__hex_contract_address, func)
     return tx
Example #9
0
 def test_list(self, list_msg: list, payer_acct, gas_limit, gas_price):
     func = InvokeFunction('testList')
     func.set_params_value(list_msg)
     tx = self.__sdk.neo_vm.make_invoke_transaction(
         self.__hex_contract_address, func, payer_acct.get_address(),
         gas_price, gas_limit)
     tx.sign_transaction(payer_acct)
     tx_hash = self.__sdk.rpc.send_raw_transaction(tx)
     return tx_hash
Example #10
0
 def new_balance_of_tx(self, owner: Union[str, bytes, Address]) -> InvokeTransaction:
     """
     This interface is used to generate transaction which can get the account balance of another account with owner address.
     """
     func = InvokeFunction('balanceOf')
     func.set_params_value(Address.b58decode(owner))
     tx = InvokeTransaction()
     tx.add_invoke_code(self._contract_address, func)
     return tx
Example #11
0
 def test_dict_in_ctx(self, map_msg, payer_acct, gas_limit, gas_price):
     func = InvokeFunction('testMapInMap')
     func.set_params_value(map_msg)
     tx = self.__sdk.neo_vm.make_invoke_transaction(
         self.__hex_contract_address, func, payer_acct.get_address(),
         gas_price, gas_limit)
     tx.sign_transaction(payer_acct)
     tx_hash = self.__sdk.rpc.send_raw_transaction(tx)
     return tx_hash
Example #12
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)
Example #13
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)
Example #14
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)
Example #15
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', ''))
Example #16
0
 def test_struct_list_and_str(self, struct_list, str_msg,
                              payer_acct: Account, gas_limit, gas_price):
     func = InvokeFunction('testStructListAndStr')
     func.set_params_value(struct_list, str_msg)
     tx = self.__sdk.neo_vm.make_invoke_transaction(
         self.__hex_contract_address, func, payer_acct.get_address(),
         gas_price, gas_limit)
     tx.sign_transaction(payer_acct)
     tx_hash = self.__sdk.rpc.send_raw_transaction(tx)
     return tx_hash
Example #17
0
 def new_transfer_tx(self, from_address: Union[str, Address], to_address: Union[str, Address], amount: int,
                     payer: Union[str, Address], gas_price: int, gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to generate a transaction which can transfer amount of tokens to to_address.
     """
     func = InvokeFunction('transfer')
     func.set_params_value(Address.b58decode(from_address), Address.b58decode(to_address), amount)
     params = InvokeTransaction.generate_invoke_code(self._contract_address, func)
     tx = InvokeTransaction(payer, gas_price, gas_limit, params)
     return tx
    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)
Example #19
0
 def new_transfer_from_tx(self, spender: Union[str, bytes, Address], owner: Union[str, bytes, Address],
                          to_address: Union[str, bytes, Address], value: int, payer: Union[str, bytes, Address],
                          gas_price: int, gas_limit: int) -> InvokeTransaction:
     func = InvokeFunction('transferFrom')
     if not isinstance(value, int):
         raise SDKException(ErrorCode.param_err('the data type of value should be int.'))
     func.set_params_value(Address.b58decode(spender), Address.b58decode(owner), Address.b58decode(to_address),
                           value)
     tx = InvokeTransaction(payer, gas_price, gas_limit)
     tx.add_invoke_code(self._contract_address, func)
     return tx
Example #20
0
 def test_hello(self, bool_msg, int_msg, bytes_msg, str_msg,
                bytes_address_msg: bytes, payer_acct: Account,
                gas_limit: int, gas_price: int) -> str:
     func = InvokeFunction('testHello')
     func.set_params_value(bool_msg, int_msg, bytes_msg, str_msg,
                           bytes_address_msg)
     tx = self.__sdk.neo_vm.make_invoke_transaction(
         self.__hex_contract_address, func, payer_acct.get_address(),
         gas_price, gas_limit)
     tx.sign_transaction(payer_acct)
     tx_hash = self.__sdk.rpc.send_raw_transaction(tx)
     return tx_hash
Example #21
0
 def new_commit_tx(self, claim_id: str, issuer_address: Union[str, bytes,
                                                              Address],
                   owner_ont_id: str, payer_address: Union[str, bytes,
                                                           Address],
                   gas_price: int, gas_limit: int) -> InvokeTransaction:
     func = InvokeFunction('Commit')
     func.set_params_value(claim_id, Address.b58decode(issuer_address),
                           owner_ont_id)
     tx = InvokeTransaction(Address.b58decode(payer_address), gas_price,
                            gas_limit)
     tx.add_invoke_code(self.__hex_contract_address, func)
     return tx
def put_one_item_to_contract(ont_id_acct: Account, ipfs_address: str, ext: str, payer_acct: Account) -> str:
    put_one_item_func = InvokeFunction('put_one_item')
    ipfs_address_bytes = ipfs_address.encode('ascii')
    aes_iv, encode_g_tilde, encrypted_ipfs_address = ECIES.encrypt_with_ont_id_in_cbc(ipfs_address_bytes, ont_id_acct)
    ont_id_acct_bytes = ont_id_acct.get_address().to_bytes()
    put_one_item_func.set_params_value(ont_id_acct_bytes, encrypted_ipfs_address, ext, aes_iv, encode_g_tilde)
    gas_limit = app.config['GAS_LIMIT']
    gas_price = app.config['GAS_PRICE']
    contract_address_bytearray = app.config['CONTRACT_ADDRESS_BYTEARRAY']
    tx_hash = app.config['ONTOLOGY'].rpc.send_neo_vm_transaction(contract_address_bytearray, ont_id_acct, payer_acct,
                                                                 gas_price, gas_limit, put_one_item_func)
    return tx_hash
Example #23
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)
Example #24
0
 def __commit_invoke(self, contract_address: str, func: Func):
     invoke_func = InvokeFunction(func.name, func.args_normalized)
     payer_address = func.payer
     if len(payer_address) == 0:
         payer_address = self.invoke_config.get('defaultPayer', '')
     if len(payer_address) == 0:
         payer_address = input('Please input payer address: ')
     tx = self.ontology.neo_vm.make_invoke_transaction(contract_address,
                                                       invoke_func,
                                                       payer_address,
                                                       self.invoke_config.get('gasPrice', 500),
                                                       self.invoke_config.get('gasLimit', 20000))
     tx = self.__add_signature(tx, func.signers, payer_address)
     tx_hash = self._send_raw_tx_with_spinner(tx)
     if len(tx_hash) != 64:
         return ''
     if self._echo_pending_tx_info(tx_hash):
         event = self.ontology.rpc.get_contract_event_by_tx_hash(tx_hash)
         notify = Event.get_notify_by_contract_address(event, contract_address)
         if len(notify) != 0:
             notify['States'] = self.parse_states(notify.get('States', dict()), func.event)
             echo('> Contract emit event:\n')
             echo('  ' + json.dumps(notify, indent=2).replace('\n', '\n  ') + '\n')
     echo(f"Using 'punica info tx {tx_hash}' to query all events in transaction.\n")
     return tx_hash
Example #25
0
 def new_total_supply_tx(self) -> InvokeTransaction:
     """
     This interface is used to generate transaction which can get the total token supply.
     """
     tx = InvokeTransaction()
     tx.add_invoke_code(self._contract_address, InvokeFunction('totalSupply'))
     return tx
Example #26
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])
Example #27
0
 def new_approve_tx(self, owner: Union[str, bytes, Address], spender: Union[str, bytes, Address], amount: int,
                    payer: Union[str, bytes, Address], gas_price: int, gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to generate a transaction which allows spender to
     withdraw from owner account multiple times, up to the _value amount.
     If this function is called again it overwrites the current allowance with amount value.
     """
     if not isinstance(amount, int):
         raise SDKException(ErrorCode.param_err('the data type of amount should be int.'))
     if amount < 0:
         raise SDKException(ErrorCode.param_err('the amount should be equal or great than 0.'))
     func = InvokeFunction('approve')
     func.set_params_value(Address.b58decode(owner), Address.b58decode(spender), amount)
     params = InvokeTransaction.generate_invoke_code(self._contract_address, func)
     tx = InvokeTransaction(payer, gas_price, gas_limit, params)
     return tx
Example #28
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)
Example #29
0
 def new_init_tx(self, payer: Union[str, bytes, Address], gas_price: int, gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to call the TotalSupply method in ope4
     that initialize smart contract parameter.
     """
     tx = InvokeTransaction(payer, gas_price, gas_limit)
     tx.add_invoke_code(self._contract_address, InvokeFunction('init'))
     return tx
Example #30
0
 def new_transfer_multi_tx(self, transfer_list: list, payer: Union[str, bytes, Address], gas_price: int,
                           gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to generate a transaction which can
     transfer amount of token from from-account to to-account multiple times.
     """
     func = InvokeFunction('transferMulti')
     for index, item in enumerate(transfer_list):
         if not isinstance(item[2], int):
             raise SDKException(ErrorCode.param_err('the data type of value should be int.'))
         if item[2] < 0:
             raise SDKException(ErrorCode.param_err('the value should be equal or great than 0.'))
         transfer_list[index] = [Address.b58decode(item[0]), Address.b58decode(item[1]), item[2]]
     for item in transfer_list:
         func.add_params_value(item)
     params = InvokeTransaction.generate_invoke_code(self._contract_address, func)
     tx = InvokeTransaction(payer, gas_price, gas_limit, params)
     return tx