mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 15:25:18 +00:00
Check spendable balance in cdp sims (#480)
* fix: check spendable balance * don't log noisy result.Log * remove dead comment
This commit is contained in:
parent
e6c0807419
commit
22bba81944
@ -111,13 +111,12 @@ func SimulateMsgPlaceBid(ak auth.AccountKeeper, keeper keeper.Keeper) simulation
|
|||||||
bidder.PrivKey,
|
bidder.PrivKey,
|
||||||
)
|
)
|
||||||
|
|
||||||
_, result, err := app.Deliver(tx)
|
_, _, err = app.Deliver(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// to aid debugging, add the stack trace to the comment field of the returned opMsg
|
// to aid debugging, add the stack trace to the comment field of the returned opMsg
|
||||||
return simulation.NewOperationMsg(msg, false, fmt.Sprintf("%+v", err)), nil, err
|
return simulation.NewOperationMsg(msg, false, fmt.Sprintf("%+v", err)), nil, err
|
||||||
}
|
}
|
||||||
// to aid debugging, add the result log to the comment field
|
return simulation.NewOperationMsg(msg, true, ""), nil, nil
|
||||||
return simulation.NewOperationMsg(msg, true, result.Log), nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/simulation"
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
||||||
|
|
||||||
appparams "github.com/kava-labs/kava/app/params"
|
appparams "github.com/kava-labs/kava/app/params"
|
||||||
@ -134,7 +133,7 @@ func SimulateMsgCdp(ak types.AccountKeeper, k keeper.Keeper, pfk types.Pricefeed
|
|||||||
|
|
||||||
// a cdp already exists, deposit to it, draw debt from it, or repay debt to it
|
// a cdp already exists, deposit to it, draw debt from it, or repay debt to it
|
||||||
// close 25% of the time
|
// close 25% of the time
|
||||||
if canClose(acc, existingCDP, debtParam.Denom) && shouldClose(r) {
|
if canClose(spendableCoins, existingCDP, debtParam.Denom) && shouldClose(r) {
|
||||||
repaymentAmount := spendableCoins.AmountOf(debtParam.Denom)
|
repaymentAmount := spendableCoins.AmountOf(debtParam.Denom)
|
||||||
msg := types.NewMsgRepayDebt(acc.GetAddress(), randCollateralParam.Denom, sdk.NewCoin(debtParam.Denom, repaymentAmount))
|
msg := types.NewMsgRepayDebt(acc.GetAddress(), randCollateralParam.Denom, sdk.NewCoin(debtParam.Denom, repaymentAmount))
|
||||||
|
|
||||||
@ -157,7 +156,7 @@ func SimulateMsgCdp(ak types.AccountKeeper, k keeper.Keeper, pfk types.Pricefeed
|
|||||||
}
|
}
|
||||||
|
|
||||||
// deposit 25% of the time
|
// deposit 25% of the time
|
||||||
if hasCoins(acc, randCollateralParam.Denom) && shouldDeposit(r) {
|
if hasCoins(spendableCoins, randCollateralParam.Denom) && shouldDeposit(r) {
|
||||||
randDepositAmount := sdk.NewInt(int64(simulation.RandIntBetween(r, 1, int(spendableCoins.AmountOf(randCollateralParam.Denom).Int64()))))
|
randDepositAmount := sdk.NewInt(int64(simulation.RandIntBetween(r, 1, int(spendableCoins.AmountOf(randCollateralParam.Denom).Int64()))))
|
||||||
msg := types.NewMsgDeposit(acc.GetAddress(), acc.GetAddress(), sdk.NewCoin(randCollateralParam.Denom, randDepositAmount))
|
msg := types.NewMsgDeposit(acc.GetAddress(), acc.GetAddress(), sdk.NewCoin(randCollateralParam.Denom, randDepositAmount))
|
||||||
|
|
||||||
@ -224,16 +223,21 @@ func SimulateMsgCdp(ak types.AccountKeeper, k keeper.Keeper, pfk types.Pricefeed
|
|||||||
}
|
}
|
||||||
|
|
||||||
// repay debt 25% of the time
|
// repay debt 25% of the time
|
||||||
if hasCoins(acc, debtParam.Denom) {
|
if hasCoins(spendableCoins, debtParam.Denom) {
|
||||||
debt := existingCDP.Principal.Amount
|
debt := existingCDP.Principal.Amount
|
||||||
maxRepay := spendableCoins.AmountOf(debtParam.Denom)
|
|
||||||
payableDebt := debt.Sub(debtParam.DebtFloor)
|
payableDebt := debt.Sub(debtParam.DebtFloor)
|
||||||
if maxRepay.GT(payableDebt) {
|
if payableDebt.IsZero() {
|
||||||
maxRepay = payableDebt
|
return simulation.NewOperationMsgBasic(types.ModuleName, "no-operation", "cannot make partial repayment, cdp at debt floor", false, nil), nil, nil
|
||||||
}
|
}
|
||||||
randRepayAmount := sdk.NewInt(int64(simulation.RandIntBetween(r, 1, int(maxRepay.Int64()))))
|
maxRepay := sdk.MinInt(
|
||||||
if debt.Equal(debtParam.DebtFloor) && spendableCoins.AmountOf(debtParam.Denom).GTE(debt) {
|
spendableCoins.AmountOf(debtParam.Denom),
|
||||||
randRepayAmount = debt
|
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()))))
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := types.NewMsgRepayDebt(acc.GetAddress(), randCollateralParam.Denom, sdk.NewCoin(debtParam.Denom, randRepayAmount))
|
msg := types.NewMsgRepayDebt(acc.GetAddress(), randCollateralParam.Denom, sdk.NewCoin(debtParam.Denom, randRepayAmount))
|
||||||
@ -272,8 +276,8 @@ func shouldDeposit(r *rand.Rand) bool {
|
|||||||
return value > threshold
|
return value > threshold
|
||||||
}
|
}
|
||||||
|
|
||||||
func hasCoins(acc authexported.Account, denom string) bool {
|
func hasCoins(spendableCoins sdk.Coins, denom string) bool {
|
||||||
return acc.GetCoins().AmountOf(denom).IsPositive()
|
return spendableCoins.AmountOf(denom).IsPositive()
|
||||||
}
|
}
|
||||||
|
|
||||||
func shouldClose(r *rand.Rand) bool {
|
func shouldClose(r *rand.Rand) bool {
|
||||||
@ -282,7 +286,7 @@ func shouldClose(r *rand.Rand) bool {
|
|||||||
return value > threshold
|
return value > threshold
|
||||||
}
|
}
|
||||||
|
|
||||||
func canClose(acc authexported.Account, c types.CDP, denom string) bool {
|
func canClose(spendableCoins sdk.Coins, c types.CDP, denom string) bool {
|
||||||
repaymentAmount := c.Principal.Add(c.AccumulatedFees).Amount
|
repaymentAmount := c.Principal.Add(c.AccumulatedFees).Amount
|
||||||
return acc.GetCoins().AmountOf(denom).GTE(repaymentAmount)
|
return spendableCoins.AmountOf(denom).GTE(repaymentAmount)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user