From 3f259d8c4902654cb7d1703561ea1b579e13223c Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:37:49 +0100 Subject: [PATCH 01/14] test class for async --- tests/test_async_tron_energy.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_async_tron_energy.py diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py new file mode 100644 index 0000000..125b569 --- /dev/null +++ b/tests/test_async_tron_energy.py @@ -0,0 +1,28 @@ +import unittest +from unittest.mock import AsyncMock, patch +from tron_energy import AsyncTronEnergy + +class TestTronEnergyMethods(unittest.IsolatedAsyncioTestCase): + + async def asyncSetUp(self): + self.tron_energy = AsyncTronEnergy(api_key='your_api_key', api_secret='your_api_secret') + + async def asyncTearDown(self): + await self.tron_energy.close() + + def test_verify_signature(self): + # Arrange + timestamp = self.tron_energy._get_timestamp() + data = { + 'energy_amount': 100, + 'period': '1H', + } + signature = self.tron_energy._sign(f"{timestamp}&{self.tron_energy._jsonify(data)}") + + # Act + result = self.tron_energy.verify_signature(signature, timestamp, data) + + # Assert + self.assertTrue(result) + + \ No newline at end of file From aefd61af5947df0bef16564e6a7ba752553f2deb Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:38:32 +0100 Subject: [PATCH 02/14] test get public data --- tests/test_async_tron_energy.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 125b569..2ad332e 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -25,4 +25,35 @@ def test_verify_signature(self): # Assert self.assertTrue(result) + @patch('tron_energy.async_tron_energy.ClientSession.get') + async def test_get_public_data(self, mock_get): + # Arrange + expected_response = { + "platform_avail_energy": 603249, + "platform_max_energy": 329009, + "minimum_order_energy": 32000, + "maximum_order_energy": 100000000, + "small_amount": 50000, + "small_addition": 0.6, + "usdt_energy_need_old": 32000, + "usdt_energy_need_new": 65000, + "tiered_pricing": [{"period": 0, "price": 100}, {"period": 1, "price": 200}, {"period": 3, "price": 152}, {"period ": 30, "price": 124}], + "balance": 813892429257 + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + # Act + response = await self.tron_energy.get_public_data() + + # Assert + self.assertEqual(response, expected_response) + \ No newline at end of file From 38b973bda83c645c631158a9e77fb99362e7d042 Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:39:05 +0100 Subject: [PATCH 03/14] test place order --- tests/test_async_tron_energy.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 2ad332e..858deaa 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -56,4 +56,34 @@ async def test_get_public_data(self, mock_get): # Assert self.assertEqual(response, expected_response) + @patch('tron_energy.async_tron_energy.ClientSession.post') + async def test_place_order(self, mock_get): + # Arrange + expected_response = { + "errno": 0, + "serial": "7297a8a2a9e39b86fc5bad0d2e9edda2", + "amount": 3120000, + "balance": 813900029257 + } + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "receive_address": "TR7NHnXw5423f8j766h899234567890", + "energy_amount": 3120000, + "period": "1H" + } + + # Act + response = await self.tron_energy.place_order(**data) + + # Assert + self.assertEqual(response, expected_response) + \ No newline at end of file From b355cdd94731e111b85e51fbf4a0c157453345fd Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:39:32 +0100 Subject: [PATCH 04/14] test transfer small trx --- tests/test_async_tron_energy.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 858deaa..6f517ec 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -86,4 +86,33 @@ async def test_place_order(self, mock_get): # Assert self.assertEqual(response, expected_response) + @patch('tron_energy.async_tron_energy.ClientSession.post') + async def test_transfer_small_trx_amount(self, mock_get): + # Arrange + expected_response = { + "errno": 0, + "txid": "9df44479551ef93c9bbfeca3cb82ef1564199d2d492ad38f7f1d2e454f5efb0f", + "balance": 813900029257 + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "receive_address": "TR7NHnXw5423f8j766h899234567890", + "amount": 500_000 # 0.5 TRX + } + + # Act + response = await self.tron_energy.transfer_small_trx_amount(**data) + + # Assert + self.assertEqual(response, expected_response) + \ No newline at end of file From 8fe42454595d46061f0ad6d8d70ac5cba73b92a1 Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:40:03 +0100 Subject: [PATCH 05/14] test purchase by number of transfers --- tests/test_async_tron_energy.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 6f517ec..1c72a07 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -115,4 +115,32 @@ async def test_transfer_small_trx_amount(self, mock_get): # Assert self.assertEqual(response, expected_response) - \ No newline at end of file + @patch('tron_energy.async_tron_energy.ClientSession.post') + async def test_purchase_by_number_of_transfers(self, mock_get): + # Arrange + expected_response = { + "errno": 0, + "balance": 813900029257 + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "receive_address": "TR7NHnXw5423f8j766h899234567890", + "times": 5 + } + + # Act + response = await self.tron_energy.purchase_by_number_of_transfers(**data) + + # Assert + self.assertEqual(response, expected_response) + + \ No newline at end of file From 490bf51b18cd260422536507e30b1151bdb5e8f0 Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:40:41 +0100 Subject: [PATCH 06/14] test list by number of transfers --- tests/test_async_tron_energy.py | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 1c72a07..a4fd11e 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -143,4 +143,53 @@ async def test_purchase_by_number_of_transfers(self, mock_get): # Assert self.assertEqual(response, expected_response) + @patch('tron_energy.async_tron_energy.ClientSession.get') + async def test_list_purchases_by_number_of_transfers(self, mock_get): + # Arrange + expected_response = { + "count": 2, + "code": 0, + "page": 1, + "next": None, + "previous": None, + "results": [ + { + "id": 2, + "receive_address": "TEX5nLeFJ1dyazhJC3P9eYJs7hxgk7knJF", + "status": 1, + "last_step": 0, + "main_delegated": False, + "expired_time": None, + "create_time": "2023-08-23T11:09:27.986610+08:00", + "update_time": "2023-08-23T11:09:27.986660+08:00", + "last_step_display": "deledate", + "status_display": "enable", + "auto_type": 1, # 1-only energy,2-energy+bandwidth + "auto_type_display": "only energy", + "max_energy": 65000, + "period": 7, + "count_limit": 8 # times + }, + ] + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "receive_address": "TR7NHnXw5423f8j766h899234567890", + } + + # Act + response = await self.tron_energy.list_purchases_by_number_of_transfers(**data) + + # Assert + self.assertEqual(response, expected_response) + \ No newline at end of file From f73dd89b304d213a077060c76d65d55f810fde94 Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:41:08 +0100 Subject: [PATCH 07/14] test create delegate --- tests/test_async_tron_energy.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index a4fd11e..2a42ec1 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -192,4 +192,34 @@ async def test_list_purchases_by_number_of_transfers(self, mock_get): # Assert self.assertEqual(response, expected_response) - \ No newline at end of file + @patch('tron_energy.async_tron_energy.ClientSession.post') + async def test_create_smart_delegate(self, mock_get): + # Arrange + expected_response = { + "errno": 0, + "message": "1 smart delegate has been added", + "balance": 813900029257 + + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "receive_address": "TR7NHnXw5423f8j766h899234567890", + "period": 1 + } + + # Act + response = await self.tron_energy.create_smart_delegate(**data) + + # Assert + self.assertEqual(response, expected_response) + + \ No newline at end of file From 6089a8c5f67b34479a10b0820e9bdc62b21eac20 Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:41:33 +0100 Subject: [PATCH 08/14] test list smart delegate --- tests/test_async_tron_energy.py | 52 ++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 2a42ec1..0562f6e 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -222,4 +222,54 @@ async def test_create_smart_delegate(self, mock_get): # Assert self.assertEqual(response, expected_response) - \ No newline at end of file + @patch('tron_energy.async_tron_energy.ClientSession.get') + async def test_list_smart_delegate(self, mock_get): + # Arrange + expected_response = { + "count": 1, + "code": 0, + "page": 1, + "next": None, + "previous": None, + "results": [ + { + "id": 21, + "receive_address": "TNfdtE7p8pEfTTbfRb88gikf2tt5ENc86b", + "status": 1, # Is it valid? + "last_step": 1, + "main_delegated": False, # Delegated status, valid during automatic renewal + "expired_time": None, + "create_time": "2023-10-09T11:13:12.288373+08:00", + "update_time": "2023-10-09T16:35:36.559760+08:00", + "last_step_display": "delegated", + "status_display": "on", + "auto_type": 1, # 1 is smart hosting, 2 is automatic renewal only + "auto_type_display": "smart hosting", + "next_delegate_time": None, # Valid during automatic renewal + "max_energy": 50000, # maintain energy + "period": 3, # Commission period + }, + ] + } + + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "receive_address": "TR7NHnXw5423f8j766h899234567890", + } + + # Act + response = await self.tron_energy.list_smart_delegate(**data) + + # Assert + self.assertEqual(response, expected_response) + + \ No newline at end of file From b66a551c663b46b6c0561e90c6978ebd10d25c91 Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:42:01 +0100 Subject: [PATCH 09/14] test modify smart delegate --- tests/test_async_tron_energy.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 0562f6e..b062478 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -272,4 +272,31 @@ async def test_list_smart_delegate(self, mock_get): # Assert self.assertEqual(response, expected_response) - \ No newline at end of file + @patch('tron_energy.async_tron_energy.ClientSession.post') + async def test_modify_smart_delegate(self, mock_get): + # Arrange + expected_response = { + "errno": 0 + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "id": 21, + "status": False, + } + + # Act + response = await self.tron_energy.modify_smart_delegate(**data) + + # Assert + self.assertEqual(response, expected_response) + + \ No newline at end of file From 9d74927d91c4b8f9b9256322531aab573a27f7ba Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:42:30 +0100 Subject: [PATCH 10/14] test get order --- tests/test_async_tron_energy.py | 48 ++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index b062478..5b89cb9 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -299,4 +299,50 @@ async def test_modify_smart_delegate(self, mock_get): # Assert self.assertEqual(response, expected_response) - \ No newline at end of file + @patch('tron_energy.async_tron_energy.ClientSession.get') + async def test_get_order(self, mock_get): + # Arrange + expected_response = { + "errno": 0, + "receive_address": "TExWKszFWYTKZH8LYiovAPKzS3L9MLZ4kw", + "order_no": "58b451473d290f92443eabf0322b9907", + "energy_amount": 32000, + "pay_amount": 0.0, + "amount": 3800000, + "details": [ + { + "delegate_hash": "e2e71df638a9e01492a50bebba072a39eb75f673e91d5374ccf517f44e113f3", + "delegate_time": "2023-10-09T10:21:07.840478Z", + "reclaim_hash": "f4672a9563947cf78e5534b4025451dfd75efa5481a67b20ee55b9be368c900", + "reclaim_time": "2023-10-12T10:21:07.840478Z", + "reclaim_time_real": "2023-10-09T10:26:12.617456Z", + "status": 30 # 20-in commission, 30-recycled + } + ], + "create_time": "2023-06-15T21:42:13.200565+08:00", + "api_name": "MY API", + "period": 0, + "status": 30, # 30 indicates that the commission was completely successful + "refund_amount": 0 + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "order_no": "58b451473d290f92443eabf0322b9907" + } + + # Act + response = await self.tron_energy.get_order(**data) + + # Assert + self.assertEqual(response, expected_response) + + \ No newline at end of file From badc9808a9b5a8d2d0c1df733ec07dd38b3680e7 Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:42:57 +0100 Subject: [PATCH 11/14] test recycle order --- tests/test_async_tron_energy.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 5b89cb9..62cc521 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -345,4 +345,31 @@ async def test_get_order(self, mock_get): # Assert self.assertEqual(response, expected_response) + @patch('tron_energy.async_tron_energy.ClientSession.post') + async def test_recycle_order(self, mock_get): + # Arrange + expected_response = { + "errno": 0, + "message": "request accept" + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "order_no": "58b451473d290f92443eabf0322b9907" + } + + # Act + response = await self.tron_energy.recycle_order(**data) + + # Assert + self.assertEqual(response, expected_response) + \ No newline at end of file From 40aeb6ac2bd8e063f23a0120ce69845b732e6018 Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:43:23 +0100 Subject: [PATCH 12/14] test estimate order --- tests/test_async_tron_energy.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 62cc521..601a050 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -372,4 +372,35 @@ async def test_recycle_order(self, mock_get): # Assert self.assertEqual(response, expected_response) - \ No newline at end of file + @patch('tron_energy.async_tron_energy.ClientSession.get') + async def test_estimate_order(self, mock_get): + # Arrange + expected_response = { + "period": "1H", + "energy_amount": 32000, + "price": 100, + "total_price": 10192000, + "addition": 600000 + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + data = { + "energy_amount": 32000, + "period": '1H', + } + + # Act + response = await self.tron_energy.estimate_order(**data) + + # Assert + self.assertEqual(response, expected_response) + + \ No newline at end of file From 61a263848c5f01371d34a2094b4652a31aa58a2e Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:43:50 +0100 Subject: [PATCH 13/14] test api usage summary --- tests/test_async_tron_energy.py | 36 ++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/test_async_tron_energy.py b/tests/test_async_tron_energy.py index 601a050..4580fec 100644 --- a/tests/test_async_tron_energy.py +++ b/tests/test_async_tron_energy.py @@ -403,4 +403,38 @@ async def test_estimate_order(self, mock_get): # Assert self.assertEqual(response, expected_response) - \ No newline at end of file + @patch('tron_energy.async_tron_energy.ClientSession.get') + async def test_get_api_usage_summary(self, mock_get): + # Arrange + expected_response = { + "name": "MY API", + "create_time": "2023-04-28 12:31:04", + "total_count": 46, + "total_sum_energy": 3696000, + "total_sum_trx": 598165000, + "today_count": 0, + "today_sum_energy": 0, + "today_sum_trx": 0, + "yesterday_count": 1, + "yesterday_sum_energy": 1300000, + "yesterday_sum_trx": 197600000 + } + + # Mock the response object + mock_response = AsyncMock() + mock_response.status = 200 + mock_response.json = AsyncMock(return_value=expected_response) + + # Mock the context manager + mock_get.return_value.__aenter__.return_value = mock_response + mock_get.return_value.__aexit__.return_value = AsyncMock() + + # Act + response = await self.tron_energy.get_api_usage_summary() + + # Assert + self.assertEqual(response, expected_response) + + +if __name__ == '__main__': + unittest.main() From ae4537c4288cefd04bc6adad1fcfc143cc48e9bf Mon Sep 17 00:00:00 2001 From: Ephraim Date: Sat, 7 Sep 2024 00:53:02 +0100 Subject: [PATCH 14/14] upate async tron --- tron_energy/async_tron_energy.py | 176 ++++++++++++++++++++++++++++++- 1 file changed, 175 insertions(+), 1 deletion(-) diff --git a/tron_energy/async_tron_energy.py b/tron_energy/async_tron_energy.py index cc179ee..e4b4ab4 100644 --- a/tron_energy/async_tron_energy.py +++ b/tron_energy/async_tron_energy.py @@ -88,4 +88,178 @@ async def get_public_data(self): url = "/api/v1/frontend/index-data" return await self.make_request("GET", url) - \ No newline at end of file + async def place_order(self, receive_address:TronAddress, energy_amount:int, period:str='1H', out_trade_no:str=None, callback_url:str=None): + """ + Places an energy order on the TronEnergy API. + + Parameters: + receive_address (TronAddress): The Tron address where the energy will be received. The address needs to be activated, otherwise the order will fail. + energy_amount (int): The amount of energy to be ordered. + period (str, optional): The period for which the energy is to be ordered. Defaults to '1H'. + out_trade_no (str, optional): The unique identifier for the order. + callback_url (str, optional): The URL to which the API will send a callback when the order is fulfilled. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = "/api/v1/frontend/order" + data = { + "receive_address": receive_address, + "energy_amount": energy_amount, + "period": period, + } + if out_trade_no: + data["out_trade_no"] = out_trade_no + if callback_url: + data["callback_url"] = callback_url + return await self.make_request("POST", url, data) + + async def transfer_small_trx_amount(self, amount:int, receive_address:TronAddress): + """ + Transfers small Tron amount to a specified address. + + Parameters: + amount (int): The amount of Tron in Sun to be transferred between 100000 - 10000000. That is, between 0.1-10 TRX + receive_address (TronAddress): The Tron address to which the Tron will be transferred. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = "/api/v1/frontend/order/transfer" + data = { + "amount": amount, + "receive_address": receive_address, + } + return await self.make_request("POST", url, data) + + async def purchase_by_number_of_transfers(self, times:int, receive_address:TronAddress): + """ + Parameters: + times (int): Number of purchases, 5-1000 times + receive_address (TronAddress): The Tron address to which the resources will be transferred. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = "/api/v1/frontend/count-delegate-policy" + data = { + "times": times, + "receive_address": receive_address, + } + return await self.make_request("POST", url, data) + + async def list_purchases_by_number_of_transfers(self, receive_address:TronAddress=None): # Note: We will have to do something about pagination here. + """ + Parameters: + receive_address (TronAddress): Query a specific address, if not filled in, return all. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = f"/api/v1/frontend/count-delegate-policy" + data = {"receive_address": receive_address} if receive_address else None + return await self.make_request("GET", url, data) + + async def create_smart_delegate(self, period:int, receive_address:TronAddress, max_energy:int=None): + """ + Parameters: + period (int): The commission period, 1-30. + receive_address (TronAddress): The Tron address to which the energy will be received.The address needs to be activated, otherwise the order will fail. + max_energy (int, optional): The Amount to keep available. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = "/api/v1/frontend/auto-delegate-policy" + data = { + "period": period, + "receive_address": receive_address, + } + if max_energy: + data["max_energy"] = max_energy + return await self.make_request("POST", url, data) + + async def list_smart_delegate(self, receive_address:TronAddress=None): # Note: We will have to do something about pagination here. + """ + Parameters: + receive_address (TronAddress): Query a specific address, if not filled in, return all. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = "/api/v1/frontend/auto-delegate-policy" + data = {"receive_address": receive_address} if receive_address else None + return await self.make_request("GET", url, data) + + async def modify_smart_delegate(self, id:int, status:bool): + """ + Parameters: + id (int): The ID of the smart delegate policy. + status (bool): The new status of the smart delegate policy. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = f"/api/v1/frontend/auto-delegate-policy/{id}/change-status" + data = {"status": int(status)} + return await self.make_request("POST", url, data) + + async def get_order(self, order_no:str): + """ + Parameters: + order_no (str): The order number returned when placing an order. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = "/api/v1/frontend/order/query" + data = {"serial": order_no} + return await self.make_request("GET", url, data) + + async def recycle_order(self, order_no:str): + """ + Parameters: + order_no (str): The order number returned when placing an order. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = "/api/v1/frontend/order/reclaim" + data = {"serial": order_no} + return await self.make_request("POST", url, data) + + async def estimate_order(self, energy_amount:int, period:str="1H"): + """ + Estimates the price of an energy order. + + Parameters: + energy_amount (int): The amount of energy to be ordered. + period (str, optional): The period for which the energy is to be ordered. Defaults to '1H'. + + Returns: + dict: A dictionary containing the response from the API. + """ + url = "/api/v1/frontend/order/price" + data = { + "energy_amount": energy_amount, + "period": period, + } + return await self.make_request("GET", url, data) + + async def get_api_usage_summary(self): + """ + Retrieves API usage summary. + + Returns: + dict: A dictionary containing the API usage summary. + """ + url = "/api/v1/frontend/userapi/summary" + return await self.make_request("GET", url) + + async def __aenter__(self): + return self + + async def __aexit__(self, *args, **kwargs): + await self.close() + +