[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:
Kevin Davis 2020-12-18 09:05:21 -07:00 committed by GitHub
parent 83a5f51c11
commit 06fd215de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 220 additions and 277 deletions

View File

@ -58,7 +58,6 @@ var (
ClaimKey = types.ClaimKey ClaimKey = types.ClaimKey
DefaultGenesisState = types.DefaultGenesisState DefaultGenesisState = types.DefaultGenesisState
DefaultParams = types.DefaultParams DefaultParams = types.DefaultParams
DepositKey = types.DepositKey
DepositTypeIteratorKey = types.DepositTypeIteratorKey DepositTypeIteratorKey = types.DepositTypeIteratorKey
GetTotalVestingPeriodLength = types.GetTotalVestingPeriodLength GetTotalVestingPeriodLength = types.GetTotalVestingPeriodLength
NewClaim = types.NewClaim NewClaim = types.NewClaim

View File

@ -52,7 +52,7 @@ func getCmdDeposit(cdc *codec.Codec) *cobra.Command {
inBuf := bufio.NewReader(cmd.InOrStdin()) inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
amount, err := sdk.ParseCoin(args[0]) amount, err := sdk.ParseCoins(args[0])
if err != nil { if err != nil {
return err return err
} }
@ -77,7 +77,7 @@ func getCmdWithdraw(cdc *codec.Codec) *cobra.Command {
inBuf := bufio.NewReader(cmd.InOrStdin()) inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
amount, err := sdk.ParseCoin(args[0]) amount, err := sdk.ParseCoins(args[0])
if err != nil { if err != nil {
return err return err
} }

View File

@ -27,7 +27,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
type PostCreateDepositReq struct { type PostCreateDepositReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
From sdk.AccAddress `json:"from" yaml:"from"` 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"` DepositType string `json:"deposit_type" yaml:"deposit_type"`
} }
@ -35,7 +35,7 @@ type PostCreateDepositReq struct {
type PostCreateWithdrawReq struct { type PostCreateWithdrawReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
From sdk.AccAddress `json:"from" yaml:"from"` 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"` DepositType string `json:"deposit_type" yaml:"deposit_type"`
} }

View File

@ -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 // Get the total borrowable USD amount at user's existing deposits
deposits := k.GetDepositsByUser(ctx, borrower) deposit, found := k.GetDeposit(ctx, borrower)
if len(deposits) == 0 { if !found {
return sdkerrors.Wrapf(types.ErrDepositsNotFound, "no deposits found for %s", borrower) return sdkerrors.Wrapf(types.ErrDepositsNotFound, "no deposits found for %s", borrower)
} }
totalBorrowableAmount := sdk.ZeroDec() totalBorrowableAmount := sdk.ZeroDec()
for _, deposit := range deposits { for _, depCoin := range deposit.Amount {
moneyMarket, ok := moneyMarketCache[deposit.Amount.Denom] moneyMarket, ok := moneyMarketCache[depCoin.Denom]
// Fetch money market and store in local cache // Fetch money market and store in local cache
if !ok { if !ok {
newMoneyMarket, found := k.GetMoneyMarketParam(ctx, deposit.Amount.Denom) newMoneyMarket, found := k.GetMoneyMarketParam(ctx, depCoin.Denom)
if !found { 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 moneyMarket = newMoneyMarket
} }
@ -202,7 +202,7 @@ func (k Keeper) ValidateBorrow(ctx sdk.Context, borrower sdk.AccAddress, amount
if err != nil { if err != nil {
sdkerrors.Wrapf(types.ErrPriceNotFound, "no price found for market %s", moneyMarket.SpotMarketID) 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) borrowableAmountForDeposit := depositUSDValue.Mul(moneyMarket.BorrowLimit.LoanToValue)
totalBorrowableAmount = totalBorrowableAmount.Add(borrowableAmountForDeposit) totalBorrowableAmount = totalBorrowableAmount.Add(borrowableAmountForDeposit)
} }

View File

@ -104,7 +104,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
priceBNB: sdk.MustNewDecFromStr("0.00"), priceBNB: sdk.MustNewDecFromStr("0.00"),
loanToValueBNB: sdk.MustNewDecFromStr("0.01"), loanToValueBNB: sdk.MustNewDecFromStr("0.01"),
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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))), 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))), 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))), 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"), priceBNB: sdk.MustNewDecFromStr("0.00"),
loanToValueBNB: sdk.MustNewDecFromStr("0.01"), loanToValueBNB: sdk.MustNewDecFromStr("0.01"),
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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))), borrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(181*USDX_CF))),
expectedAccountBalance: sdk.NewCoins(), expectedAccountBalance: sdk.NewCoins(),
expectedModAccountBalance: sdk.NewCoins(), expectedModAccountBalance: sdk.NewCoins(),
@ -146,7 +146,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
priceBNB: sdk.MustNewDecFromStr("5.00"), priceBNB: sdk.MustNewDecFromStr("5.00"),
loanToValueBNB: sdk.MustNewDecFromStr("0.8"), loanToValueBNB: sdk.MustNewDecFromStr("0.8"),
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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))), 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))), 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))), 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"), priceBNB: sdk.MustNewDecFromStr("5.00"),
loanToValueBNB: sdk.MustNewDecFromStr("0.8"), loanToValueBNB: sdk.MustNewDecFromStr("0.8"),
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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))), 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))), borrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(1*USDX_CF))),
expectedAccountBalance: sdk.NewCoins(), expectedAccountBalance: sdk.NewCoins(),
@ -190,7 +190,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
priceBNB: sdk.MustNewDecFromStr("0.00"), priceBNB: sdk.MustNewDecFromStr("0.00"),
loanToValueBNB: sdk.MustNewDecFromStr("0.01"), loanToValueBNB: sdk.MustNewDecFromStr("0.01"),
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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(), previousBorrowCoins: sdk.NewCoins(),
borrowCoins: sdk.NewCoins(sdk.NewCoin("xyz", sdk.NewInt(1))), 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))), 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"), priceBNB: sdk.MustNewDecFromStr("0.00"),
loanToValueBNB: sdk.MustNewDecFromStr("0.01"), loanToValueBNB: sdk.MustNewDecFromStr("0.01"),
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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(), previousBorrowCoins: sdk.NewCoins(),
borrowCoins: sdk.NewCoins(sdk.NewCoin("busd", sdk.NewInt(101*BUSD_CF))), borrowCoins: sdk.NewCoins(sdk.NewCoin("busd", sdk.NewInt(101*BUSD_CF))),
expectedAccountBalance: sdk.NewCoins(), expectedAccountBalance: sdk.NewCoins(),
@ -234,7 +234,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
priceBNB: sdk.MustNewDecFromStr("0.00"), priceBNB: sdk.MustNewDecFromStr("0.00"),
loanToValueBNB: sdk.MustNewDecFromStr("0.01"), loanToValueBNB: sdk.MustNewDecFromStr("0.01"),
borrower: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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(), previousBorrowCoins: sdk.NewCoins(),
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(25*USDX_CF))), borrowCoins: sdk.NewCoins(sdk.NewCoin("usdx", sdk.NewInt(25*USDX_CF))),
expectedAccountBalance: sdk.NewCoins(), expectedAccountBalance: sdk.NewCoins(),
@ -353,13 +353,8 @@ func (suite *KeeperTestSuite) TestBorrow() {
// Run BeginBlocker once to transition MoneyMarkets // Run BeginBlocker once to transition MoneyMarkets
harvest.BeginBlocker(suite.ctx, suite.keeper) harvest.BeginBlocker(suite.ctx, suite.keeper)
// Deposit coins to harvest err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
depositedCoins := sdk.NewCoins()
for _, depositCoin := range tc.args.depositCoins {
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, depositCoin)
suite.Require().NoError(err) suite.Require().NoError(err)
depositedCoins.Add(depositCoin)
}
// Execute user's previous borrows // Execute user's previous borrows
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.previousBorrowCoins) err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.previousBorrowCoins)
@ -377,11 +372,11 @@ func (suite *KeeperTestSuite) TestBorrow() {
// Check borrower balance // Check borrower balance
acc := suite.getAccount(tc.args.borrower) 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 // Check module account balance
mAcc := suite.getModuleAccount(types.ModuleAccountName) 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 // Check that borrow struct is in store
_, f := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower) _, f := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)

