cdp

package
v0.0.0-...-55fa119 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2019 License: Apache-2.0 Imports: 12 Imported by: 0

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

View Source
const (
	QueryGetCdps   = "cdps"
	QueryGetParams = "params"
)
View Source
const GovDenom = "kava"

GovDenom asset code of the governance coin

View Source
const ModuleName = "cdp"

ModuleName name of module

View Source
const StableDenom = "usdx" // TODO allow to be changed

StableDenom asset code of the dollar-denominated debt coin

Variables

View Source
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 NewHandler

func NewHandler(keeper Keeper) sdk.Handler

Handle all cdp messages.

func NewQuerier

func NewQuerier(keeper Keeper) sdk.Querier

func RegisterCodec

func RegisterCodec(cdc *codec.Codec)

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

func NewAppModule(keeper Keeper) AppModule

NewAppModule creates a new AppModule object

func (AppModule) BeginBlock

func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) sdk.Tags

BeginBlock module begin-block

func (AppModule) EndBlock

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) Name

func (AppModule) Name() string

Name module name

func (AppModule) NewHandler

func (am AppModule) NewHandler() sdk.Handler

NewHandler module handler

func (AppModule) NewQuerierHandler

func (am AppModule) NewQuerierHandler() sdk.Querier

NewQuerierHandler module querier

func (AppModule) QuerierRoute

func (AppModule) QuerierRoute() string

QuerierRoute module querier route name

func (AppModule) RegisterInvariants

func (AppModule) RegisterInvariants(_ sdk.InvariantRouter)

RegisterInvariants register module invariants

func (AppModule) Route

func (AppModule) Route() string

Route module message route name

type AppModuleBasic

type AppModuleBasic struct{}

AppModuleBasic app module basics object

func (AppModuleBasic) DefaultGenesis

func (AppModuleBasic) DefaultGenesis() json.RawMessage

DefaultGenesis default genesis state

func (AppModuleBasic) Name

func (AppModuleBasic) Name() string

Name get module name

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

func (cdp CDP) IsUnderCollateralized(price sdk.Dec, liquidationRatio sdk.Dec) bool

func (CDP) String

func (cdp CDP) String() string

type CDPs

type CDPs []CDP

func (CDPs) String

func (cdps CDPs) String() string

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 CollateralParams struct {
	Denom            string  // Coin name of collateral type
	LiquidationRatio sdk.Dec // The ratio (Collateral (priced in stable coin) / Debt) under which a CDP will be liquidated
	DebtLimit        sdk.Int // Maximum amount of debt allowed to be drawn from this collateral type

}

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) AddCoins

func (k Keeper) AddCoins(ctx sdk.Context, address sdk.AccAddress, amount sdk.Coins) (sdk.Coins, sdk.Error)

func (Keeper) GetCDP

func (k Keeper) GetCDP(ctx sdk.Context, owner sdk.AccAddress, collateralDenom string) (CDP, bool)

func (Keeper) GetCDPs

func (k Keeper) GetCDPs(ctx sdk.Context, collateralDenom string, price sdk.Dec) (CDPs, sdk.Error)

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

func (k Keeper) GetCoins(ctx sdk.Context, address sdk.AccAddress) sdk.Coins

TODO Should this return anything for the gov coin balance? Currently returns nothing.

func (Keeper) GetCollateralState

func (k Keeper) GetCollateralState(ctx sdk.Context, collateralDenom string) (CollateralState, bool)

func (Keeper) GetGlobalDebt

func (k Keeper) GetGlobalDebt(ctx sdk.Context) sdk.Int

func (Keeper) GetGovDenom

func (k Keeper) GetGovDenom() string

func (Keeper) GetLiquidatorAccountAddress

func (k Keeper) GetLiquidatorAccountAddress() sdk.AccAddress

func (Keeper) GetParams

func (k Keeper) GetParams(ctx sdk.Context) CdpModuleParams

func (Keeper) GetStableDenom

func (k Keeper) GetStableDenom() string

func (Keeper) HasCoins

func (k Keeper) HasCoins(ctx sdk.Context, address sdk.AccAddress, amount sdk.Coins) bool

TODO test this with unsorted coins

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

func (k Keeper) ReduceGlobalDebt(ctx sdk.Context, amount sdk.Int) sdk.Error

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?

func (Keeper) SubtractCoins

func (k Keeper) SubtractCoins(ctx sdk.Context, address sdk.AccAddress, amount sdk.Coins) (sdk.Coins, sdk.Error)

TODO abstract stuff better

type LiquidatorModuleAccount

type LiquidatorModuleAccount struct {
	Coins sdk.Coins // keeps track of seized collateral, surplus usdx, and mints/burns gov coins
}

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 MsgTransferCDP

type MsgTransferCDP struct {
}

MsgTransferCDP changes the ownership of a cdp

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.
}

Directories

Path Synopsis
cli

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL