Skip to content

Commit 30c1195

Browse files
author
Eunsoo Park
committed
Implement wait APIs
- Add wait_transaction_result for icx_waitTransactionResult JSON-RPC API - Add send_transaction_and_wait for icx_sendTransactionAndWait JSON-RPC API
1 parent 183724f commit 30c1195

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

iconsdk/icon_service.py

+39
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,28 @@ def get_transaction_result(self, tx_hash: str, full_response: bool = False) -> d
188188

189189
return result
190190

191+
def wait_transaction_result(self, tx_hash: str, full_response: bool = False) -> dict:
192+
"""
193+
Returns the result of a transaction specified by the transaction hash like get_transaction_result,
194+
but waits for some time to get the transaction result instead of returning immediately
195+
if there is no finalized result.
196+
Delegates to icx_WaitTransactionResult RPC method.
197+
198+
:param tx_hash: Hash of a transaction prefixed with '0x'
199+
:param full_response: Boolean to check whether get naive dict or refined data from server
200+
:return A transaction result object
201+
"""
202+
if not is_T_HASH(tx_hash):
203+
raise DataTypeException("This hash value is unrecognized.")
204+
205+
params = {'txHash': tx_hash}
206+
result = self.__provider.make_request('icx_waitTransactionResult', params, full_response)
207+
208+
if not full_response:
209+
result = convert(result, TRANSACTION_RESULT)
210+
211+
return result
212+
191213
def get_transaction(self, tx_hash: str, full_response: bool = False) -> dict:
192214
"""
193215
Returns the transaction information requested by transaction hash.
@@ -248,6 +270,23 @@ def send_transaction(self, signed_transaction: SignedTransaction, full_response:
248270
params = signed_transaction.signed_transaction_dict
249271
return self.__provider.make_request('icx_sendTransaction', params, full_response)
250272

273+
def send_transaction_and_wait(self, signed_transaction: SignedTransaction, full_response: bool = False) -> dict:
274+
"""
275+
Sends a transaction like icx_sendTransaction, then it will wait for the result of it for specified time.
276+
Delegates to icx_sendTransactionAndWait RPC method.
277+
278+
:param signed_transaction: A signed transaction object
279+
:param full_response: Boolean to check whether get naive dict or refined data from server
280+
:return A transaction result object
281+
"""
282+
params = signed_transaction.signed_transaction_dict
283+
result = self.__provider.make_request('icx_sendTransactionAndWait', params, full_response)
284+
285+
if not full_response:
286+
result = convert(result, TRANSACTION_RESULT)
287+
288+
return result
289+
251290
def estimate_step(self, transaction: Transaction, full_response: bool = False) -> int:
252291
"""
253292
Returns an estimated step of how much step is necessary to allow the transaction to complete.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2021 ICON Foundation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import json
17+
from unittest import main
18+
from unittest.mock import patch
19+
20+
import requests_mock
21+
22+
from iconsdk.exception import DataTypeException, JSONRPCException
23+
from iconsdk.utils.hexadecimal import remove_0x_prefix, add_cx_prefix
24+
from iconsdk.utils.validation import is_transaction_result
25+
from tests.api_send.test_send_super import TestSendSuper
26+
27+
28+
@patch('iconsdk.providers.http_provider.HTTPProvider._make_id', return_value=1234)
29+
class TestWaitTransactionResult(TestSendSuper):
30+
def test_wait_transaction_result(self, _make_id):
31+
tx_hash: str = "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"
32+
with requests_mock.Mocker() as m:
33+
expected_request = {
34+
'id': 1234,
35+
'jsonrpc': '2.0',
36+
'method': 'icx_waitTransactionResult',
37+
'params': {
38+
'txHash': tx_hash
39+
}
40+
}
41+
response_json = {
42+
"jsonrpc": "2.0",
43+
"result": {
44+
"txHash": "0x33db06f38424207daa69c9df153649fd3913c21e162f16f4839c9c3318e44388",
45+
"blockHeight": "0x13f",
46+
"blockHash": "0x069e8a2431ae2c7e55924af477be87518476aa1eb1b2e7d1ee8d61d7874ea907",
47+
"txIndex": "0x1",
48+
"to": "cx0000000000000000000000000000000000000000",
49+
"stepUsed": "0x263b8",
50+
"stepPrice": "0x2540be400",
51+
"cumulativeStepUsed": "0x263b8",
52+
"eventLogs": [
53+
{
54+
"scoreAddress": "cx0000000000000000000000000000000000000000",
55+
"indexed": [
56+
"PRepSet(Address)"
57+
],
58+
"data": [
59+
"hx86aba2210918a9b116973f3c4b27c41a54d5dafe"
60+
]
61+
}
62+
],
63+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000080000000000000000000000000000000000000000000000000000000000020000000000000008000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
64+
"status": "0x1"
65+
},
66+
"id": 1234
67+
}
68+
m.post(self.matcher, json=response_json)
69+
self.icon_service.wait_transaction_result(tx_hash)
70+
actual_request = json.loads(m._adapter.last_request.text)
71+
self.assertEqual(expected_request, actual_request)
72+
73+
def test_wait_transaction_result_invalid(self, _make_id):
74+
invalid_tx_hash: str = "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"
75+
# case 1: when tx_hash is invalid - no prefixed
76+
self.assertRaises(
77+
DataTypeException,
78+
self.icon_service.wait_transaction_result,
79+
remove_0x_prefix(invalid_tx_hash)
80+
)
81+
# case 2: when tx_hash is invalid - wrong prefixed
82+
self.assertRaises(
83+
DataTypeException,
84+
self.icon_service.wait_transaction_result,
85+
add_cx_prefix(remove_0x_prefix(invalid_tx_hash))
86+
)
87+
# case 3: when tx_hash is invalid - too short
88+
self.assertRaises(
89+
DataTypeException,
90+
self.icon_service.wait_transaction_result,
91+
invalid_tx_hash[:15]
92+
)
93+
94+
95+
if __name__ == "__main__":
96+
main()

tests/api_send/test_send_tx_wait.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2021 ICON Foundation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
import json
16+
from unittest.mock import patch
17+
18+
import requests_mock
19+
20+
from iconsdk.builder.transaction_builder import TransactionBuilder
21+
from iconsdk.exception import JSONRPCException, DataTypeException
22+
from iconsdk.signed_transaction import SignedTransaction
23+
from iconsdk.utils.validation import is_icx_transaction, is_T_HASH
24+
from tests.api_send.test_send_super import TestSendSuper
25+
26+
27+
@patch('iconsdk.providers.http_provider.HTTPProvider._make_id', return_value=1234)
28+
class TestSendAndWait(TestSendSuper):
29+
30+
def test_transfer(self, _make_id):
31+
icx_transaction = TransactionBuilder() \
32+
.from_(self.setting["from"]) \
33+
.to(self.setting["to"]) \
34+
.value(self.setting["value"]) \
35+
.step_limit(self.setting["step_limit"]) \
36+
.nid(3) \
37+
.nonce(self.setting["nonce"]) \
38+
.version(3) \
39+
.timestamp(self.setting["timestamp"]) \
40+
.build()
41+
tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(icx_transaction)
42+
self.assertTrue(is_icx_transaction(tx_dict))
43+
signed_transaction = SignedTransaction(icx_transaction, self.wallet)
44+
45+
with requests_mock.Mocker() as m:
46+
tx_hash: str = "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"
47+
expected_request = {
48+
'id': 1234,
49+
'jsonrpc': '2.0',
50+
'method': 'icx_sendTransactionAndWait',
51+
'params': {
52+
'from': self.setting["from"],
53+
'nid': hex(self.setting["nid"]),
54+
'nonce': hex(self.setting["nonce"]),
55+
'signature': signed_transaction.signed_transaction_dict["signature"],
56+
'stepLimit': hex(self.setting["step_limit"]),
57+
'timestamp': hex(self.setting["timestamp"]),
58+
'to': self.setting["to"],
59+
'value': hex(self.setting["value"]),
60+
'version': '0x3'
61+
}
62+
}
63+
64+
response_json: dict = {
65+
"jsonrpc": "2.0",
66+
"result": tx_hash,
67+
"id": 1234
68+
}
69+
m.post(self.matcher, json=response_json)
70+
self.icon_service.send_transaction_and_wait(signed_transaction)
71+
actual_request = json.loads(m._adapter.last_request.text)
72+
self.assertEqual(expected_request, actual_request)

0 commit comments

Comments
 (0)