Skip to content

Commit

Permalink
chore: move sdkutil coin functions to the coin package (#1780)
Browse files Browse the repository at this point in the history
## Description

skdutil should be as small as possible. 

---

### 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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](https://github.com/umee-network/umee/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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)
  • Loading branch information
robert-zaremba authored Feb 3, 2023
1 parent b7eb055 commit ab8e7e0
Show file tree
Hide file tree
Showing 15 changed files with 520 additions and 510 deletions.
23 changes: 23 additions & 0 deletions util/coin/coin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package coin

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Zero returns new coin with zero amount
func Zero(denom string) sdk.Coin {
return sdk.NewInt64Coin(denom, 0)
}

// Normalize transform nil coins to empty list
func Normalize(cs sdk.Coins) sdk.Coins {
if cs == nil {
return sdk.Coins{}
}
return cs
}

// New creates a mkCoin with a given base denom and amount
func New(denom string, amount int64) sdk.Coin {
return sdk.NewInt64Coin(denom, amount)
}
18 changes: 0 additions & 18 deletions util/sdkutil/coin.go

This file was deleted.

107 changes: 54 additions & 53 deletions x/leverage/keeper/borrows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/umee-network/umee/v4/util/coin"
"github.com/umee-network/umee/v4/x/leverage/types"
)

Expand All @@ -11,21 +12,21 @@ func (s *IntegrationTestSuite) TestGetBorrow() {

// get uumee borrow amount of empty account address (zero)
borrowed := s.tk.GetBorrow(ctx, sdk.AccAddress{}, umeeDenom)
require.Equal(coin(umeeDenom, 0), borrowed)
require.Equal(coin.New(umeeDenom, 0), borrowed)

// creates account which has supplied and collateralized 1000 uumee, and borrowed 123 uumee
addr := s.newAccount(coin(umeeDenom, 1000))
s.supply(addr, coin(umeeDenom, 1000))
s.collateralize(addr, coin("u/"+umeeDenom, 1000))
s.borrow(addr, coin(umeeDenom, 123))
addr := s.newAccount(coin.New(umeeDenom, 1000))
s.supply(addr, coin.New(umeeDenom, 1000))
s.collateralize(addr, coin.New("u/"+umeeDenom, 1000))
s.borrow(addr, coin.New(umeeDenom, 123))

// confirm borrowed amount is 123 uumee
borrowed = s.tk.GetBorrow(ctx, addr, umeeDenom)
require.Equal(coin(umeeDenom, 123), borrowed)
require.Equal(coin.New(umeeDenom, 123), borrowed)

// unregistered denom (zero)
borrowed = s.tk.GetBorrow(ctx, addr, "abcd")
require.Equal(coin("abcd", 0), borrowed)
require.Equal(coin.New("abcd", 0), borrowed)

// we do not test empty denom, as that will cause a panic
}
Expand All @@ -34,74 +35,74 @@ func (s *IntegrationTestSuite) TestSetBorrow() {
ctx, require := s.ctx, s.Require()

// empty account address
err := s.tk.SetBorrow(ctx, sdk.AccAddress{}, coin(umeeDenom, 123))
err := s.tk.SetBorrow(ctx, sdk.AccAddress{}, coin.New(umeeDenom, 123))
require.ErrorIs(err, types.ErrEmptyAddress)

addr := s.newAccount()

// set nonzero borrow, and confirm effect
err = s.tk.SetBorrow(ctx, addr, coin(umeeDenom, 123))
err = s.tk.SetBorrow(ctx, addr, coin.New(umeeDenom, 123))
require.NoError(err)
require.Equal(coin(umeeDenom, 123), s.tk.GetBorrow(ctx, addr, umeeDenom))
require.Equal(coin.New(umeeDenom, 123), s.tk.GetBorrow(ctx, addr, umeeDenom))

// set zero borrow, and confirm effect
err = s.tk.SetBorrow(ctx, addr, coin(umeeDenom, 0))
err = s.tk.SetBorrow(ctx, addr, coin.New(umeeDenom, 0))
require.NoError(err)
require.Equal(coin(umeeDenom, 0), s.tk.GetBorrow(ctx, addr, umeeDenom))
require.Equal(coin.New(umeeDenom, 0), s.tk.GetBorrow(ctx, addr, umeeDenom))

// unregistered (but valid) denom
err = s.tk.SetBorrow(ctx, addr, coin("abcd", 123))
err = s.tk.SetBorrow(ctx, addr, coin.New("abcd", 123))
require.NoError(err)

// interest scalar test - ensure borrowing smallest possible amount doesn't round to zero at scalar = 1.0001
require.NoError(s.tk.SetInterestScalar(ctx, umeeDenom, sdk.MustNewDecFromStr("1.0001")))
require.NoError(s.tk.SetBorrow(ctx, addr, coin(umeeDenom, 1)))
require.Equal(coin(umeeDenom, 1), s.tk.GetBorrow(ctx, addr, umeeDenom))
require.NoError(s.tk.SetBorrow(ctx, addr, coin.New(umeeDenom, 1)))
require.Equal(coin.New(umeeDenom, 1), s.tk.GetBorrow(ctx, addr, umeeDenom))

// interest scalar test - scalar changing after borrow (as it does when interest accrues)
require.NoError(s.tk.SetInterestScalar(ctx, umeeDenom, sdk.MustNewDecFromStr("1.0")))
require.NoError(s.tk.SetBorrow(ctx, addr, coin(umeeDenom, 200)))
require.NoError(s.tk.SetBorrow(ctx, addr, coin.New(umeeDenom, 200)))
require.NoError(s.tk.SetInterestScalar(ctx, umeeDenom, sdk.MustNewDecFromStr("2.33")))
require.Equal(coin(umeeDenom, 466), s.tk.GetBorrow(ctx, addr, umeeDenom))
require.Equal(coin.New(umeeDenom, 466), s.tk.GetBorrow(ctx, addr, umeeDenom))

// interest scalar extreme case - rounding up becomes apparent at high borrow amount
require.NoError(s.tk.SetInterestScalar(ctx, umeeDenom, sdk.MustNewDecFromStr("555444333222111")))
require.NoError(s.tk.SetBorrow(ctx, addr, coin(umeeDenom, 1)))
require.Equal(coin(umeeDenom, 1), s.tk.GetBorrow(ctx, addr, umeeDenom))
require.NoError(s.tk.SetBorrow(ctx, addr, coin(umeeDenom, 20000)))
require.Equal(coin(umeeDenom, 20001), s.tk.GetBorrow(ctx, addr, umeeDenom))
require.NoError(s.tk.SetBorrow(ctx, addr, coin.New(umeeDenom, 1)))
require.Equal(coin.New(umeeDenom, 1), s.tk.GetBorrow(ctx, addr, umeeDenom))
require.NoError(s.tk.SetBorrow(ctx, addr, coin.New(umeeDenom, 20000)))
require.Equal(coin.New(umeeDenom, 20001), s.tk.GetBorrow(ctx, addr, umeeDenom))
}

func (s *IntegrationTestSuite) TestGetTotalBorrowed() {
ctx, require := s.ctx, s.Require()

// unregistered denom (zero)
borrowed := s.tk.GetTotalBorrowed(ctx, "abcd")
require.Equal(coin("abcd", 0), borrowed)
require.Equal(coin.New("abcd", 0), borrowed)

// creates account which has supplied and collateralized 1000 uumee, and borrowed 123 uumee
borrower := s.newAccount(coin(umeeDenom, 1000))
s.supply(borrower, coin(umeeDenom, 1000))
s.collateralize(borrower, coin("u/"+umeeDenom, 1000))
s.borrow(borrower, coin(umeeDenom, 123))
borrower := s.newAccount(coin.New(umeeDenom, 1000))
s.supply(borrower, coin.New(umeeDenom, 1000))
s.collateralize(borrower, coin.New("u/"+umeeDenom, 1000))
s.borrow(borrower, coin.New(umeeDenom, 123))

// confirm total borrowed amount is 123 uumee
borrowed = s.tk.GetTotalBorrowed(ctx, umeeDenom)
require.Equal(coin(umeeDenom, 123), borrowed)
require.Equal(coin.New(umeeDenom, 123), borrowed)

// creates account which has supplied and collateralized 1000 uumee, and borrowed 234 uumee
borrower2 := s.newAccount(coin(umeeDenom, 1000))
s.supply(borrower2, coin(umeeDenom, 1000))
s.collateralize(borrower2, coin("u/"+umeeDenom, 1000))
s.borrow(borrower2, coin(umeeDenom, 234))
borrower2 := s.newAccount(coin.New(umeeDenom, 1000))
s.supply(borrower2, coin.New(umeeDenom, 1000))
s.collateralize(borrower2, coin.New("u/"+umeeDenom, 1000))
s.borrow(borrower2, coin.New(umeeDenom, 234))

// confirm total borrowed amount is 357 uumee
borrowed = s.tk.GetTotalBorrowed(ctx, umeeDenom)
require.Equal(coin(umeeDenom, 357), borrowed)
require.Equal(coin.New(umeeDenom, 357), borrowed)

// interest scalar test - scalar changing after borrow (as it does when interest accrues)
require.NoError(s.tk.SetInterestScalar(ctx, umeeDenom, sdk.MustNewDecFromStr("2.00")))
require.Equal(coin(umeeDenom, 714), s.tk.GetTotalBorrowed(ctx, umeeDenom))
require.Equal(coin.New(umeeDenom, 714), s.tk.GetTotalBorrowed(ctx, umeeDenom))
}

func (s *IntegrationTestSuite) TestLiquidity() {
Expand All @@ -112,26 +113,26 @@ func (s *IntegrationTestSuite) TestLiquidity() {
require.Equal(sdk.ZeroInt(), available)

// creates account which has supplied and collateralized 1000 uumee
supplier := s.newAccount(coin(umeeDenom, 1000))
s.supply(supplier, coin(umeeDenom, 1000))
s.collateralize(supplier, coin("u/"+umeeDenom, 1000))
supplier := s.newAccount(coin.New(umeeDenom, 1000))
s.supply(supplier, coin.New(umeeDenom, 1000))
s.collateralize(supplier, coin.New("u/"+umeeDenom, 1000))

// confirm lending pool is 1000 uumee
available = s.tk.AvailableLiquidity(ctx, umeeDenom)
require.Equal(sdk.NewInt(1000), available)

// creates account which has supplied and collateralized 1000 uumee, and borrowed 123 uumee
borrower := s.newAccount(coin(umeeDenom, 1000))
s.supply(borrower, coin(umeeDenom, 1000))
s.collateralize(borrower, coin("u/"+umeeDenom, 1000))
s.borrow(borrower, coin(umeeDenom, 123))
borrower := s.newAccount(coin.New(umeeDenom, 1000))
s.supply(borrower, coin.New(umeeDenom, 1000))
s.collateralize(borrower, coin.New("u/"+umeeDenom, 1000))
s.borrow(borrower, coin.New(umeeDenom, 123))

// confirm lending pool is 1877 uumee
available = s.tk.AvailableLiquidity(ctx, umeeDenom)
require.Equal(sdk.NewInt(1877), available)

// artificially reserve 200 uumee, reducing available amount to 1677
s.setReserves(coin(umeeDenom, 200))
s.setReserves(coin.New(umeeDenom, 200))
available = s.tk.AvailableLiquidity(ctx, umeeDenom)
require.Equal(sdk.NewInt(1677), available)
}
Expand All @@ -144,9 +145,9 @@ func (s *IntegrationTestSuite) TestDeriveBorrowUtilization() {
require.Equal(sdk.OneDec(), utilization)

// creates account which has supplied and collateralized 1000 uumee
addr := s.newAccount(coin(umeeDenom, 1000))
s.supply(addr, coin(umeeDenom, 1000))
s.collateralize(addr, coin("u/"+umeeDenom, 1000))
addr := s.newAccount(coin.New(umeeDenom, 1000))
s.supply(addr, coin.New(umeeDenom, 1000))
s.collateralize(addr, coin.New("u/"+umeeDenom, 1000))

// All tests below are commented with the following equation in mind:
// utilization = (Total Borrowed / (Total Borrowed + Module Balance - Reserved Amount))
Expand All @@ -156,42 +157,42 @@ func (s *IntegrationTestSuite) TestDeriveBorrowUtilization() {
require.Equal(sdk.ZeroDec(), utilization)

// user borrows 200 uumee, reducing module account to 800 uumee
s.borrow(addr, coin(umeeDenom, 200))
s.borrow(addr, coin.New(umeeDenom, 200))

// 20% utilization (200 / 200+800-0)
utilization = s.tk.SupplyUtilization(ctx, umeeDenom)
require.Equal(sdk.MustNewDecFromStr("0.2"), utilization)

// artificially reserve 200 uumee
s.setReserves(coin(umeeDenom, 200))
s.setReserves(coin.New(umeeDenom, 200))

// 25% utilization (200 / 200+800-200)
utilization = s.tk.SupplyUtilization(ctx, umeeDenom)
require.Equal(sdk.MustNewDecFromStr("0.25"), utilization)

// user borrows 600 uumee (disregard borrow limit), reducing module account to 0 uumee
s.forceBorrow(addr, coin(umeeDenom, 600))
s.forceBorrow(addr, coin.New(umeeDenom, 600))

// 100% utilization (800 / 800+200-200))
utilization = s.tk.SupplyUtilization(ctx, umeeDenom)
require.Equal(sdk.MustNewDecFromStr("1.0"), utilization)

// artificially set user borrow to 1200 umee
require.NoError(s.tk.SetBorrow(ctx, addr, coin(umeeDenom, 1200)))
require.NoError(s.tk.SetBorrow(ctx, addr, coin.New(umeeDenom, 1200)))

// still 100% utilization (1200 / 1200+200-200)
utilization = s.tk.SupplyUtilization(ctx, umeeDenom)
require.Equal(sdk.MustNewDecFromStr("1.0"), utilization)

// artificially set reserves to 800 uumee
s.setReserves(coin(umeeDenom, 800))
s.setReserves(coin.New(umeeDenom, 800))

// edge case interpreted as 100% utilization (1200 / 1200+200-800)
utilization = s.tk.SupplyUtilization(ctx, umeeDenom)
require.Equal(sdk.MustNewDecFromStr("1.0"), utilization)

// artificially set reserves to 4000 uumee
s.setReserves(coin(umeeDenom, 4000))
s.setReserves(coin.New(umeeDenom, 4000))

// impossible case interpreted as 100% utilization (1200 / 1200+200-4000)
utilization = s.tk.SupplyUtilization(ctx, umeeDenom)
Expand All @@ -207,13 +208,13 @@ func (s *IntegrationTestSuite) TestCalculateBorrowLimit() {
require.Equal(sdk.ZeroDec(), borrowLimit)

// Unregistered asset
invalidCoins := sdk.NewCoins(coin("abcd", 1000))
invalidCoins := sdk.NewCoins(coin.New("abcd", 1000))
_, err = app.LeverageKeeper.CalculateBorrowLimit(ctx, invalidCoins)
require.ErrorIs(err, types.ErrNotUToken)

// Create collateral uTokens (1k u/umee)
umeeCollatDenom := types.ToUTokenDenom(umeeDenom)
umeeCollateral := sdk.NewCoins(coin(umeeCollatDenom, 1000_000000))
umeeCollateral := sdk.NewCoins(coin.New(umeeCollatDenom, 1000_000000))

// Manually compute borrow limit using collateral weight of 0.25
// and placeholder of 1 umee = $4.21.
Expand All @@ -228,7 +229,7 @@ func (s *IntegrationTestSuite) TestCalculateBorrowLimit() {

// Create collateral atom uTokens (1k u/uatom)
atomCollatDenom := types.ToUTokenDenom(atomDenom)
atomCollateral := sdk.NewCoins(coin(atomCollatDenom, 1000_000000))
atomCollateral := sdk.NewCoins(coin.New(atomCollatDenom, 1000_000000))

// Manually compute borrow limit using collateral weight of 0.25
// and placeholder of 1 atom = $39.38
Expand Down
Loading

0 comments on commit ab8e7e0

Please sign in to comment.