Skip to content

Commit

Permalink
feat: implement MaxSupply (#1319)
Browse files Browse the repository at this point in the history
## Description

This implements one of the token safety parameters on the leverage checklist.

closes: #1227

---

### Author Checklist

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] added appropriate labels to the PR
- [x] targeted the correct branch (see [PR Targeting](https://github.com/umee-network/umee/blob/main/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed
  • Loading branch information
toteki authored Sep 7, 2022
1 parent d3f634c commit 5cbf948
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- [1323](https://github.com/umee-network/umee/pull/1323) Oracle cli - Add validator address override option.
- [1329](https://github.com/umee-network/umee/pull/1329) Implement MaxCollateralShare.
- [1330](https://github.com/umee-network/umee/pull/1330) Implemented MaxSupplyUtilization.
- [1319](https://github.com/umee-network/umee/pull/1319) Implemented MaxSupply.
- [1331](https://github.com/umee-network/umee/pull/1331) Implemented MinCollateralLiquidity.

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion x/leverage/fixtures/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func Token(base, symbol string) types.Token {
MaxCollateralShare: sdk.MustNewDecFromStr("1"),
MaxSupplyUtilization: sdk.MustNewDecFromStr("0.9"),
MinCollateralLiquidity: sdk.MustNewDecFromStr("0"),
MaxSupply: sdk.NewInt(100000000000),
MaxSupply: sdk.NewInt(100_000_000000),
}
}
15 changes: 15 additions & 0 deletions x/leverage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ func (k Keeper) Supply(ctx sdk.Context, supplierAddr sdk.AccAddress, coin sdk.Co
return sdk.Coin{}, err
}

token, err := k.GetTokenSettings(ctx, coin.Denom)
if err != nil {
return sdk.Coin{}, err
}

total, err := k.GetTotalSupply(ctx, coin.Denom)
if err != nil {
return sdk.Coin{}, err
}

if token.MaxSupply.IsPositive() && total.Add(coin).Amount.GTE(token.MaxSupply) {
return sdk.Coin{}, types.ErrMaxSupply.Wrapf("attempted: %s, current supply: %s, max supply: %s",
coin, total.Amount, token.MaxSupply)
}

// determine uToken amount to mint
uToken, err := k.ExchangeToken(ctx, coin)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions x/leverage/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func (s *IntegrationTestSuite) TestSupply() {
s.borrow(borrower, coin(atomDenom, 10_000000))
s.tk.SetBorrow(ctx, borrower, coin(atomDenom, 60_000000))

// create a supplier that will exceed token's default MaxSupply
whale := s.newAccount(coin(umeeDenom, 1_000_000_000000))

tcs := []testCase{
{
"unregistered denom",
Expand Down Expand Up @@ -78,6 +81,13 @@ func (s *IntegrationTestSuite) TestSupply() {
coin("u/"+atomDenom, 40_000000),
nil,
},
{
"max supply",
whale,
coin(umeeDenom, 1_000_000_000000),
sdk.Coin{},
types.ErrMaxSupply,
},
}

for _, tc := range tcs {
Expand Down
1 change: 1 addition & 0 deletions x/leverage/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
ErrMaxSupplyUtilization = sdkerrors.Register(ModuleName, 501, "market would exceed MaxSupplyUtilization")
ErrMinCollateralLiquidity = sdkerrors.Register(ModuleName, 502, "market would fall below MinCollateralLiquidity")
ErrMaxCollateralShare = sdkerrors.Register(ModuleName, 503, "market would exceed MaxCollateralShare")
ErrMaxSupply = sdkerrors.Register(ModuleName, 504, "market would exceed MaxSupply")

// 6XX = Internal Failsafes
ErrInvalidUtilization = sdkerrors.Register(ModuleName, 600, "invalid token utilization")
Expand Down

0 comments on commit 5cbf948

Please sign in to comment.