mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 00:05:18 +00:00
Harvest: borrows limited by LTV (#710)
* basic borrow types * borrow keeper scaffolding * borrow limits param * integrate pricefeed keeper * msg handling and querier * borrow user validation * update migration scripts for compile * borrows querier, fixes * add money market param * add spot market ID to params, refactor pricefeed * working bnb -> ukava borrows * refactor to getAssetPrice * conversion_factor param, refactor validateBorrow() * address misc revisions * remove validation code * add borrow test * update test params * single borrow with sdk.Coins per user * fix harvest test * add borrow validation * simplify borrow validation * add test case * master compatibility * fix build * refactor to calculateUSDValue() * add maxLoanToValue to test
This commit is contained in:
parent
36a32d7962
commit
e9d04cd7c6
@ -2,13 +2,20 @@ package keeper
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||||
|
|
||||||
"github.com/kava-labs/kava/x/harvest/types"
|
"github.com/kava-labs/kava/x/harvest/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Borrow funds
|
// Borrow funds
|
||||||
func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins) error {
|
func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins) error {
|
||||||
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, borrower, coins)
|
// TODO: Here we assume borrower only has one coin. To be addressed in future card.
|
||||||
|
err := k.ValidateBorrow(ctx, borrower, coins[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, borrower, coins)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -19,7 +26,6 @@ func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins
|
|||||||
} else {
|
} else {
|
||||||
borrow.Amount = borrow.Amount.Add(coins...)
|
borrow.Amount = borrow.Amount.Add(coins...)
|
||||||
}
|
}
|
||||||
|
|
||||||
k.SetBorrow(ctx, borrow)
|
k.SetBorrow(ctx, borrow)
|
||||||
|
|
||||||
ctx.EventManager().EmitEvent(
|
ctx.EventManager().EmitEvent(
|
||||||
@ -32,3 +38,58 @@ func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValidateBorrow validates a borrow request against borrower and protocol requirements
|
||||||
|
func (k Keeper) ValidateBorrow(ctx sdk.Context, borrower sdk.AccAddress, amount sdk.Coin) error {
|
||||||
|
moneyMarket, found := k.GetMoneyMarket(ctx, amount.Denom)
|
||||||
|
if !found {
|
||||||
|
sdkerrors.Wrapf(types.ErrMarketNotFound, "no market found for denom %s", amount.Denom)
|
||||||
|
}
|
||||||
|
assetPriceInfo, err := k.pricefeedKeeper.GetCurrentPrice(ctx, moneyMarket.SpotMarketID)
|
||||||
|
if err != nil {
|
||||||
|
return sdkerrors.Wrapf(types.ErrPriceNotFound, "no price found for market %s", moneyMarket.SpotMarketID)
|
||||||
|
}
|
||||||
|
proprosedBorrowUSDValue := sdk.NewDecFromInt(amount.Amount).Quo(sdk.NewDecFromInt(moneyMarket.ConversionFactor).Mul(assetPriceInfo.Price))
|
||||||
|
|
||||||
|
// Get the total value of the user's deposits
|
||||||
|
deposits := k.GetDepositsByUser(ctx, borrower)
|
||||||
|
if len(deposits) == 0 {
|
||||||
|
return sdkerrors.Wrapf(types.ErrDepositsNotFound, "no deposits found for %s", borrower)
|
||||||
|
}
|
||||||
|
deposit := deposits[0] // TODO: Here we assume there's only one deposit. To be addressed in future cards.
|
||||||
|
depositUSDValue, err := k.calculateUSDValue(ctx, deposit.Amount.Amount, deposit.Amount.Denom)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
previousBorrowUSDValue := sdk.ZeroDec()
|
||||||
|
previousBorrows, found := k.GetBorrow(ctx, borrower)
|
||||||
|
if found {
|
||||||
|
// TODO: here we're assuming that the user only has 1 previous borrow. To be addressed in future cards.
|
||||||
|
previousBorrow := previousBorrows.Amount[0]
|
||||||
|
previousBorrowUSDValue, err = k.calculateUSDValue(ctx, previousBorrow.Amount, previousBorrow.Denom)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value of borrow cannot be greater than:
|
||||||
|
// (total value of user's deposits * the borrow asset denom's LTV ratio) - funds already borrowed
|
||||||
|
borrowValueLimit := depositUSDValue.Mul(moneyMarket.BorrowLimit.LoanToValue).Sub(previousBorrowUSDValue)
|
||||||
|
if proprosedBorrowUSDValue.GT(borrowValueLimit) {
|
||||||
|
return sdkerrors.Wrapf(types.ErrInsufficientLoanToValue, "requested borrow %s is greater than maximum valid borrow", amount)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Keeper) calculateUSDValue(ctx sdk.Context, amount sdk.Int, denom string) (sdk.Dec, error) {
|
||||||
|
moneyMarket, found := k.GetMoneyMarket(ctx, denom)
|
||||||
|
if !found {
|
||||||
|
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrMarketNotFound, "no market found for denom %s", denom)
|
||||||
|
}
|
||||||
|
assetPriceInfo, err := k.pricefeedKeeper.GetCurrentPrice(ctx, moneyMarket.SpotMarketID)
|
||||||
|
if err != nil {
|
||||||
|
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrPriceNotFound, "no price found for market %s", moneyMarket.SpotMarketID)
|
||||||
|
}
|
||||||
|
return sdk.NewDecFromInt(amount).Quo(sdk.NewDecFromInt(moneyMarket.ConversionFactor).Mul(assetPriceInfo.Price)), nil
|
||||||
|
}
|
||||||
|
@ -16,7 +16,9 @@ import (
|
|||||||
func (suite *KeeperTestSuite) TestBorrow() {
|
func (suite *KeeperTestSuite) TestBorrow() {
|
||||||
type args struct {
|
type args struct {
|
||||||
borrower sdk.AccAddress
|
borrower sdk.AccAddress
|
||||||
|
depositCoin sdk.Coin
|
||||||
coins sdk.Coins
|
coins sdk.Coins
|
||||||
|
maxLoanToValue string
|
||||||
expectedAccountBalance sdk.Coins
|
expectedAccountBalance sdk.Coins
|
||||||
expectedModAccountBalance sdk.Coins
|
expectedModAccountBalance sdk.Coins
|
||||||
}
|
}
|
||||||
@ -34,7 +36,9 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
|||||||
"valid",
|
"valid",
|
||||||
args{
|
args{
|
||||||
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||||
|
depositCoin: sdk.NewCoin("ukava", sdk.NewInt(100)),
|
||||||
coins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50))),
|
coins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50))),
|
||||||
|
maxLoanToValue: "0.6",
|
||||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(150))),
|
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(150))),
|
||||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(950))),
|
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(950))),
|
||||||
},
|
},
|
||||||
@ -43,18 +47,31 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
|||||||
contains: "",
|
contains: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"loan-to-value limited",
|
||||||
|
args{
|
||||||
|
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||||
|
depositCoin: sdk.NewCoin("ukava", sdk.NewInt(20)), // 20 KAVA x $5.00 price = $100
|
||||||
|
coins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(61))), // 61 USDX x $1 price = $61
|
||||||
|
maxLoanToValue: "0.6",
|
||||||
|
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(150))),
|
||||||
|
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(950))),
|
||||||
|
},
|
||||||
|
errArgs{
|
||||||
|
expectPass: false,
|
||||||
|
contains: "total deposited value is insufficient for borrow request",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
suite.Run(tc.name, func() {
|
suite.Run(tc.name, func() {
|
||||||
// create new app with one funded account
|
|
||||||
|
|
||||||
// Initialize test app and set context
|
// Initialize test app and set context
|
||||||
tApp := app.NewTestApp()
|
tApp := app.NewTestApp()
|
||||||
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: tmtime.Now()})
|
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: tmtime.Now()})
|
||||||
authGS := app.NewAuthGenState(
|
authGS := app.NewAuthGenState(
|
||||||
[]sdk.AccAddress{tc.args.borrower},
|
[]sdk.AccAddress{tc.args.borrower},
|
||||||
[]sdk.Coins{sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100)))})
|
[]sdk.Coins{sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100)))})
|
||||||
loanToValue := sdk.MustNewDecFromStr("0.6")
|
loanToValue := sdk.MustNewDecFromStr(tc.args.maxLoanToValue)
|
||||||
harvestGS := types.NewGenesisState(types.NewParams(
|
harvestGS := types.NewGenesisState(types.NewParams(
|
||||||
true,
|
true,
|
||||||
types.DistributionSchedules{
|
types.DistributionSchedules{
|
||||||
@ -71,7 +88,8 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
|||||||
types.NewMoneyMarket("ukava", sdk.NewInt(1000000000000000), loanToValue, "kava:usd", sdk.NewInt(1000000)),
|
types.NewMoneyMarket("ukava", sdk.NewInt(1000000000000000), loanToValue, "kava:usd", sdk.NewInt(1000000)),
|
||||||
},
|
},
|
||||||
), types.DefaultPreviousBlockTime, types.DefaultDistributionTimes)
|
), types.DefaultPreviousBlockTime, types.DefaultDistributionTimes)
|
||||||
tApp.InitializeFromGenesisStates(authGS, app.GenesisState{types.ModuleName: types.ModuleCdc.MustMarshalJSON(harvestGS)})
|
tApp.InitializeFromGenesisStates(authGS, NewPricefeedGenStateMulti(),
|
||||||
|
app.GenesisState{types.ModuleName: types.ModuleCdc.MustMarshalJSON(harvestGS)})
|
||||||
keeper := tApp.GetHarvestKeeper()
|
keeper := tApp.GetHarvestKeeper()
|
||||||
supplyKeeper := tApp.GetSupplyKeeper()
|
supplyKeeper := tApp.GetSupplyKeeper()
|
||||||
supplyKeeper.MintCoins(ctx, types.ModuleAccountName, sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(1000))))
|
supplyKeeper.MintCoins(ctx, types.ModuleAccountName, sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(1000))))
|
||||||
@ -79,17 +97,22 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
|||||||
suite.ctx = ctx
|
suite.ctx = ctx
|
||||||
suite.keeper = keeper
|
suite.keeper = keeper
|
||||||
|
|
||||||
// run the test
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
// deposit some coins
|
||||||
|
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoin, types.LP)
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
|
// run the test
|
||||||
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.coins)
|
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.coins)
|
||||||
|
|
||||||
// verify results
|
// verify results
|
||||||
if tc.errArgs.expectPass {
|
if tc.errArgs.expectPass {
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
acc := suite.getAccount(tc.args.borrower)
|
acc := suite.getAccount(tc.args.borrower)
|
||||||
suite.Require().Equal(tc.args.expectedAccountBalance, acc.GetCoins())
|
suite.Require().Equal(tc.args.expectedAccountBalance.Sub(sdk.NewCoins(tc.args.depositCoin)), acc.GetCoins())
|
||||||
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
||||||
suite.Require().Equal(tc.args.expectedModAccountBalance, mAcc.GetCoins())
|
suite.Require().Equal(tc.args.expectedModAccountBalance.Add(tc.args.depositCoin), mAcc.GetCoins())
|
||||||
_, f := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)
|
_, f := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)
|
||||||
suite.Require().True(f)
|
suite.Require().True(f)
|
||||||
} else {
|
} else {
|
||||||
|
50
x/harvest/keeper/integration_test.go
Normal file
50
x/harvest/keeper/integration_test.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package keeper_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/app"
|
||||||
|
"github.com/kava-labs/kava/x/pricefeed"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewPricefeedGenStateMulti() app.GenesisState {
|
||||||
|
pfGenesis := pricefeed.GenesisState{
|
||||||
|
Params: pricefeed.Params{
|
||||||
|
Markets: []pricefeed.Market{
|
||||||
|
{MarketID: "btc:usd", BaseAsset: "btc", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
||||||
|
{MarketID: "xrp:usd", BaseAsset: "xrp", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
||||||
|
{MarketID: "bnb:usd", BaseAsset: "bnb", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
||||||
|
{MarketID: "kava:usd", BaseAsset: "kava", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
PostedPrices: []pricefeed.PostedPrice{
|
||||||
|
{
|
||||||
|
MarketID: "btc:usd",
|
||||||
|
OracleAddress: sdk.AccAddress{},
|
||||||
|
Price: sdk.MustNewDecFromStr("8000.00"),
|
||||||
|
Expiry: time.Now().Add(1 * time.Hour),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
MarketID: "xrp:usd",
|
||||||
|
OracleAddress: sdk.AccAddress{},
|
||||||
|
Price: sdk.MustNewDecFromStr("0.25"),
|
||||||
|
Expiry: time.Now().Add(1 * time.Hour),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
MarketID: "bnb:usd",
|
||||||
|
OracleAddress: sdk.AccAddress{},
|
||||||
|
Price: sdk.MustNewDecFromStr("17.25"),
|
||||||
|
Expiry: time.Now().Add(1 * time.Hour),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
MarketID: "kava:usd",
|
||||||
|
OracleAddress: sdk.AccAddress{},
|
||||||
|
Price: sdk.MustNewDecFromStr("5.00"),
|
||||||
|
Expiry: time.Now().Add(1 * time.Hour),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return app.GenesisState{pricefeed.ModuleName: pricefeed.ModuleCdc.MustMarshalJSON(pfGenesis)}
|
||||||
|
}
|
@ -51,6 +51,7 @@ func (suite *GenesisTestSuite) TestGenesisValidation() {
|
|||||||
time.Hour*24,
|
time.Hour*24,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
types.DefaultMoneyMarkets,
|
||||||
),
|
),
|
||||||
pbt: time.Date(2020, 10, 8, 12, 0, 0, 0, time.UTC),
|
pbt: time.Date(2020, 10, 8, 12, 0, 0, 0, time.UTC),
|
||||||
pdts: types.GenesisDistributionTimes{
|
pdts: types.GenesisDistributionTimes{
|
||||||
@ -73,6 +74,7 @@ func (suite *GenesisTestSuite) TestGenesisValidation() {
|
|||||||
time.Hour*24,
|
time.Hour*24,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
types.DefaultMoneyMarkets,
|
||||||
),
|
),
|
||||||
pbt: time.Time{},
|
pbt: time.Time{},
|
||||||
pdts: types.GenesisDistributionTimes{
|
pdts: types.GenesisDistributionTimes{
|
||||||
@ -95,6 +97,7 @@ func (suite *GenesisTestSuite) TestGenesisValidation() {
|
|||||||
time.Hour*24,
|
time.Hour*24,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
types.DefaultMoneyMarkets,
|
||||||
),
|
),
|
||||||
pbt: time.Date(2020, 10, 8, 12, 0, 0, 0, time.UTC),
|
pbt: time.Date(2020, 10, 8, 12, 0, 0, 0, time.UTC),
|
||||||
pdts: types.GenesisDistributionTimes{
|
pdts: types.GenesisDistributionTimes{
|
||||||
|
@ -21,6 +21,7 @@ func (suite *ParamTestSuite) TestParamValidation() {
|
|||||||
lps types.DistributionSchedules
|
lps types.DistributionSchedules
|
||||||
gds types.DistributionSchedules
|
gds types.DistributionSchedules
|
||||||
dds types.DelegatorDistributionSchedules
|
dds types.DelegatorDistributionSchedules
|
||||||
|
mms types.MoneyMarkets
|
||||||
active bool
|
active bool
|
||||||
}
|
}
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
@ -50,6 +51,7 @@ func (suite *ParamTestSuite) TestParamValidation() {
|
|||||||
time.Hour*24,
|
time.Hour*24,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
mms: types.DefaultMoneyMarkets,
|
||||||
active: true,
|
active: true,
|
||||||
},
|
},
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
@ -66,6 +68,7 @@ func (suite *ParamTestSuite) TestParamValidation() {
|
|||||||
time.Hour*24,
|
time.Hour*24,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
mms: types.DefaultMoneyMarkets,
|
||||||
active: true,
|
active: true,
|
||||||
},
|
},
|
||||||
expectPass: false,
|
expectPass: false,
|
||||||
@ -74,7 +77,7 @@ func (suite *ParamTestSuite) TestParamValidation() {
|
|||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
suite.Run(tc.name, func() {
|
suite.Run(tc.name, func() {
|
||||||
params := types.NewParams(tc.args.active, tc.args.lps, tc.args.dds)
|
params := types.NewParams(tc.args.active, tc.args.lps, tc.args.dds, tc.args.mms)
|
||||||
err := params.Validate()
|
err := params.Validate()
|
||||||
if tc.expectPass {
|
if tc.expectPass {
|
||||||
suite.NoError(err)
|
suite.NoError(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user