Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better fee calculation #370

Merged
merged 28 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2376d5d
Add better support for txn flags
LimpidCrypto Mar 15, 2022
973dfbf
support Transaction.has_flag()
LimpidCrypto Mar 15, 2022
8533884
support Transaction.has_flag()
LimpidCrypto Mar 15, 2022
0bfb37d
Add better support for txn flags.
LimpidCrypto Mar 17, 2022
3c1c1a2
bump version; fix import TypedDict
LimpidCrypto Mar 18, 2022
4dc7ed5
Update CHANGELOG.md
LimpidCrypto Mar 18, 2022
b148cf8
All flags to upper case; Update Changelog; Update pyproject
LimpidCrypto Mar 21, 2022
aa9bfa1
All flags to upper case; Update Changelog; Update pyproject
LimpidCrypto Mar 21, 2022
a958b0b
Merge branch 'master' into master
LimpidCrypto Mar 21, 2022
6053562
Merge branch 'XRPLF:master' into master
LimpidCrypto Mar 29, 2022
abc18d8
Merge branch 'XRPLF:master' into master
LimpidCrypto Apr 5, 2022
34cda4d
better fee calculation
LimpidCrypto Apr 5, 2022
3e5804a
Edit docstring; Add inline-comments
LimpidCrypto Apr 5, 2022
a125a75
Fix typo
LimpidCrypto Apr 5, 2022
d2bea47
Add referral to original formula
LimpidCrypto Apr 5, 2022
5cae934
Update settings.json
LimpidCrypto Apr 5, 2022
5a6b90f
add helper function; add tests
LimpidCrypto Apr 6, 2022
2479ac6
Merge branch 'master' into better_fee_calculation
LimpidCrypto Apr 6, 2022
dac32d3
fix linter; add option dynamic; change default back to open
LimpidCrypto Apr 6, 2022
5108588
fix exception message
LimpidCrypto Apr 7, 2022
76302a4
Edit docstrings; Remove
LimpidCrypto Apr 11, 2022
7934511
Add comments for calculations
LimpidCrypto Apr 11, 2022
fa0eb25
Merge branch 'master' into better_fee_calculation
LimpidCrypto Apr 15, 2022
6ccce1d
Update CHANGELOG.md
LimpidCrypto Apr 16, 2022
c07c4c3
Merge branch 'master' into better_fee_calculation
LimpidCrypto Apr 22, 2022
008869c
Merge branch 'master' into better_fee_calculation
LimpidCrypto Apr 25, 2022
72febec
Update CHANGELOG.md
LimpidCrypto Apr 25, 2022
151d06b
Update CHANGELOG.md
LimpidCrypto Apr 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 74 additions & 14 deletions xrpl/asyncio/ledger/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import Optional, cast

from typing_extensions import Literal

from xrpl.asyncio.clients import Client, XRPLRequestFailureException
from xrpl.constants import XRPLException
from xrpl.models.requests import Fee, Ledger
Expand Down Expand Up @@ -49,22 +51,32 @@ async def get_latest_open_ledger_sequence(client: Client) -> int:


