Documentation ¶
Overview ¶
Package CDP manages the storage of Collateralized Debt Positions. It handles their creation, modification, and stores the global state of all CDPs.
Notes
- sdk.Int is used for all the number types to maintain compatibility with internal type of sdk.Coin - saves type conversion when doing maths. Also it allows for changes to a CDP to be expressed as a +ve or -ve number.
- Only allowing one CDP per account-collateralDenom pair for now to keep things simple.
- Genesis forces the global debt to start at zero, ie no stable coins in existence. This could be changed.
- The cdp module fulfills the bank keeper interface and keeps track of the liquidator module's coins. This won't be needed with module accounts.
- GetCDPs does not return an iterator, but instead reads out (potentially) all CDPs from the store. This isn't a huge performance concern as it is never used during a block, only for querying. An iterator could be created, following the queue style construct in gov and auction, where CDP IDs are stored under ordered keys. These keys could be a collateral-denom:collateral-ratio so that it is efficient to obtain the undercollateralized CDP for a given price and liquidation ratio. However creating a byte sortable representation of a collateral ratio wasn't very easy so the simpler approach was chosen.
TODO
- A shorter name for an under-collateralized CDP would shorten a lot of function names
- remove fake bank keeper and setup a proper liquidator module account
- what happens if a collateral type is removed from the list of allowed ones?
- Should the values used to generate a key for a stored struct be in the struct?
- Add constants for the module and route names
- Many more TODOs in the code
- add more aggressive test cases
- tags
- custom error types, codespace
Index ¶
- Constants
- Variables
- func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState)
- func NewHandler(keeper Keeper) sdk.Handler
- func NewQuerier(keeper Keeper) sdk.Querier
- func RegisterCodec(cdc *codec.Codec)
- func ValidateGenesis(data GenesisState) error
- type AppModule
- func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) sdk.Tags
- func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, sdk.Tags)
- func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage
- func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate
- func (AppModule) Name() string
- func (am AppModule) NewHandler() sdk.Handler
- func (am AppModule) NewQuerierHandler() sdk.Querier
- func (AppModule) QuerierRoute() string
- func (AppModule) RegisterInvariants(_ sdk.InvariantRouter)
- func (AppModule) Route() string
- type AppModuleBasic
- type CDP
- type CDPs
- type CdpModuleParams
- type CollateralParams
- type CollateralState
- type GenesisState
- type Keeper
- func (k Keeper) AddCoins(ctx sdk.Context, address sdk.AccAddress, amount sdk.Coins) (sdk.Coins, sdk.Error)
- func (k Keeper) GetCDP(ctx sdk.Context, owner sdk.AccAddress, collateralDenom string) (CDP, bool)
- func (k Keeper) GetCDPs(ctx sdk.Context, collateralDenom string, price sdk.Dec) (CDPs, sdk.Error)
- func (k Keeper) GetCoins(ctx sdk.Context, address sdk.AccAddress) sdk.Coins
- func (k Keeper) GetCollateralState(ctx sdk.Context, collateralDenom string) (CollateralState, bool)
- func (k Keeper) GetGlobalDebt(ctx sdk.Context) sdk.Int
- func (k Keeper) GetGovDenom() string
- func (k Keeper) GetLiquidatorAccountAddress() sdk.AccAddress
- func (k Keeper) GetParams(ctx sdk.Context) CdpModuleParams
- func (k Keeper) GetStableDenom() string
- func (k Keeper) HasCoins(ctx sdk.Context, address sdk.AccAddress, amount sdk.Coins) bool
- func (k Keeper) ModifyCDP(ctx sdk.Context, owner sdk.AccAddress, collateralDenom string, ...) sdk.Error
- func (k Keeper) PartialSeizeCDP(ctx sdk.Context, owner sdk.AccAddress, collateralDenom string, ...) sdk.Error
- func (k Keeper) ReduceGlobalDebt(ctx sdk.Context, amount sdk.Int) sdk.Error
- func (k Keeper) SubtractCoins(ctx sdk.Context, address sdk.AccAddress, amount sdk.Coins) (sdk.Coins, sdk.Error)
- type LiquidatorModuleAccount
- type MsgCreateOrModifyCDP
- type MsgTransferCDP
- type QueryCdpsParams
Constants ¶
const ( QueryGetCdps = "cdps" QueryGetParams = "params" )
const GovDenom = "kava"
GovDenom asset code of the governance coin
const ModuleName = "cdp"
ModuleName name of module
const StableDenom = "usdx" // TODO allow to be changed
StableDenom asset code of the dollar-denominated debt coin
Variables ¶
var LiquidatorAccountAddress = sdk.AccAddress([]byte("whatever"))
Functions ¶
func InitGenesis ¶
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState)
InitGenesis sets the genesis state in the keeper.
func NewQuerier ¶
func RegisterCodec ¶
RegisterCodec registers concrete types on the codec.
func ValidateGenesis ¶
func ValidateGenesis(data GenesisState) error
ValidateGenesis performs basic validation of genesis data returning an error for any failed validation criteria.
Types ¶
type AppModule ¶
type AppModule struct { AppModuleBasic // contains filtered or unexported fields }
AppModule app module type
func NewAppModule ¶
NewAppModule creates a new AppModule object
func (AppModule) BeginBlock ¶
BeginBlock module begin-block
func (AppModule) EndBlock ¶
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, sdk.Tags)
EndBlock module end-block
func (AppModule) ExportGenesis ¶
func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage
ExportGenesis module export genesis
func (AppModule) InitGenesis ¶
func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate
InitGenesis module init-genesis
func (AppModule) NewHandler ¶
NewHandler module handler
func (AppModule) NewQuerierHandler ¶
NewQuerierHandler module querier
func (AppModule) QuerierRoute ¶
QuerierRoute module querier route name
func (AppModule) RegisterInvariants ¶
func (AppModule) RegisterInvariants(_ sdk.InvariantRouter)
RegisterInvariants register module invariants
type AppModuleBasic ¶
type AppModuleBasic struct{}
AppModuleBasic app module basics object
func (AppModuleBasic) DefaultGenesis ¶
func (AppModuleBasic) DefaultGenesis() json.RawMessage
DefaultGenesis default genesis state
func (AppModuleBasic) RegisterCodec ¶
func (AppModuleBasic) RegisterCodec(cdc *codec.Codec)
RegisterCodec register module codec
func (AppModuleBasic) ValidateGenesis ¶
func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error
ValidateGenesis module validate genesis
type CDP ¶
type CDP struct { //ID []byte // removing IDs for now to make things simpler Owner sdk.AccAddress `json:"owner"` // Account that authorizes changes to the CDP CollateralDenom string `json:"collateral_denom"` // Type of collateral stored in this CDP CollateralAmount sdk.Int `json:"collateral_amount"` // Amount of collateral stored in this CDP Debt sdk.Int `json:"debt"` // Amount of stable coin drawn from this CDP }
CDP is the state of a single Collateralized Debt Position.
func (CDP) IsUnderCollateralized ¶
type CdpModuleParams ¶
type CdpModuleParams struct { GlobalDebtLimit sdk.Int CollateralParams []CollateralParams }
func (CdpModuleParams) GetCollateralParams ¶
func (p CdpModuleParams) GetCollateralParams(collateralDenom string) CollateralParams
func (CdpModuleParams) IsCollateralPresent ¶
func (p CdpModuleParams) IsCollateralPresent(collateralDenom string) bool
func (CdpModuleParams) String ¶
func (p CdpModuleParams) String() string
Implement fmt.Stringer interface for cli querying
type CollateralParams ¶
type CollateralState ¶
type CollateralState struct { Denom string // Type of collateral TotalDebt sdk.Int // total debt collateralized by a this coin type }
CollateralState stores global information tied to a particular collateral type.
type GenesisState ¶
type GenesisState struct { CdpModuleParams CdpModuleParams `json:"params"` GlobalDebt sdk.Int `json:"global_debt"` }
GenesisState is the state that must be provided at genesis.
func DefaultGenesisState ¶
func DefaultGenesisState() GenesisState
DefaultGenesisState returns a default genesis state
func ExportGenesis ¶
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState
ExportGenesis returns a GenesisState for a given context and keeper.
type Keeper ¶
type Keeper struct {
// contains filtered or unexported fields
}
Keeper cdp Keeper
func NewKeeper ¶
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, subspace params.Subspace, pricefeed pricefeedKeeper, bank bankKeeper) Keeper
NewKeeper creates a new keeper
func (Keeper) GetCDPs ¶
GetCDPs returns all CDPs, optionally filtered by collateral type and liquidation price. `price` filters for CDPs that will be below the liquidation ratio when the collateral is at that specified price.
func (Keeper) GetCoins ¶
TODO Should this return anything for the gov coin balance? Currently returns nothing.
func (Keeper) GetCollateralState ¶
func (Keeper) GetGovDenom ¶
func (Keeper) GetLiquidatorAccountAddress ¶
func (k Keeper) GetLiquidatorAccountAddress() sdk.AccAddress
func (Keeper) GetStableDenom ¶
func (Keeper) ModifyCDP ¶
func (k Keeper) ModifyCDP(ctx sdk.Context, owner sdk.AccAddress, collateralDenom string, changeInCollateral sdk.Int, changeInDebt sdk.Int) sdk.Error
ModifyCDP creates, changes, or deletes a CDP TODO can/should this function be split up?
func (Keeper) PartialSeizeCDP ¶
func (k Keeper) PartialSeizeCDP(ctx sdk.Context, owner sdk.AccAddress, collateralDenom string, collateralToSeize sdk.Int, debtToSeize sdk.Int) sdk.Error
PartialSeizeCDP removes collateral and debt from a CDP and decrements global debt counters. It does not move collateral to another account so is unsafe. TODO should this be made safer by moving collateral to liquidatorModuleAccount ? If so how should debt be moved?
func (Keeper) ReduceGlobalDebt ¶
ReduceGlobalDebt decreases the stored global debt counter. It is used by the liquidator when it annihilates debt and stable coin. TODO Can the interface between cdp and liquidator modules be improved so that this function doesn't exist?
type LiquidatorModuleAccount ¶
type MsgCreateOrModifyCDP ¶
type MsgCreateOrModifyCDP struct { Sender sdk.AccAddress CollateralDenom string CollateralChange sdk.Int DebtChange sdk.Int }
MsgCreateOrModifyCDP creates, adds/removes collateral/stable coin from a cdp TODO Make this more user friendly - maybe split into four functions.
func NewMsgCreateOrModifyCDP ¶
func NewMsgCreateOrModifyCDP(sender sdk.AccAddress, collateralDenom string, collateralChange sdk.Int, debtChange sdk.Int) MsgCreateOrModifyCDP
NewMsgPlaceBid returns a new MsgPlaceBid.
func (MsgCreateOrModifyCDP) GetSignBytes ¶
func (msg MsgCreateOrModifyCDP) GetSignBytes() []byte
GetSignBytes gets the canonical byte representation of the Msg.
func (MsgCreateOrModifyCDP) GetSigners ¶
func (msg MsgCreateOrModifyCDP) GetSigners() []sdk.AccAddress
GetSigners returns the addresses of signers that must sign.
func (MsgCreateOrModifyCDP) Route ¶
func (msg MsgCreateOrModifyCDP) Route() string
Route return the message type used for routing the message.
func (MsgCreateOrModifyCDP) Type ¶
func (msg MsgCreateOrModifyCDP) Type() string
Type returns a human-readable string for the message, intended for utilization within tags.
func (MsgCreateOrModifyCDP) ValidateBasic ¶
func (msg MsgCreateOrModifyCDP) ValidateBasic() sdk.Error
ValidateBasic does a simple validation check that doesn't require access to any other information.
type QueryCdpsParams ¶
type QueryCdpsParams struct { CollateralDenom string // get CDPs with this collateral denom Owner sdk.AccAddress // get CDPs belonging to this owner UnderCollateralizedAt sdk.Dec // get CDPs that will be below the liquidation ratio when the collateral is at this price. }