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

feat!: convert LiquidationIncentive from Dec to uint32 #1149

Closed
wants to merge 18 commits into from

Conversation

robert-zaremba
Copy link
Member

Description

Following discussion about using fixed point integers to represent small decimal values (percentage, basis points), this PR demonstrates how we can do it without adding more complexity.

See #1107 for discussion and motivation.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • added appropriate labels to the PR
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@robert-zaremba
Copy link
Member Author

NOTE: I didn't update all the tests and CLI. Let's have initial review before moving it further.

@robert-zaremba
Copy link
Member Author

Another thing to consider is the size of small decimal numbers. Is basis points too precise or per-mile will need good enough?

@toteki
Copy link
Member

toteki commented Jul 21, 2022

Percent should be okay in my opinion, but it might help to have the extra precision in case it's needed. Make sure we're capable of handling values like MaxBorrowRate: 120% for APY though if we're including those fields.

@robert-zaremba
Copy link
Member Author

I have added a new type for package: BP to represent operations beyond "1" (10'000bp)

@robert-zaremba
Copy link
Member Author

I will add MaxBorrowRate to this PR to cover two examples and have better taste of how this will work.

@robert-zaremba
Copy link
Member Author

I've added MaxBorrowRate to this PR. In the keeper code there is almost no change.

@RafilxTenfen
Copy link
Contributor

Changing everything from sdk.Dec to uint32 would be API breaking right?
Would also affect the structures on the rust side

@robert-zaremba
Copy link
Member Author

Changing everything from sdk.Dec to uint32 would be API breaking right?

These are not parameters to the messages. Except the general gov param update, for which we don't have any additional interface yet. Token is part of the QueryRegisteredTokensResponse - so that one will indeed be the only change seeing externally.

Copy link
Collaborator

@adamewozniak adamewozniak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utack 👍 just has to pass e2e

Copy link
Member

@toteki toteki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do these params all at once, and without the custom math types at first (so uint32 only in proto)

@robert-zaremba
Copy link
Member Author

Let's do these params all at once, and without the custom math types at first (so uint32 only in proto)

Why? I don't see the reason why not using the custom type. Custom type gives us more control in fact and provides the essence of the solution.
Otherwise it's like using string without using sdk.Dec. Doesn't make much sense.

)

const (
// UTokenPrefix defines the uToken denomination prefix for all uToken types.
UTokenPrefix = "u/"
UTokenPrefix = "u/"
maxBorrowRateLimit = bpmath.ONE * 10_000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This limit isn't necessary to hard code

@@ -432,7 +433,7 @@ func (k Keeper) LiquidateBorrow(
}

// apply liquidation incentive
reward.Amount = reward.Amount.ToDec().Mul(sdk.OneDec().Add(liquidationIncentive)).TruncateInt()
reward.Amount = bpmath.Mul(reward.Amount, liquidationIncentive).Add(reward.Amount)
Copy link
Member

@toteki toteki Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only use of Mul (or Quo) I could find in the current diff

Interestingly enough, the original line had to be removed in my liquidation PR #1118 , because even the small sdk.Dec early rounding was contributing to dust issues. Now liquidation incentive is multiplied to a Dec without rounding back to Int.

I think it is possible to avoid 4-precision Mul and Quo altogether

@toteki
Copy link
Member

toteki commented Jul 28, 2022

Preferences:

  • Convert FixedBP -> BP
  • See if we can avoid Mul and Quo on BP

I did the merge conflict cleanup on this, so it should be ready after that. We can merge it before #1118 if you're worried about merge conflicts.

edit: Oh, but we need to fix tests here too

@toteki
Copy link
Member

toteki commented Jul 28, 2022

I can also handle conflicts here if #1118 merges first - as far as I know it's being blocked not because it has problems but just to keep this one first.

@stale
Copy link

stale bot commented Sep 3, 2022

This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the S:Stale label Sep 3, 2022
@stale
Copy link

stale bot commented Oct 6, 2022

This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the S:Stale label Oct 6, 2022
@stale
Copy link

stale bot commented Nov 5, 2022

This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the S:Stale label Nov 5, 2022
@stale
Copy link

stale bot commented Dec 31, 2022

This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the S:Stale label Dec 31, 2022
@@ -115,9 +115,9 @@ func IntegrationTestNetworkConfig() network.Config {
LiquidationThreshold: sdk.MustNewDecFromStr("0.05"),
BaseBorrowRate: sdk.MustNewDecFromStr("0.02"),
KinkBorrowRate: sdk.MustNewDecFromStr("0.2"),
MaxBorrowRate: sdk.MustNewDecFromStr("1.5"),
MaxBorrowRate: 1_5000,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for bigger numbers we can use _ to separate "decimal". Note: we can't write 0_100 (for 0.1), because go would interpret it in octal notation.

@stale
Copy link

stale bot commented Feb 3, 2023

This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the S:Stale label Feb 3, 2023
@stale
Copy link

stale bot commented Mar 9, 2023

This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the S:Stale label Mar 9, 2023
@stale stale bot closed this Mar 17, 2023
@robert-zaremba robert-zaremba deleted the robert/basis-points branch July 19, 2023 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants