mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 23:35:19 +00:00
Harvest: borrows capped by utilization (#714)
* wrap error borrow exceeds mod acc balance * address revisions
This commit is contained in:
parent
e43fb9d030
commit
cfb1905ad3
@ -1,6 +1,8 @@
|
|||||||
package keeper
|
package keeper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||||
|
|
||||||
@ -16,7 +18,18 @@ func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins
|
|||||||
|
|
||||||
err = k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, borrower, coins)
|
err = k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, borrower, coins)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
if strings.Contains(err.Error(), "insufficient account funds") {
|
||||||
|
modAccCoins := k.supplyKeeper.GetModuleAccount(ctx, types.ModuleAccountName).GetCoins()
|
||||||
|
for _, coin := range coins {
|
||||||
|
_, isNegative := modAccCoins.SafeSub(sdk.NewCoins(coin))
|
||||||
|
if isNegative {
|
||||||
|
return sdkerrors.Wrapf(types.ErrBorrowExceedsAvailableBalance,
|
||||||
|
"the requested borrow amount of %s exceeds the total amount of %s%s available to borrow",
|
||||||
|
coin, modAccCoins.AmountOf(coin.Denom), coin.Denom,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
borrow, found := k.GetBorrow(ctx, borrower)
|
borrow, found := k.GetBorrow(ctx, borrower)
|
||||||
|
@ -192,6 +192,27 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
|||||||
contains: "no price found for market",
|
contains: "no price found for market",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"invalid: borrow exceed module account balance",
|
||||||
|
args{
|
||||||
|
priceKAVA: sdk.MustNewDecFromStr("2.00"),
|
||||||
|
loanToValueKAVA: sdk.MustNewDecFromStr("0.8"),
|
||||||
|
priceBTCB: sdk.MustNewDecFromStr("0.00"),
|
||||||
|
loanToValueBTCB: sdk.MustNewDecFromStr("0.01"),
|
||||||
|
priceBNB: sdk.MustNewDecFromStr("0.00"),
|
||||||
|
loanToValueBNB: sdk.MustNewDecFromStr("0.01"),
|
||||||
|
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||||
|
depositCoins: []sdk.Coin{sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))},
|
||||||
|
previousBorrowCoins: sdk.NewCoins(),
|
||||||
|
borrowCoins: sdk.NewCoins(sdk.NewCoin("busd", sdk.NewInt(101*BUSD_CF))),
|
||||||
|
expectedAccountBalance: sdk.NewCoins(),
|
||||||
|
expectedModAccountBalance: sdk.NewCoins(),
|
||||||
|
},
|
||||||
|
errArgs{
|
||||||
|
expectPass: false,
|
||||||
|
contains: "exceeds module account balance:",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
suite.Run(tc.name, func() {
|
suite.Run(tc.name, func() {
|
||||||
|
@ -45,4 +45,6 @@ var (
|
|||||||
ErrMarketNotFound = sdkerrors.Register(ModuleName, 19, "no market found for denom")
|
ErrMarketNotFound = sdkerrors.Register(ModuleName, 19, "no market found for denom")
|
||||||
// ErrPriceNotFound error for when a price for the input market is not found
|
// ErrPriceNotFound error for when a price for the input market is not found
|
||||||
ErrPriceNotFound = sdkerrors.Register(ModuleName, 20, "no price found for market")
|
ErrPriceNotFound = sdkerrors.Register(ModuleName, 20, "no price found for market")
|
||||||
|
// ErrBorrowExceedsAvailableBalance for when a requested borrow exceeds available module acc balances
|
||||||
|
ErrBorrowExceedsAvailableBalance = sdkerrors.Register(ModuleName, 21, "exceeds module account balance")
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user