async def get_fee(
client: Client, *, max_fee: Optional[float] = 2, fee_type: str = "open"
client: Client,
*,
max_fee: Optional[float] = 2,
fee_type: Optional[Literal["open", "minimum"]] = None,
LimpidCrypto marked this conversation as resolved.
Show resolved Hide resolved
) -> str:
"""
Query the ledger for the current transaction fee.
Query the ledger for the current transaction fee and adjust the fee based on
the queue size.

Args:
client: the network client used to make network calls.
max_fee: The maximum fee in XRP that the user wants to pay. If load gets too
high, then the fees will not scale past the maximum fee. If None, there is
no ceiling for the fee. The default is 2 XRP.
fee_type: The type of fee to return. The options are "open" (the load-scaled
fee_type: DEPRECATED.
The type of fee to return. The options are "open" (the load-scaled
fee to get into the open ledger) or "minimum" (the minimum transaction
fee). The default is "open".
fee). The default is `None`.

Recommended: Do not define any type of return so the
fee is calculated more dynamically based on the queue size of the
node. It increases the chances for the transaction to succeed.
LimpidCrypto marked this conversation as resolved.
Show resolved Hide resolved

Returns:
The transaction fee, in drops.
`Read more about drops <https://xrpl.org/currency-formats.html#xrp-amounts>`_
LimpidCrypto marked this conversation as resolved.
Show resolved Hide resolved

Raises:
XRPLException: if an incorrect option for `fee_type` is passed in.
Expand All @@ -74,18 +86,66 @@ async def get_fee(
if not response.is_successful():
raise XRPLRequestFailureException(response.result)

result = response.result["drops"]
if fee_type == "open":
fee = cast(str, result["open_ledger_fee"])
elif fee_type == "minimum":
fee = cast(str, result["minimum_fee"])
else:
raise XRPLException(
f'`fee_type` param must be "open" or "minimum". {fee_type} is not a '
"valid option."
result = response.result
drops = result["drops"]
if fee_type:
if fee_type == "open":
fee = cast(str, drops["open_ledger_fee"])
elif fee_type == "minimum":
fee = cast(str, drops["minimum_fee"])
else:
raise XRPLException(
f'`fee_type` param must be "open" or "minimum". {fee_type} is not a '
"valid option."
)
else: # use https://gist.github.com/WietseWind/3e9f9339f37a5881978a9661f49b0e52
LimpidCrypto marked this conversation as resolved.
Show resolved Hide resolved
LimpidCrypto marked this conversation as resolved.
Show resolved Hide resolved
current_queue_size = int(result["current_queue_size"])
max_queue_size = int(result["max_queue_size"])
queue_pct = current_queue_size / max_queue_size
minimum_fee = int(drops["minimum_fee"])
median_fee = int(drops["median_fee"])
open_ledger_fee = int(drops["open_ledger_fee"])
LimpidCrypto marked this conversation as resolved.
Show resolved Hide resolved

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, min(minimum_fee, open_ledger_fee)
)
LimpidCrypto marked this conversation as resolved.
Show resolved Hide resolved
else:
possible_fee_medium = max(
10 * minimum_fee, round((minimum_fee + median_fee) / 2)
)
fee_medium = round(
min(
possible_fee_medium,
fee_low * 15,
10000,
),
)
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 queue_pct == 1: # if queue is full
fee = str(fee_high)
else:
fee = str(fee_medium)
if max_fee is not None:
max_fee_drops = int(xrp_to_drops(max_fee))
if max_fee_drops < int(fee):
if max_fee_drops < int(fee): # if 'fee' exceeds the 'max_fee' use 'max_fee'
fee = str(max_fee_drops)
return fee
22 changes: 17 additions & 5 deletions xrpl/ledger/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import asyncio
from typing import Optional

from typing_extensions import Literal

from xrpl.asyncio.ledger import main
from xrpl.clients.sync_client import SyncClient

Expand Down Expand Up @@ -40,22 +42,32 @@ def get_latest_open_ledger_sequence(client: SyncClient) -> int:


def get_fee(
client: SyncClient, *, max_fee: Optional[float] = 2, fee_type: str = "open"
client: SyncClient,
*,
max_fee: Optional[float] = 2,
fee_type: Optional[Literal["open", "minimum"]] = None,
) -> str:
"""
Query the ledger for the current minimum transaction fee.
Query the ledger for the current transaction fee and adjust the fee based on
the queue size.

Args:
client: the network client used to make network calls.
max_fee: The maximum fee in XRP that the user wants to pay. If load gets too
high, then the fees will not scale past the maximum fee. If None, there is
no ceiling for the fee. The default is 2 XRP.
fee_type: The type of fee to return. The options are "open" (the load-scaled
fee_type: DEPRECATED.
The type of fee to return. The options are "open" (the load-scaled
fee to get into the open ledger) or "minimum" (the minimum transaction
cost). The default is "open".
fee). The default is `None`.

Recommended: Do not define any type of return so the
fee is calculated more dynamically based on the queue size of the
node. It increases the chances for the transaction to succeed.

Returns:
The minimum fee for transactions.
The transaction fee, in drops.
`Read more about drops <https://xrpl.org/currency-formats.html#xrp-amounts>`_

Raises:
XRPLRequestFailureException: if the rippled API call fails.
Expand Down