Skip to content

Commit

Permalink
Merge branch 'main' into robert/go-workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki authored Jun 28, 2022
2 parents 0a89f65 + 6c32f68 commit 40a6890
Show file tree
Hide file tree
Showing 27 changed files with 238 additions and 325 deletions.
2 changes: 1 addition & 1 deletion app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func IntegrationTestNetworkConfig() network.Config {
BaseBorrowRate: sdk.MustNewDecFromStr("0.020000000000000000"),
KinkBorrowRate: sdk.MustNewDecFromStr("0.200000000000000000"),
MaxBorrowRate: sdk.MustNewDecFromStr("1.50000000000000000"),
KinkUtilizationRate: sdk.MustNewDecFromStr("0.200000000000000000"),
KinkUtilization: sdk.MustNewDecFromStr("0.200000000000000000"),
LiquidationIncentive: sdk.MustNewDecFromStr("0.180000000000000000"),
EnableMsgLend: true,
EnableMsgBorrow: true,
Expand Down
11 changes: 5 additions & 6 deletions proto/umee/leverage/v1/leverage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ message Token {
];

// The kink_borrow_rate defines the interest rate for borrowing this
// asset when utilization is at the 'kink' utilization value as defined
// on the utilization:interest graph.
// asset when utilization equals to 'kink_utilization'.
string kink_borrow_rate = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
Expand All @@ -100,12 +99,12 @@ message Token {
(gogoproto.moretags) = "yaml:\"max_borrow_rate\""
];

// The kink_utilization_rate defines the borrow utilization rate for this
// asset where the 'kink' on the utilization:interest graph occurs.
string kink_utilization_rate = 8 [
// The kink_utilization defines the value where the kink rate kicks off for
// borrow rates.
string kink_utilization = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"kink_utilization_rate\""
(gogoproto.moretags) = "yaml:\"kink_utilization\""
];

// The liquidation_incentive determines the portion of bonus collateral of
Expand Down
4 changes: 2 additions & 2 deletions starport.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ genesis:
base_borrow_rate: "0.020000000000000000"
kink_borrow_rate: "0.200000000000000000"
max_borrow_rate: "1.50000000000000000"
kink_utilization_rate: "0.200000000000000000"
kink_utilization: "0.200000000000000000"
liquidation_incentive: "0.180000000000000000"
- base_denom: "ibc/FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
symbol_denom: "ATOM"
Expand All @@ -29,7 +29,7 @@ genesis:
base_borrow_rate: "0.020000000000000000"
kink_borrow_rate: "0.200000000000000000"
max_borrow_rate: "1.50000000000000000"
kink_utilization_rate: "0.200000000000000000"
kink_utilization: "0.200000000000000000"
liquidation_incentive: "0.180000000000000000"
gravity:
delegate_keys:
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/e2e_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (s *IntegrationTestSuite) initGenesis() {
BaseBorrowRate: sdk.MustNewDecFromStr("0.020000000000000000"),
KinkBorrowRate: sdk.MustNewDecFromStr("0.200000000000000000"),
MaxBorrowRate: sdk.MustNewDecFromStr("1.50000000000000000"),
KinkUtilizationRate: sdk.MustNewDecFromStr("0.200000000000000000"),
KinkUtilization: sdk.MustNewDecFromStr("0.200000000000000000"),
LiquidationIncentive: sdk.MustNewDecFromStr("0.180000000000000000"),
EnableMsgLend: true,
EnableMsgBorrow: true,
Expand Down
2 changes: 1 addition & 1 deletion x/leverage/client/cli/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestParseUpdateRegistryProposal(t *testing.T) {
"base_borrow_rate": "0.02",
"kink_borrow_rate": "0.2",
"max_borrow_rate": "1.5",
"kink_utilization_rate": "0.2",
"kink_utilization": "0.2",
"liquidation_incentive": "0.1",
"symbol_denom": "UMEE",
"exponent": 6,
Expand Down
2 changes: 1 addition & 1 deletion x/leverage/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Where proposal.json contains:
"base_borrow_rate": "0.02",
"kink_borrow_rate": "0.2",
"max_borrow_rate": "1.5",
"kink_utilization_rate": "0.2",
"kink_utilization": "0.2",
"liquidation_incentive": "0.1",
"symbol_denom": "UMEE",
"exponent": 6,
Expand Down
2 changes: 1 addition & 1 deletion x/leverage/client/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
BaseBorrowRate: sdk.MustNewDecFromStr("0.02"),
KinkBorrowRate: sdk.MustNewDecFromStr("0.2"),
MaxBorrowRate: sdk.MustNewDecFromStr("1.5"),
KinkUtilizationRate: sdk.MustNewDecFromStr("0.2"),
KinkUtilization: sdk.MustNewDecFromStr("0.2"),
LiquidationIncentive: sdk.MustNewDecFromStr("0.18"),
EnableMsgLend: true,
EnableMsgBorrow: true,
Expand Down
2 changes: 1 addition & 1 deletion x/leverage/gov_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewUpdateRegistryProposalHandler(k keeper.Keeper) govtypes.Handler {

func handleUpdateRegistryProposalHandler(ctx sdk.Context, k keeper.Keeper, p *types.UpdateRegistryProposal) error {
for _, token := range p.Registry {
if err := k.SetRegisteredToken(ctx, token); err != nil {
if err := k.SetTokenSettings(ctx, token); err != nil {
return err
}
}
Expand Down
10 changes: 5 additions & 5 deletions x/leverage/gov_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newTestToken(base, symbol, reserveFactor string) types.Token {
BaseBorrowRate: sdk.MustNewDecFromStr("0.02"),
KinkBorrowRate: sdk.MustNewDecFromStr("0.22"),
MaxBorrowRate: sdk.MustNewDecFromStr("1.52"),
KinkUtilizationRate: sdk.MustNewDecFromStr("0.8"),
KinkUtilization: sdk.MustNewDecFromStr("0.8"),
LiquidationIncentive: sdk.MustNewDecFromStr("0.1"),
EnableMsgLend: true,
EnableMsgBorrow: true,
Expand Down Expand Up @@ -60,10 +60,10 @@ func TestUpdateRegistryProposalHandler(t *testing.T) {
})

t.Run("valid proposal", func(t *testing.T) {
require.NoError(t, k.SetRegisteredToken(ctx,
require.NoError(t, k.SetTokenSettings(ctx,
newTestToken("uosmo", "OSMO", "0.2"),
))
require.NoError(t, k.SetRegisteredToken(ctx,
require.NoError(t, k.SetTokenSettings(ctx,
newTestToken("uatom", "ATOM", "0.2"),
))

Expand All @@ -81,10 +81,10 @@ func TestUpdateRegistryProposalHandler(t *testing.T) {
tokens := k.GetAllRegisteredTokens(ctx)
require.Len(t, tokens, 3)

_, err := k.GetRegisteredToken(ctx, "uumee")
_, err := k.GetTokenSettings(ctx, "uumee")
require.NoError(t, err)

token, err := k.GetRegisteredToken(ctx, "uosmo")
token, err := k.GetTokenSettings(ctx, "uosmo")
require.NoError(t, err)
require.Equal(t, "0.300000000000000000", token.ReserveFactor.String())
})
Expand Down
21 changes: 10 additions & 11 deletions x/leverage/keeper/borrows.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (k Keeper) GetAvailableToBorrow(ctx sdk.Context, denom string) sdk.Int {
return sdk.MaxInt(moduleBalance.Sub(reserveAmount), sdk.ZeroInt())
}

// DeriveBorrowUtilization derives the current borrow utilization of a token denom.
func (k Keeper) DeriveBorrowUtilization(ctx sdk.Context, denom string) sdk.Dec {
// ComputeBorrowUtilization derives the current borrow utilization of a token denom.
func (k Keeper) ComputeBorrowUtilization(ctx sdk.Context, denom string) sdk.Dec {
// Borrow utilization is equal to total borrows divided by the token supply
// (including borrowed tokens yet to be repaid and excluding tokens reserved).
moduleBalance := k.ModuleBalance(ctx, denom).ToDec()
Expand Down Expand Up @@ -91,18 +91,18 @@ func (k Keeper) CalculateBorrowLimit(ctx sdk.Context, collateral sdk.Coins) (sdk
}

// get USD value of base assets
value, err := k.TokenValue(ctx, baseAsset)
v, err := k.TokenValue(ctx, baseAsset)
if err != nil {
return sdk.ZeroDec(), err
}

weight, err := k.GetCollateralWeight(ctx, baseAsset.Denom)
ts, err := k.GetTokenSettings(ctx, baseAsset.Denom)
if err != nil {
return sdk.ZeroDec(), err
}

// add each collateral coin's weighted value to borrow limit
limit = limit.Add(value.Mul(weight))
limit = limit.Add(v.Mul(ts.CollateralWeight))
}

return limit, nil
Expand All @@ -114,7 +114,7 @@ func (k Keeper) CalculateBorrowLimit(ctx sdk.Context, collateral sdk.Coins) (sdk
// An error is returned if any input coins are not uTokens or if value
// calculation fails.
func (k Keeper) CalculateLiquidationThreshold(ctx sdk.Context, collateral sdk.Coins) (sdk.Dec, error) {
threshold := sdk.ZeroDec()
totalThreshold := sdk.ZeroDec()

for _, coin := range collateral {
// convert uToken collateral to base assets
Expand All @@ -124,21 +124,20 @@ func (k Keeper) CalculateLiquidationThreshold(ctx sdk.Context, collateral sdk.Co
}

// get USD value of base assets
value, err := k.TokenValue(ctx, baseAsset)
v, err := k.TokenValue(ctx, baseAsset)
if err != nil {
return sdk.ZeroDec(), err
}

weight, err := k.GetLiquidationThreshold(ctx, baseAsset.Denom)
ts, err := k.GetTokenSettings(ctx, baseAsset.Denom)
if err != nil {
return sdk.ZeroDec(), err
}

// add each liquidation threshold value to total
threshold = threshold.Add(value.Mul(weight))
totalThreshold = totalThreshold.Add(v.Mul(ts.LiquidationThreshold))
}

return threshold, nil
return totalThreshold, nil
}

// setBadDebtAddress sets or deletes an address in a denom's list of addresses with unpaid bad debt.
Expand Down
18 changes: 9 additions & 9 deletions x/leverage/keeper/borrows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *IntegrationTestSuite) TestGetAvailableToBorrow() {

func (s *IntegrationTestSuite) TestDeriveBorrowUtilization() {
// unregistered denom (0 borrowed and 0 lending pool is considered 100%)
utilization := s.tk.DeriveBorrowUtilization(s.ctx, "abcd")
utilization := s.tk.ComputeBorrowUtilization(s.ctx, "abcd")
s.Require().Equal(sdk.OneDec(), utilization)

// creates account which has loaned 1000 uumee, and borrowed 0 uumee
Expand All @@ -130,56 +130,56 @@ func (s *IntegrationTestSuite) TestDeriveBorrowUtilization() {
// utilization = (Total Borrowed / (Total Borrowed + Module Balance - Reserved Amount))

// 0% utilization (0 / 0+1000-0)
utilization = s.tk.DeriveBorrowUtilization(s.ctx, umeeDenom)
utilization = s.tk.ComputeBorrowUtilization(s.ctx, umeeDenom)
s.Require().Equal(sdk.ZeroDec(), utilization)

// lender borrows 200 uumee, reducing module account to 800 uumee
s.Require().NoError(s.tk.BorrowAsset(s.ctx, addr, sdk.NewInt64Coin(umeeDenom, 200)))

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

// artificially reserve 200 uumee
s.Require().NoError(s.tk.SetReserveAmount(s.ctx, sdk.NewInt64Coin(umeeDenom, 200)))

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

// Setting umee collateral weight to 1.0 to allow lender to borrow heavily
umeeToken := newToken("uumee", "UMEE")
umeeToken.CollateralWeight = sdk.MustNewDecFromStr("1")
umeeToken.LiquidationThreshold = sdk.MustNewDecFromStr("1")

s.Require().NoError(s.app.LeverageKeeper.SetRegisteredToken(s.ctx, umeeToken))
s.Require().NoError(s.app.LeverageKeeper.SetTokenSettings(s.ctx, umeeToken))

// lender borrows 600 uumee, reducing module account to 0 uumee
s.Require().NoError(s.tk.BorrowAsset(s.ctx, addr, sdk.NewInt64Coin(umeeDenom, 600)))

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

// artificially set lender borrow to 1200 umee
s.Require().NoError(s.tk.SetBorrow(s.ctx, addr, sdk.NewInt64Coin(umeeDenom, 1200)))

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

// artificially set reserves to 800 uumee
s.Require().NoError(s.tk.SetReserveAmount(s.ctx, sdk.NewInt64Coin(umeeDenom, 800)))

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

// artificially set reserves to 4000 uumee
s.Require().NoError(s.tk.SetReserveAmount(s.ctx, sdk.NewInt64Coin(umeeDenom, 4000)))

// impossible case interpreted as 100% utilization (1200 / 1200+200-4000)
utilization = s.tk.DeriveBorrowUtilization(s.ctx, umeeDenom)
utilization = s.tk.ComputeBorrowUtilization(s.ctx, umeeDenom)
s.Require().Equal(sdk.MustNewDecFromStr("1.0"), utilization)
}

Expand Down
2 changes: 1 addition & 1 deletion x/leverage/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
k.SetParams(ctx, genState.Params)

for _, token := range genState.Registry {
if err := k.SetRegisteredToken(ctx, token); err != nil {
if err := k.SetTokenSettings(ctx, token); err != nil {
panic(err)
}
}
Expand Down
5 changes: 1 addition & 4 deletions x/leverage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func (q Querier) Borrowed(

if len(req.Denom) == 0 {
tokens := q.Keeper.GetBorrowerBorrows(ctx, borrower)

return &types.QueryBorrowedResponse{Borrowed: tokens}, nil
}

Expand Down Expand Up @@ -400,7 +399,6 @@ func (q Querier) Collateral(

if len(req.Denom) == 0 {
tokens := q.Keeper.GetBorrowerCollateral(ctx, borrower)

return &types.QueryCollateralResponse{Collateral: tokens}, nil
}

Expand Down Expand Up @@ -441,7 +439,6 @@ func (q Querier) CollateralValue(
}

collateral := q.Keeper.GetCollateralAmount(ctx, lender, req.Denom)

uTokens = sdk.NewCoins(collateral)
}

Expand Down Expand Up @@ -572,7 +569,7 @@ func (q Querier) MarketSummary(

ctx := sdk.UnwrapSDKContext(goCtx)

token, err := q.Keeper.GetRegisteredToken(ctx, req.Denom)
token, err := q.Keeper.GetTokenSettings(ctx, req.Denom)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "not accepted Token denom")
}
Expand Down
30 changes: 15 additions & 15 deletions x/leverage/keeper/interest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// using its borrow utilization and token-specific params. Returns zero on
// invalid asset.
func (k Keeper) DeriveBorrowAPY(ctx sdk.Context, denom string) sdk.Dec {
token, err := k.GetRegisteredToken(ctx, denom)
token, err := k.GetTokenSettings(ctx, denom)
if err != nil {
return sdk.ZeroDec()
}
Expand All @@ -23,38 +23,38 @@ func (k Keeper) DeriveBorrowAPY(ctx sdk.Context, denom string) sdk.Dec {
return sdk.ZeroDec()
}

utilization := k.DeriveBorrowUtilization(ctx, denom)
utilization := k.ComputeBorrowUtilization(ctx, denom)

if utilization.GTE(token.KinkUtilizationRate) {
if utilization.GTE(token.KinkUtilization) {
return Interpolate(
utilization, // x
token.KinkUtilizationRate, // x1
token.KinkBorrowRate, // y1
sdk.OneDec(), // x2
token.MaxBorrowRate, // y2
utilization, // x
token.KinkUtilization, // x1
token.KinkBorrowRate, // y1
sdk.OneDec(), // x2
token.MaxBorrowRate, // y2
)
}

// utilization is between 0% and kink value
return Interpolate(
utilization, // x
sdk.ZeroDec(), // x1
token.BaseBorrowRate, // y1
token.KinkUtilizationRate, // x2
token.KinkBorrowRate, // y2
utilization, // x
sdk.ZeroDec(), // x1
token.BaseBorrowRate, // y1
token.KinkUtilization, // x2
token.KinkBorrowRate, // y2
)
}

// DeriveLendAPY derives the current lend interest rate on a token denom
// using its borrow utilization borrow APY. Returns zero on invalid asset.
func (k Keeper) DeriveLendAPY(ctx sdk.Context, denom string) sdk.Dec {
token, err := k.GetRegisteredToken(ctx, denom)
token, err := k.GetTokenSettings(ctx, denom)
if err != nil {
return sdk.ZeroDec()
}

borrowRate := k.DeriveBorrowAPY(ctx, denom)
borrowUtilization := k.DeriveBorrowUtilization(ctx, denom)
borrowUtilization := k.ComputeBorrowUtilization(ctx, denom)
reduction := k.GetParams(ctx).OracleRewardFactor.Add(token.ReserveFactor)

// lend APY = borrow APY * utilization, reduced by reserve factor and oracle reward factor
Expand Down
Loading

0 comments on commit 40a6890

Please sign in to comment.