2020-04-23 16:35:58 +00:00
|
|
|
package simulation
|
2020-04-14 01:29:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2020-04-23 16:35:58 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
2020-04-14 01:29:46 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
2020-04-23 16:35:58 +00:00
|
|
|
|
|
|
|
appparams "github.com/kava-labs/kava/app/params"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Simulation operation weights constants
|
|
|
|
const (
|
|
|
|
OpWeightMsgCdp = "op_weight_msg_cdp"
|
2020-04-14 01:29:46 +00:00
|
|
|
)
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
// WeightedOperations returns all the operations from the module with their respective weights
|
|
|
|
func WeightedOperations(
|
2020-04-29 14:10:41 +00:00
|
|
|
appParams simulation.AppParams, cdc *codec.Codec, ak types.AccountKeeper,
|
2020-04-23 16:35:58 +00:00
|
|
|
k keeper.Keeper, pfk types.PricefeedKeeper,
|
|
|
|
) simulation.WeightedOperations {
|
|
|
|
var weightMsgCdp int
|
|
|
|
|
|
|
|
appParams.GetOrGenerate(cdc, OpWeightMsgCdp, &weightMsgCdp, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
weightMsgCdp = appparams.DefaultWeightMsgCdp
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return simulation.WeightedOperations{
|
|
|
|
simulation.NewWeightedOperation(
|
|
|
|
weightMsgCdp,
|
|
|
|
SimulateMsgCdp(ak, k, pfk),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:29:46 +00:00
|
|
|
// SimulateMsgCdp generates a MsgCreateCdp or MsgDepositCdp with random values.
|
2020-04-29 14:10:41 +00:00
|
|
|
func SimulateMsgCdp(ak types.AccountKeeper, k keeper.Keeper, pfk types.PricefeedKeeper) simulation.Operation {
|
2020-04-23 16:35:58 +00:00
|
|
|
return func(
|
|
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, chainID string,
|
|
|
|
) (simulation.OperationMsg, []simulation.FutureOperation, error) {
|
2020-04-14 01:29:46 +00:00
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
simAccount, _ := simulation.RandomAcc(r, accs)
|
|
|
|
acc := ak.GetAccount(ctx, simAccount.Address)
|
2020-04-14 01:29:46 +00:00
|
|
|
if acc == nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-04-14 01:29:46 +00:00
|
|
|
coins := acc.GetCoins()
|
|
|
|
collateralParams := k.GetParams(ctx).CollateralParams
|
|
|
|
if len(collateralParams) == 0 {
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-04-14 01:29:46 +00:00
|
|
|
randCollateralParam := collateralParams[r.Intn(len(collateralParams))]
|
2020-04-27 14:40:34 +00:00
|
|
|
debtParam, _ := k.GetDebtParam(ctx, randCollateralParam.DebtLimit.Denom)
|
2020-04-14 01:29:46 +00:00
|
|
|
if coins.AmountOf(randCollateralParam.Denom).IsZero() {
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 13:42:46 +00:00
|
|
|
price, err := pfk.GetCurrentPrice(ctx, randCollateralParam.SpotMarketID)
|
2020-04-14 01:29:46 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, err
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
// convert the price to the same units as the debt param
|
2020-04-27 14:40:34 +00:00
|
|
|
priceShifted := ShiftDec(price.Price, debtParam.ConversionFactor)
|
2020-04-14 01:29:46 +00:00
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
spendableCoins := acc.SpendableCoins(ctx.BlockTime())
|
|
|
|
fees, err := simulation.RandomFees(r, ctx, spendableCoins)
|
|
|
|
if err != nil {
|
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, err
|
|
|
|
}
|
|
|
|
spendableCoins = spendableCoins.Sub(fees)
|
|
|
|
|
2020-04-14 01:29:46 +00:00
|
|
|
existingCDP, found := k.GetCdpByOwnerAndDenom(ctx, acc.GetAddress(), randCollateralParam.Denom)
|
|
|
|
if !found {
|
|
|
|
// calculate the minimum amount of collateral that is needed to create a cdp with the debt floor amount of debt and the minimum liquidation ratio
|
|
|
|
// (debtFloor * liquidationRatio)/priceShifted
|
2020-04-27 14:40:34 +00:00
|
|
|
minCollateralDeposit := (sdk.NewDecFromInt(debtParam.DebtFloor).Mul(randCollateralParam.LiquidationRatio)).Quo(priceShifted)
|
2020-04-14 01:29:46 +00:00
|
|
|
// convert to proper collateral units
|
|
|
|
minCollateralDeposit = ShiftDec(minCollateralDeposit, randCollateralParam.ConversionFactor)
|
|
|
|
// convert to integer and always round up
|
|
|
|
minCollateralDepositRounded := minCollateralDeposit.TruncateInt().Add(sdk.OneInt())
|
2020-04-23 16:35:58 +00:00
|
|
|
if spendableCoins.AmountOf(randCollateralParam.Denom).LT(minCollateralDepositRounded) {
|
2020-04-15 17:50:14 +00:00
|
|
|
// account doesn't have enough funds to open a cdp for the min debt amount
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NewOperationMsgBasic(types.ModuleName, "no-operation", "insufficient funds to open cdp", false, nil), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
// set the max collateral deposit to the amount of coins in the account
|
2020-04-23 16:35:58 +00:00
|
|
|
maxCollateralDeposit := spendableCoins.AmountOf(randCollateralParam.Denom)
|
2020-04-14 01:29:46 +00:00
|
|
|
|
|
|
|
// randomly select a collateral deposit amount
|
|
|
|
collateralDeposit := sdk.NewInt(int64(simulation.RandIntBetween(r, int(minCollateralDepositRounded.Int64()), int(maxCollateralDeposit.Int64()))))
|
|
|
|
// calculate how much the randomly selected deposit is worth
|
|
|
|
collateralDepositValue := ShiftDec(sdk.NewDecFromInt(collateralDeposit), randCollateralParam.ConversionFactor.Neg()).Mul(priceShifted)
|
|
|
|
// calculate the max amount of debt that could be drawn for the chosen deposit
|
|
|
|
maxDebtDraw := collateralDepositValue.Quo(randCollateralParam.LiquidationRatio).TruncateInt()
|
2020-04-15 17:50:14 +00:00
|
|
|
// check that the debt limit hasn't been reached
|
2020-04-27 14:40:34 +00:00
|
|
|
availableAssetDebt := randCollateralParam.DebtLimit.Amount.Sub(k.GetTotalPrincipal(ctx, randCollateralParam.Denom, debtParam.Denom))
|
|
|
|
if availableAssetDebt.LTE(debtParam.DebtFloor) {
|
2020-04-15 17:50:14 +00:00
|
|
|
// debt limit has been reached
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NewOperationMsgBasic(types.ModuleName, "no-operation", "debt limit reached, cannot open cdp", false, nil), nil, nil
|
2020-04-15 17:50:14 +00:00
|
|
|
}
|
|
|
|
// ensure that the debt draw does not exceed the debt limit
|
|
|
|
maxDebtDraw = sdk.MinInt(maxDebtDraw, availableAssetDebt)
|
2020-04-14 01:29:46 +00:00
|
|
|
// randomly select a debt draw amount
|
2020-04-27 14:40:34 +00:00
|
|
|
debtDraw := sdk.NewInt(int64(simulation.RandIntBetween(r, int(debtParam.DebtFloor.Int64()), int(maxDebtDraw.Int64()))))
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-04-27 14:40:34 +00:00
|
|
|
msg := types.NewMsgCreateCDP(acc.GetAddress(), sdk.NewCoin(randCollateralParam.Denom, collateralDeposit), sdk.NewCoin(debtParam.Denom, debtDraw))
|
2020-04-23 16:35:58 +00:00
|
|
|
|
|
|
|
tx := helpers.GenTx(
|
|
|
|
[]sdk.Msg{msg},
|
|
|
|
fees,
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
chainID,
|
|
|
|
[]uint64{acc.GetAccountNumber()},
|
|
|
|
[]uint64{acc.GetSequence()},
|
|
|
|
simAccount.PrivKey,
|
|
|
|
)
|
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
_, _, err := app.Deliver(tx)
|
2020-04-14 01:29:46 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, err
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// a cdp already exists, deposit to it, draw debt from it, or repay debt to it
|
|
|
|
// close 25% of the time
|
2020-04-30 21:02:42 +00:00
|
|
|
if canClose(spendableCoins, existingCDP, debtParam.Denom) && shouldClose(r) {
|
2020-04-27 14:40:34 +00:00
|
|
|
repaymentAmount := spendableCoins.AmountOf(debtParam.Denom)
|
|
|
|
msg := types.NewMsgRepayDebt(acc.GetAddress(), randCollateralParam.Denom, sdk.NewCoin(debtParam.Denom, repaymentAmount))
|
2020-04-23 16:35:58 +00:00
|
|
|
|
|
|
|
tx := helpers.GenTx(
|
|
|
|
[]sdk.Msg{msg},
|
|
|
|
fees,
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
chainID,
|
|
|
|
[]uint64{acc.GetAccountNumber()},
|
|
|
|
[]uint64{acc.GetSequence()},
|
|
|
|
simAccount.PrivKey,
|
|
|
|
)
|
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
_, _, err := app.Deliver(tx)
|
2020-04-14 01:29:46 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, err
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// deposit 25% of the time
|
2020-04-30 21:02:42 +00:00
|
|
|
if hasCoins(spendableCoins, randCollateralParam.Denom) && shouldDeposit(r) {
|
2020-04-23 16:35:58 +00:00
|
|
|
randDepositAmount := sdk.NewInt(int64(simulation.RandIntBetween(r, 1, int(spendableCoins.AmountOf(randCollateralParam.Denom).Int64()))))
|
2020-04-27 14:40:34 +00:00
|
|
|
msg := types.NewMsgDeposit(acc.GetAddress(), acc.GetAddress(), sdk.NewCoin(randCollateralParam.Denom, randDepositAmount))
|
2020-04-23 16:35:58 +00:00
|
|
|
|
|
|
|
tx := helpers.GenTx(
|
|
|
|
[]sdk.Msg{msg},
|
|
|
|
fees,
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
chainID,
|
|
|
|
[]uint64{acc.GetAccountNumber()},
|
|
|
|
[]uint64{acc.GetSequence()},
|
|
|
|
simAccount.PrivKey,
|
|
|
|
)
|
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
_, _, err := app.Deliver(tx)
|
2020-04-14 01:29:46 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, err
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw debt 25% of the time
|
|
|
|
if shouldDraw(r) {
|
2020-04-27 14:40:34 +00:00
|
|
|
collateralShifted := ShiftDec(sdk.NewDecFromInt(existingCDP.Collateral.Amount), randCollateralParam.ConversionFactor.Neg())
|
2020-04-14 01:29:46 +00:00
|
|
|
collateralValue := collateralShifted.Mul(priceShifted)
|
2020-04-27 14:40:34 +00:00
|
|
|
newFeesAccumulated := k.CalculateFees(ctx, existingCDP.Principal, sdk.NewInt(ctx.BlockTime().Unix()-existingCDP.FeesUpdated.Unix()), randCollateralParam.Denom).Amount
|
|
|
|
totalFees := existingCDP.AccumulatedFees.Amount.Add(newFeesAccumulated)
|
2020-04-15 17:50:14 +00:00
|
|
|
// given the current collateral value, calculate how much debt we could add while maintaining a valid liquidation ratio
|
2020-04-27 14:40:34 +00:00
|
|
|
debt := existingCDP.Principal.Amount.Add(totalFees)
|
2020-04-14 01:29:46 +00:00
|
|
|
maxTotalDebt := collateralValue.Quo(randCollateralParam.LiquidationRatio)
|
2020-04-18 15:34:25 +00:00
|
|
|
maxDebt := (maxTotalDebt.Sub(sdk.NewDecFromInt(debt))).Mul(sdk.MustNewDecFromStr("0.95")).TruncateInt()
|
2020-04-15 17:50:14 +00:00
|
|
|
if maxDebt.LTE(sdk.OneInt()) {
|
|
|
|
// debt in cdp is maxed out
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NewOperationMsgBasic(types.ModuleName, "no-operation", "cdp debt maxed out, cannot draw more debt", false, nil), nil, nil
|
2020-04-15 17:50:14 +00:00
|
|
|
}
|
|
|
|
// check if the debt limit has been reached
|
2020-04-27 14:40:34 +00:00
|
|
|
availableAssetDebt := randCollateralParam.DebtLimit.Amount.Sub(k.GetTotalPrincipal(ctx, randCollateralParam.Denom, debtParam.Denom))
|
2020-04-15 17:50:14 +00:00
|
|
|
if availableAssetDebt.LTE(sdk.OneInt()) {
|
|
|
|
// debt limit has been reached
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NewOperationMsgBasic(types.ModuleName, "no-operation", "debt limit reached, cannot draw more debt", false, nil), nil, nil
|
2020-04-15 17:50:14 +00:00
|
|
|
}
|
|
|
|
maxDraw := sdk.MinInt(maxDebt, availableAssetDebt)
|
|
|
|
|
|
|
|
randDrawAmount := sdk.NewInt(int64(simulation.RandIntBetween(r, 1, int(maxDraw.Int64()))))
|
2020-04-27 14:40:34 +00:00
|
|
|
msg := types.NewMsgDrawDebt(acc.GetAddress(), randCollateralParam.Denom, sdk.NewCoin(debtParam.Denom, randDrawAmount))
|
2020-04-23 16:35:58 +00:00
|
|
|
|
|
|
|
tx := helpers.GenTx(
|
|
|
|
[]sdk.Msg{msg},
|
|
|
|
fees,
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
chainID,
|
|
|
|
[]uint64{acc.GetAccountNumber()},
|
|
|
|
[]uint64{acc.GetSequence()},
|
|
|
|
simAccount.PrivKey,
|
|
|
|
)
|
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
_, _, err := app.Deliver(tx)
|
|
|
|
|
2020-04-14 01:29:46 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, err
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// repay debt 25% of the time
|
2020-04-30 21:02:42 +00:00
|
|
|
if hasCoins(spendableCoins, debtParam.Denom) {
|
2020-04-27 14:40:34 +00:00
|
|
|
debt := existingCDP.Principal.Amount
|
|
|
|
payableDebt := debt.Sub(debtParam.DebtFloor)
|
2020-04-30 21:02:42 +00:00
|
|
|
if payableDebt.IsZero() {
|
|
|
|
return simulation.NewOperationMsgBasic(types.ModuleName, "no-operation", "cannot make partial repayment, cdp at debt floor", false, nil), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
2020-04-30 21:02:42 +00:00
|
|
|
maxRepay := sdk.MinInt(
|
|
|
|
spendableCoins.AmountOf(debtParam.Denom),
|
|
|
|
payableDebt,
|
|
|
|
)
|
|
|
|
var randRepayAmount sdk.Int
|
|
|
|
if maxRepay.Equal(sdk.OneInt()) {
|
|
|
|
randRepayAmount = sdk.OneInt()
|
|
|
|
} else {
|
|
|
|
randRepayAmount = sdk.NewInt(int64(simulation.RandIntBetween(r, 1, int(maxRepay.Int64()))))
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-04-27 14:40:34 +00:00
|
|
|
msg := types.NewMsgRepayDebt(acc.GetAddress(), randCollateralParam.Denom, sdk.NewCoin(debtParam.Denom, randRepayAmount))
|
2020-04-23 16:35:58 +00:00
|
|
|
|
|
|
|
tx := helpers.GenTx(
|
|
|
|
[]sdk.Msg{msg},
|
|
|
|
fees,
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
chainID,
|
|
|
|
[]uint64{acc.GetAccountNumber()},
|
|
|
|
[]uint64{acc.GetSequence()},
|
|
|
|
simAccount.PrivKey,
|
|
|
|
)
|
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
_, _, err := app.Deliver(tx)
|
2020-04-14 01:29:46 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, err
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:10:41 +00:00
|
|
|
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
2020-04-23 16:35:58 +00:00
|
|
|
}
|
2020-04-14 01:29:46 +00:00
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, nil
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldDraw(r *rand.Rand) bool {
|
|
|
|
threshold := 50
|
|
|
|
value := simulation.RandIntBetween(r, 1, 100)
|
2020-04-23 16:35:58 +00:00
|
|
|
return value > threshold
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func shouldDeposit(r *rand.Rand) bool {
|
|
|
|
threshold := 66
|
|
|
|
value := simulation.RandIntBetween(r, 1, 100)
|
2020-04-23 16:35:58 +00:00
|
|
|
return value > threshold
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 21:02:42 +00:00
|
|
|
func hasCoins(spendableCoins sdk.Coins, denom string) bool {
|
|
|
|
return spendableCoins.AmountOf(denom).IsPositive()
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func shouldClose(r *rand.Rand) bool {
|
|
|
|
threshold := 75
|
|
|
|
value := simulation.RandIntBetween(r, 1, 100)
|
2020-04-23 16:35:58 +00:00
|
|
|
return value > threshold
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 21:02:42 +00:00
|
|
|
func canClose(spendableCoins sdk.Coins, c types.CDP, denom string) bool {
|
2020-04-27 14:40:34 +00:00
|
|
|
repaymentAmount := c.Principal.Add(c.AccumulatedFees).Amount
|
2020-04-30 21:02:42 +00:00
|
|
|
return spendableCoins.AmountOf(denom).GTE(repaymentAmount)
|
2020-04-14 01:29:46 +00:00
|
|
|
}
|