2020-04-23 16:35:58 +00:00
|
|
|
package simulation
|
2020-04-12 16:34:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2020-04-17 22:29:54 +00:00
|
|
|
"sync"
|
2020-04-12 16:34:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"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-12 16:34:01 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-23 16:35:58 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2020-04-12 16:34:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
appparams "github.com/kava-labs/kava/app/params"
|
2020-04-12 16:34:01 +00:00
|
|
|
"github.com/kava-labs/kava/x/pricefeed/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-04-23 16:35:58 +00:00
|
|
|
noOpMsg = simulation.NoOpMsg(types.ModuleName)
|
2020-04-17 22:29:54 +00:00
|
|
|
btcPrices = []sdk.Dec{}
|
|
|
|
bnbPrices = []sdk.Dec{}
|
|
|
|
xrpPrices = []sdk.Dec{}
|
|
|
|
genPrices sync.Once
|
2020-04-12 16:34:01 +00:00
|
|
|
)
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
// Simulation operation weights constants
|
|
|
|
const (
|
|
|
|
OpWeightMsgUpdatePrices = "op_weight_msg_update_prices"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WeightedOperations returns all the operations from the module with their respective weights
|
|
|
|
func WeightedOperations(
|
|
|
|
appParams simulation.AppParams, cdc *codec.Codec, ak auth.AccountKeeper, k keeper.Keeper,
|
|
|
|
) simulation.WeightedOperations {
|
|
|
|
var weightMsgUpdatePrices int
|
|
|
|
// var numBlocks int
|
|
|
|
|
|
|
|
appParams.GetOrGenerate(cdc, OpWeightMsgUpdatePrices, &weightMsgUpdatePrices, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
weightMsgUpdatePrices = appparams.DefaultWeightMsgUpdatePrices
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return simulation.WeightedOperations{
|
|
|
|
simulation.NewWeightedOperation(
|
|
|
|
weightMsgUpdatePrices,
|
|
|
|
SimulateMsgUpdatePrices(ak, k, 10000),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 16:34:01 +00:00
|
|
|
// SimulateMsgUpdatePrices updates the prices of various assets by randomly varying them based on current price
|
2020-04-23 16:35:58 +00:00
|
|
|
func SimulateMsgUpdatePrices(ak auth.AccountKeeper, keeper keeper.Keeper, blocks int) simulation.Operation {
|
|
|
|
return func(
|
|
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, chainID string,
|
|
|
|
) (simulation.OperationMsg, []simulation.FutureOperation, error) {
|
2020-04-12 16:34:01 +00:00
|
|
|
|
2020-04-17 22:29:54 +00:00
|
|
|
genPrices.Do(func() {
|
|
|
|
// generate a random walk for each asset exactly once, with observations equal to the number of blocks in the sim
|
|
|
|
for _, m := range keeper.GetMarkets(ctx) {
|
|
|
|
startPrice := getStartPrice(m.MarketID)
|
|
|
|
// allow prices to fluctuate from 10x GAINZ to 100x REKT
|
|
|
|
maxPrice := sdk.MustNewDecFromStr("10.0").Mul(startPrice)
|
|
|
|
minPrice := sdk.MustNewDecFromStr("0.01").Mul(startPrice)
|
|
|
|
previousPrice := startPrice
|
|
|
|
for i := 0; i < blocks; i++ {
|
|
|
|
increment := getIncrement(m.MarketID)
|
|
|
|
// note calling r instead of rand here breaks determinism
|
|
|
|
upDown := rand.Intn(2)
|
|
|
|
if upDown == 0 {
|
|
|
|
if previousPrice.Add(increment).GT(maxPrice) {
|
|
|
|
previousPrice = maxPrice
|
|
|
|
} else {
|
|
|
|
previousPrice = previousPrice.Add(increment)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if previousPrice.Sub(increment).LT(minPrice) {
|
|
|
|
previousPrice = minPrice
|
|
|
|
} else {
|
|
|
|
previousPrice = previousPrice.Sub(increment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setPrice(m.MarketID, previousPrice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-04-12 16:34:01 +00:00
|
|
|
|
|
|
|
randomMarket := pickRandomAsset(ctx, keeper, r)
|
|
|
|
marketID := randomMarket.MarketID
|
|
|
|
address := getRandomOracle(r, randomMarket)
|
2020-04-23 16:35:58 +00:00
|
|
|
|
|
|
|
oracle, found := simulation.FindAccount(accs, address)
|
|
|
|
if !found {
|
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
oracleAcc := ak.GetAccount(ctx, oracle.Address)
|
|
|
|
if oracleAcc == nil {
|
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, nil
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:29:54 +00:00
|
|
|
price := pickNewRandomPrice(marketID, int(ctx.BlockHeight()))
|
2020-04-12 16:34:01 +00:00
|
|
|
|
|
|
|
// get the expiry time based off the current time
|
|
|
|
expiry := getExpiryTime(ctx)
|
|
|
|
|
|
|
|
// now create the msg to post price
|
2020-04-23 16:35:58 +00:00
|
|
|
msg := types.NewMsgPostPrice(oracle.Address, marketID, price, expiry)
|
2020-04-12 16:34:01 +00:00
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
spendable := oracleAcc.SpendableCoins(ctx.BlockTime())
|
|
|
|
fees, err := simulation.RandomFees(r, ctx, spendable)
|
|
|
|
if err != nil {
|
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, err
|
2020-04-12 16:34:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
tx := helpers.GenTx(
|
|
|
|
[]sdk.Msg{msg},
|
|
|
|
fees,
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
chainID,
|
|
|
|
[]uint64{oracleAcc.GetAccountNumber()},
|
|
|
|
[]uint64{oracleAcc.GetSequence()},
|
|
|
|
oracle.PrivKey,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, result, err := app.Deliver(tx)
|
|
|
|
if err != nil {
|
|
|
|
return simulation.NoOpMsg(types.ModuleName), nil, err
|
2020-04-12 16:34:01 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
return simulation.NewOperationMsg(msg, true, result.Log), nil, nil
|
2020-04-12 16:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:29:54 +00:00
|
|
|
func getStartPrice(marketID string) (startPrice sdk.Dec) {
|
|
|
|
switch marketID {
|
|
|
|
case "btc:usd":
|
|
|
|
return sdk.MustNewDecFromStr("7000")
|
|
|
|
case "bnb:usd":
|
|
|
|
return sdk.MustNewDecFromStr("15")
|
|
|
|
case "xrp:usd":
|
|
|
|
return sdk.MustNewDecFromStr("0.25")
|
|
|
|
}
|
|
|
|
return sdk.MustNewDecFromStr("100")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIncrement(marketID string) (increment sdk.Dec) {
|
|
|
|
startPrice := getStartPrice(marketID)
|
|
|
|
divisor := sdk.MustNewDecFromStr("20")
|
|
|
|
increment = startPrice.Quo(divisor)
|
|
|
|
return increment
|
|
|
|
}
|
|
|
|
|
|
|
|
func setPrice(marketID string, price sdk.Dec) {
|
|
|
|
switch marketID {
|
|
|
|
case "btc:usd":
|
|
|
|
btcPrices = append(btcPrices, price)
|
|
|
|
return
|
|
|
|
case "bnb:usd":
|
|
|
|
bnbPrices = append(bnbPrices, price)
|
|
|
|
return
|
|
|
|
case "xrp:usd":
|
|
|
|
xrpPrices = append(xrpPrices, price)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func pickNewRandomPrice(marketID string, blockHeight int) (newPrice sdk.Dec) {
|
|
|
|
switch marketID {
|
|
|
|
case "btc:usd":
|
|
|
|
return btcPrices[blockHeight-1]
|
|
|
|
case "bnb:usd":
|
|
|
|
return bnbPrices[blockHeight-1]
|
|
|
|
case "xrp:usd":
|
|
|
|
return xrpPrices[blockHeight-1]
|
|
|
|
}
|
|
|
|
panic("invalid price request")
|
|
|
|
}
|
|
|
|
|
2020-04-12 16:34:01 +00:00
|
|
|
// getRandomOracle picks a random oracle from the list of oracles
|
2020-04-23 16:35:58 +00:00
|
|
|
func getRandomOracle(r *rand.Rand, market types.Market) sdk.AccAddress {
|
2020-04-12 16:34:01 +00:00
|
|
|
randomIndex := simulation.RandIntBetween(r, 0, len(market.Oracles))
|
2020-04-23 16:35:58 +00:00
|
|
|
return market.Oracles[randomIndex]
|
2020-04-12 16:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pickRandomAsset picks a random asset out of the assets with equal probability
|
|
|
|
// it returns the Market which includes the base asset as one of its fields
|
|
|
|
func pickRandomAsset(ctx sdk.Context, keeper keeper.Keeper, r *rand.Rand) (market types.Market) {
|
|
|
|
// get the params
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
// now pick a random asset
|
|
|
|
randomIndex := simulation.RandIntBetween(r, 0, len(params.Markets))
|
2020-04-23 16:35:58 +00:00
|
|
|
return params.Markets[randomIndex]
|
2020-04-12 16:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getExpiryTime gets a price expiry time by taking the current time and adding a delta to it
|
|
|
|
func getExpiryTime(ctx sdk.Context) (t time.Time) {
|
|
|
|
// need to use the blocktime from the context as the context generates random start time when running simulations
|
2020-04-23 16:35:58 +00:00
|
|
|
return ctx.BlockTime().Add(time.Second * 1000000)
|
2020-04-12 16:34:01 +00:00
|
|
|
}
|