示例#1
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 = NeoInvokeFunction('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_neo_vm_invoke_code(
         self._contract_address, func)
     tx = InvokeTransaction(payer, gas_price, gas_limit, params)
     return tx
 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 = NeoInvokeFunction('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
示例#3
0
 def new_allowance_tx(self, from_address: Union[str, Address],
                      to_address: Union[str, Address]) -> InvokeTransaction:
     args = dict(from_address=Address.b58decode(from_address),
                 to_address=Address.b58decode(to_address))
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'allowance',
                                            args)
     return InvokeTransaction(payload=invoke_code)
示例#4
0
 def new_allowance_tx(
         self, owner: Union[str, bytes, Address],
         spender: Union[str, bytes, Address]) -> InvokeTransaction:
     func = NeoInvokeFunction('allowance')
     func.set_params_value(Address.b58decode(owner),
                           Address.b58decode(spender))
     tx = InvokeTransaction()
     tx.add_invoke_code(self._contract_address, func)
     return tx
 def test_b58decode(self):
     length = 20
     rand_code = utils.get_random_bytes(length)
     address = Address(rand_code)
     b58_address = address.b58encode()
     zero = Address.b58decode(b58_address).to_bytes()
     self.assertEqual(rand_code, zero)
     decode_address = Address.b58decode(b58_address).to_bytes()
     self.assertEqual(rand_code, decode_address)
 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 = NeoInvokeFunction('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
示例#7
0
 def new_change_recovery_tx(self, did: str, b58_new_recovery_address: str,
                            b58_recovery_address: str,
                            payer: Union[str, bytes,
                                         Address], gas_price: int,
                            gas_limit: int) -> InvokeTransaction:
     bytes_new_recovery = Address.b58decode(
         b58_new_recovery_address).to_bytes()
     bytes_recovery = Address.b58decode(b58_recovery_address).to_bytes()
     args = dict(did=did.encode('utf-8'),
                 new_recovery=bytes_new_recovery,
                 recovery=bytes_recovery)
     tx = self._generate_transaction('changeRecovery', args, payer,
                                     gas_price, gas_limit)
     return tx
示例#8
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 = NeoInvokeFunction('transfer')
     func.set_params_value(Address.b58decode(from_address),
                           Address.b58decode(to_address), amount)
     params = InvokeTransaction.generate_neo_vm_invoke_code(
         self._contract_address, func)
     tx = InvokeTransaction(payer, gas_price, gas_limit, params)
     return tx
示例#9
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 = NeoInvokeFunction('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
示例#10
0
 def new_balance_of_tx(self, owner: Union[str,
                                          Address]) -> InvokeTransaction:
     owner = Address.b58decode(owner)
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'balanceOf',
                                            owner)
     return InvokeTransaction(payload=invoke_code)
示例#11
0
 def new_transfer_from_tx(self, spender: Union[str, Address],
                          from_address: Union[str, Address],
                          receiver: Union[str, Address], amount: int,
                          payer: Union[str, Address], gas_price: int,
                          gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to generate a Transaction object that allow one account to transfer
     a amount of ONT or ONG Asset to another account, in the condition of the first account had been approved.
     """
     args = dict(spender=Address.b58decode(spender),
                 from_address=Address.b58decode(from_address),
                 to_address=Address.b58decode(receiver),
                 amount=amount)
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'transferFrom',
                                            args)
     return InvokeTransaction(Address.b58decode(payer), gas_price,
                              gas_limit, invoke_code)
示例#12
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 = NeoInvokeFunction('balanceOf')
     func.set_params_value(Address.b58decode(owner))
     tx = InvokeTransaction()
     tx.add_invoke_code(self._contract_address, func)
     return tx
示例#13
0
 def new_approve_tx(self, approver: Union[str,
                                          Address], spender: Union[str,
                                                                   Address],
                    amount: int, payer: Union[str, Address], gas_price: int,
                    gas_limit: int) -> Transaction:
     """
     This interface is used to generate a Transaction object for approve.
     """
     if amount <= 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the amount should be greater than than zero.'))
     args = dict(sender=Address.b58decode(approver),
                 receiver=Address.b58decode(spender),
                 amount=amount)
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'approve', args)
     return InvokeTransaction(Address.b58decode(payer), gas_price,
                              gas_limit, invoke_code)
示例#14
0
 def test_balance_of_transaction(self):
     func = WasmInvokeFunction('balanceOf')
     func.set_params_value(Address.b58decode('ANDfjwrUroaVtvBguDtrWKRMyxFwvVwnZD'))
     tx = sdk.wasm_vm.make_invoke_transaction(self.oep4_contract_address, func, acct4.get_address(), self.gas_price,
                                              self.gas_limit)
     target_payload = '0faeff23255536928b308e5caa38bc2dc14f30c31e0962616c6' \
                      '16e63654f6646b1a18af6b7c9f8a4602f9f73eeb3030f0c29b7'
     self.assertEqual(target_payload, tx.payload.hex())
     tx.sign_transaction(acct4)
     result = sdk.rpc.send_raw_transaction_pre_exec(tx)
     self.assertGreaterEqual(WasmData.to_int(result.get('Result')), 0)
示例#15
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) -> Transaction:
     """
     This interface is used to generate a Transaction object for transfer.
     """
     if amount <= 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the amount should be greater than than zero.'))
     state = [{
         'from': Address.b58decode(from_address),
         'to': Address.b58decode(to_address),
         'amount': amount
     }]
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'transfer',
                                            state)
     return InvokeTransaction(Address.b58decode(payer), gas_price,
                              gas_limit, invoke_code)
示例#16
0
def ensure_bytes_address(*objs: str or bytes or Account):
    result = list()
    for obj in objs:
        if isinstance(obj, bytes):
            result.append(obj)
        elif isinstance(obj, str):
            result.append(Address.b58decode(obj).to_bytes())
        elif isinstance(obj, Account):
            result.append(obj.get_address_bytes())
        else:
            raise SDKException(ErrorCode.param_error)
    return tuple(result)
示例#17
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 = NeoInvokeFunction('approve')
     func.set_params_value(Address.b58decode(owner),
                           Address.b58decode(spender), amount)
     params = InvokeTransaction.generate_neo_vm_invoke_code(
         self._contract_address, func)
     tx = InvokeTransaction(payer, gas_price, gas_limit, params)
     return tx
示例#18
0
 def test_transfer_tx(self):
     amount = 100
     func = WasmInvokeFunction('transfer')
     func.set_params_value(acct1.get_address(), Address.b58decode('AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve'), amount)
     tx = sdk.wasm_vm.make_invoke_transaction(self.oep4_contract_address, func, acct2.get_address(), self.gas_price,
                                              self.gas_limit)
     target_payload = '0faeff23255536928b308e5caa38bc2dc14f30c341087472616e7366657246b1a18af6b7c9f8a4602f9f73' \
                      'eeb3030f0c29b7d2c124dd088190f709b684e0bc676d70c41b377664000000000000000000000000000000'
     self.assertEqual(target_payload, tx.payload.hex())
     tx.sign_transaction(acct1, acct2)
     result = sdk.rpc.send_raw_transaction_pre_exec(tx)
     self.assertEqual('01', result.get('Result'))
     self.assertEqual(1, result.get('State'))
     notify_list = Event.get_event_from_event_list_by_contract_address(result.get('Notify'),
                                                                       self.oep4_contract_address)
     self.assertEqual(self.oep4_contract_address, notify_list[0].get('ContractAddress'))
     states = notify_list[1].get('States')
     self.assertEqual('transfer', WasmData.to_utf8(states[0]))
     self.assertEqual(acct1.get_address().b58encode(), WasmData.to_b58_address(states[1]))
     self.assertEqual('AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve', WasmData.to_b58_address(states[2]))
     self.assertEqual(amount, WasmData.to_int(states[3]))
示例#19
0
 def __init__(self,
              version=0,
              tx_type: TxType or int = None,
              gas_price: int = 0,
              gas_limit: int = 0,
              payer: Union[str, bytes, Address, None] = b'',
              payload: bytearray = bytearray(),
              nonce: int = None,
              attributes: bytearray = bytearray(),
              sig_list: List[Sig] = None):
     if gas_price < 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the gas price should be equal or greater than zero.'))
     if gas_limit < 0:
         raise SDKException(
             ErrorCode.other_error(
                 'the gas limit should be equal or greater than zero.'))
     self.version = version
     if isinstance(tx_type, int):
         tx_type = TxType(tx_type)
     if tx_type is not None:
         self.tx_type = tx_type.value
     if not nonce:
         nonce = randint(0, 0xFFFFFFFF)
     self.nonce = nonce
     self.gas_price = gas_price
     self.gas_limit = gas_limit
     if not payer:
         payer = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
     if isinstance(payer, str):
         payer = Address.b58decode(payer).to_bytes()
     if isinstance(payer, Address):
         payer = payer.to_bytes()
     self.payer = payer
     self.payload = payload
     self.attributes = attributes
     if not sig_list:
         sig_list = list()
     self.sig_list = sig_list
 def test_push_address(self):
     b58_address_list = [
         'AS7MjVEicEsJ4zjEfm2LoKoYoFsmapD7rT',
         'AFmseVrdL9f9oyCzZefL9tG6UbviEH9ugK',
         'AS3SCXw8GKTEeXpdwVw7EcC4rqSebFYpfb',
         'AJkkLbouowk6teTaxz1F2DYKfJh24PVk3r',
         'Ad4pjz2bqep4RhQrUAzMuZJkBC3qJ1tZuT',
         'AK98G45DhmPXg4TFPG1KjftvkEaHbU8SHM'
     ]
     wasm_address_list = [
         '71609b2c2f7b9447b089ad1da31586f42ca9eb10',
         '0000000000000000000000000000000000000007',
         '70a2ababdae0a9d1f9fc7296df3c6d343b772cf7',
         '20b1dc499cdf56ba70a574a1e17ac986d1f06ec2',
         'e98f4998d837fcdd44a50561f7f32140c7c6c260',
         '24ed4f965d3a5a76f5d0e87633c0b76941fc8827'
     ]
     for index, b58_address in enumerate(b58_address_list):
         self.builder.push_address(Address.b58decode(b58_address))
         self.assertEqual(wasm_address_list[index],
                          self.builder.to_bytes().hex())
         self.builder.clear_up()
示例#21
0
 def new_add_recovery_tx(self, did: str, pub_key: str or bytes,
                         b58_recovery_address: str,
                         payer: Union[str, bytes, Address], gas_price: int,
                         gas_limit: int) -> InvokeTransaction:
     """
     This interface is used to generate a Transaction object which is used to add the recovery.
     """
     if isinstance(pub_key, str):
         bytes_pub_key = bytes.fromhex(pub_key)
     elif isinstance(pub_key, bytes):
         bytes_pub_key = pub_key
     else:
         raise SDKException(
             ErrorCode.params_type_error(
                 'a bytes or str type of public key is required.'))
     bytes_recovery_address = Address.b58decode(
         b58_recovery_address).to_bytes()
     args = dict(did=did.encode('utf-8'),
                 recovery=bytes_recovery_address,
                 pk=bytes_pub_key)
     tx = self._generate_transaction('addRecovery', args, payer, gas_price,
                                     gas_limit)
     return tx