View File

@ -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"
supplyExported "github.com/cosmos/cosmos-sdk/x/supply/exported" supplyExported "github.com/cosmos/cosmos-sdk/x/supply/exported"
@ -9,7 +11,7 @@ import (
) )
// Deposit deposit // 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 // Get current stored LTV based on stored borrows/deposits
prevLtv, shouldRemoveIndex, err := k.GetStoreLTV(ctx, depositor) prevLtv, shouldRemoveIndex, err := k.GetStoreLTV(ctx, depositor)
if err != nil { if err != nil {
@ -18,21 +20,35 @@ func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Co
k.SyncOutstandingInterest(ctx, depositor) k.SyncOutstandingInterest(ctx, depositor)
err = k.ValidateDeposit(ctx, amount) err = k.ValidateDeposit(ctx, coins)
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom) deposit, found := k.GetDeposit(ctx, depositor)
if !found { if !found {
deposit = types.NewDeposit(depositor, amount) deposit = types.NewDeposit(depositor, coins)
} else { } else {
deposit.Amount = deposit.Amount.Add(amount) deposit.Amount = deposit.Amount.Add(coins...)
} }
k.SetDeposit(ctx, deposit) k.SetDeposit(ctx, deposit)
@ -42,9 +58,8 @@ func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Co
ctx.EventManager().EmitEvent( ctx.EventManager().EmitEvent(
sdk.NewEvent( sdk.NewEvent(
types.EventTypeHarvestDeposit, types.EventTypeHarvestDeposit,
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()), sdk.NewAttribute(sdk.AttributeKeyAmount, coins.String()),
sdk.NewAttribute(types.AttributeKeyDepositor, deposit.Depositor.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 // 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) params := k.GetParams(ctx)
for _, depCoin := range coins {
found := false
for _, lps := range params.LiquidityProviderSchedules { for _, lps := range params.LiquidityProviderSchedules {
if lps.DepositDenom == amount.Denom { if lps.DepositDenom == depCoin.Denom {
found = true
}
}
if !found {
return sdkerrors.Wrapf(types.ErrInvalidDepositDenom, "liquidity provider denom %s not found", depCoin.Denom)
}
}
return nil return nil
}
}
return sdkerrors.Wrapf(types.ErrInvalidDepositDenom, "liquidity provider denom %s not found", amount.Denom)
} }
// Withdraw returns some or all of a deposit back to original depositor // 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 { func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error {
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom) deposit, found := k.GetDeposit(ctx, depositor)
if !found { 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) { if !deposit.Amount.IsAllGTE(coins) { // TODO test that this works how I think it does
return sdkerrors.Wrapf(types.ErrInvalidWithdrawAmount, "%s>%s", amount, deposit.Amount) 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 { if err != nil {
return err return err
} }
@ -81,25 +103,23 @@ func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.C
ctx.EventManager().EmitEvent( ctx.EventManager().EmitEvent(
sdk.NewEvent( sdk.NewEvent(
types.EventTypeHarvestWithdrawal, types.EventTypeHarvestWithdrawal,
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()), sdk.NewAttribute(sdk.AttributeKeyAmount, coins.String()),
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.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( ctx.EventManager().EmitEvent(
sdk.NewEvent( sdk.NewEvent(
types.EventTypeDeleteHarvestDeposit, types.EventTypeDeleteHarvestDeposit,
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()), sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
sdk.NewAttribute(types.AttributeKeyDepositDenom, amount.Denom),
), ),
) )
k.DeleteDeposit(ctx, deposit) k.DeleteDeposit(ctx, deposit)
return nil return nil
} }
deposit.Amount = deposit.Amount.Sub(amount) deposit.Amount = deposit.Amount.Sub(coins)
k.SetDeposit(ctx, deposit) k.SetDeposit(ctx, deposit)
return nil return nil

View File

@ -1,6 +1,7 @@
package keeper_test package keeper_test
import ( import (
"fmt"
"strings" "strings"
"time" "time"
@ -18,10 +19,11 @@ import (
func (suite *KeeperTestSuite) TestDeposit() { func (suite *KeeperTestSuite) TestDeposit() {
type args struct { type args struct {
depositor sdk.AccAddress depositor sdk.AccAddress
amount sdk.Coin amount sdk.Coins
numberDeposits int numberDeposits int
expectedAccountBalance sdk.Coins expectedAccountBalance sdk.Coins
expectedModAccountBalance sdk.Coins expectedModAccountBalance sdk.Coins
expectedDepositCoins sdk.Coins
} }
type errArgs struct { type errArgs struct {
expectPass bool expectPass bool
@ -37,10 +39,11 @@ func (suite *KeeperTestSuite) TestDeposit() {
"valid", "valid",
args{ args{
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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, numberDeposits: 1,
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))), expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))), expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
expectedDepositCoins: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
}, },
errArgs{ errArgs{
expectPass: true, expectPass: true,
@ -51,10 +54,11 @@ func (suite *KeeperTestSuite) TestDeposit() {
"valid multi deposit", "valid multi deposit",
args{ args{
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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, numberDeposits: 2,
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(800)), sdk.NewCoin("btcb", sdk.NewInt(1000))), expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(800)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))), expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
expectedDepositCoins: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
}, },
errArgs{ errArgs{
expectPass: true, expectPass: true,
@ -65,10 +69,11 @@ func (suite *KeeperTestSuite) TestDeposit() {
"invalid deposit denom", "invalid deposit denom",
args{ args{
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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, numberDeposits: 1,
expectedAccountBalance: sdk.Coins{}, expectedAccountBalance: sdk.Coins{},
expectedModAccountBalance: sdk.Coins{}, expectedModAccountBalance: sdk.Coins{},
expectedDepositCoins: sdk.Coins{},
}, },
errArgs{ errArgs{
expectPass: false, expectPass: false,
@ -79,10 +84,11 @@ func (suite *KeeperTestSuite) TestDeposit() {
"insufficient funds", "insufficient funds",
args{ args{
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))), 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, numberDeposits: 1,
expectedAccountBalance: sdk.Coins{}, expectedAccountBalance: sdk.Coins{},
expectedModAccountBalance: sdk.Coins{}, expectedModAccountBalance: sdk.Coins{},
expectedDepositCoins: sdk.Coins{},
}, },
errArgs{ errArgs{
expectPass: false, expectPass: false,
@ -181,8 +187,9 @@ func (suite *KeeperTestSuite) TestDeposit() {
suite.Require().Equal(tc.args.expectedAccountBalance, acc.GetCoins()) suite.Require().Equal(tc.args.expectedAccountBalance, 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, 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().True(f)
suite.Require().Equal(tc.args.expectedDepositCoins, dep.Amount)
} else { } else {
suite.Require().Error(err) suite.Require().Error(err)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains)) suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
@ -194,13 +201,13 @@ func (suite *KeeperTestSuite) TestDeposit() {
func (suite *KeeperTestSuite) TestWithdraw() { func (suite *KeeperTestSuite) TestWithdraw() {
type args struct { type args struct {
depositor sdk.AccAddress depositor sdk.AccAddress
depositAmount sdk.Coin depositAmount sdk.Coins
withdrawAmount sdk.Coin withdrawAmount sdk.Coins
createDeposit bool createDeposit bool
expectedAccountBalance sdk.Coins expectedAccountBalance sdk.Coins
expectedModAccountBalance sdk.Coins expectedModAccountBalance sdk.Coins
depositExists bool depositExists bool
finalDepositAmount sdk.Coin finalDepositAmount sdk.Coins
} }
type errArgs struct { type errArgs struct {
expectPass bool expectPass bool
@ -216,13 +223,13 @@ func (suite *KeeperTestSuite) TestWithdraw() {
"valid partial withdraw", "valid partial withdraw",
args{ args{
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))), depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)), depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(100)), withdrawAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
createDeposit: true, createDeposit: true,
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))), expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))), expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
depositExists: true, depositExists: true,
finalDepositAmount: sdk.NewCoin("bnb", sdk.NewInt(100)), finalDepositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
}, },
errArgs{ errArgs{
expectPass: true, expectPass: true,
@ -233,13 +240,13 @@ func (suite *KeeperTestSuite) TestWithdraw() {
"valid full withdraw", "valid full withdraw",
args{ args{
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))), depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)), depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(200)), withdrawAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
createDeposit: true, createDeposit: true,
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))), expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
expectedModAccountBalance: sdk.Coins(nil), expectedModAccountBalance: sdk.Coins(nil),
depositExists: false, depositExists: false,
finalDepositAmount: sdk.Coin{}, finalDepositAmount: sdk.Coins{},
}, },
errArgs{ errArgs{
expectPass: true, expectPass: true,
@ -247,37 +254,37 @@ func (suite *KeeperTestSuite) TestWithdraw() {
}, },
}, },
{ {
"deposit not found invalid denom", "withdraw invalid, denom not found",
args{ args{
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))), depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)), depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
withdrawAmount: sdk.NewCoin("btcb", sdk.NewInt(200)), withdrawAmount: sdk.NewCoins(sdk.NewCoin("btcb", sdk.NewInt(200))),
createDeposit: true, createDeposit: true,
expectedAccountBalance: sdk.Coins{}, expectedAccountBalance: sdk.Coins{},
expectedModAccountBalance: sdk.Coins{}, expectedModAccountBalance: sdk.Coins{},
depositExists: false, depositExists: false,
finalDepositAmount: sdk.Coin{}, finalDepositAmount: sdk.Coins{},
}, },
errArgs{ errArgs{
expectPass: false, expectPass: false,
contains: "deposit not found", contains: "invalid withdrawal amount",
}, },
}, },
{ {
"withdraw exceeds deposit", "withdraw exceeds deposit",
args{ args{
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))), depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)), depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(300)), withdrawAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(300))),
createDeposit: true, createDeposit: true,
expectedAccountBalance: sdk.Coins{}, expectedAccountBalance: sdk.Coins{},
expectedModAccountBalance: sdk.Coins{}, expectedModAccountBalance: sdk.Coins{},
depositExists: false, depositExists: false,
finalDepositAmount: sdk.Coin{}, finalDepositAmount: sdk.Coins{},
}, },
errArgs{ errArgs{
expectPass: false, 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()) suite.Require().Equal(tc.args.expectedAccountBalance, 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, 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 { if tc.args.depositExists {
suite.Require().True(f) suite.Require().True(f)
suite.Require().Equal(tc.args.finalDepositAmount, testDeposit.Amount) suite.Require().Equal(tc.args.finalDepositAmount, testDeposit.Amount)
@ -334,6 +341,7 @@ func (suite *KeeperTestSuite) TestWithdraw() {
} }
} else { } else {
suite.Require().Error(err) suite.Require().Error(err)
fmt.Printf("%s\n", err.Error())
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains)) suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
} }
}) })

View File

@ -706,10 +706,12 @@ func (suite *KeeperTestSuite) TestInterest() {
harvest.BeginBlocker(suite.ctx, suite.keeper) harvest.BeginBlocker(suite.ctx, suite.keeper)
// Deposit 2x as many coins for each coin we intend to borrow // Deposit 2x as many coins for each coin we intend to borrow
for _, coin := range tc.args.borrowCoins { depositCoins := sdk.NewCoins()
err = suite.keeper.Deposit(suite.ctx, tc.args.user, sdk.NewCoin(coin.Denom, coin.Amount.Mul(sdk.NewInt(2)))) for _, borrowCoin := range tc.args.borrowCoins {
suite.Require().NoError(err) 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 // Borrow coins
err = suite.keeper.Borrow(suite.ctx, tc.args.user, tc.args.borrowCoins) err = suite.keeper.Borrow(suite.ctx, tc.args.user, tc.args.borrowCoins)

View File

@ -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 // 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) store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
bz := store.Get(types.DepositKey(denom, depositor)) bz := store.Get(depositor.Bytes())
if bz == nil { if bz == nil {
return types.Deposit{}, false 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) { func (k Keeper) SetDeposit(ctx sdk.Context, deposit types.Deposit) {
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix) store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
bz := k.cdc.MustMarshalBinaryBare(deposit) 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 // DeleteDeposit deletes a deposit from the store
func (k Keeper) DeleteDeposit(ctx sdk.Context, deposit types.Deposit) { func (k Keeper) DeleteDeposit(ctx sdk.Context, deposit types.Deposit) {
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix) 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 // 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 // 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) { 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) store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimsKeyPrefix)

View File

@ -76,27 +76,27 @@ func (suite *KeeperTestSuite) TestGetSetPreviousDelegatorDistribution() {
} }
func (suite *KeeperTestSuite) TestGetSetDeleteDeposit() { 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.Require().False(f)
suite.keeper.SetDeposit(suite.ctx, dep) 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().True(f)
suite.Require().Equal(dep, testDeposit) suite.Require().Equal(dep, testDeposit)
suite.Require().NotPanics(func() { suite.keeper.DeleteDeposit(suite.ctx, dep) }) 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) suite.Require().False(f)
} }
func (suite *KeeperTestSuite) TestIterateDeposits() { func (suite *KeeperTestSuite) TestIterateDeposits() {
for i := 0; i < 5; i++ { 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) }) suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, dep) })
} }
var deposits []types.Deposit var deposits []types.Deposit
@ -107,41 +107,6 @@ func (suite *KeeperTestSuite) TestIterateDeposits() {
suite.Require().Equal(5, len(deposits)) 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() { func (suite *KeeperTestSuite) TestGetSetDeleteClaim() {
claim := types.NewClaim(sdk.AccAddress("test"), "bnb", sdk.NewCoin("hard", sdk.NewInt(100)), "lp") 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") _, f := suite.keeper.GetClaim(suite.ctx, sdk.AccAddress("test"), "bnb", "lp")

View File

@ -44,10 +44,13 @@ func (k Keeper) AttemptKeeperLiquidation(ctx sdk.Context, keeper sdk.AccAddress,
k.UpdateItemInLtvIndex(ctx, prevLtv, shouldInsertIndex, borrower) k.UpdateItemInLtvIndex(ctx, prevLtv, shouldInsertIndex, borrower)
// Fetch deposits and parse coin denoms // 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{} depositDenoms := []string{}
for _, deposit := range deposits { for _, depCoin := range deposit.Amount {
depositDenoms = append(depositDenoms, deposit.Amount.Denom) depositDenoms = append(depositDenoms, depCoin.Denom)
} }
// Fetch borrow balances and parse coin denoms // Fetch borrow balances and parse coin denoms
@ -77,9 +80,9 @@ func (k Keeper) AttemptKeeperLiquidation(ctx sdk.Context, keeper sdk.AccAddress,
totalBorrowableUSDAmount := sdk.ZeroDec() totalBorrowableUSDAmount := sdk.ZeroDec()
totalDepositedUSDAmount := sdk.ZeroDec() totalDepositedUSDAmount := sdk.ZeroDec()
for _, deposit := range deposits { for _, depCoin := range deposit.Amount {
lData := liqMap[deposit.Amount.Denom] lData := liqMap[depCoin.Denom]
usdValue := sdk.NewDecFromInt(deposit.Amount.Amount).Quo(sdk.NewDecFromInt(lData.conversionFactor)).Mul(lData.price) usdValue := sdk.NewDecFromInt(depCoin.Amount).Quo(sdk.NewDecFromInt(lData.conversionFactor)).Mul(lData.price)
totalDepositedUSDAmount = totalDepositedUSDAmount.Add(usdValue) totalDepositedUSDAmount = totalDepositedUSDAmount.Add(usdValue)
borrowableUSDAmountForDeposit := usdValue.Mul(lData.ltv) borrowableUSDAmountForDeposit := usdValue.Mul(lData.ltv)
totalBorrowableUSDAmount = totalBorrowableUSDAmount.Add(borrowableUSDAmountForDeposit) 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) return false, sdkerrors.Wrapf(types.ErrBorrowNotLiquidatable, "borrowed %s <= borrowable %s", totalBorrowedUSDAmount, totalBorrowableUSDAmount)
} }
// Seize deposits and auciton them off // Sending coins to auction module with keeper address getting % of the profits
err = k.SeizeDeposits(ctx, keeper, liqMap, deposits, borrows.Amount, depositDenoms, borrowDenoms) err = k.SeizeDeposits(ctx, keeper, liqMap, deposit, borrows.Amount, depositDenoms, borrowDenoms)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -111,23 +114,20 @@ func (k Keeper) AttemptKeeperLiquidation(ctx sdk.Context, keeper sdk.AccAddress,
borrow, _ := k.GetBorrow(ctx, borrower) borrow, _ := k.GetBorrow(ctx, borrower)
k.DeleteBorrow(ctx, borrow) k.DeleteBorrow(ctx, borrow)
k.DeleteDeposit(ctx, deposit)
for _, oldDeposit := range deposits {
k.DeleteDeposit(ctx, oldDeposit)
}
return true, err return true, err
} }
// SeizeDeposits seizes a list of deposits and sends them to auction // 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, 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 // Seize % of every deposit and send to the keeper
aucDeposits := sdk.Coins{} aucDeposits := sdk.Coins{}
for _, deposit := range deposits { for _, depCoin := range deposit.Amount {
denom := deposit.Amount.Denom denom := depCoin.Denom
amount := deposit.Amount.Amount amount := depCoin.Amount
mm, _ := k.GetMoneyMarket(ctx, denom) mm, _ := k.GetMoneyMarket(ctx, denom)
// No rewards for anyone if liquidated by LTV index // 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 // Loan-to-Value ratio after sending keeper their reward
ltv := borrowCoinValues.Sum().Quo(depositCoinValues.Sum()) 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 { if err != nil {
return err return err
} }
@ -280,10 +280,13 @@ func (k Keeper) StartAuctions(ctx sdk.Context, borrower sdk.AccAddress, borrows,
// and does not include any outsanding interest. // and does not include any outsanding interest.
func (k Keeper) GetStoreLTV(ctx sdk.Context, addr sdk.AccAddress) (sdk.Dec, bool, error) { func (k Keeper) GetStoreLTV(ctx sdk.Context, addr sdk.AccAddress) (sdk.Dec, bool, error) {
// Fetch deposits and parse coin denoms // 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{} depositDenoms := []string{}
for _, deposit := range deposits { for _, depCoin := range deposit.Amount {
depositDenoms = append(depositDenoms, deposit.Amount.Denom) depositDenoms = append(depositDenoms, depCoin.Denom)
} }
// Fetch borrow balances and parse coin denoms // 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 // Build valuation map to hold deposit coin USD valuations
depositCoinValues := types.NewValuationMap() depositCoinValues := types.NewValuationMap()
for _, deposit := range deposits { for _, depCoin := range deposit.Amount {
dData := liqMap[deposit.Amount.Denom] dData := liqMap[depCoin.Denom]
dCoinUsdValue := sdk.NewDecFromInt(deposit.Amount.Amount).Quo(sdk.NewDecFromInt(dData.conversionFactor)).Mul(dData.price) dCoinUsdValue := sdk.NewDecFromInt(depCoin.Amount).Quo(sdk.NewDecFromInt(dData.conversionFactor)).Mul(dData.price)
depositCoinValues.Increment(deposit.Amount.Denom, dCoinUsdValue) depositCoinValues.Increment(depCoin.Denom, dCoinUsdValue)
} }
// Build valuation map to hold borrow coin USD valuations // Build valuation map to hold borrow coin USD valuations

View File

@ -280,10 +280,8 @@ func (suite *KeeperTestSuite) TestIndexLiquidation() {
harvest.BeginBlocker(suite.ctx, suite.keeper) harvest.BeginBlocker(suite.ctx, suite.keeper)
// Deposit coins // Deposit coins
for _, coin := range tc.args.depositCoins { err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, coin)
suite.Require().NoError(err) suite.Require().NoError(err)
}
// Borrow coins // Borrow coins
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins) err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins)
@ -294,10 +292,8 @@ func (suite *KeeperTestSuite) TestIndexLiquidation() {
suite.Require().True(foundBorrowBefore) suite.Require().True(foundBorrowBefore)
// Check that the user's deposits exist before liquidation // Check that the user's deposits exist before liquidation
for _, coin := range tc.args.depositCoins { _, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, tc.args.borrower)
_, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, tc.args.borrower, coin.Denom)
suite.Require().True(foundDepositBefore) suite.Require().True(foundDepositBefore)
}
// Liquidate the borrow by running begin blocker // Liquidate the borrow by running begin blocker
runAtTime := time.Unix(suite.ctx.BlockTime().Unix()+(tc.args.beginBlockerTime), 0) 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) _, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
suite.Require().False(foundBorrowAfter) suite.Require().False(foundBorrowAfter)
// Check deposits do not exist after liquidation // Check deposits do not exist after liquidation
for _, coin := range tc.args.depositCoins { _, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
suite.Require().False(foundDepositAfter) suite.Require().False(foundDepositAfter)
}
// Check that borrower's balance contains the expected coins // Check that borrower's balance contains the expected coins
accBorrower := suite.getAccountAtCtx(tc.args.borrower, liqCtx) accBorrower := suite.getAccountAtCtx(tc.args.borrower, liqCtx)
@ -327,10 +321,8 @@ func (suite *KeeperTestSuite) TestIndexLiquidation() {
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower) _, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
suite.Require().True(foundBorrowAfter) suite.Require().True(foundBorrowAfter)
// Check that the user's deposits exist // Check that the user's deposits exist
for _, coin := range tc.args.depositCoins { _, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
suite.Require().True(foundDepositAfter) suite.Require().True(foundDepositAfter)
}
// Check that no auctions have been created // Check that no auctions have been created
auctions := suite.auctionKeeper.GetAllAuctions(liqCtx) auctions := suite.auctionKeeper.GetAllAuctions(liqCtx)
@ -637,20 +629,16 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
// Other borrowers take out positions by depositing and borrowing coins // Other borrowers take out positions by depositing and borrowing coins
for _, otherBorrower := range tc.args.otherBorrowers { for _, otherBorrower := range tc.args.otherBorrowers {
for _, coin := range tc.args.depositCoins { err = suite.keeper.Deposit(suite.ctx, otherBorrower, tc.args.depositCoins)
err = suite.keeper.Deposit(suite.ctx, otherBorrower, coin)
suite.Require().NoError(err) suite.Require().NoError(err)
}
err = suite.keeper.Borrow(suite.ctx, otherBorrower, tc.args.otherBorrowCoins) err = suite.keeper.Borrow(suite.ctx, otherBorrower, tc.args.otherBorrowCoins)
suite.Require().NoError(err) suite.Require().NoError(err)
} }
// Primary borrower deposits and borrows // Primary borrower deposits and borrows
for _, coin := range tc.args.depositCoins { err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, coin)
suite.Require().NoError(err) suite.Require().NoError(err)
}
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins) err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins)
suite.Require().NoError(err) suite.Require().NoError(err)
@ -661,20 +649,16 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
_, foundBorrowBefore := suite.keeper.GetBorrow(suite.ctx, otherBorrower) _, foundBorrowBefore := suite.keeper.GetBorrow(suite.ctx, otherBorrower)
suite.Require().True(foundBorrowBefore) suite.Require().True(foundBorrowBefore)
for _, coin := range tc.args.depositCoins { _, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, otherBorrower)
_, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, otherBorrower, coin.Denom)
suite.Require().True(foundDepositBefore) suite.Require().True(foundDepositBefore)
} }
}
// Primary borrower // Primary borrower
_, foundBorrowBefore := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower) _, foundBorrowBefore := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)
suite.Require().True(foundBorrowBefore) suite.Require().True(foundBorrowBefore)
for _, coin := range tc.args.depositCoins { _, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, tc.args.borrower)
_, foundDepositBefore := suite.keeper.GetDeposit(suite.ctx, tc.args.borrower, coin.Denom)
suite.Require().True(foundDepositBefore) suite.Require().True(foundDepositBefore)
}
// ----------- Liquidate and check state ----------- // ----------- Liquidate and check state -----------
// Liquidate the borrow by running begin blocker // Liquidate the borrow by running begin blocker
@ -687,10 +671,8 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower) _, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
suite.Require().False(foundBorrowAfter) suite.Require().False(foundBorrowAfter)
// Check deposits do not exist after liquidation // Check deposits do not exist after liquidation
for _, coin := range tc.args.depositCoins { _, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
suite.Require().False(foundDepositAfter) suite.Require().False(foundDepositAfter)
}
// Check that borrower's balance contains the expected coins // Check that borrower's balance contains the expected coins
accBorrower := suite.getAccountAtCtx(tc.args.borrower, liqCtx) accBorrower := suite.getAccountAtCtx(tc.args.borrower, liqCtx)
@ -705,10 +687,8 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower) _, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
suite.Require().True(foundBorrowAfter) suite.Require().True(foundBorrowAfter)
// Check that the user's deposits exist // Check that the user's deposits exist
for _, coin := range tc.args.depositCoins { _, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
suite.Require().True(foundDepositAfter) suite.Require().True(foundDepositAfter)
}
if !tc.errArgs.expectLiquidateOtherBorrowers { if !tc.errArgs.expectLiquidateOtherBorrowers {
// Check that no auctions have been created // Check that no auctions have been created
@ -725,11 +705,10 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
suite.Require().False(foundBorrowAfter) suite.Require().False(foundBorrowAfter)
// Check deposits do not exist after liquidation // Check deposits do not exist after liquidation
for _, coin := range tc.args.depositCoins {
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, otherBorrower, coin.Denom) _, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, otherBorrower)
suite.Require().False(foundDepositAfter) suite.Require().False(foundDepositAfter)
} }
}
var expectedLtvIndexItemCount int var expectedLtvIndexItemCount int
if tc.errArgs.expectLiquidate { if tc.errArgs.expectLiquidate {
@ -746,11 +725,10 @@ func (suite *KeeperTestSuite) TestFullIndexLiquidation() {
suite.Require().True(foundBorrowAfter) suite.Require().True(foundBorrowAfter)
// Check deposits do not exist after liquidation // Check deposits do not exist after liquidation
for _, coin := range tc.args.depositCoins {
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, otherBorrower, coin.Denom) _, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, otherBorrower)
suite.Require().True(foundDepositAfter) suite.Require().True(foundDepositAfter)
} }
}
var expectedLtvIndexItemCount int var expectedLtvIndexItemCount int
if tc.errArgs.expectLiquidate { if tc.errArgs.expectLiquidate {
@ -817,7 +795,7 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))), initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
initialBorrowerCoins: 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))), 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))), borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(8*KAVA_CF))),
liquidateAfter: oneMonthInSeconds, liquidateAfter: oneMonthInSeconds,
auctionSize: sdk.NewInt(KAVA_CF * 1000), 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))), 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))), initialBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
initialKeeperCoins: 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 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, liquidateAfter: oneMonthInSeconds,
auctionSize: sdk.NewInt(KAVA_CF * 1000), auctionSize: sdk.NewInt(KAVA_CF * 1000),
@ -938,7 +916,7 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(1000*KAVA_CF))), 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))), 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))), 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 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 borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(120*KAVA_CF))), // $240 borrowed
liquidateAfter: oneMonthInSeconds, liquidateAfter: oneMonthInSeconds,
auctionSize: sdk.NewInt(KAVA_CF * 1000), auctionSize: sdk.NewInt(KAVA_CF * 1000),
@ -1007,7 +985,7 @@ 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))), 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))), 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))), 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 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 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, liquidateAfter: oneMonthInSeconds,
auctionSize: sdk.NewInt(KAVA_CF * 1000), auctionSize: sdk.NewInt(KAVA_CF * 1000),
@ -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))), 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))), 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))), 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))), borrowCoins: sdk.NewCoins(sdk.NewCoin("usdt", sdk.NewInt(250*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(245*KAVA_CF))),
liquidateAfter: oneMonthInSeconds, liquidateAfter: oneMonthInSeconds,
auctionSize: sdk.NewInt(KAVA_CF * 100000), auctionSize: sdk.NewInt(KAVA_CF * 100000),
@ -1173,7 +1151,7 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))), initialModuleCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF))),
initialBorrowerCoins: 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))), 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 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 borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(5*KAVA_CF))), // Borrow 5 KAVA
liquidateAfter: oneMonthInSeconds, liquidateAfter: oneMonthInSeconds,
auctionSize: sdk.NewInt(KAVA_CF * 1000), auctionSize: sdk.NewInt(KAVA_CF * 1000),
@ -1360,10 +1338,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
harvest.BeginBlocker(suite.ctx, suite.keeper) harvest.BeginBlocker(suite.ctx, suite.keeper)
// Deposit coins // Deposit coins
for _, coin := range tc.args.depositCoins { err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, coin)
suite.Require().NoError(err) suite.Require().NoError(err)
}
// Borrow coins // Borrow coins
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins) 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 // Check borrow exists before liquidation
_, foundBorrowBefore := suite.keeper.GetBorrow(liqCtx, tc.args.borrower) _, foundBorrowBefore := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
suite.Require().True(foundBorrowBefore) suite.Require().True(foundBorrowBefore)
// Check that the user's deposits exist before liquidation // Check that the user's deposit exists before liquidation
for _, coin := range tc.args.depositCoins { _, foundDepositBefore := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
_, foundDepositBefore := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
suite.Require().True(foundDepositBefore) suite.Require().True(foundDepositBefore)
}
// Attempt to liquidate // Attempt to liquidate
liquidated, err := suite.keeper.AttemptKeeperLiquidation(liqCtx, tc.args.keeper, tc.args.borrower) 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) _, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
suite.Require().False(foundBorrowAfter) suite.Require().False(foundBorrowAfter)
// Check deposits do not exist after liquidation // Check deposits do not exist after liquidation
for _, coin := range tc.args.depositCoins { _, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
suite.Require().False(foundDepositAfter) suite.Require().False(foundDepositAfter)
}
// Check that the keeper's balance increased by reward % of all the borrowed coins // Check that the keeper's balance increased by reward % of all the borrowed coins
accKeeper := suite.getAccountAtCtx(tc.args.keeper, liqCtx) accKeeper := suite.getAccountAtCtx(tc.args.keeper, liqCtx)
@ -1419,10 +1391,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
_, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower) _, foundBorrowAfter := suite.keeper.GetBorrow(liqCtx, tc.args.borrower)
suite.Require().True(foundBorrowAfter) suite.Require().True(foundBorrowAfter)
// Check that the user's deposits exist // Check that the user's deposits exist
for _, coin := range tc.args.depositCoins { _, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
_, foundDepositAfter := suite.keeper.GetDeposit(liqCtx, tc.args.borrower, coin.Denom)
suite.Require().True(foundDepositAfter) suite.Require().True(foundDepositAfter)
}
// Check that no auctions have been created // Check that no auctions have been created
auctions := suite.auctionKeeper.GetAllAuctions(liqCtx) auctions := suite.auctionKeeper.GetAllAuctions(liqCtx)

