2020-02-25 15:11:09 +00:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-12 16:34:01 +00:00
|
|
|
"time"
|
2020-02-25 15:11:09 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-04-14 01:29:46 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-25 15:11:09 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2020-04-12 16:34:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-02-25 15:11:09 +00:00
|
|
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
2020-04-12 16:34:01 +00:00
|
|
|
pricefeed "github.com/kava-labs/kava/x/pricefeed/types"
|
2020-02-25 15:11:09 +00:00
|
|
|
)
|
|
|
|
|
2020-04-17 22:29:54 +00:00
|
|
|
var (
|
|
|
|
// BaseAssets is a list of collateral asset denoms
|
|
|
|
BaseAssets = [3]string{"bnb", "xrp", "btc"}
|
|
|
|
QuoteAsset = "usd"
|
|
|
|
)
|
|
|
|
|
2020-02-25 15:11:09 +00:00
|
|
|
// RandomizedGenState generates a random GenesisState for pricefeed
|
|
|
|
func RandomizedGenState(simState *module.SimulationState) {
|
2020-04-17 22:29:54 +00:00
|
|
|
pricefeedGenesis := loadPricefeedGenState(simState)
|
2020-02-25 15:11:09 +00:00
|
|
|
fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, codec.MustMarshalJSONIndent(simState.Cdc, pricefeedGenesis))
|
|
|
|
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(pricefeedGenesis)
|
|
|
|
}
|
2020-04-12 16:34:01 +00:00
|
|
|
|
2020-04-17 22:29:54 +00:00
|
|
|
// loadPricefeedGenState loads a valid pricefeed gen state
|
|
|
|
func loadPricefeedGenState(simState *module.SimulationState) pricefeed.GenesisState {
|
|
|
|
var markets []pricefeed.Market
|
|
|
|
var postedPrices []pricefeed.PostedPrice
|
|
|
|
for _, denom := range BaseAssets {
|
|
|
|
// Select an account to be the oracle
|
2020-04-23 16:35:58 +00:00
|
|
|
oracle, _ := simulation.RandomAcc(simState.Rand, simState.Accounts)
|
2020-04-17 22:29:54 +00:00
|
|
|
|
|
|
|
marketID := fmt.Sprintf("%s:%s", denom, QuoteAsset)
|
|
|
|
// Construct market for asset
|
|
|
|
market := pricefeed.Market{
|
|
|
|
MarketID: marketID,
|
|
|
|
BaseAsset: denom,
|
|
|
|
QuoteAsset: QuoteAsset,
|
|
|
|
Oracles: []sdk.AccAddress{oracle.Address},
|
|
|
|
Active: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct posted price for asset
|
|
|
|
postedPrice := pricefeed.PostedPrice{
|
|
|
|
MarketID: market.MarketID,
|
|
|
|
OracleAddress: oracle.Address,
|
|
|
|
Price: getInitialPrice(marketID),
|
|
|
|
Expiry: simState.GenTimestamp.Add(time.Hour * 24),
|
|
|
|
}
|
|
|
|
markets = append(markets, market)
|
|
|
|
postedPrices = append(postedPrices, postedPrice)
|
2020-04-12 16:34:01 +00:00
|
|
|
}
|
2020-04-17 22:29:54 +00:00
|
|
|
params := pricefeed.NewParams(markets)
|
|
|
|
return pricefeed.NewGenesisState(params, postedPrices)
|
2020-04-12 16:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getInitialPrice gets the starting price for each of the base assets
|
2020-04-17 22:29:54 +00:00
|
|
|
func getInitialPrice(marketID string) (price sdk.Dec) {
|
|
|
|
switch marketID {
|
2020-04-12 16:34:01 +00:00
|
|
|
case "btc:usd":
|
|
|
|
return sdk.MustNewDecFromStr("7000")
|
|
|
|
case "bnb:usd":
|
|
|
|
return sdk.MustNewDecFromStr("14")
|
|
|
|
case "xrp:usd":
|
|
|
|
return sdk.MustNewDecFromStr("0.2")
|
2020-04-17 22:29:54 +00:00
|
|
|
default:
|
|
|
|
return sdk.MustNewDecFromStr("20") // Catch future additional assets
|
2020-04-12 16:34:01 +00:00
|
|
|
}
|
|
|
|
}
|