Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add better support for txn flags * support Transaction.has_flag() * support Transaction.has_flag() * Add better support for txn flags. * bump version; fix import TypedDict * Update CHANGELOG.md * All flags to upper case; Update Changelog; Update pyproject * better fee calculation * Edit docstring; Add inline-comments * Fix typo * Add referral to original formula * Update settings.json * add helper function; add tests * fix linter; add option dynamic; change default back to open * fix exception message * Edit docstrings; Remove * Add comments for calculations * Update CHANGELOG.md * Update CHANGELOG.md * Update CHANGELOG.md
- Loading branch information
1 parent
811afa8
commit b82b004
Showing
7 changed files
with
176 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from unittest import TestCase | ||
|
||
from xrpl.asyncio.ledger.main import calculate_fee_dynamically | ||
|
||
|
||
class TestCalculateFeeDynamically(TestCase): | ||
def test_queue_empty(self): | ||
actual = { | ||
"current_ledger_size": "46", | ||
"current_queue_size": "0", | ||
"drops": { | ||
"base_fee": "10", | ||
"median_fee": "5000", | ||
"minimum_fee": "10", | ||
"open_ledger_fee": "10", | ||
}, | ||
"expected_ledger_size": "176", | ||
"ledger_current_index": 70813866, | ||
"levels": { | ||
"median_level": "128000", | ||
"minimum_level": "256", | ||
"open_ledger_level": "256", | ||
"reference_level": "256", | ||
}, | ||
"max_queue_size": "3520", | ||
} | ||
expected = "15" | ||
self.assertEqual(calculate_fee_dynamically(fee_data_set=actual), expected) | ||
|
||
def test_queue_partially_filled(self): | ||
actual = { | ||
"current_ledger_size": "46", | ||
"current_queue_size": "1760", | ||
"drops": { | ||
"base_fee": "10", | ||
"median_fee": "5000", | ||
"minimum_fee": "10", | ||
"open_ledger_fee": "10", | ||
}, | ||
"expected_ledger_size": "176", | ||
"ledger_current_index": 70813866, | ||
"levels": { | ||
"median_level": "128000", | ||
"minimum_level": "256", | ||
"open_ledger_level": "256", | ||
"reference_level": "256", | ||
}, | ||
"max_queue_size": "3520", | ||
} | ||
expected = "225" | ||
self.assertEqual(calculate_fee_dynamically(fee_data_set=actual), expected) | ||
|
||
def test_queue_full(self): | ||
actual = { | ||
"current_ledger_size": "46", | ||
"current_queue_size": "3520", | ||
"drops": { | ||
"base_fee": "10", | ||
"median_fee": "5000", | ||
"minimum_fee": "10", | ||
"open_ledger_fee": "10", | ||
}, | ||
"expected_ledger_size": "176", | ||
"ledger_current_index": 70813866, | ||
"levels": { | ||
"median_level": "128000", | ||
"minimum_level": "256", | ||
"open_ledger_level": "256", | ||
"reference_level": "256", | ||
}, | ||
"max_queue_size": "3520", | ||
} | ||
expected = "5500" | ||
self.assertEqual(calculate_fee_dynamically(fee_data_set=actual), expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Helper functions for the ledger module.""" | ||
|
||
from typing import Any, Dict | ||
|
||
|
||
def calculate_fee_dynamically(fee_data_set: Dict[str, Any]) -> str: | ||
"""Calculate the transaction fee dynamically | ||
based on the size of the queue of the node. | ||
Args: | ||
fee_data_set (Dict[str, Any]): The result of the `fee` method. | ||
Returns: | ||
str: The transaction fee, in drops. | ||
`Read more about drops <https://xrpl.org/currency-formats.html#xrp-amounts>`_ | ||
Based on fee-calculation code here: | ||
`<https://gist.github.com/WietseWind/3e9f9339f37a5881978a9661f49b0e52>`_ | ||
""" | ||
current_queue_size = int(fee_data_set["current_queue_size"]) | ||
max_queue_size = int(fee_data_set["max_queue_size"]) | ||
queue_pct = current_queue_size / max_queue_size | ||
drops = fee_data_set["drops"] | ||
minimum_fee = int(drops["minimum_fee"]) | ||
median_fee = int(drops["median_fee"]) | ||
open_ledger_fee = int(drops["open_ledger_fee"]) | ||
|
||
# calculate the lowest fee the user is able to pay if the queue is empty | ||
fee_low = round( | ||
min( | ||
max(minimum_fee * 1.5, round(max(median_fee, open_ledger_fee) / 500)), | ||
1000, | ||
), | ||
) | ||
if queue_pct > 0.1: # if 'current_queue_size' is >10 % of 'max_queue_size' | ||
possible_fee_medium = round( | ||
(minimum_fee + median_fee + open_ledger_fee) / 3, | ||
) | ||
elif queue_pct == 0: # if 'current_queue_size' is 0 | ||
possible_fee_medium = max( | ||
10 * minimum_fee, | ||
open_ledger_fee, | ||
) | ||
else: | ||
possible_fee_medium = max( | ||
10 * minimum_fee, | ||
round((minimum_fee + median_fee) / 2), | ||
) | ||
# calculate the lowest fee the user is able to pay if there are txns in the queue | ||
fee_medium = round( | ||
min( | ||
possible_fee_medium, | ||
fee_low * 15, | ||
10000, | ||
), | ||
) | ||
# calculate the lowest fee the user is able to pay if the txn queue is full | ||
fee_high = round( | ||
min( | ||
max(10 * minimum_fee, round(max(median_fee, open_ledger_fee) * 1.1)), | ||
100000, | ||
), | ||
) | ||
|
||
if queue_pct == 0: # if queue is empty | ||
fee = str(fee_low) | ||
elif 0 < queue_pct < 1: # queue has txns in it but is not full | ||
fee = str(fee_medium) | ||
else: # if queue is full | ||
fee = str(fee_high) | ||
|
||
return fee |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters