def transfer(self, conf: dict) -> dict: """Transfer ICX Coin. :param conf: transfer command configuration. :return: response of transfer. """ # check value type (must be int), address and keystore file # if valid, return user input password password = conf.get('password', None) password = self._check_transfer(conf, password) if password: try: wallet = KeyWallet.load(conf['keyStore'], password) from_ = wallet.get_address() except KeyStoreException as e: print(e.args[0]) return None else: # make dummy wallet wallet = KeyWallet.create() from_ = conf['from'] uri, version = uri_parser(conf['uri']) icon_service = IconService(HTTPProvider(uri, version)) transaction = TransactionBuilder() \ .from_(from_) \ .to(conf['to']) \ .value(int(conf['value'])) \ .nid(convert_hex_str_to_int(conf['nid'])) \ .timestamp(int(time() * 10 ** 6)) \ .build() if 'stepLimit' not in conf: step_limit = icon_service.estimate_step(transaction) else: step_limit = convert_hex_str_to_int(conf['stepLimit']) transaction.step_limit = step_limit # Returns the signed transaction object having a signature signed_transaction = SignedTransaction(transaction, wallet) if not password: signed_transaction.signed_transaction_dict['signature'] = 'sig' # Sends transaction and return response response = send_transaction_with_logger(icon_service, signed_transaction, uri) if 'result' in response: print('Send transfer request successfully.') tx_hash = response['result'] print(f"transaction hash: {tx_hash}") else: print('Got an error response') print(json.dumps(response, indent=4)) return response
def test_to_dict(self): # Transfer # When having an optional property, nonce icx_transaction = TransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \ .value(self.setting["value"]).step_limit(self.setting["step_limit"]).nid(self.setting["nid"]) \ .nonce(self.setting["nonce"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( icx_transaction) self.assertTrue(is_icx_transaction(tx_dict)) # When not having an optional property, nonce icx_transaction = TransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \ .value(self.setting["value"]).step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( icx_transaction) self.assertTrue(is_icx_transaction(tx_dict)) # When not having an required property, value icx_transaction = TransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \ .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( icx_transaction) self.assertFalse(is_icx_transaction(tx_dict)) # Update SCORE deploy_transaction = DeployTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \ .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).content_type(self.setting["content_type"]) \ .content(self.setting["content_update"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( deploy_transaction) self.assertTrue(is_deploy_transaction(tx_dict)) # Install SCORE deploy_transaction = DeployTransactionBuilder().from_(self.setting["from"]).to(self.setting["to_install"]) \ .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).nonce(self.setting["nonce"]) \ .content_type(self.setting["content_type"]).content(self.setting["content_install"]) \ .params(self.setting["params_install"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( deploy_transaction) self.assertTrue(is_deploy_transaction(tx_dict)) # SCORE method call call_transaction = CallTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \ .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).nonce(self.setting["nonce"]) \ .method(self.setting["method"]).params(self.setting["params_call"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( call_transaction) self.assertTrue(is_call_transaction(tx_dict)) # Message send msg_transaction = MessageTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \ .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).data(self.setting["data"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( msg_transaction) self.assertTrue(is_message_transaction(tx_dict))
def test_send_transfer(self, _make_id): icx_transaction = TransactionBuilder() \ .from_(self.setting["from"]) \ .to(self.setting["to"]) \ .value(self.setting["value"]) \ .step_limit(self.setting["step_limit"]) \ .nid(3) \ .nonce(self.setting["nonce"]) \ .version(3) \ .timestamp(self.setting["timestamp"]) \ .build() signed_transaction = SignedTransaction(icx_transaction, self.wallet) with requests_mock.Mocker() as m: response_json: dict = { "jsonrpc": "2.0", "result": "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238", "id": 1234 } m.post(self.matcher, json=response_json) result_dict = self.icon_service.send_transaction( signed_transaction, full_response=True) actual_request = json.loads(m._adapter.last_request.text) result_content = result_dict['result'] self.assertTrue(is_T_HASH(result_content)) self.assertEqual(result_success_v3.keys(), result_dict.keys())
def generate_tx(file, password, icx_value, to_addr, nid, api_url, is_send, timestamp): step_limit = 1000000 value = int(icx_value * 10**18) wallet = KeyWallet.load(file, password) owner_from_addr = wallet.get_address() transaction = TransactionBuilder() \ .from_(owner_from_addr) \ .to(to_addr) \ .step_limit(step_limit) \ .nid(int(nid)) \ .nonce(100) \ .value(value) \ .timestamp(timestamp)\ .build() signed_params = SignedTransaction(transaction, wallet) signed_payload = { 'jsonrpc': '2.0', 'method': "icx_sendTransaction", 'id': 1234, "params": signed_params.signed_transaction_dict } kvPrint("[before] signed_payload", json.dumps(signed_payload, indent=4, sort_keys=True)) kvPrint("[before] calculate the tx_hash", f"0x{generate_message(signed_params.signed_transaction_dict)}") if is_send: print("===== send tx =====") provider = HTTPProvider(f"{api_url}/api/v3") icon_service = IconService(provider) tx_hash = icon_service.send_transaction(signed_params) kvPrint(f"[after] sendTX() txResult", tx_hash) return signed_payload
def icx_transfer_call(icon_integrate_test_base: IconIntegrateTestBase, from_: KeyWallet, to_: str, value: int = 0, icon_service: IconService = None) -> dict: """Sends the transaction sending ICX by using SDK :param icon_integrate_test_base: IconIntegrateTestBase :param from_: wallet address making a transaction :param to_: wallet address to receive coin or SCORE address to receive a transaction :param value: amount of ICX to be sent (Optional) :param icon_service: IconService :return: transaction result as dict """ # Generates an instance of transaction for calling method in SCORE. transaction = TransactionBuilder() \ .from_(from_.get_address()) \ .to(to_) \ .step_limit(10_000_000) \ .nid(3) \ .nonce(100) \ .value(value) \ .build() # Returns the signed transaction object having a signature signed_transaction = SignedTransaction(transaction, from_) # Sends the transaction to the network tx_result = icon_integrate_test_base.process_transaction( signed_transaction, icon_service) assert 'status' in tx_result assert 1 == tx_result['status'] return tx_result
def _make_account(self, balance: int = 1000) -> 'KeyWallet': # create account account: 'KeyWallet' = KeyWallet.create() # Generates an instance of transaction for sending icx. transaction = TransactionBuilder() \ .from_(self._test1.get_address()) \ .to(account.get_address()) \ .value(balance) \ .step_limit(1000000) \ .nid(3) \ .nonce(100) \ .build() # Returns the signed transaction object having a signature signed_transaction = SignedTransaction(transaction, self._test1) # process the transaction tx_result = self.process_transaction(signed_transaction, self.icon_service) self.assertTrue('status' in tx_result) self.assertEqual(1, tx_result['status']) return account
def transfer(self, wallet, to, amount, limit=100000): transaction = TransactionBuilder() \ .from_(wallet.get_address()) \ .to(to) \ .nid(self._nid) \ .value(amount) \ .build() return self._send_transaction(transaction, wallet, limit)
def test_signed_transaction_transfer(self): icx_transaction = TransactionBuilder().from_(self.wallet.get_address()).to(self.setting["to"]) \ .value(self.setting["value"]).step_limit(self.setting["step_limit"]).nid(self.setting["nid"]) \ .nonce(self.setting["nonce"]).build() signed_transaction_dict = SignedTransaction(icx_transaction, self.wallet) result = self.icon_service.send_transaction(signed_transaction_dict) self.assertTrue(is_T_HASH(result))
def build_send_icx(self, from_: KeyWallet, to: str, value: int) -> SignedTransaction: send_icx_transaction = TransactionBuilder(from_=from_.get_address(), to=to, value=value, step_limit=1000000, nid=3, nonce=3).build() signed_icx_transaction = SignedTransaction(send_icx_transaction, from_) return signed_icx_transaction
def test_safe_withdraw(self): donation = 10 transaction = CallTransactionBuilder() \ .from_(self._test1.get_address()) \ .to(self.token_score_address).method({}) \ .step_limit(2000000) \ .method("transfer") \ .params({"_to": self.crowdsale_score_address, "_value": 1000}) \ .build() signed_transaction = SignedTransaction(transaction, self._test1) tx_result = self.process_transaction(signed_transaction) self.assertTrue('status' in tx_result) self.assertEqual(1, tx_result['status']) transaction = TransactionBuilder()\ .from_(self._test1.get_address())\ .to(self.crowdsale_score_address)\ .step_limit(2000000)\ .value(donation)\ .build() signed_transaction = SignedTransaction(transaction, self._test1) tx_result = self.process_transaction(signed_transaction) self.assertTrue('status' in tx_result) self.assertEqual(1, tx_result['status']) call = CallBuilder().from_(self._test1.get_address()) \ .to(self.crowdsale_score_address) \ .method("getBalance") \ .build() response = self.process_call(call, self.icon_service) self.assertEqual(hex(donation), response) transaction = CallTransactionBuilder() \ .from_(self._test1.get_address()) \ .to(self.crowdsale_score_address).method({}) \ .step_limit(2000000) \ .method("safeWithdraw") \ .build() signed_transaction = SignedTransaction(transaction, self._test1) tx_result = self.process_transaction(signed_transaction) self.assertTrue('status' in tx_result) self.assertEqual(1, tx_result['status']) call = CallBuilder().from_(self._test1.get_address()) \ .to(self.crowdsale_score_address) \ .method("getBalance") \ .build() response = self.process_call(call, self.icon_service) self.assertEqual(hex(0), response)
def test_transfer(self, _make_id): icx_transaction = TransactionBuilder() \ .from_(self.setting["from"]) \ .to(self.setting["to"]) \ .value(self.setting["value"]) \ .step_limit(self.setting["step_limit"]) \ .nid(3) \ .nonce(self.setting["nonce"]) \ .version(3) \ .timestamp(self.setting["timestamp"]) \ .build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( icx_transaction) self.assertTrue(is_icx_transaction(tx_dict)) signed_transaction = SignedTransaction(icx_transaction, self.wallet) with requests_mock.Mocker() as m: tx_hash: str = "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238" expected_request = { 'id': 1234, 'jsonrpc': '2.0', 'method': 'icx_sendTransaction', 'params': { 'from': self.setting["from"], 'nid': hex(self.setting["nid"]), 'nonce': hex(self.setting["nonce"]), 'signature': signed_transaction.signed_transaction_dict["signature"], 'stepLimit': hex(self.setting["step_limit"]), 'timestamp': hex(self.setting["timestamp"]), 'to': self.setting["to"], 'value': hex(self.setting["value"]), 'version': '0x3' } } response_json: dict = { "jsonrpc": "2.0", "result": tx_hash, "id": 1234 } m.post(self.matcher, json=response_json) result = self.icon_service.send_transaction(signed_transaction) self.assertTrue(is_T_HASH(result)) actual_request = json.loads(m._adapter.last_request.text) self.assertEqual(expected_request, actual_request)
def test_check_goal_reached(self): donation = 10 transaction = CallTransactionBuilder() \ .from_(self._test1.get_address()) \ .to(self.token_score_address).method({}) \ .step_limit(2000000) \ .method("transfer") \ .params({"_to": self.crowdsale_score_address, "_value": 1000}) \ .build() signed_transaction = SignedTransaction(transaction, self._test1) tx_result = self.process_transaction(signed_transaction) self.assertTrue('status' in tx_result) self.assertEqual(1, tx_result['status']) transaction = TransactionBuilder()\ .from_(self._test1.get_address())\ .to(self.crowdsale_score_address)\ .step_limit(2000000)\ .value(donation)\ .build() signed_transaction = SignedTransaction(transaction, self._test1) tx_result = self.process_transaction(signed_transaction) self.assertTrue('status' in tx_result) self.assertEqual(1, tx_result['status']) transaction = CallTransactionBuilder() \ .from_(self._test1.get_address()) \ .to(self.crowdsale_score_address).method({}) \ .step_limit(2000000) \ .method("checkGoalReached") \ .build() signed_transaction = SignedTransaction(transaction, self._test1) tx_result = self.process_transaction(signed_transaction) self.assertTrue('status' in tx_result) self.assertEqual(1, tx_result['status']) call = CallBuilder().from_(self._test1.get_address()) \ .to(self.crowdsale_score_address) \ .method("isCrowdsaleClosed") \ .build() response = self.process_call(call, self.icon_service) self.assertEqual(hex(True), response) call = CallBuilder().from_(self._test1.get_address()) \ .to(self.crowdsale_score_address) \ .method("isFundingGoalReached") \ .build() response = self.process_call(call, self.icon_service) self.assertEqual(hex(True), response)
def test_signed_transaction_with_icx_transaction_without_step_limit(self): icx_transaction_without_step_limit = TransactionBuilder() \ .from_(self.wallet.get_address()) \ .to(self.setting["to"]) \ .value(self.setting["value"]) \ .nid(self.setting["nid"]) \ .nonce(self.setting["nonce"]) \ .build() # fail without step limit self.assertRaises(DataTypeException, SignedTransaction, icx_transaction_without_step_limit, self.wallet)
def test_duplicated_tx(self): # test start, deploy, stop, clean command conf = self.cmd.cmdUtil.get_init_args(project=self.project_name, score_class=self.project_class) # init self.cmd.cmdUtil.init(conf) # start tbears_config_path = os.path.join(TEST_UTIL_DIRECTORY, f'test_tbears_server_config.json') start_conf = IconConfig(tbears_config_path, tbears_server_config) start_conf.load() start_conf['config'] = tbears_config_path self.start_conf = start_conf self.cmd.cmdServer.start(start_conf) self.assertTrue(self.cmd.cmdServer.is_service_running()) # prepare to send genesis_info = start_conf['genesis']['accounts'][0] from_addr = genesis_info['address'] uri = f'http://127.0.0.1:{start_conf["port"]}/api/v3' uri, version = uri_parser(uri) icon_service = IconService(HTTPProvider(uri, version)) to_addr = f'hx{"d" * 40}' timestamp = int(time.time() * 10**6) transaction = TransactionBuilder()\ .from_(from_addr)\ .to(to_addr)\ .timestamp(timestamp)\ .step_limit(convert_hex_str_to_int('0x100000'))\ .build() wallet = KeyWallet.create() signed_transaction = SignedTransaction(transaction, wallet) signed_transaction.signed_transaction_dict['signature'] = 'sig' # send transaction response = send_transaction_with_logger(icon_service, signed_transaction, uri) self.assertTrue('result' in response) # send again response = send_transaction_with_logger(icon_service, signed_transaction, uri) self.assertTrue('error' in response) self.assertEqual( responseCodeMap[Response.fail_tx_invalid_duplicated_hash][1], response['error']['message'])
def test_convert_tx_to_jsonrpc_request_for_icx_transaction(self): # Transfer # When having an optional property, nonce icx_transaction = TransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \ .value(self.setting["value"]).step_limit(self.setting["step_limit"]).nid(self.setting["nid"]) \ .nonce(self.setting["nonce"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( icx_transaction) self.assertTrue(is_icx_transaction(tx_dict)) # When not having an optional property, nonce icx_transaction = TransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \ .value(self.setting["value"]).step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( icx_transaction) self.assertTrue(is_icx_transaction(tx_dict)) # When not having an required property, value icx_transaction = TransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \ .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).build() tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request( icx_transaction) self.assertFalse(is_icx_transaction(tx_dict))
def test_invalid_transfer1(self, _make_id): # When value is wrong prefixed with '0x' wrong_value = "0x34330000000" icx_transaction = TransactionBuilder() \ .from_(self.setting["from"]) \ .to(self.setting["to"]) \ .value(wrong_value) \ .step_limit(self.setting["step_limit"]) \ .nid(self.setting["nid"]) \ .nonce(self.setting["nonce"]) \ .build() self.assertRaises(DataTypeException, SignedTransaction, icx_transaction, self.wallet)
def test_fallback(self): transaction = TransactionBuilder() \ .to(self._score_address) \ .value(1000000000) \ .nid(3) \ .step_limit(1000000000) \ .build() signed_transaction = SignedTransaction(transaction, self._test1) tx_result = self.process_transaction(signed_transaction, self.icon_service) self.assertEqual(1, tx_result['status']) print(tx_result['eventLogs'])
def test_estimate_step_with_send_icx_transaction(self): # When having an optional property, nonce icx_transaction = TransactionBuilder() \ .from_(self.setting["from"]) \ .to(self.setting["to"]) \ .value(self.setting["value"]) \ .step_limit(self.setting["step_limit"]) \ .nid(3) \ .nonce(self.setting["nonce"]) \ .version(3) \ .build() self.assertEqual(100000, self.icon_service.estimate_step(icx_transaction))
def test_invalid_transfer2(self, _make_id): icx_transaction = TransactionBuilder() \ .from_(self.setting["from"]) \ .to(self.setting["to"][2:]) \ .value(self.setting["value"]) \ .step_limit(self.setting["step_limit"]) \ .nid(self.setting["nid"]) \ .timestamp(self.setting["timestamp"]) \ .build() signed_transaction = SignedTransaction(icx_transaction, self.wallet) with requests_mock.Mocker() as m: expected_request = { 'id': 1234, 'jsonrpc': '2.0', 'method': 'icx_sendTransaction', 'params': { 'from': self.setting["from"], 'to': self.setting["to"][2:], 'nid': hex(self.setting["nid"]), 'signature': signed_transaction.signed_transaction_dict["signature"], 'stepLimit': hex(self.setting["step_limit"]), 'timestamp': hex(self.setting["timestamp"]), 'value': hex(self.setting["value"]), 'version': '0x3' } } response_json = { "jsonrpc": "2.0", "error": { "code": -32602, "message": "Server error" }, "id": 5 } m.post(self.matcher, json=response_json, status_code=500) self.assertRaises(JSONRPCException, self.icon_service.send_transaction, signed_transaction) actual_request = json.loads(m._adapter.last_request.text) self.assertEqual(expected_request, actual_request)
def _transfer(self, to: str): transaction = TransactionBuilder().from_(self.test1_wallet.get_address()) \ .to(to) \ .step_limit(10_000_000) \ .nid(3) \ .value(1_000_000_000) \ .build() signed_transaction = SignedTransaction(transaction, self.test1_wallet) tx_result = self.process_transaction(signed_transaction, self.icon_service) self.assertTrue('status' in tx_result) self.assertEqual(1, tx_result['status'])
def test_transfer(self): transaction = TransactionBuilder() \ .from_(self._test1.get_address()) \ .to(self._score_address) \ .value(150000000) \ .step_limit(10000000) \ .nid(3) \ .nonce(100) \ .build() # # Returns the signed transaction object having a signature signed_transaction = SignedTransaction(transaction, self._test1) # # print("signed_transaction: ", signed_transaction) self.process_transaction(signed_transaction, self.icon_service) self.process_transaction(signed_transaction, self.icon_service)
def _distribute_icx(self, addresses: List['KeyWallet']): tx_list = [] for key_wallet in addresses: transaction = TransactionBuilder(). \ value(10**20). \ from_(self._test1.get_address()). \ to(key_wallet.get_address()). \ nid(3). \ nonce(1). \ step_limit(1000000). \ version(3). \ build() signed_transaction = SignedTransaction(transaction, self._test1) tx_list.append(signed_transaction) return tx_list
def test_signed_transaction_with_icx_transaction_without_step_limit(self): icx_transaction_without_step_limit = TransactionBuilder().from_(self.wallet.get_address()).to( self.setting["to"]) \ .value(self.setting["value"]).nid(self.setting["nid"]) \ .nonce(self.setting["nonce"]).build() # fail without step limit self.assertRaises(DataTypeException, SignedTransaction, icx_transaction_without_step_limit, self.wallet) # success with param of step limit signed_transaction_dict = SignedTransaction( icx_transaction_without_step_limit, self.wallet, self.setting["step_limit"]) result = self.icon_service.send_transaction(signed_transaction_dict) self.assertTrue(is_T_HASH(result))
def _send_icx_to_smart_wallet(wallet, name): balance = IconServiceContainer.icon_service.get_balance(IconServiceContainer.wallet.get_address()) print(f"wallet balance: {balance}") transaction = TransactionBuilder() \ .from_(wallet.get_address()) \ .to(IconServiceContainer.contract_addr) \ .value(800) \ .step_limit(100000000000) \ .nid(3) \ .nonce(100) \ .build() result = _send_transaction(transaction, wallet, 10) account_info = json.loads(result['eventLogs'][0]['indexed'][1]) print(f"created init account : {account_info}") account_info['name'] = name IconServiceContainer.accounts.append(account_info)
def process_confirm_block_tx(self, network: IconService): # build message tx transaction = TransactionBuilder() \ .from_(self._test1.get_address()) \ .to("hx0000000000000000000000000000000000000000") \ .value(0) \ .step_limit(10_000_000_000) \ .nonce(0) \ .build() # signing message tx request = SignedTransaction(transaction, self._test1) network.send_transaction(request) if self._block_confirm_interval > 0: sleep(self._block_confirm_interval) else: sleep(self._network_delay)
def create_transfer_icx_tx(from_: 'KeyWallet', to_: str, value: int, step_limit: int = DEFAULT_STEP_LIMIT, nid: int = DEFAULT_NID, nonce: int = 0) -> 'SignedTransaction': transaction = TransactionBuilder() \ .from_(from_.get_address()) \ .to(to_) \ .value(value) \ .step_limit(step_limit) \ .nid(nid) \ .nonce(nonce) \ .build() # Returns the signed transaction object having a signature signed_transaction = SignedTransaction(transaction, from_) return signed_transaction
def get_transaction(conf: dict, params: dict): data_type = params.get('dataType') params_data = params.get('data', {}) try: transaction_params = { "from_": params['from'], "to": params['to'], "nid": convert_hex_str_to_int(conf['nid']), "value": convert_hex_str_to_int(params.get('value', "0x0")) } if data_type is None: transaction_builder = TransactionBuilder(**transaction_params) elif data_type == "call": transaction_params['method'] = params_data.get('method') transaction_params['params'] = params_data.get('params') transaction_builder = CallTransactionBuilder( **transaction_params) elif data_type == "deploy": transaction_params['content'] = params_data.get('content') transaction_params['content_type'] = params_data.get( 'contentType') transaction_params['params'] = params_data.get('params') transaction_builder = DeployTransactionBuilder(**params) elif data_type == "message": transaction_params['data'] = params_data transaction_builder = MessageTransactionBuilder( **transaction_params) elif data_type == "deposit": transaction_params['action'] = params_data.get('action') transaction_params['id'] = params_data.get('id') transaction_builder = DepositTransactionBuilder( **transaction_params) else: raise JsonContentsException("Invalid dataType") transaction = transaction_builder.build() except KeyError: raise JsonContentsException( "Invalid json content. check json contents") except TypeError: raise JsonContentsException("Invalid json content. check keys") except DataTypeException: raise JsonContentsException("Invalid json content. check values") else: return transaction
def _build_transaction(self, type_="write", **kwargs): if type_ not in ("transfer", "write", "read"): raise ValueError("Wrong type value") from_ = self._wallet.get_address( ) if "from_" not in kwargs else kwargs["from_"] to_ = self._score_address if "to" not in kwargs else kwargs["to"] margin_ = 2500000 if "margin" not in kwargs else kwargs["margin"] value_ = 0 if "value" not in kwargs else kwargs["value"] method_ = None if "method" not in kwargs else kwargs["method"] params_ = {} if "params" not in kwargs else kwargs["params"] tx = None if type_ == "write": steps_ = self._estimate_steps(margin_) tx = CallTransactionBuilder() \ .from_(from_) \ .to(to_) \ .value(value_) \ .nid(3) \ .step_limit(steps_) \ .nonce(100) \ .method(method_) \ .params(params_) \ .build() elif type_ == "read": tx = CallBuilder() \ .from_(from_) \ .to(to_) \ .method(method_) \ .params(params_) \ .build() elif type_ == "transfer": steps_ = self._estimate_steps(margin_) tx = TransactionBuilder() \ .from_(from_) \ .to(to_) \ .value(value_) \ .step_limit(steps_) \ .nid(3) \ .build() return tx
def setUpClass(cls): super().setUpClass() # When having an optional property, nonce icx_transaction = TransactionBuilder() \ .from_(cls.setting["from"]) \ .to(cls.setting["to"]) \ .value(cls.setting["value"]) \ .step_limit(cls.setting["step_limit"]) \ .nid(3) \ .nonce(cls.setting["nonce"]) \ .version(3) \ .build() signed_transaction_dict = SignedTransaction(icx_transaction, cls.wallet) tx_result = cls.icon_service.send_transaction(signed_transaction_dict) sleep(2) cls.tx_hash = tx_result cls.tx_hash_invalid = "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"
def test_estimate_step_with_send_icx_transaction(self, _make_id): icx_transaction = TransactionBuilder() \ .from_(self.setting["from"]) \ .to(self.setting["to"]) \ .value(self.setting["value"]) \ .timestamp(self.setting["timestamp"]) \ .step_limit(self.setting["step_limit"]) \ .nid(self.setting["nid"]) \ .nonce(self.setting["nonce"]) \ .version(self.version) \ .build() with requests_mock.Mocker() as m: expected_step = 100_000 expected_request = { 'jsonrpc': '2.0', 'method': 'debug_estimateStep', 'id': 1234, 'params': { 'from': self.setting["from"], 'nid': hex(self.setting["nid"]), 'nonce': hex(self.setting["nonce"]), 'timestamp': hex(self.setting["timestamp"]), 'to': self.setting["to"], 'value': hex(self.setting["value"]), 'version': hex(self.version) } } response_json = { 'jsonrpc': '2.0', 'result': hex(expected_step), 'id': 1234 } m.post(self.matcher, json=response_json) result = self.icon_service.estimate_step(icx_transaction) actual_request = json.loads(m._adapter.last_request.text) self.assertEqual(expected_request, actual_request) self.assertEqual(expected_step, result)