mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-14 03:55:19 +00:00
c63ecf908a
* Add 'InterestFactor' to CDP type (#734) * update cdp type to include interest factor * fix build * Add cdp accumulator methods (#735) * remame fees to interest * add accumulate interest method * add basic test * add note * address review comments * update tests * Add sync cdp interest method (#737) * remame fees to interest * add accumulate interest method * add basic test * add note * address review comments * update tests * remove old fee functions * add method to synchronize cdp interest * add multi-cdp tests * add test with many blocks * add test for interest getter * address review comments * calculate time difference then convert to seconds * fix: update collateral index when syncing interest * fix: differentiate between case when apy is zero and all fees are being rounded to zero * fix: round time difference properly * update cdp genesis state and migrations (#738) * remame fees to interest * add accumulate interest method * add basic test * add note * address review comments * update tests * remove old fee functions * add method to synchronize cdp interest * add multi-cdp tests * add test with many blocks * add test for interest getter * update cdp genesis state and migrations * address review comments * calculate time difference then convert to seconds * fix: update collateral index when syncing interest * fix: differentiate between case when apy is zero and all fees are being rounded to zero * fix: simplify add/remove/update collateral index * update genesis state to include total principal amounts * update migration * Delete kava-4-cdp-state-block-500000.json * Add cdp liquidations by external keeper (#750) * feat: split liquidations between external keepers and automated begin blocker * address review comments * USDX incentive accumulators (#752) * feat: split liquidations between external keepers and automated begin blocker * wip: refactor usdx minting incentives to use accumulators/hooks * wip: refactor usdx minting claim object * feat: use accumulators/hooks for usdx minting rewards * fix: get tests passing * fix: don't create claim objects unless that cdp type is eligable for rewards * add begin blocker * update client * cleanup comments/tests * update querier * address review comments * fix: check for division by zero * address review comments * run hook before interest is synced * Remove savings rate (#764) * remove savings rate * remove savings rate from debt param * update migrations * address review comments * Add usdx incentives calculation test (#765) * add usdx incentive calculation test * update reward calculation * add allowable error to test criteria * Update x/incentive/keeper/rewards_test.go Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> * fix: remove old fields from test genesis state Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
170 lines
6.4 KiB
Go
170 lines
6.4 KiB
Go
package simulation
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
|
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
)
|
|
|
|
// RandomizedGenState generates a random GenesisState for cdp
|
|
func RandomizedGenState(simState *module.SimulationState) {
|
|
|
|
cdpGenesis := randomCdpGenState(simState.Rand.Intn(2))
|
|
|
|
// hacky way to give accounts coins so they can create cdps (coins includes usdx so it's possible to have sufficient balance to close a cdp)
|
|
var authGenesis auth.GenesisState
|
|
simState.Cdc.MustUnmarshalJSON(simState.GenState[auth.ModuleName], &authGenesis)
|
|
totalCdpCoins := sdk.NewCoins()
|
|
for _, acc := range authGenesis.Accounts {
|
|
_, ok := acc.(supplyexported.ModuleAccountI)
|
|
if ok {
|
|
continue
|
|
}
|
|
coinsToAdd := sdk.NewCoins(
|
|
sdk.NewCoin("bnb", sdk.NewInt(int64(simState.Rand.Intn(100000000000)))),
|
|
sdk.NewCoin("xrp", sdk.NewInt(int64(simState.Rand.Intn(100000000000)))),
|
|
sdk.NewCoin("btc", sdk.NewInt(int64(simState.Rand.Intn(500000000)))),
|
|
sdk.NewCoin("usdx", sdk.NewInt(int64(simState.Rand.Intn(1000000000)))),
|
|
sdk.NewCoin("ukava", sdk.NewInt(int64(simState.Rand.Intn(500000000000)))),
|
|
)
|
|
err := acc.SetCoins(acc.GetCoins().Add(coinsToAdd...))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
totalCdpCoins = totalCdpCoins.Add(coinsToAdd...)
|
|
authGenesis.Accounts = replaceOrAppendAccount(authGenesis.Accounts, acc)
|
|
}
|
|
simState.GenState[auth.ModuleName] = simState.Cdc.MustMarshalJSON(authGenesis)
|
|
|
|
var supplyGenesis supply.GenesisState
|
|
simState.Cdc.MustUnmarshalJSON(simState.GenState[supply.ModuleName], &supplyGenesis)
|
|
supplyGenesis.Supply = supplyGenesis.Supply.Add(totalCdpCoins...)
|
|
simState.GenState[supply.ModuleName] = simState.Cdc.MustMarshalJSON(supplyGenesis)
|
|
|
|
fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, codec.MustMarshalJSONIndent(simState.Cdc, cdpGenesis))
|
|
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(cdpGenesis)
|
|
}
|
|
|
|
// In a list of accounts, replace the first account found with the same address. If not found, append the account.
|
|
func replaceOrAppendAccount(accounts []authexported.GenesisAccount, acc authexported.GenesisAccount) []authexported.GenesisAccount {
|
|
newAccounts := accounts
|
|
for i, a := range accounts {
|
|
if a.GetAddress().Equals(acc.GetAddress()) {
|
|
newAccounts[i] = acc
|
|
return newAccounts
|
|
}
|
|
}
|
|
return append(newAccounts, acc)
|
|
}
|
|
|
|
func randomCdpGenState(selection int) types.GenesisState {
|
|
switch selection {
|
|
case 0:
|
|
return types.GenesisState{
|
|
Params: types.Params{
|
|
GlobalDebtLimit: sdk.NewInt64Coin("usdx", 100000000000000),
|
|
SurplusAuctionThreshold: types.DefaultSurplusThreshold,
|
|
SurplusAuctionLot: types.DefaultSurplusLot,
|
|
DebtAuctionLot: types.DefaultDebtLot,
|
|
DebtAuctionThreshold: types.DefaultDebtThreshold,
|
|
CollateralParams: types.CollateralParams{
|
|
{
|
|
Denom: "xrp",
|
|
Type: "xrp-a",
|
|
LiquidationRatio: sdk.MustNewDecFromStr("2.0"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 20000000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000004431822130"),
|
|
LiquidationPenalty: sdk.MustNewDecFromStr("0.075"),
|
|
AuctionSize: sdk.NewInt(100000000000),
|
|
Prefix: 0x20,
|
|
SpotMarketID: "xrp:usd",
|
|
LiquidationMarketID: "xrp:usd",
|
|
ConversionFactor: sdk.NewInt(6),
|
|
},
|
|
{
|
|
Denom: "btc",
|
|
Type: "btc-a",
|
|
LiquidationRatio: sdk.MustNewDecFromStr("1.25"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 50000000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000000782997609"),
|
|
LiquidationPenalty: sdk.MustNewDecFromStr("0.05"),
|
|
AuctionSize: sdk.NewInt(1000000000),
|
|
Prefix: 0x21,
|
|
SpotMarketID: "btc:usd",
|
|
LiquidationMarketID: "btc:usd",
|
|
ConversionFactor: sdk.NewInt(8),
|
|
},
|
|
{
|
|
Denom: "bnb",
|
|
Type: "bnb-a",
|
|
LiquidationRatio: sdk.MustNewDecFromStr("1.5"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 30000000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000002293273137"),
|
|
LiquidationPenalty: sdk.MustNewDecFromStr("0.15"),
|
|
AuctionSize: sdk.NewInt(1000000000000),
|
|
Prefix: 0x22,
|
|
SpotMarketID: "bnb:usd",
|
|
LiquidationMarketID: "bnb:usd",
|
|
ConversionFactor: sdk.NewInt(8),
|
|
},
|
|
},
|
|
DebtParam: types.DebtParam{
|
|
Denom: "usdx",
|
|
ReferenceAsset: "usd",
|
|
ConversionFactor: sdk.NewInt(6),
|
|
DebtFloor: sdk.NewInt(10000000),
|
|
},
|
|
},
|
|
StartingCdpID: types.DefaultCdpStartingID,
|
|
DebtDenom: types.DefaultDebtDenom,
|
|
GovDenom: types.DefaultGovDenom,
|
|
CDPs: types.CDPs{},
|
|
}
|
|
case 1:
|
|
return types.GenesisState{
|
|
Params: types.Params{
|
|
GlobalDebtLimit: sdk.NewInt64Coin("usdx", 100000000000000),
|
|
SurplusAuctionThreshold: types.DefaultSurplusThreshold,
|
|
DebtAuctionThreshold: types.DefaultDebtThreshold,
|
|
SurplusAuctionLot: types.DefaultSurplusLot,
|
|
DebtAuctionLot: types.DefaultDebtLot,
|
|
CollateralParams: types.CollateralParams{
|
|
{
|
|
Denom: "bnb",
|
|
Type: "bnb-a",
|
|
LiquidationRatio: sdk.MustNewDecFromStr("1.5"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 100000000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000002293273137"),
|
|
LiquidationPenalty: sdk.MustNewDecFromStr("0.075"),
|
|
AuctionSize: sdk.NewInt(10000000000),
|
|
Prefix: 0x20,
|
|
SpotMarketID: "bnb:usd",
|
|
LiquidationMarketID: "bnb:usd",
|
|
ConversionFactor: sdk.NewInt(8),
|
|
},
|
|
},
|
|
DebtParam: types.DebtParam{
|
|
Denom: "usdx",
|
|
ReferenceAsset: "usd",
|
|
ConversionFactor: sdk.NewInt(6),
|
|
DebtFloor: sdk.NewInt(10000000),
|
|
},
|
|
},
|
|
StartingCdpID: types.DefaultCdpStartingID,
|
|
DebtDenom: types.DefaultDebtDenom,
|
|
GovDenom: types.DefaultGovDenom,
|
|
CDPs: types.CDPs{},
|
|
}
|
|
default:
|
|
panic("invalid genesis state selector")
|
|
}
|
|
}
|