View File

@ -90,23 +90,27 @@ func queryGetDeposits(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte,
var deposits []types.Deposit var deposits []types.Deposit
switch { switch {
case depositDenom && owner: case depositDenom && owner:
deposit, found := k.GetDeposit(ctx, params.Owner, params.DepositDenom) deposit, found := k.GetDeposit(ctx, params.Owner)
if found { if found {
for _, depCoin := range deposit.Amount {
if depCoin.Denom == params.DepositDenom {
deposits = append(deposits, deposit) deposits = append(deposits, deposit)
} }
}
}
case depositDenom: case depositDenom:
k.IterateDepositsByDenom(ctx, params.DepositDenom, func(deposit types.Deposit) (stop bool) { k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
if deposit.Amount.AmountOf(params.DepositDenom).IsPositive() {
deposits = append(deposits, deposit) deposits = append(deposits, deposit)
}
return false return false
}) })
case owner: case owner:
schedules := k.GetParams(ctx).LiquidityProviderSchedules deposit, found := k.GetDeposit(ctx, params.Owner)
for _, lps := range schedules {
deposit, found := k.GetDeposit(ctx, params.Owner, lps.DepositDenom)
if found { if found {
deposits = append(deposits, deposit) deposits = append(deposits, deposit)
} }
}
default: default:
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) { k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
deposits = append(deposits, deposit) deposits = append(deposits, deposit)

View File

@ -192,12 +192,8 @@ func (suite *KeeperTestSuite) TestRepay() {
harvest.BeginBlocker(suite.ctx, suite.keeper) harvest.BeginBlocker(suite.ctx, suite.keeper)
// Deposit coins to harvest // Deposit coins to harvest
depositedCoins := sdk.NewCoins() err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, tc.args.depositCoins)
for _, depositCoin := range tc.args.depositCoins {
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, depositCoin)
suite.Require().NoError(err) suite.Require().NoError(err)
depositedCoins.Add(depositCoin)
}
// Borrow coins from harvest // Borrow coins from harvest
err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins) err = suite.keeper.Borrow(suite.ctx, tc.args.borrower, tc.args.borrowCoins)

View File

@ -43,8 +43,8 @@ func (k Keeper) ApplyDepositRewards(ctx sdk.Context) {
continue continue
} }
rewardsDistributed := sdk.ZeroInt() rewardsDistributed := sdk.ZeroInt()
k.IterateDepositsByDenom(ctx, lps.DepositDenom, func(dep types.Deposit) (stop bool) { k.IterateDeposits(ctx, func(dep types.Deposit) (stop bool) {
rewardsShare := sdk.NewDecFromInt(dep.Amount.Amount).Quo(sdk.NewDecFromInt(totalDeposited)) rewardsShare := sdk.NewDecFromInt(dep.Amount.AmountOf(lps.DepositDenom)).Quo(sdk.NewDecFromInt(totalDeposited))
if rewardsShare.IsZero() { if rewardsShare.IsZero() {
return false return false
} }
@ -52,7 +52,7 @@ func (k Keeper) ApplyDepositRewards(ctx sdk.Context) {
if rewardsEarned.IsZero() { if rewardsEarned.IsZero() {
return false 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) rewardsDistributed = rewardsDistributed.Add(rewardsEarned)
return false return false
}) })

View File

@ -21,7 +21,7 @@ func (suite *KeeperTestSuite) TestApplyDepositRewards() {
type args struct { type args struct {
depositor sdk.AccAddress depositor sdk.AccAddress
denom string denom string
depositAmount sdk.Coin depositAmount sdk.Coins
totalDeposits sdk.Coin totalDeposits sdk.Coin
rewardRate sdk.Coin rewardRate sdk.Coin
claimType types.ClaimType claimType types.ClaimType
@ -45,7 +45,7 @@ func (suite *KeeperTestSuite) TestApplyDepositRewards() {
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))), depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
denom: "bnb", denom: "bnb",
rewardRate: c("hard", 500), rewardRate: c("hard", 500),
depositAmount: c("bnb", 100), depositAmount: cs(c("bnb", 100)),
totalDeposits: c("bnb", 1000), totalDeposits: c("bnb", 1000),
claimType: types.LP, claimType: types.LP,
previousBlockTime: time.Date(2020, 11, 1, 13, 59, 50, 0, time.UTC), previousBlockTime: time.Date(2020, 11, 1, 13, 59, 50, 0, time.UTC),

View File

@ -27,7 +27,7 @@ func TestDecodeDistributionStore(t *testing.T) {
cdc := makeTestCodec() cdc := makeTestCodec()
prevBlockTime := time.Now().UTC() 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") claim := types.NewClaim(sdk.AccAddress("test"), "bnb", sdk.NewCoin("hard", sdk.NewInt(100)), "stake")
kvPairs := kv.Pairs{ kvPairs := kv.Pairs{

View File

@ -7,11 +7,11 @@ import (
// Deposit defines an amount of coins deposited into a harvest module account // Deposit defines an amount of coins deposited into a harvest module account
type Deposit struct { type Deposit struct {
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"` 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 // 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{ return Deposit{
Depositor: depositor, Depositor: depositor,
Amount: amount, Amount: amount,

View File

@ -12,7 +12,7 @@ var (
// ErrDepositNotFound error for deposit not found // ErrDepositNotFound error for deposit not found
ErrDepositNotFound = sdkerrors.Register(ModuleName, 3, "deposit not found") ErrDepositNotFound = sdkerrors.Register(ModuleName, 3, "deposit not found")
// ErrInvalidWithdrawAmount error for invalid withdrawal amount // 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 error for invalid claim type
ErrInvalidClaimType = sdkerrors.Register(ModuleName, 5, "invalid claim type") ErrInvalidClaimType = sdkerrors.Register(ModuleName, 5, "invalid claim type")
// ErrClaimNotFound error for claim not found // ErrClaimNotFound error for claim not found

View File

@ -48,11 +48,6 @@ var (
sep = []byte(":") 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 // DepositTypeIteratorKey returns an interator prefix for interating over deposits by deposit denom
func DepositTypeIteratorKey(denom string) []byte { func DepositTypeIteratorKey(denom string) []byte {
return createKey([]byte(denom)) return createKey([]byte(denom))

View File

@ -58,11 +58,11 @@ var (
// MsgDeposit deposit collateral to the harvest module. // MsgDeposit deposit collateral to the harvest module.
type MsgDeposit struct { type MsgDeposit struct {
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"` 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 // 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{ return MsgDeposit{
Depositor: depositor, Depositor: depositor,
Amount: amount, Amount: amount,
@ -108,11 +108,11 @@ func (msg MsgDeposit) String() string {
// MsgWithdraw withdraw from the harvest module. // MsgWithdraw withdraw from the harvest module.
type MsgWithdraw struct { type MsgWithdraw struct {
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"` 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 // 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{ return MsgWithdraw{
Depositor: depositor, Depositor: depositor,
Amount: amount, Amount: amount,

View File

@ -18,7 +18,7 @@ type MsgTestSuite struct {
func (suite *MsgTestSuite) TestMsgDeposit() { func (suite *MsgTestSuite) TestMsgDeposit() {
type args struct { type args struct {
depositor sdk.AccAddress depositor sdk.AccAddress
amount sdk.Coin amount sdk.Coins
} }
addrs := []sdk.AccAddress{ addrs := []sdk.AccAddress{
sdk.AccAddress("test1"), sdk.AccAddress("test1"),
@ -34,7 +34,7 @@ func (suite *MsgTestSuite) TestMsgDeposit() {
name: "valid", name: "valid",
args: args{ args: args{
depositor: addrs[0], depositor: addrs[0],
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)), amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000000))),
}, },
expectPass: true, expectPass: true,
expectedErr: "", expectedErr: "",
@ -43,7 +43,7 @@ func (suite *MsgTestSuite) TestMsgDeposit() {
name: "valid2", name: "valid2",
args: args{ args: args{
depositor: addrs[0], depositor: addrs[0],
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)), amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000000))),
}, },
expectPass: true, expectPass: true,
expectedErr: "", expectedErr: "",
@ -66,7 +66,7 @@ func (suite *MsgTestSuite) TestMsgDeposit() {
func (suite *MsgTestSuite) TestMsgWithdraw() { func (suite *MsgTestSuite) TestMsgWithdraw() {
type args struct { type args struct {
depositor sdk.AccAddress depositor sdk.AccAddress
amount sdk.Coin amount sdk.Coins
} }
addrs := []sdk.AccAddress{ addrs := []sdk.AccAddress{
sdk.AccAddress("test1"), sdk.AccAddress("test1"),
@ -82,7 +82,7 @@ func (suite *MsgTestSuite) TestMsgWithdraw() {
name: "valid", name: "valid",
args: args{ args: args{
depositor: addrs[0], depositor: addrs[0],
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)), amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000000))),
}, },
expectPass: true, expectPass: true,
expectedErr: "", expectedErr: "",
@ -91,7 +91,7 @@ func (suite *MsgTestSuite) TestMsgWithdraw() {
name: "valid2", name: "valid2",
args: args{ args: args{
depositor: addrs[0], depositor: addrs[0],
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)), amount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000000))),
}, },
expectPass: true, expectPass: true,
expectedErr: "", expectedErr: "",