mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-25 07:45:18 +00:00
[hard] Update deposit type to use sdk.Coins (#744)
* update deposits to use sdk.Coins * update tests * update liquidation tests
This commit is contained in:
parent
83a5f51c11
commit
06fd215de1
@ -58,7 +58,6 @@ var (
|
||||
ClaimKey = types.ClaimKey
|
||||
DefaultGenesisState = types.DefaultGenesisState
|
||||
DefaultParams = types.DefaultParams
|
||||
DepositKey = types.DepositKey
|
||||
DepositTypeIteratorKey = types.DepositTypeIteratorKey
|
||||
GetTotalVestingPeriodLength = types.GetTotalVestingPeriodLength
|
||||
NewClaim = types.NewClaim
|
||||
|
@ -52,7 +52,7 @@ func getCmdDeposit(cdc *codec.Codec) *cobra.Command {
|
||||
inBuf := bufio.NewReader(cmd.InOrStdin())
|
||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
||||
amount, err := sdk.ParseCoin(args[0])
|
||||
amount, err := sdk.ParseCoins(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -77,7 +77,7 @@ func getCmdWithdraw(cdc *codec.Codec) *cobra.Command {
|
||||
inBuf := bufio.NewReader(cmd.InOrStdin())
|
||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
|
||||
amount, err := sdk.ParseCoin(args[0])
|
||||
amount, err := sdk.ParseCoins(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
||||
type PostCreateDepositReq struct {
|
||||
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
|
||||
From sdk.AccAddress `json:"from" yaml:"from"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
Amount sdk.Coins `json:"amount" yaml:"amount"`
|
||||
DepositType string `json:"deposit_type" yaml:"deposit_type"`
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ type PostCreateDepositReq struct {
|
||||
type PostCreateWithdrawReq struct {
|
||||
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
|
||||
From sdk.AccAddress `json:"from" yaml:"from"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
Amount sdk.Coins `json:"amount" yaml:"amount"`
|
||||
DepositType string `json:"deposit_type" yaml:"deposit_type"`
|
||||
}
|
||||
|
||||
|
@ -180,20 +180,20 @@ func (k Keeper) ValidateBorrow(ctx sdk.Context, borrower sdk.AccAddress, amount
|
||||
}
|
||||
|
||||
// Get the total borrowable USD amount at user's existing deposits
|
||||
deposits := k.GetDepositsByUser(ctx, borrower)
|
||||
if len(deposits) == 0 {
|
||||
deposit, found := k.GetDeposit(ctx, borrower)
|
||||
if !found {
|
||||
return sdkerrors.Wrapf(types.ErrDepositsNotFound, "no deposits found for %s", borrower)
|
||||
}
|
||||
totalBorrowableAmount := sdk.ZeroDec()
|
||||
for _, deposit := range deposits {
|
||||
moneyMarket, ok := moneyMarketCache[deposit.Amount.Denom]
|
||||
for _, depCoin := range deposit.Amount {
|
||||
moneyMarket, ok := moneyMarketCache[depCoin.Denom]
|
||||
// Fetch money market and store in local cache
|
||||
if !ok {
|
||||
newMoneyMarket, found := k.GetMoneyMarketParam(ctx, deposit.Amount.Denom)
|
||||
newMoneyMarket, found := k.GetMoneyMarketParam(ctx, depCoin.Denom)
|
||||
if !found {
|
||||
return sdkerrors.Wrapf(types.ErrMarketNotFound, "no market found for denom %s", deposit.Amount.Denom)
|
||||
return sdkerrors.Wrapf(types.ErrMarketNotFound, "no market found for denom %s", depCoin.Denom)
|
||||
}
|
||||
moneyMarketCache[deposit.Amount.Denom] = newMoneyMarket
|
||||
moneyMarketCache[depCoin.Denom] = newMoneyMarket
|
||||
moneyMarket = newMoneyMarket
|
||||
}
|
||||
|
||||
@ -202,7 +202,7 @@ func (k Keeper) ValidateBorrow(ctx sdk.Context, borrower sdk.AccAddress, amount
|
||||
if err != nil {
|
||||
sdkerrors.Wrapf(types.ErrPriceNotFound, "no price found for market %s", moneyMarket.SpotMarketID)
|
||||
}
|
||||
depositUSDValue := sdk.NewDecFromInt(deposit.Amount.Amount).Quo(sdk.NewDecFromInt(moneyMarket.ConversionFactor)).Mul(assetPriceInfo.Price)
|
||||
depositUSDValue := sdk.NewDecFromInt(depCoin.Amount).Quo(sdk.NewDecFromInt(moneyMarket.ConversionFactor)).Mul(assetPriceInfo.Price)
|
||||
borrowableAmountForDeposit := depositUSDValue.Mul(moneyMarket.BorrowLimit.LoanToValue)
|
||||
totalBorrowableAmount = totalBorrowableAmount.Add(borrowableAmountForDeposit)
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
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(50*KAVA_CF)), sdk.NewCoin("btcb", sdk.NewInt(0.1*BTCB_CF))},
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF)), sdk.NewCoin("btcb", sdk.NewInt(0.1*BTCB_CF))),
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(180*USDX_CF))),
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF)), sdk.NewCoin("btcb", sdk.NewInt(99.9*BTCB_CF)), sdk.NewCoin("usdx", sdk.NewInt(180*USDX_CF)), sdk.NewCoin("bnb", sdk.NewInt(100*BNB_CF)), sdk.NewCoin("xyz", sdk.NewInt(1))),
|
||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(1050*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(20*USDX_CF)), sdk.NewCoin("btcb", sdk.NewInt(0.1*BTCB_CF)), sdk.NewCoin("busd", sdk.NewInt(100*BUSD_CF))),
|
||||
@ -125,7 +125,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
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(50*KAVA_CF)), sdk.NewCoin("btcb", sdk.NewInt(0.1*BTCB_CF))},
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF)), sdk.NewCoin("btcb", sdk.NewInt(0.1*BTCB_CF))),
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(181*USDX_CF))),
|
||||
expectedAccountBalance: sdk.NewCoins(),
|
||||
expectedModAccountBalance: sdk.NewCoins(),
|
||||
@ -146,7 +146,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
priceBNB: sdk.MustNewDecFromStr("5.00"),
|
||||
loanToValueBNB: sdk.MustNewDecFromStr("0.8"),
|
||||
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositCoins: []sdk.Coin{sdk.NewCoin("bnb", sdk.NewInt(30*BNB_CF)), sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF))}, // (50 KAVA x $2.00 price = $100) + (30 BNB x $5.00 price = $150) = $250
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(30*BNB_CF)), sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF))), // (50 KAVA x $2.00 price = $100) + (30 BNB x $5.00 price = $150) = $250
|
||||
previousBorrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(99*USDX_CF)), sdk.NewCoin("busd", sdk.NewInt(100*BUSD_CF))),
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(1*USDX_CF))),
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF)), sdk.NewCoin("btcb", sdk.NewInt(100*BTCB_CF)), sdk.NewCoin("usdx", sdk.NewInt(100*USDX_CF)), sdk.NewCoin("busd", sdk.NewInt(100*BUSD_CF)), sdk.NewCoin("bnb", sdk.NewInt(70*BNB_CF)), sdk.NewCoin("xyz", sdk.NewInt(1))),
|
||||
@ -168,7 +168,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
priceBNB: sdk.MustNewDecFromStr("5.00"),
|
||||
loanToValueBNB: sdk.MustNewDecFromStr("0.8"),
|
||||
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositCoins: []sdk.Coin{sdk.NewCoin("bnb", sdk.NewInt(30*BNB_CF)), sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF))}, // (50 KAVA x $2.00 price = $100) + (30 BNB x $5.00 price = $150) = $250
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(30*BNB_CF)), sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF))), // (50 KAVA x $2.00 price = $100) + (30 BNB x $5.00 price = $150) = $250
|
||||
previousBorrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(100*USDX_CF)), sdk.NewCoin("busd", sdk.NewInt(100*BUSD_CF))),
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(1*USDX_CF))),
|
||||
expectedAccountBalance: sdk.NewCoins(),
|
||||
@ -190,7 +190,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
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))},
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
previousBorrowCoins: sdk.NewCoins(),
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("xyz", sdk.NewInt(1))),
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(20*KAVA_CF)), sdk.NewCoin("btcb", sdk.NewInt(100*BTCB_CF)), sdk.NewCoin("bnb", sdk.NewInt(100*BNB_CF)), sdk.NewCoin("xyz", sdk.NewInt(1))),
|
||||
@ -212,7 +212,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
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))},
|
||||
depositCoins: sdk.NewCoins(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(),
|
||||
@ -234,7 +234,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
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(50*KAVA_CF))},
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF))),
|
||||
previousBorrowCoins: sdk.NewCoins(),
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(25*USDX_CF))),
|
||||
expectedAccountBalance: sdk.NewCoins(),
|
||||
@ -353,13 +353,8 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
// Run BeginBlocker once to transition MoneyMarkets
|
||||
harvest.BeginBlocker(suite.ctx, suite.keeper)
|
||||
|
||||
// Deposit coins to harvest
|
||||
depositedCoins := sdk.NewCoins()
|
||||
for _, depositCoin := range tc.args.depositCoins {
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, depositCoin)
|
||||
suite.Require().NoError(err)
|
||||
depositedCoins.Add(depositCoin)
|
||||
}
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// Execute user's previous borrows
|
||||
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.previousBorrowCoins)
|
||||
@ -377,11 +372,11 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
|
||||
// Check borrower balance
|
||||
acc := suite.getAccount(tc.args.borrower)
|
||||
suite.Require().Equal(tc.args.expectedAccountBalance.Sub(depositedCoins), acc.GetCoins())
|
||||
suite.Require().Equal(tc.args.expectedAccountBalance, acc.GetCoins())
|
||||
|
||||
// Check module account balance
|
||||
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
||||
suite.Require().Equal(tc.args.expectedModAccountBalance.Add(depositedCoins...), mAcc.GetCoins())
|
||||
suite.Require().Equal(tc.args.expectedModAccountBalance, mAcc.GetCoins())
|
||||
|
||||
// Check that borrow struct is in store
|
||||
_, f := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)
|
||||
|
@ -1,6 +1,8 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
supplyExported "github.com/cosmos/cosmos-sdk/x/supply/exported"
|
||||
@ -9,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// Deposit deposit
|
||||
func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin) error {
|
||||
func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error {
|
||||
// Get current stored LTV based on stored borrows/deposits
|
||||
prevLtv, shouldRemoveIndex, err := k.GetStoreLTV(ctx, depositor)
|
||||
if err != nil {
|
||||
@ -18,21 +20,35 @@ func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Co
|
||||
|
||||
k.SyncOutstandingInterest(ctx, depositor)
|
||||
|
||||
err = k.ValidateDeposit(ctx, amount)
|
||||
err = k.ValidateDeposit(ctx, coins)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = k.supplyKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, sdk.NewCoins(amount))
|
||||
err = k.supplyKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, coins)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "insufficient account funds") {
|
||||
accCoins := k.accountKeeper.GetAccount(ctx, depositor).SpendableCoins(ctx.BlockTime())
|
||||
for _, coin := range coins {
|
||||
_, isNegative := accCoins.SafeSub(sdk.NewCoins(coin))
|
||||
if isNegative {
|
||||
return sdkerrors.Wrapf(types.ErrBorrowExceedsAvailableBalance,
|
||||
"insufficient funds: the requested deposit amount of %s exceeds the total available account funds of %s%s",
|
||||
coin, accCoins.AmountOf(coin.Denom), coin.Denom,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom)
|
||||
deposit, found := k.GetDeposit(ctx, depositor)
|
||||
if !found {
|
||||
deposit = types.NewDeposit(depositor, amount)
|
||||
deposit = types.NewDeposit(depositor, coins)
|
||||
} else {
|
||||
deposit.Amount = deposit.Amount.Add(amount)
|
||||
deposit.Amount = deposit.Amount.Add(coins...)
|
||||
}
|
||||
|
||||
k.SetDeposit(ctx, deposit)
|
||||
@ -42,9 +58,8 @@ func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Co
|
||||
ctx.EventManager().EmitEvent(
|
||||
sdk.NewEvent(
|
||||
types.EventTypeHarvestDeposit,
|
||||
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()),
|
||||
sdk.NewAttribute(sdk.AttributeKeyAmount, coins.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositor, deposit.Depositor.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositDenom, deposit.Amount.Denom),
|
||||
),
|
||||
)
|
||||
|
||||
@ -52,28 +67,35 @@ func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Co
|
||||
}
|
||||
|
||||
// ValidateDeposit validates a deposit
|
||||
func (k Keeper) ValidateDeposit(ctx sdk.Context, amount sdk.Coin) error {
|
||||
func (k Keeper) ValidateDeposit(ctx sdk.Context, coins sdk.Coins) error {
|
||||
params := k.GetParams(ctx)
|
||||
for _, lps := range params.LiquidityProviderSchedules {
|
||||
if lps.DepositDenom == amount.Denom {
|
||||
return nil
|
||||
for _, depCoin := range coins {
|
||||
found := false
|
||||
for _, lps := range params.LiquidityProviderSchedules {
|
||||
if lps.DepositDenom == depCoin.Denom {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return sdkerrors.Wrapf(types.ErrInvalidDepositDenom, "liquidity provider denom %s not found", depCoin.Denom)
|
||||
}
|
||||
}
|
||||
return sdkerrors.Wrapf(types.ErrInvalidDepositDenom, "liquidity provider denom %s not found", amount.Denom)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Withdraw returns some or all of a deposit back to original depositor
|
||||
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin) error {
|
||||
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom)
|
||||
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error {
|
||||
deposit, found := k.GetDeposit(ctx, depositor)
|
||||
if !found {
|
||||
return sdkerrors.Wrapf(types.ErrDepositNotFound, "no %s deposit found for %s", amount.Denom, depositor)
|
||||
return sdkerrors.Wrapf(types.ErrDepositNotFound, "no deposit found for %s", depositor)
|
||||
}
|
||||
|
||||
if !deposit.Amount.IsGTE(amount) {
|
||||
return sdkerrors.Wrapf(types.ErrInvalidWithdrawAmount, "%s>%s", amount, deposit.Amount)
|
||||
if !deposit.Amount.IsAllGTE(coins) { // TODO test that this works how I think it does
|
||||
return sdkerrors.Wrapf(types.ErrInvalidWithdrawAmount, "%s>%s", coins, deposit.Amount)
|
||||
}
|
||||
|
||||
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, depositor, sdk.NewCoins(amount))
|
||||
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, depositor, coins)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -81,25 +103,23 @@ func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.C
|
||||
ctx.EventManager().EmitEvent(
|
||||
sdk.NewEvent(
|
||||
types.EventTypeHarvestWithdrawal,
|
||||
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()),
|
||||
sdk.NewAttribute(sdk.AttributeKeyAmount, coins.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositDenom, amount.Denom),
|
||||
),
|
||||
)
|
||||
|
||||
if deposit.Amount.IsEqual(amount) {
|
||||
if deposit.Amount.IsEqual(coins) {
|
||||
ctx.EventManager().EmitEvent(
|
||||
sdk.NewEvent(
|
||||
types.EventTypeDeleteHarvestDeposit,
|
||||
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositDenom, amount.Denom),
|
||||
),
|
||||
)
|
||||
k.DeleteDeposit(ctx, deposit)
|
||||
return nil
|
||||
}
|
||||
|
||||
deposit.Amount = deposit.Amount.Sub(amount)
|
||||
deposit.Amount = deposit.Amount.Sub(coins)
|
||||
k.SetDeposit(ctx, deposit)
|
||||
|
||||
return nil
|
||||
|
@ -1,6 +1,7 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -18,10 +19,11 @@ import (
|
||||
func (suite *KeeperTestSuite) TestDeposit() {
|
||||
type args struct {
|
||||
depositor sdk.AccAddress
|
||||
amount sdk.Coin
|
||||
amount sdk.Coins
|
||||
numberDeposits int
|
||||
expectedAccountBalance sdk.Coins
|
||||
expectedModAccountBalance sdk.Coins
|
||||
expectedDepositCoins sdk.Coins
|
||||
}
|
||||
type errArgs struct {
|
||||
expectPass bool
|
||||
@ -37,10 +39,11 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
"valid",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(100)),
|
||||
amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||
numberDeposits: 1,
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||
expectedDepositCoins: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||
},
|
||||
errArgs{
|
||||
expectPass: true,
|
||||
@ -51,10 +54,11 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
"valid multi deposit",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(100)),
|
||||
amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||
numberDeposits: 2,
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(800)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||||
expectedDepositCoins: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||||
},
|
||||
errArgs{
|
||||
expectPass: true,
|
||||
@ -65,10 +69,11 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
"invalid deposit denom",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
amount: sdk.NewCoin("btcb", sdk.NewInt(100)),
|
||||
amount: sdk.NewCoins(sdk.NewCoin("btcb", sdk.NewInt(100))),
|
||||
numberDeposits: 1,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
expectedDepositCoins: sdk.Coins{},
|
||||
},
|
||||
errArgs{
|
||||
expectPass: false,
|
||||
@ -79,10 +84,11 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
"insufficient funds",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000)),
|
||||
amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000))),
|
||||
numberDeposits: 1,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
expectedDepositCoins: sdk.Coins{},
|
||||
},
|
||||
errArgs{
|
||||
expectPass: false,
|
||||
@ -181,8 +187,9 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
suite.Require().Equal(tc.args.expectedAccountBalance, acc.GetCoins())
|
||||
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
||||
suite.Require().Equal(tc.args.expectedModAccountBalance, mAcc.GetCoins())
|
||||
_, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor, tc.args.amount.Denom)
|
||||
dep, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor)
|
||||
suite.Require().True(f)
|
||||
suite.Require().Equal(tc.args.expectedDepositCoins, dep.Amount)
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
|
||||
@ -194,13 +201,13 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
type args struct {
|
||||
depositor sdk.AccAddress
|
||||
depositAmount sdk.Coin
|
||||
withdrawAmount sdk.Coin
|
||||
depositAmount sdk.Coins
|
||||
withdrawAmount sdk.Coins
|
||||
createDeposit bool
|
||||
expectedAccountBalance sdk.Coins
|
||||
expectedModAccountBalance sdk.Coins
|
||||
depositExists bool
|
||||
finalDepositAmount sdk.Coin
|
||||
finalDepositAmount sdk.Coins
|
||||
}
|
||||
type errArgs struct {
|
||||
expectPass bool
|
||||
@ -216,13 +223,13 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
"valid partial withdraw",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(100)),
|
||||
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||||
withdrawAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||
createDeposit: true,
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||
depositExists: true,
|
||||
finalDepositAmount: sdk.NewCoin("bnb", sdk.NewInt(100)),
|
||||
finalDepositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||
},
|
||||
errArgs{
|
||||
expectPass: true,
|
||||
@ -233,13 +240,13 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
"valid full withdraw",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||||
withdrawAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||||
createDeposit: true,
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||
expectedModAccountBalance: sdk.Coins(nil),
|
||||
depositExists: false,
|
||||
finalDepositAmount: sdk.Coin{},
|
||||
finalDepositAmount: sdk.Coins{},
|
||||
},
|
||||
errArgs{
|
||||
expectPass: true,
|
||||
@ -247,37 +254,37 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
},
|
||||
},
|
||||
{
|
||||
"deposit not found invalid denom",
|
||||
"withdraw invalid, denom not found",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
withdrawAmount: sdk.NewCoin("btcb", sdk.NewInt(200)),
|
||||
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||||
withdrawAmount: sdk.NewCoins(sdk.NewCoin("btcb", sdk.NewInt(200))),
|
||||
createDeposit: true,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
depositExists: false,
|
||||
finalDepositAmount: sdk.Coin{},
|
||||
finalDepositAmount: sdk.Coins{},
|
||||
},
|
||||
errArgs{
|
||||
expectPass: false,
|
||||
contains: "deposit not found",
|
||||
contains: "invalid withdrawal amount",
|
||||
},
|
||||
},
|
||||
{
|
||||
"withdraw exceeds deposit",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(300)),
|
||||
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||||
withdrawAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(300))),
|
||||
createDeposit: true,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
depositExists: false,
|
||||
finalDepositAmount: sdk.Coin{},
|
||||
finalDepositAmount: sdk.Coins{},
|
||||
},
|
||||
errArgs{
|
||||
expectPass: false,
|
||||
contains: "withdrawal amount exceeds deposit amount",
|
||||
contains: "invalid withdrawal amount",
|
||||
},
|
||||
},
|
||||
}
|
||||
@ -325,7 +332,7 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
suite.Require().Equal(tc.args.expectedAccountBalance, acc.GetCoins())
|
||||
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
||||
suite.Require().Equal(tc.args.expectedModAccountBalance, mAcc.GetCoins())
|
||||
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor, tc.args.depositAmount.Denom)
|
||||
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor)
|
||||
if tc.args.depositExists {
|
||||
suite.Require().True(f)
|
||||
suite.Require().Equal(tc.args.finalDepositAmount, testDeposit.Amount)
|
||||
@ -334,6 +341,7 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
}
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
fmt.Printf("%s\n", err.Error())
|
||||
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
|
||||
}
|
||||
})
|
||||
|
@ -706,10 +706,12 @@ func (suite *KeeperTestSuite) TestInterest() {
|
||||
harvest.BeginBlocker(suite.ctx, suite.keeper)
|
||||
|
||||
// Deposit 2x as many coins for each coin we intend to borrow
|
||||
for _, coin := range tc.args.borrowCoins {
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.user, sdk.NewCoin(coin.Denom, coin.Amount.Mul(sdk.NewInt(2))))
|
||||
suite.Require().NoError(err)
|
||||
depositCoins := sdk.NewCoins()
|
||||
for _, borrowCoin := range tc.args.borrowCoins {
|
||||
depositCoins = depositCoins.Add(sdk.NewCoin(borrowCoin.Denom, borrowCoin.Amount.Mul(sdk.NewInt(2))))
|
||||
}
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.user, depositCoins)
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// Borrow coins
|
||||
err = suite.keeper.Borrow(suite.ctx, tc.args.user, tc.args.borrowCoins)
|
||||
|
@ -78,9 +78,9 @@ func (k Keeper) SetPreviousDelegationDistribution(ctx sdk.Context, distTime time
|
||||
}
|
||||
|
||||
// GetDeposit returns a deposit from the store for a particular depositor address, deposit denom
|
||||
func (k Keeper) GetDeposit(ctx sdk.Context, depositor sdk.AccAddress, denom string) (types.Deposit, bool) {
|
||||
func (k Keeper) GetDeposit(ctx sdk.Context, depositor sdk.AccAddress) (types.Deposit, bool) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
||||
bz := store.Get(types.DepositKey(denom, depositor))
|
||||
bz := store.Get(depositor.Bytes())
|
||||
if bz == nil {
|
||||
return types.Deposit{}, false
|
||||
}
|
||||
@ -93,13 +93,13 @@ func (k Keeper) GetDeposit(ctx sdk.Context, depositor sdk.AccAddress, denom stri
|
||||
func (k Keeper) SetDeposit(ctx sdk.Context, deposit types.Deposit) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
||||
bz := k.cdc.MustMarshalBinaryBare(deposit)
|
||||
store.Set(types.DepositKey(deposit.Amount.Denom, deposit.Depositor), bz)
|
||||
store.Set(deposit.Depositor.Bytes(), bz)
|
||||
}
|
||||
|
||||
// DeleteDeposit deletes a deposit from the store
|
||||
func (k Keeper) DeleteDeposit(ctx sdk.Context, deposit types.Deposit) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
||||
store.Delete(types.DepositKey(deposit.Amount.Denom, deposit.Depositor))
|
||||
store.Delete(deposit.Depositor.Bytes())
|
||||
}
|
||||
|
||||
// IterateDeposits iterates over all deposit objects in the store and performs a callback function
|
||||
@ -116,20 +116,6 @@ func (k Keeper) IterateDeposits(ctx sdk.Context, cb func(deposit types.Deposit)
|
||||
}
|
||||
}
|
||||
|
||||
// IterateDepositsByDenom iterates over all deposit objects in the store with the matching deposit denom and performs a callback function
|
||||
func (k Keeper) IterateDepositsByDenom(ctx sdk.Context, depositDenom string, cb func(deposit types.Deposit) (stop bool)) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
||||
iterator := sdk.KVStorePrefixIterator(store, types.DepositTypeIteratorKey(depositDenom))
|
||||
defer iterator.Close()
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
var deposit types.Deposit
|
||||
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &deposit)
|
||||
if cb(deposit) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetClaim returns a claim from the store for a particular claim owner, deposit denom, and claim type
|
||||
func (k Keeper) GetClaim(ctx sdk.Context, owner sdk.AccAddress, depositDenom string, claimType types.ClaimType) (types.Claim, bool) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimsKeyPrefix)
|
||||
|
@ -76,27 +76,27 @@ func (suite *KeeperTestSuite) TestGetSetPreviousDelegatorDistribution() {
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGetSetDeleteDeposit() {
|
||||
dep := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoin("bnb", sdk.NewInt(100)))
|
||||
dep := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))))
|
||||
|
||||
_, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"), "bnb")
|
||||
_, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
|
||||
suite.Require().False(f)
|
||||
|
||||
suite.keeper.SetDeposit(suite.ctx, dep)
|
||||
|
||||
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"), "bnb")
|
||||
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
|
||||
suite.Require().True(f)
|
||||
suite.Require().Equal(dep, testDeposit)
|
||||
|
||||
suite.Require().NotPanics(func() { suite.keeper.DeleteDeposit(suite.ctx, dep) })
|
||||
|
||||
_, f = suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"), "bnb")
|
||||
_, f = suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
|
||||
suite.Require().False(f)
|
||||
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestIterateDeposits() {
|
||||
for i := 0; i < 5; i++ {
|
||||
dep := types.NewDeposit(sdk.AccAddress("test"+fmt.Sprint(i)), sdk.NewCoin("bnb", sdk.NewInt(100)))
|
||||
dep := types.NewDeposit(sdk.AccAddress("test"+fmt.Sprint(i)), sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))))
|
||||
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, dep) })
|
||||
}
|
||||
var deposits []types.Deposit
|
||||
@ -107,41 +107,6 @@ func (suite *KeeperTestSuite) TestIterateDeposits() {
|
||||
suite.Require().Equal(5, len(deposits))
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestIterateDepositsByDenom() {
|
||||
for i := 0; i < 5; i++ {
|
||||
depA := types.NewDeposit(sdk.AccAddress("test"+fmt.Sprint(i)), sdk.NewCoin("bnb", sdk.NewInt(100)))
|
||||
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, depA) })
|
||||
depB := types.NewDeposit(sdk.AccAddress("test"+fmt.Sprint(i)), sdk.NewCoin("bnb", sdk.NewInt(100)))
|
||||
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, depB) })
|
||||
depC := types.NewDeposit(sdk.AccAddress("test"+fmt.Sprint(i)), sdk.NewCoin("btcb", sdk.NewInt(100)))
|
||||
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, depC) })
|
||||
}
|
||||
|
||||
// Check BNB deposits
|
||||
var bnbDeposits []types.Deposit
|
||||
suite.keeper.IterateDepositsByDenom(suite.ctx, "bnb", func(d types.Deposit) bool {
|
||||
bnbDeposits = append(bnbDeposits, d)
|
||||
return false
|
||||
})
|
||||
suite.Require().Equal(5, len(bnbDeposits))
|
||||
|
||||
// Check BTCB deposits
|
||||
var btcbDeposits []types.Deposit
|
||||
suite.keeper.IterateDepositsByDenom(suite.ctx, "btcb", func(d types.Deposit) bool {
|
||||
btcbDeposits = append(btcbDeposits, d)
|
||||
return false
|
||||
})
|
||||
suite.Require().Equal(5, len(btcbDeposits))
|
||||
|
||||
// Fetch all deposits
|
||||
var deposits []types.Deposit
|
||||
suite.keeper.IterateDeposits(suite.ctx, func(d types.Deposit) bool {
|
||||
deposits = append(deposits, d)
|
||||
return false
|
||||
})
|
||||
suite.Require().Equal(len(bnbDeposits)+len(btcbDeposits), len(deposits))
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGetSetDeleteClaim() {
|
||||
claim := types.NewClaim(sdk.AccAddress("test"), "bnb", sdk.NewCoin("hard", sdk.NewInt(100)), "lp")
|
||||
_, f := suite.keeper.GetClaim(suite.ctx, sdk.AccAddress("test"), "bnb", "lp")
|
||||
|
@ -44,10 +44,13 @@ func (k Keeper) AttemptKeeperLiquidation(ctx sdk.Context, keeper sdk.AccAddress,
|
||||
k.UpdateItemInLtvIndex(ctx, prevLtv, shouldInsertIndex, borrower)
|
||||
|
||||
// Fetch deposits and parse coin denoms
|
||||
deposits := k.GetDepositsByUser(ctx, borrower)
|
||||
deposit, found := k.GetDeposit(ctx, borrower)
|
||||
if !found {
|
||||
return false, sdkerrors.Wrapf(types.ErrDepositsNotFound, "no deposits found for %s", borrower)
|
||||
}
|
||||
depositDenoms := []string{}
|
||||
for _, deposit := range deposits {
|
||||
depositDenoms = append(depositDenoms, deposit.Amount.Denom)
|
||||
for _, depCoin := range deposit.Amount {
|
||||
depositDenoms = append(depositDenoms, depCoin.Denom)
|
||||
}
|
||||
|
||||
// Fetch borrow balances and parse coin denoms
|
||||
@ -77,9 +80,9 @@ func (k Keeper) AttemptKeeperLiquidation(ctx sdk.Context, keeper sdk.AccAddress,
|
||||
|
||||
totalBorrowableUSDAmount := sdk.ZeroDec()
|
||||
totalDepositedUSDAmount := sdk.ZeroDec()
|
||||
for _, deposit := range deposits {
|
||||
lData := liqMap[deposit.Amount.Denom]
|
||||
usdValue := sdk.NewDecFromInt(deposit.Amount.Amount).Quo(sdk.NewDecFromInt(lData.conversionFactor)).Mul(lData.price)
|
||||
for _, depCoin := range deposit.Amount {
|
||||
lData := liqMap[depCoin.Denom]
|
||||
usdValue := sdk.NewDecFromInt(depCoin.Amount).Quo(sdk.NewDecFromInt(lData.conversionFactor)).Mul(lData.price)
|
||||
totalDepositedUSDAmount = totalDepositedUSDAmount.Add(usdValue)
|
||||
borrowableUSDAmountForDeposit := usdValue.Mul(lData.ltv)
|
||||
totalBorrowableUSDAmount = totalBorrowableUSDAmount.Add(borrowableUSDAmountForDeposit)
|
||||
@ -97,8 +100,8 @@ func (k Keeper) AttemptKeeperLiquidation(ctx sdk.Context, keeper sdk.AccAddress,
|
||||
return false, sdkerrors.Wrapf(types.ErrBorrowNotLiquidatable, "borrowed %s <= borrowable %s", totalBorrowedUSDAmount, totalBorrowableUSDAmount)
|
||||
}
|
||||
|
||||
// Seize deposits and auciton them off
|
||||
err = k.SeizeDeposits(ctx, keeper, liqMap, deposits, borrows.Amount, depositDenoms, borrowDenoms)
|
||||
// Sending coins to auction module with keeper address getting % of the profits
|
||||
err = k.SeizeDeposits(ctx, keeper, liqMap, deposit, borrows.Amount, depositDenoms, borrowDenoms)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@ -111,23 +114,20 @@ func (k Keeper) AttemptKeeperLiquidation(ctx sdk.Context, keeper sdk.AccAddress,
|
||||
|
||||
borrow, _ := k.GetBorrow(ctx, borrower)
|
||||
k.DeleteBorrow(ctx, borrow)
|
||||
|
||||
for _, oldDeposit := range deposits {
|
||||
k.DeleteDeposit(ctx, oldDeposit)
|
||||
}
|
||||
k.DeleteDeposit(ctx, deposit)
|
||||
|
||||
return true, err
|
||||
}
|
||||
|
||||
// SeizeDeposits seizes a list of deposits and sends them to auction
|
||||
func (k Keeper) SeizeDeposits(ctx sdk.Context, keeper sdk.AccAddress, liqMap map[string]LiqData,
|
||||
deposits []types.Deposit, borrowBalances sdk.Coins, dDenoms, bDenoms []string) error {
|
||||
deposit types.Deposit, borrowBalances sdk.Coins, dDenoms, bDenoms []string) error {
|
||||
|
||||
// Seize % of every deposit and send to the keeper
|
||||
aucDeposits := sdk.Coins{}
|
||||
for _, deposit := range deposits {
|
||||
denom := deposit.Amount.Denom
|
||||
amount := deposit.Amount.Amount
|
||||
for _, depCoin := range deposit.Amount {
|
||||
denom := depCoin.Denom
|
||||
amount := depCoin.Amount
|
||||
mm, _ := k.GetMoneyMarket(ctx, denom)
|
||||
|
||||
// No rewards for anyone if liquidated by LTV index
|
||||
@ -167,7 +167,7 @@ func (k Keeper) SeizeDeposits(ctx sdk.Context, keeper sdk.AccAddress, liqMap map
|
||||
// Loan-to-Value ratio after sending keeper their reward
|
||||
ltv := borrowCoinValues.Sum().Quo(depositCoinValues.Sum())
|
||||
|
||||
err := k.StartAuctions(ctx, deposits[0].Depositor, borrowBalances, aucDeposits, depositCoinValues, borrowCoinValues, ltv, liqMap)
|
||||
err := k.StartAuctions(ctx, deposit.Depositor, borrowBalances, aucDeposits, depositCoinValues, borrowCoinValues, ltv, liqMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -280,10 +280,13 @@ func (k Keeper) StartAuctions(ctx sdk.Context, borrower sdk.AccAddress, borrows,
|
||||
// and does not include any outsanding interest.
|
||||
func (k Keeper) GetStoreLTV(ctx sdk.Context, addr sdk.AccAddress) (sdk.Dec, bool, error) {
|
||||
// Fetch deposits and parse coin denoms
|
||||
deposits := k.GetDepositsByUser(ctx, addr)
|
||||
deposit, found := k.GetDeposit(ctx, addr)
|
||||
if !found {
|
||||
return sdk.ZeroDec(), false, nil
|
||||
}
|
||||
depositDenoms := []string{}
|
||||
for _, deposit := range deposits {
|
||||
depositDenoms = append(depositDenoms, deposit.Amount.Denom)
|
||||
for _, depCoin := range deposit.Amount {
|
||||
depositDenoms = append(depositDenoms, depCoin.Denom)
|
||||
}
|
||||
|
||||
// Fetch borrow balances and parse coin denoms
|
||||
@ -313,10 +316,10 @@ func (k Keeper) GetStoreLTV(ctx sdk.Context, addr sdk.AccAddress) (sdk.Dec, bool
|
||||
|
||||
// Build valuation map to hold deposit coin USD valuations
|
||||
depositCoinValues := types.NewValuationMap()
|
||||
for _, deposit := range deposits {
|
||||
dData := liqMap[deposit.Amount.Denom]
|
||||
dCoinUsdValue := sdk.NewDecFromInt(deposit.Amount.Amount).Quo(sdk.NewDecFromInt(dData.conversionFactor)).Mul(dData.price)
|
||||
depositCoinValues.Increment(deposit.Amount.Denom, dCoinUsdValue)
|
||||
for _, depCoin := range deposit.Amount {
|
||||
dData := liqMap[depCoin.Denom]
|
||||
dCoinUsdValue := sdk.NewDecFromInt(depCoin.Amount).Quo(sdk.NewDecFromInt(dData.conversionFactor)).Mul(dData.price)
|
||||
depositCoinValues.Increment(depCoin.Denom, dCoinUsdValue)
|
||||
}
|
||||
|
||||
// Build valuation map to hold borrow coin USD valuations
|
||||
|
@ -280,10 +280,8 @@ func (suite *KeeperTestSuite) TestIndexLiquidation() {
|
||||
harvest.BeginBlocker(suite.ctx, suite.keeper)
|
||||
|
||||
// Deposit coins
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, coin)
|
||||
suite.Require().NoError(err)
|
||||
}
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// Borrow coins
|
||||
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins)
|
||||
@ -294,10 +292,8 @@ func (suite *KeeperTestSuite) TestIndexLiquidation() {
|
||||
suite.Require().True(foundBorrowBefore)
|
||||
|
||||
// Check that the user's deposits exist before liquidation
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, tc.args.borrower, coin.Denom)
|
||||
suite.Require().True(foundDepositBefore)
|
||||
}
|
||||
_, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, tc.args.borrower)
|
||||
suite.Require().True(foundDepositBefore)
|
||||
|
||||
// Liquidate the borrow by running begin blocker
|
||||
runAtTime := time.Unix(suite.ctx.BlockTime().Unix()+(tc.args.beginBlockerTime), 0)
|
||||
@ -309,10 +305,8 @@ func (suite *KeeperTestSuite) TestIndexLiquidation() {
|
||||
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
|
||||
suite.Require().False(foundBorrowAfter)
|
||||
// Check deposits do not exist after liquidation
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
|
||||
suite.Require().False(foundDepositAfter)
|
||||
}
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
|
||||
suite.Require().False(foundDepositAfter)
|
||||
|
||||
// Check that borrower's balance contains the expected coins
|
||||
accBorrower := suite.getAccountAtCtx(tc.args.borrower, liqCtx)
|
||||
@ -327,10 +321,8 @@ func (suite *KeeperTestSuite) TestIndexLiquidation() {
|
||||
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
|
||||
suite.Require().True(foundBorrowAfter)
|
||||
// Check that the user's deposits exist
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
|
||||
suite.Require().True(foundDepositAfter)
|
||||
}
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
|
||||
suite.Require().True(foundDepositAfter)
|
||||
|
||||
// Check that no auctions have been created
|
||||
auctions := suite.auctionKeeper.GetAllAuctions(liqCtx)
|
||||
@ -637,20 +629,16 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
|
||||
|
||||
// Other borrowers take out positions by depositing and borrowing coins
|
||||
for _, otherBorrower := range tc.args.otherBorrowers {
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
err = suite.keeper.Deposit(suite.ctx, otherBorrower, coin)
|
||||
suite.Require().NoError(err)
|
||||
}
|
||||
err = suite.keeper.Deposit(suite.ctx, otherBorrower, tc.args.depositCoins)
|
||||
suite.Require().NoError(err)
|
||||
|
||||
err = suite.keeper.Borrow(suite.ctx, otherBorrower, tc.args.otherBorrowCoins)
|
||||
suite.Require().NoError(err)
|
||||
}
|
||||
|
||||
// Primary borrower deposits and borrows
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, coin)
|
||||
suite.Require().NoError(err)
|
||||
}
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
|
||||
suite.Require().NoError(err)
|
||||
|
||||
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins)
|
||||
suite.Require().NoError(err)
|
||||
@ -661,20 +649,16 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
|
||||
_, foundBorrowBefore := suite.keeper.GetBorrow(suite.ctx, otherBorrower)
|
||||
suite.Require().True(foundBorrowBefore)
|
||||
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, otherBorrower, coin.Denom)
|
||||
suite.Require().True(foundDepositBefore)
|
||||
}
|
||||
_, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, otherBorrower)
|
||||
suite.Require().True(foundDepositBefore)
|
||||
}
|
||||
|
||||
// Primary borrower
|
||||
_, foundBorrowBefore := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)
|
||||
suite.Require().True(foundBorrowBefore)
|
||||
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, tc.args.borrower, coin.Denom)
|
||||
suite.Require().True(foundDepositBefore)
|
||||
}
|
||||
_, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, tc.args.borrower)
|
||||
suite.Require().True(foundDepositBefore)
|
||||
|
||||
// ----------- Liquidate and check state -----------
|
||||
// Liquidate the borrow by running begin blocker
|
||||
@ -687,10 +671,8 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
|
||||
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
|
||||
suite.Require().False(foundBorrowAfter)
|
||||
// Check deposits do not exist after liquidation
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
|
||||
suite.Require().False(foundDepositAfter)
|
||||
}
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
|
||||
suite.Require().False(foundDepositAfter)
|
||||
|
||||
// Check that borrower's balance contains the expected coins
|
||||
accBorrower := suite.getAccountAtCtx(tc.args.borrower, liqCtx)
|
||||
@ -705,10 +687,8 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
|
||||
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
|
||||
suite.Require().True(foundBorrowAfter)
|
||||
// Check that the user's deposits exist
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
|
||||
suite.Require().True(foundDepositAfter)
|
||||
}
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
|
||||
suite.Require().True(foundDepositAfter)
|
||||
|
||||
if !tc.errArgs.expectLiquidateOtherBorrowers {
|
||||
// Check that no auctions have been created
|
||||
@ -725,10 +705,9 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
|
||||
suite.Require().False(foundBorrowAfter)
|
||||
|
||||
// Check deposits do not exist after liquidation
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, otherBorrower, coin.Denom)
|
||||
suite.Require().False(foundDepositAfter)
|
||||
}
|
||||
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, otherBorrower)
|
||||
suite.Require().False(foundDepositAfter)
|
||||
}
|
||||
|
||||
var expectedLtvIndexItemCount int
|
||||
@ -746,10 +725,9 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
|
||||
suite.Require().True(foundBorrowAfter)
|
||||
|
||||
// Check deposits do not exist after liquidation
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, otherBorrower, coin.Denom)
|
||||
suite.Require().True(foundDepositAfter)
|
||||
}
|
||||
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, otherBorrower)
|
||||
suite.Require().True(foundDepositAfter)
|
||||
}
|
||||
|
||||
var expectedLtvIndexItemCount int
|
||||
@ -817,7 +795,7 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
initialBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
initialKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
depositCoins: []sdk.Coin{sdk.NewCoin("ukava", sdk.NewInt(10*KAVA_CF))},
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(10*KAVA_CF))),
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(8*KAVA_CF))),
|
||||
liquidateAfter: oneMonthInSeconds,
|
||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||
@ -855,7 +833,7 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(1000*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1000*BTCB_CF))),
|
||||
initialBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
initialKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
depositCoins: []sdk.Coin{sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF))}, // $100 * 0.8 = $80 borrowable
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF))), // $100 * 0.8 = $80 borrowable
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(20*KAVA_CF)), sdk.NewCoin("ukava", sdk.NewInt(10*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(2*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(0.2*BTCB_CF))), // $20+$20+$20 = $80 borrowed
|
||||
liquidateAfter: oneMonthInSeconds,
|
||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||
@ -938,8 +916,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(1000*KAVA_CF))),
|
||||
initialBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(100*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(100*BTCB_CF))),
|
||||
initialKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
depositCoins: []sdk.Coin{sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF))}, // $100 + $100 + $100 = $300 * 0.8 = $240 borrowable // $100 * 0.8 = $80 borrowable
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(120*KAVA_CF))), // $240 borrowed
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF))), // $100 + $100 + $100 = $300 * 0.8 = $240 borrowable // $100 * 0.8 = $80 borrowable
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(120*KAVA_CF))), // $240 borrowed
|
||||
liquidateAfter: oneMonthInSeconds,
|
||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(102.5*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(0.5*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(0.05*BTCB_CF))), // 5% of each seized coin + initial balances
|
||||
@ -1007,8 +985,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(1000*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1000*BTCB_CF))),
|
||||
initialBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(100*KAVA_CF))),
|
||||
initialKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
depositCoins: []sdk.Coin{sdk.NewCoin("usdc", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(100*KAVA_CF))}, // $100 + $100 + $100 = $300 * 0.9 = $270 borrowable
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(35*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF))), // $270 borrowed
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(100*KAVA_CF))), // $100 + $100 + $100 = $300 * 0.9 = $270 borrowable
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(35*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF))), // $270 borrowed
|
||||
liquidateAfter: oneMonthInSeconds,
|
||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(5*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(5*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(5*KAVA_CF))), // 5% of each seized coin + initial balances
|
||||
@ -1105,7 +1083,7 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
initialModuleCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("dai", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(1000*KAVA_CF))),
|
||||
initialBorrowerCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("dai", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(1000*KAVA_CF))),
|
||||
initialKeeperCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("dai", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(1000*KAVA_CF))),
|
||||
depositCoins: []sdk.Coin{sdk.NewCoin("dai", sdk.NewInt(350*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(200*KAVA_CF))},
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("dai", sdk.NewInt(350*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(200*KAVA_CF))),
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdt", sdk.NewInt(250*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(245*KAVA_CF))),
|
||||
liquidateAfter: oneMonthInSeconds,
|
||||
auctionSize: sdk.NewInt(KAVA_CF * 100000),
|
||||
@ -1173,8 +1151,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
initialBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
initialKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
|
||||
depositCoins: []sdk.Coin{sdk.NewCoin("ukava", sdk.NewInt(20*KAVA_CF))}, // Deposit 20 KAVA
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(5*KAVA_CF))), // Borrow 5 KAVA
|
||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(20*KAVA_CF))), // Deposit 20 KAVA
|
||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(5*KAVA_CF))), // Borrow 5 KAVA
|
||||
liquidateAfter: oneMonthInSeconds,
|
||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100.5*KAVA_CF))),
|
||||
@ -1360,10 +1338,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
harvest.BeginBlocker(suite.ctx, suite.keeper)
|
||||
|
||||
// Deposit coins
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, coin)
|
||||
suite.Require().NoError(err)
|
||||
}
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// Borrow coins
|
||||
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins)
|
||||
@ -1377,11 +1353,9 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
// Check borrow exists before liquidation
|
||||
_, foundBorrowBefore := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
|
||||
suite.Require().True(foundBorrowBefore)
|
||||
// Check that the user's deposits exist before liquidation
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositBefore := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
|
||||
suite.Require().True(foundDepositBefore)
|
||||
}
|
||||
// Check that the user's deposit exists before liquidation
|
||||
_, foundDepositBefore := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
|
||||
suite.Require().True(foundDepositBefore)
|
||||
|
||||
// Attempt to liquidate
|
||||
liquidated, err := suite.keeper.AttemptKeeperLiquidation(liqCtx, tc.args.keeper, tc.args.borrower)
|
||||
@ -1393,10 +1367,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
|
||||
suite.Require().False(foundBorrowAfter)
|
||||
// Check deposits do not exist after liquidation
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
|
||||
suite.Require().False(foundDepositAfter)
|
||||
}
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
|
||||
suite.Require().False(foundDepositAfter)
|
||||
|
||||
// Check that the keeper's balance increased by reward % of all the borrowed coins
|
||||
accKeeper := suite.getAccountAtCtx(tc.args.keeper, liqCtx)
|
||||
@ -1419,10 +1391,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
||||
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
|
||||
suite.Require().True(foundBorrowAfter)
|
||||
// Check that the user's deposits exist
|
||||
for _, coin := range tc.args.depositCoins {
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
|
||||
suite.Require().True(foundDepositAfter)
|
||||
}
|
||||
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
|
||||
suite.Require().True(foundDepositAfter)
|
||||
|
||||
// Check that no auctions have been created
|
||||
auctions := suite.auctionKeeper.GetAllAuctions(liqCtx)
|
||||
|
@ -90,22 +90,26 @@ func queryGetDeposits(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte,
|
||||
var deposits []types.Deposit
|
||||
switch {
|
||||
case depositDenom && owner:
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner, params.DepositDenom)
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner)
|
||||
if found {
|
||||
deposits = append(deposits, deposit)
|
||||
for _, depCoin := range deposit.Amount {
|
||||
if depCoin.Denom == params.DepositDenom {
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
case depositDenom:
|
||||
k.IterateDepositsByDenom(ctx, params.DepositDenom, func(deposit types.Deposit) (stop bool) {
|
||||
deposits = append(deposits, deposit)
|
||||
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
|
||||
if deposit.Amount.AmountOf(params.DepositDenom).IsPositive() {
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
return false
|
||||
})
|
||||
case owner:
|
||||
schedules := k.GetParams(ctx).LiquidityProviderSchedules
|
||||
for _, lps := range schedules {
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner, lps.DepositDenom)
|
||||
if found {
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner)
|
||||
if found {
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
default:
|
||||
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
|
||||
|
@ -192,12 +192,8 @@ func (suite *KeeperTestSuite) TestRepay() {
|
||||
harvest.BeginBlocker(suite.ctx, suite.keeper)
|
||||
|
||||
// Deposit coins to harvest
|
||||
depositedCoins := sdk.NewCoins()
|
||||
for _, depositCoin := range tc.args.depositCoins {
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, depositCoin)
|
||||
suite.Require().NoError(err)
|
||||
depositedCoins.Add(depositCoin)
|
||||
}
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// Borrow coins from harvest
|
||||
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins)
|
||||
|
@ -43,8 +43,8 @@ func (k Keeper) ApplyDepositRewards(ctx sdk.Context) {
|
||||
continue
|
||||
}
|
||||
rewardsDistributed := sdk.ZeroInt()
|
||||
k.IterateDepositsByDenom(ctx, lps.DepositDenom, func(dep types.Deposit) (stop bool) {
|
||||
rewardsShare := sdk.NewDecFromInt(dep.Amount.Amount).Quo(sdk.NewDecFromInt(totalDeposited))
|
||||
k.IterateDeposits(ctx, func(dep types.Deposit) (stop bool) {
|
||||
rewardsShare := sdk.NewDecFromInt(dep.Amount.AmountOf(lps.DepositDenom)).Quo(sdk.NewDecFromInt(totalDeposited))
|
||||
if rewardsShare.IsZero() {
|
||||
return false
|
||||
}
|
||||
@ -52,7 +52,7 @@ func (k Keeper) ApplyDepositRewards(ctx sdk.Context) {
|
||||
if rewardsEarned.IsZero() {
|
||||
return false
|
||||
}
|
||||
k.AddToClaim(ctx, dep.Depositor, dep.Amount.Denom, types.LP, sdk.NewCoin(lps.RewardsPerSecond.Denom, rewardsEarned))
|
||||
k.AddToClaim(ctx, dep.Depositor, lps.DepositDenom, types.LP, sdk.NewCoin(lps.RewardsPerSecond.Denom, rewardsEarned))
|
||||
rewardsDistributed = rewardsDistributed.Add(rewardsEarned)
|
||||
return false
|
||||
})
|
||||
|
@ -21,7 +21,7 @@ func (suite *KeeperTestSuite) TestApplyDepositRewards() {
|
||||
type args struct {
|
||||
depositor sdk.AccAddress
|
||||
denom string
|
||||
depositAmount sdk.Coin
|
||||
depositAmount sdk.Coins
|
||||
totalDeposits sdk.Coin
|
||||
rewardRate sdk.Coin
|
||||
claimType types.ClaimType
|
||||
@ -45,7 +45,7 @@ func (suite *KeeperTestSuite) TestApplyDepositRewards() {
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
denom: "bnb",
|
||||
rewardRate: c("hard", 500),
|
||||
depositAmount: c("bnb", 100),
|
||||
depositAmount: cs(c("bnb", 100)),
|
||||
totalDeposits: c("bnb", 1000),
|
||||
claimType: types.LP,
|
||||
previousBlockTime: time.Date(2020, 11, 1, 13, 59, 50, 0, time.UTC),
|
||||
|
@ -27,7 +27,7 @@ func TestDecodeDistributionStore(t *testing.T) {
|
||||
cdc := makeTestCodec()
|
||||
|
||||
prevBlockTime := time.Now().UTC()
|
||||
deposit := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoin("bnb", sdk.NewInt(1)))
|
||||
deposit := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1))))
|
||||
claim := types.NewClaim(sdk.AccAddress("test"), "bnb", sdk.NewCoin("hard", sdk.NewInt(100)), "stake")
|
||||
|
||||
kvPairs := kv.Pairs{
|
||||
|
@ -7,11 +7,11 @@ import (
|
||||
// Deposit defines an amount of coins deposited into a harvest module account
|
||||
type Deposit struct {
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
Amount sdk.Coins `json:"amount" yaml:"amount"`
|
||||
}
|
||||
|
||||
// NewDeposit returns a new deposit
|
||||
func NewDeposit(depositor sdk.AccAddress, amount sdk.Coin) Deposit {
|
||||
func NewDeposit(depositor sdk.AccAddress, amount sdk.Coins) Deposit {
|
||||
return Deposit{
|
||||
Depositor: depositor,
|
||||
Amount: amount,
|
||||
|
@ -12,7 +12,7 @@ var (
|
||||
// ErrDepositNotFound error for deposit not found
|
||||
ErrDepositNotFound = sdkerrors.Register(ModuleName, 3, "deposit not found")
|
||||
// ErrInvalidWithdrawAmount error for invalid withdrawal amount
|
||||
ErrInvalidWithdrawAmount = sdkerrors.Register(ModuleName, 4, "withdrawal amount exceeds deposit amount")
|
||||
ErrInvalidWithdrawAmount = sdkerrors.Register(ModuleName, 4, "invalid withdrawal amount")
|
||||
// ErrInvalidClaimType error for invalid claim type
|
||||
ErrInvalidClaimType = sdkerrors.Register(ModuleName, 5, "invalid claim type")
|
||||
// ErrClaimNotFound error for claim not found
|
||||
|
@ -48,11 +48,6 @@ var (
|
||||
sep = []byte(":")
|
||||
)
|
||||
|
||||
// DepositKey key of a specific deposit in the store
|
||||
func DepositKey(denom string, depositor sdk.AccAddress) []byte {
|
||||
return createKey([]byte(denom), sep, depositor)
|
||||
}
|
||||
|
||||
// DepositTypeIteratorKey returns an interator prefix for interating over deposits by deposit denom
|
||||
func DepositTypeIteratorKey(denom string) []byte {
|
||||
return createKey([]byte(denom))
|
||||
|
@ -58,11 +58,11 @@ var (
|
||||
// MsgDeposit deposit collateral to the harvest module.
|
||||
type MsgDeposit struct {
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
Amount sdk.Coins `json:"amount" yaml:"amount"`
|
||||
}
|
||||
|
||||
// NewMsgDeposit returns a new MsgDeposit
|
||||
func NewMsgDeposit(depositor sdk.AccAddress, amount sdk.Coin) MsgDeposit {
|
||||
func NewMsgDeposit(depositor sdk.AccAddress, amount sdk.Coins) MsgDeposit {
|
||||
return MsgDeposit{
|
||||
Depositor: depositor,
|
||||
Amount: amount,
|
||||
@ -108,11 +108,11 @@ func (msg MsgDeposit) String() string {
|
||||
// MsgWithdraw withdraw from the harvest module.
|
||||
type MsgWithdraw struct {
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
Amount sdk.Coins `json:"amount" yaml:"amount"`
|
||||
}
|
||||
|
||||
// NewMsgWithdraw returns a new MsgWithdraw
|
||||
func NewMsgWithdraw(depositor sdk.AccAddress, amount sdk.Coin) MsgWithdraw {
|
||||
func NewMsgWithdraw(depositor sdk.AccAddress, amount sdk.Coins) MsgWithdraw {
|
||||
return MsgWithdraw{
|
||||
Depositor: depositor,
|
||||
Amount: amount,
|
||||
|
@ -18,7 +18,7 @@ type MsgTestSuite struct {
|
||||
func (suite *MsgTestSuite) TestMsgDeposit() {
|
||||
type args struct {
|
||||
depositor sdk.AccAddress
|
||||
amount sdk.Coin
|
||||
amount sdk.Coins
|
||||
}
|
||||
addrs := []sdk.AccAddress{
|
||||
sdk.AccAddress("test1"),
|
||||
@ -34,7 +34,7 @@ func (suite *MsgTestSuite) TestMsgDeposit() {
|
||||
name: "valid",
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000000))),
|
||||
},
|
||||
expectPass: true,
|
||||
expectedErr: "",
|
||||
@ -43,7 +43,7 @@ func (suite *MsgTestSuite) TestMsgDeposit() {
|
||||
name: "valid2",
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000000))),
|
||||
},
|
||||
expectPass: true,
|
||||
expectedErr: "",
|
||||
@ -66,7 +66,7 @@ func (suite *MsgTestSuite) TestMsgDeposit() {
|
||||
func (suite *MsgTestSuite) TestMsgWithdraw() {
|
||||
type args struct {
|
||||
depositor sdk.AccAddress
|
||||
amount sdk.Coin
|
||||
amount sdk.Coins
|
||||
}
|
||||
addrs := []sdk.AccAddress{
|
||||
sdk.AccAddress("test1"),
|
||||
@ -82,7 +82,7 @@ func (suite *MsgTestSuite) TestMsgWithdraw() {
|
||||
name: "valid",
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000000))),
|
||||
},
|
||||
expectPass: true,
|
||||
expectedErr: "",
|
||||
@ -91,7 +91,7 @@ func (suite *MsgTestSuite) TestMsgWithdraw() {
|
||||
name: "valid2",
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000000))),
|
||||
},
|
||||
expectPass: true,
|
||||
expectedErr: "",
|
||||
|
Loading…
Reference in New Issue
Block a user