mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +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>
294 lines
12 KiB
Go
294 lines
12 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"time"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
"github.com/kava-labs/kava/x/cdp"
|
|
"github.com/kava-labs/kava/x/pricefeed"
|
|
)
|
|
|
|
// Avoid cluttering test cases with long function names
|
|
func i(in int64) sdk.Int { return sdk.NewInt(in) }
|
|
func d(str string) sdk.Dec { return sdk.MustNewDecFromStr(str) }
|
|
func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amount) }
|
|
func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) }
|
|
|
|
func NewPricefeedGenState(asset string, price sdk.Dec) app.GenesisState {
|
|
pfGenesis := pricefeed.GenesisState{
|
|
Params: pricefeed.Params{
|
|
Markets: []pricefeed.Market{
|
|
{MarketID: asset + ":usd", BaseAsset: asset, QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
|
},
|
|
},
|
|
PostedPrices: []pricefeed.PostedPrice{
|
|
{
|
|
MarketID: asset + ":usd",
|
|
OracleAddress: sdk.AccAddress{},
|
|
Price: price,
|
|
Expiry: time.Now().Add(1 * time.Hour),
|
|
},
|
|
},
|
|
}
|
|
return app.GenesisState{pricefeed.ModuleName: pricefeed.ModuleCdc.MustMarshalJSON(pfGenesis)}
|
|
}
|
|
|
|
func NewCDPGenState(asset string, liquidationRatio sdk.Dec) app.GenesisState {
|
|
cdpGenesis := cdp.GenesisState{
|
|
Params: cdp.Params{
|
|
GlobalDebtLimit: sdk.NewInt64Coin("usdx", 1000000000000),
|
|
SurplusAuctionThreshold: cdp.DefaultSurplusThreshold,
|
|
SurplusAuctionLot: cdp.DefaultSurplusLot,
|
|
DebtAuctionThreshold: cdp.DefaultDebtThreshold,
|
|
DebtAuctionLot: cdp.DefaultDebtLot,
|
|
CollateralParams: cdp.CollateralParams{
|
|
{
|
|
Denom: asset,
|
|
Type: asset + "-a",
|
|
LiquidationRatio: liquidationRatio,
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 1000000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000001547125958"), // %5 apr
|
|
LiquidationPenalty: d("0.05"),
|
|
AuctionSize: i(100),
|
|
Prefix: 0x20,
|
|
SpotMarketID: asset + ":usd",
|
|
LiquidationMarketID: asset + ":usd",
|
|
KeeperRewardPercentage: d("0.01"),
|
|
CheckCollateralizationIndexCount: i(10),
|
|
ConversionFactor: i(6),
|
|
},
|
|
},
|
|
DebtParam: cdp.DebtParam{
|
|
Denom: "usdx",
|
|
ReferenceAsset: "usd",
|
|
ConversionFactor: i(6),
|
|
DebtFloor: i(10000000),
|
|
},
|
|
},
|
|
StartingCdpID: cdp.DefaultCdpStartingID,
|
|
DebtDenom: cdp.DefaultDebtDenom,
|
|
GovDenom: cdp.DefaultGovDenom,
|
|
CDPs: cdp.CDPs{},
|
|
PreviousAccumulationTimes: cdp.GenesisAccumulationTimes{
|
|
cdp.NewGenesisAccumulationTime(asset+"-a", time.Time{}, sdk.OneDec()),
|
|
},
|
|
TotalPrincipals: cdp.GenesisTotalPrincipals{
|
|
cdp.NewGenesisTotalPrincipal(asset+"-a", sdk.ZeroInt()),
|
|
},
|
|
}
|
|
return app.GenesisState{cdp.ModuleName: cdp.ModuleCdc.MustMarshalJSON(cdpGenesis)}
|
|
}
|
|
|
|
func NewPricefeedGenStateMulti() app.GenesisState {
|
|
pfGenesis := pricefeed.GenesisState{
|
|
Params: pricefeed.Params{
|
|
Markets: []pricefeed.Market{
|
|
{MarketID: "btc:usd", BaseAsset: "btc", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
|
{MarketID: "xrp:usd", BaseAsset: "xrp", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
|
{MarketID: "bnb:usd", BaseAsset: "bnb", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
|
{MarketID: "busd:usd", BaseAsset: "busd", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
|
|
},
|
|
},
|
|
PostedPrices: []pricefeed.PostedPrice{
|
|
{
|
|
MarketID: "btc:usd",
|
|
OracleAddress: sdk.AccAddress{},
|
|
Price: sdk.MustNewDecFromStr("8000.00"),
|
|
Expiry: time.Now().Add(1 * time.Hour),
|
|
},
|
|
{
|
|
MarketID: "xrp:usd",
|
|
OracleAddress: sdk.AccAddress{},
|
|
Price: sdk.MustNewDecFromStr("0.25"),
|
|
Expiry: time.Now().Add(1 * time.Hour),
|
|
},
|
|
{
|
|
MarketID: "bnb:usd",
|
|
OracleAddress: sdk.AccAddress{},
|
|
Price: sdk.MustNewDecFromStr("17.25"),
|
|
Expiry: time.Now().Add(1 * time.Hour),
|
|
},
|
|
{
|
|
MarketID: "busd:usd",
|
|
OracleAddress: sdk.AccAddress{},
|
|
Price: sdk.OneDec(),
|
|
Expiry: time.Now().Add(1 * time.Hour),
|
|
},
|
|
},
|
|
}
|
|
return app.GenesisState{pricefeed.ModuleName: pricefeed.ModuleCdc.MustMarshalJSON(pfGenesis)}
|
|
}
|
|
func NewCDPGenStateMulti() app.GenesisState {
|
|
cdpGenesis := cdp.GenesisState{
|
|
Params: cdp.Params{
|
|
GlobalDebtLimit: sdk.NewInt64Coin("usdx", 2000000000000),
|
|
SurplusAuctionThreshold: cdp.DefaultSurplusThreshold,
|
|
SurplusAuctionLot: cdp.DefaultSurplusLot,
|
|
DebtAuctionThreshold: cdp.DefaultDebtThreshold,
|
|
DebtAuctionLot: cdp.DefaultDebtLot,
|
|
CollateralParams: cdp.CollateralParams{
|
|
{
|
|
Denom: "xrp",
|
|
Type: "xrp-a",
|
|
LiquidationRatio: sdk.MustNewDecFromStr("2.0"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 500000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000001547125958"), // %5 apr
|
|
LiquidationPenalty: d("0.05"),
|
|
AuctionSize: i(7000000000),
|
|
Prefix: 0x20,
|
|
SpotMarketID: "xrp:usd",
|
|
LiquidationMarketID: "xrp:usd",
|
|
KeeperRewardPercentage: d("0.01"),
|
|
CheckCollateralizationIndexCount: i(10),
|
|
ConversionFactor: i(6),
|
|
},
|
|
{
|
|
Denom: "btc",
|
|
Type: "btc-a",
|
|
LiquidationRatio: sdk.MustNewDecFromStr("1.5"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 500000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000000782997609"), // %2.5 apr
|
|
LiquidationPenalty: d("0.025"),
|
|
AuctionSize: i(10000000),
|
|
Prefix: 0x21,
|
|
SpotMarketID: "btc:usd",
|
|
LiquidationMarketID: "btc:usd",
|
|
KeeperRewardPercentage: d("0.01"),
|
|
CheckCollateralizationIndexCount: i(10),
|
|
ConversionFactor: i(8),
|
|
},
|
|
{
|
|
Denom: "bnb",
|
|
Type: "bnb-a",
|
|
LiquidationRatio: sdk.MustNewDecFromStr("1.5"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 500000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000001547125958"), // %5 apr
|
|
LiquidationPenalty: d("0.05"),
|
|
AuctionSize: i(50000000000),
|
|
Prefix: 0x22,
|
|
SpotMarketID: "bnb:usd",
|
|
LiquidationMarketID: "bnb:usd",
|
|
KeeperRewardPercentage: d("0.01"),
|
|
CheckCollateralizationIndexCount: i(10),
|
|
ConversionFactor: i(8),
|
|
},
|
|
{
|
|
Denom: "busd",
|
|
Type: "busd-a",
|
|
LiquidationRatio: d("1.01"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 500000000000),
|
|
StabilityFee: sdk.OneDec(), // %0 apr
|
|
LiquidationPenalty: d("0.05"),
|
|
AuctionSize: i(10000000000),
|
|
Prefix: 0x23,
|
|
SpotMarketID: "busd:usd",
|
|
LiquidationMarketID: "busd:usd",
|
|
KeeperRewardPercentage: d("0.01"),
|
|
CheckCollateralizationIndexCount: i(10),
|
|
ConversionFactor: i(8),
|
|
},
|
|
},
|
|
DebtParam: cdp.DebtParam{
|
|
Denom: "usdx",
|
|
ReferenceAsset: "usd",
|
|
ConversionFactor: i(6),
|
|
DebtFloor: i(10000000),
|
|
},
|
|
},
|
|
StartingCdpID: cdp.DefaultCdpStartingID,
|
|
DebtDenom: cdp.DefaultDebtDenom,
|
|
GovDenom: cdp.DefaultGovDenom,
|
|
CDPs: cdp.CDPs{},
|
|
PreviousAccumulationTimes: cdp.GenesisAccumulationTimes{
|
|
cdp.NewGenesisAccumulationTime("btc-a", time.Time{}, sdk.OneDec()),
|
|
cdp.NewGenesisAccumulationTime("xrp-a", time.Time{}, sdk.OneDec()),
|
|
cdp.NewGenesisAccumulationTime("busd-a", time.Time{}, sdk.OneDec()),
|
|
cdp.NewGenesisAccumulationTime("bnb-a", time.Time{}, sdk.OneDec()),
|
|
},
|
|
TotalPrincipals: cdp.GenesisTotalPrincipals{
|
|
cdp.NewGenesisTotalPrincipal("btc-a", sdk.ZeroInt()),
|
|
cdp.NewGenesisTotalPrincipal("xrp-a", sdk.ZeroInt()),
|
|
cdp.NewGenesisTotalPrincipal("busd-a", sdk.ZeroInt()),
|
|
cdp.NewGenesisTotalPrincipal("bnb-a", sdk.ZeroInt()),
|
|
},
|
|
}
|
|
return app.GenesisState{cdp.ModuleName: cdp.ModuleCdc.MustMarshalJSON(cdpGenesis)}
|
|
}
|
|
|
|
func NewCDPGenStateHighDebtLimit() app.GenesisState {
|
|
cdpGenesis := cdp.GenesisState{
|
|
Params: cdp.Params{
|
|
GlobalDebtLimit: sdk.NewInt64Coin("usdx", 100000000000000),
|
|
SurplusAuctionThreshold: cdp.DefaultSurplusThreshold,
|
|
SurplusAuctionLot: cdp.DefaultSurplusLot,
|
|
DebtAuctionThreshold: cdp.DefaultDebtThreshold,
|
|
DebtAuctionLot: cdp.DefaultDebtLot,
|
|
CollateralParams: cdp.CollateralParams{
|
|
{
|
|
Denom: "xrp",
|
|
Type: "xrp-a",
|
|
LiquidationRatio: sdk.MustNewDecFromStr("2.0"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 50000000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000001547125958"), // %5 apr
|
|
LiquidationPenalty: d("0.05"),
|
|
AuctionSize: i(7000000000),
|
|
Prefix: 0x20,
|
|
SpotMarketID: "xrp:usd",
|
|
LiquidationMarketID: "xrp:usd",
|
|
KeeperRewardPercentage: d("0.01"),
|
|
CheckCollateralizationIndexCount: i(10),
|
|
ConversionFactor: i(6),
|
|
},
|
|
{
|
|
Denom: "btc",
|
|
Type: "btc-a",
|
|
LiquidationRatio: sdk.MustNewDecFromStr("1.5"),
|
|
DebtLimit: sdk.NewInt64Coin("usdx", 50000000000000),
|
|
StabilityFee: sdk.MustNewDecFromStr("1.000000000782997609"), // %2.5 apr
|
|
LiquidationPenalty: d("0.025"),
|
|
AuctionSize: i(10000000),
|
|
Prefix: 0x21,
|
|
SpotMarketID: "btc:usd",
|
|
LiquidationMarketID: "btc:usd",
|
|
KeeperRewardPercentage: d("0.01"),
|
|
CheckCollateralizationIndexCount: i(10),
|
|
ConversionFactor: i(8),
|
|
},
|
|
},
|
|
DebtParam: cdp.DebtParam{
|
|
Denom: "usdx",
|
|
ReferenceAsset: "usd",
|
|
ConversionFactor: i(6),
|
|
DebtFloor: i(10000000),
|
|
},
|
|
},
|
|
StartingCdpID: cdp.DefaultCdpStartingID,
|
|
DebtDenom: cdp.DefaultDebtDenom,
|
|
GovDenom: cdp.DefaultGovDenom,
|
|
CDPs: cdp.CDPs{},
|
|
PreviousAccumulationTimes: cdp.GenesisAccumulationTimes{
|
|
cdp.NewGenesisAccumulationTime("btc-a", time.Time{}, sdk.OneDec()),
|
|
cdp.NewGenesisAccumulationTime("xrp-a", time.Time{}, sdk.OneDec()),
|
|
},
|
|
TotalPrincipals: cdp.GenesisTotalPrincipals{
|
|
cdp.NewGenesisTotalPrincipal("btc-a", sdk.ZeroInt()),
|
|
cdp.NewGenesisTotalPrincipal("xrp-a", sdk.ZeroInt()),
|
|
},
|
|
}
|
|
return app.GenesisState{cdp.ModuleName: cdp.ModuleCdc.MustMarshalJSON(cdpGenesis)}
|
|
}
|
|
|
|
func cdps() (cdps cdp.CDPs) {
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(3)
|
|
c1 := cdp.NewCDP(uint64(1), addrs[0], sdk.NewCoin("xrp", sdk.NewInt(10000000)), "xrp-a", sdk.NewCoin("usdx", sdk.NewInt(8000000)), tmtime.Canonical(time.Now()), sdk.OneDec())
|
|
c2 := cdp.NewCDP(uint64(2), addrs[1], sdk.NewCoin("xrp", sdk.NewInt(100000000)), "xrp-a", sdk.NewCoin("usdx", sdk.NewInt(10000000)), tmtime.Canonical(time.Now()), sdk.OneDec())
|
|
c3 := cdp.NewCDP(uint64(3), addrs[1], sdk.NewCoin("btc", sdk.NewInt(1000000000)), "btc-a", sdk.NewCoin("usdx", sdk.NewInt(10000000)), tmtime.Canonical(time.Now()), sdk.OneDec())
|
|
c4 := cdp.NewCDP(uint64(4), addrs[2], sdk.NewCoin("xrp", sdk.NewInt(1000000000)), "xrp-a", sdk.NewCoin("usdx", sdk.NewInt(500000000)), tmtime.Canonical(time.Now()), sdk.OneDec())
|
|
cdps = append(cdps, c1, c2, c3, c4)
|
|
return
|
|
}
|