-
Notifications
You must be signed in to change notification settings - Fork 44
refactor(fees): simplify fee abstractions #1469
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,88 @@ | ||
| # Copyright 2025 Hathor Labs | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing_extensions import assert_never | ||
|
|
||
| from hathor.conf.settings import HathorSettings | ||
| from hathor.nanocontracts.exception import NCInvalidFeePaymentToken | ||
| from hathor.transaction.token_info import TokenDescription, TokenVersion | ||
| from hathor.transaction.util import get_deposit_token_deposit_amount, get_deposit_token_withdraw_amount | ||
|
|
||
|
|
||
| def calculate_mint_fee( | ||
| *, | ||
| settings: HathorSettings, | ||
| token_version: TokenVersion, | ||
| amount: int, | ||
| fee_payment_token: TokenDescription, | ||
| ) -> int: | ||
| """Calculate the fee for a mint operation.""" | ||
| match token_version: | ||
| case TokenVersion.NATIVE: | ||
| raise AssertionError | ||
| case TokenVersion.DEPOSIT: | ||
| _validate_deposit_based_payment_token(fee_payment_token) | ||
| return -get_deposit_token_deposit_amount(settings, amount) | ||
| case TokenVersion.FEE: | ||
| _validate_fee_based_payment_token(fee_payment_token) | ||
| return -_calculate_unit_fee_token_fee(settings, fee_payment_token) | ||
| case _: # pragma: no cover | ||
| assert_never(token_version) | ||
|
|
||
|
|
||
| def calculate_melt_fee( | ||
| *, | ||
| settings: HathorSettings, | ||
| token_version: TokenVersion, | ||
| amount: int, | ||
| fee_payment_token: TokenDescription, | ||
| ) -> int: | ||
| """Calculate the fee for a melt operation.""" | ||
| match token_version: | ||
| case TokenVersion.NATIVE: | ||
| raise AssertionError | ||
| case TokenVersion.DEPOSIT: | ||
| _validate_deposit_based_payment_token(fee_payment_token) | ||
| return +get_deposit_token_withdraw_amount(settings, amount) | ||
| case TokenVersion.FEE: | ||
| _validate_fee_based_payment_token(fee_payment_token) | ||
| return -_calculate_unit_fee_token_fee(settings, fee_payment_token) | ||
| case _: # pragma: no cover | ||
| assert_never(token_version) | ||
|
|
||
|
|
||
| def _validate_deposit_based_payment_token(fee_payment_token: TokenDescription) -> None: | ||
| """Validate the token used to pay the fee of a deposit-based token operation.""" | ||
| from hathor import HATHOR_TOKEN_UID | ||
| if fee_payment_token.token_id != HATHOR_TOKEN_UID: | ||
| raise NCInvalidFeePaymentToken('Only HTR is allowed to be used with deposit based token syscalls') | ||
|
|
||
|
|
||
| def _validate_fee_based_payment_token(fee_payment_token: TokenDescription) -> None: | ||
msbrogli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Validate the token used to pay the fee of a fee-based token operation.""" | ||
| match fee_payment_token.token_version: | ||
| case TokenVersion.FEE: | ||
| raise NCInvalidFeePaymentToken("fee-based tokens aren't allowed for paying fees") | ||
| case TokenVersion.DEPOSIT | TokenVersion.NATIVE: | ||
| pass | ||
| case _: # pragma: no cover | ||
| assert_never(fee_payment_token.token_version) | ||
|
|
||
|
|
||
| def _calculate_unit_fee_token_fee(settings: HathorSettings, fee_payment_token: TokenDescription) -> int: | ||
| """Calculate the fee for handling a fee-based token""" | ||
| from hathor import HATHOR_TOKEN_UID | ||
| if fee_payment_token.token_id == HATHOR_TOKEN_UID: | ||
| return settings.FEE_PER_OUTPUT | ||
| return int(settings.FEE_PER_OUTPUT / settings.TOKEN_DEPOSIT_PERCENTAGE) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.