Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collateral type to cdp #629

Merged
merged 8 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions rest_test/setup/setuptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func sendBtcCdp() {
addr,
sdk.NewInt64Coin("btc", 200000000),
sdk.NewInt64Coin("usdx", 10000000),
"btc-a",
)

// helper methods for transactions
Expand Down Expand Up @@ -154,6 +155,7 @@ func sendXrpCdp() {
addr,
sdk.NewInt64Coin("xrp", 200000000),
sdk.NewInt64Coin("usdx", 10000000),
"xrp-a",
)

// helper methods for transactions
Expand Down
4 changes: 2 additions & 2 deletions x/cdp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {
continue
}

err := k.UpdateFeesForAllCdps(ctx, cp.Denom)
err := k.UpdateFeesForAllCdps(ctx, cp.Type)
if err != nil {
panic(err)
}

err = k.LiquidateCdps(ctx, cp.LiquidationMarketID, cp.Denom, cp.LiquidationRatio)
err = k.LiquidateCdps(ctx, cp.LiquidationMarketID, cp.Type, cp.LiquidationRatio)
if err != nil && !errors.Is(err, pricefeedtypes.ErrNoValidPrice) {
panic(err)
}
Expand Down
12 changes: 6 additions & 6 deletions x/cdp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (suite *ModuleTestSuite) createCdps() {
tracker.debt += int64(debt)
}
}
suite.Nil(suite.keeper.AddCdp(suite.ctx, addrs[j], c(collateral, int64(amount)), c("usdx", int64(debt))))
c, f := suite.keeper.GetCDP(suite.ctx, collateral, uint64(j+1))
suite.Nil(suite.keeper.AddCdp(suite.ctx, addrs[j], c(collateral, int64(amount)), c("usdx", int64(debt)), collateral+"-a"))
c, f := suite.keeper.GetCDP(suite.ctx, collateral+"-a", uint64(j+1))
suite.True(f)
cdps[j] = c
}
Expand Down Expand Up @@ -153,9 +153,9 @@ func (suite *ModuleTestSuite) TestBeginBlock() {
}

func (suite *ModuleTestSuite) TestSeizeSingleCdpWithFees() {
err := suite.keeper.AddCdp(suite.ctx, suite.addrs[0], c("xrp", 10000000000), c("usdx", 1000000000))
err := suite.keeper.AddCdp(suite.ctx, suite.addrs[0], c("xrp", 10000000000), c("usdx", 1000000000), "xrp-a")
suite.NoError(err)
suite.Equal(i(1000000000), suite.keeper.GetTotalPrincipal(suite.ctx, "xrp", "usdx"))
suite.Equal(i(1000000000), suite.keeper.GetTotalPrincipal(suite.ctx, "xrp-a", "usdx"))
sk := suite.app.GetSupplyKeeper()
cdpMacc := sk.GetModuleAccount(suite.ctx, cdp.ModuleName)
suite.Equal(i(1000000000), cdpMacc.GetCoins().AmountOf("debt"))
Expand All @@ -166,11 +166,11 @@ func (suite *ModuleTestSuite) TestSeizeSingleCdpWithFees() {

cdpMacc = sk.GetModuleAccount(suite.ctx, cdp.ModuleName)
suite.Equal(i(1000000900), (cdpMacc.GetCoins().AmountOf("debt")))
cdp, _ := suite.keeper.GetCDP(suite.ctx, "xrp", 1)
cdp, _ := suite.keeper.GetCDP(suite.ctx, "xrp-a", 1)

err = suite.keeper.SeizeCollateral(suite.ctx, cdp)
suite.NoError(err)
_, found := suite.keeper.GetCDP(suite.ctx, "xrp", 1)
_, found := suite.keeper.GetCDP(suite.ctx, "xrp-a", 1)
suite.False(found)
}

Expand Down
2 changes: 1 addition & 1 deletion x/cdp/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization
QueryGetParams = types.QueryGetParams
RestOwner = types.RestOwner
RestCollateralDenom = types.RestCollateralDenom
RestCollateralType = types.RestCollateralType
RestRatio = types.RestRatio
)

Expand Down
42 changes: 21 additions & 21 deletions x/cdp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {

cdpQueryCmd.AddCommand(flags.GetCommands(
QueryCdpCmd(queryRoute, cdc),
QueryCdpsByDenomCmd(queryRoute, cdc),
QueryCdpsByDenomAndRatioCmd(queryRoute, cdc),
QueryCdpsByCollateralTypeCmd(queryRoute, cdc),
QueryCdpsByCollateralTypeAndRatioCmd(queryRoute, cdc),
QueryCdpDepositsCmd(queryRoute, cdc),
QueryParamsCmd(queryRoute, cdc),
QueryGetAccounts(queryRoute, cdc),
Expand All @@ -39,13 +39,13 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
// QueryCdpCmd returns the command handler for querying a particular cdp
func QueryCdpCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "cdp [owner-addr] [collateral-name]",
Use: "cdp [owner-addr] [collateral-type]",
Short: "get info about a cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Get a CDP by the owner address and the collateral name.

Example:
$ %s query %s cdp kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw uatom
$ %s query %s cdp kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw atom-a
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -57,8 +57,8 @@ $ %s query %s cdp kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw uatom
return err
}
bz, err := cdc.MarshalJSON(types.QueryCdpParams{
CollateralDenom: args[1],
Owner: ownerAddress,
CollateralType: args[1],
Owner: ownerAddress,
})
if err != nil {
return err
Expand All @@ -79,23 +79,23 @@ $ %s query %s cdp kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw uatom
}
}

// QueryCdpsByDenomCmd returns the command handler for querying cdps for a collateral type
func QueryCdpsByDenomCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
// QueryCdpsByCollateralTypeCmd returns the command handler for querying cdps for a collateral type
func QueryCdpsByCollateralTypeCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "cdps [collateral-name]",
Use: "cdps [collateral-type]",
Short: "query CDPs by collateral",
Long: strings.TrimSpace(
fmt.Sprintf(`List all CDPs collateralized with the specified asset.

Example:
$ %s query %s cdps uatom
$ %s query %s cdps atom-a
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

// Prepare params for querier
bz, err := cdc.MarshalJSON(types.QueryCdpsParams{CollateralDenom: args[0]})
bz, err := cdc.MarshalJSON(types.QueryCdpsParams{CollateralType: args[0]})
if err != nil {
return err
}
Expand All @@ -115,18 +115,18 @@ $ %s query %s cdps uatom
}
}

// QueryCdpsByDenomAndRatioCmd returns the command handler for querying cdps
// QueryCdpsByCollateralTypeAndRatioCmd returns the command handler for querying cdps
// that are under the specified collateral ratio
func QueryCdpsByDenomAndRatioCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
func QueryCdpsByCollateralTypeAndRatioCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "cdps-by-ratio [collateral-name] [collateralization-ratio]",
Use: "cdps-by-ratio [collateral-type] [collateralization-ratio]",
Short: "get cdps under a collateralization ratio",
Long: strings.TrimSpace(
fmt.Sprintf(`List all CDPs under a specified collateralization ratio.
Collateralization ratio is: collateral * price / debt.

Example:
$ %s query %s cdps-by-ratio uatom 1.5
$ %s query %s cdps-by-ratio atom-a 1.6
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -138,8 +138,8 @@ $ %s query %s cdps-by-ratio uatom 1.5
return err
}
bz, err := cdc.MarshalJSON(types.QueryCdpsByRatioParams{
CollateralDenom: args[0],
Ratio: ratio,
CollateralType: args[0],
Ratio: ratio,
})
if err != nil {
return err
Expand All @@ -163,13 +163,13 @@ $ %s query %s cdps-by-ratio uatom 1.5
// QueryCdpDepositsCmd returns the command handler for querying the deposits of a particular cdp
func QueryCdpDepositsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "deposits [owner-addr] [collateral-name]",
Use: "deposits [owner-addr] [collateral-type]",
Short: "get deposits for a cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Get the deposits of a CDP.

Example:
$ %s query %s deposits kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw uatom
$ %s query %s deposits kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw atom-a
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -181,8 +181,8 @@ $ %s query %s deposits kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw uatom
return err
}
bz, err := cdc.MarshalJSON(types.QueryCdpParams{
CollateralDenom: args[1],
Owner: ownerAddress,
CollateralType: args[1],
Owner: ownerAddress,
})
if err != nil {
return err
Expand Down
28 changes: 14 additions & 14 deletions x/cdp/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
// GetCmdCreateCdp returns the command handler for creating a cdp
func GetCmdCreateCdp(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "create [collateral] [debt]",
Use: "create [collateral] [debt] [collateral-type]",
Short: "create a new cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Create a new cdp, depositing some collateral and drawing some debt.

Example:
$ %s tx %s create 10000000uatom 1000usdx --from myKeyName
$ %s tx %s create 10000000uatom 1000usdx atom-a --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)
Expand All @@ -61,7 +61,7 @@ $ %s tx %s create 10000000uatom 1000usdx --from myKeyName
if err != nil {
return err
}
msg := types.NewMsgCreateCDP(cliCtx.GetFromAddress(), collateral, debt)
msg := types.NewMsgCreateCDP(cliCtx.GetFromAddress(), collateral, debt, args[2])
err = msg.ValidateBasic()
if err != nil {
return err
Expand All @@ -74,15 +74,15 @@ $ %s tx %s create 10000000uatom 1000usdx --from myKeyName
// GetCmdDeposit cli command for depositing to a cdp.
func GetCmdDeposit(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "deposit [owner-addr] [collateral]",
Use: "deposit [owner-addr] [collateral] [collateral-type]",
Short: "deposit collateral to an existing cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Add collateral to an existing cdp.

Example:
$ %s tx %s deposit kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom --from myKeyName
$ %s tx %s deposit kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom atom-a --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)
Expand All @@ -96,7 +96,7 @@ $ %s tx %s deposit kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom --f
if err != nil {
return err
}
msg := types.NewMsgDeposit(owner, cliCtx.GetFromAddress(), collateral)
msg := types.NewMsgDeposit(owner, cliCtx.GetFromAddress(), collateral, args[2])
err = msg.ValidateBasic()
if err != nil {
return err
Expand All @@ -109,13 +109,13 @@ $ %s tx %s deposit kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom --f
// GetCmdWithdraw cli command for withdrawing from a cdp.
func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "withdraw [owner-addr] [collateral]",
Use: "withdraw [owner-addr] [collateral] [collateral-type]",
Short: "withdraw collateral from an existing cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Remove collateral from an existing cdp.

Example:
$ %s tx %s withdraw kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom --from myKeyName
$ %s tx %s withdraw kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom atom-a --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -131,7 +131,7 @@ $ %s tx %s withdraw kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom --
if err != nil {
return err
}
msg := types.NewMsgWithdraw(owner, cliCtx.GetFromAddress(), collateral)
msg := types.NewMsgWithdraw(owner, cliCtx.GetFromAddress(), collateral, args[2])
err = msg.ValidateBasic()
if err != nil {
return err
Expand All @@ -144,13 +144,13 @@ $ %s tx %s withdraw kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom --
// GetCmdDraw cli command for depositing to a cdp.
func GetCmdDraw(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "draw [collateral-name] [debt]",
Use: "draw [collateral-type] [debt]",
Short: "draw debt off an existing cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Create debt in an existing cdp and send the newly minted asset to your account.

Example:
$ %s tx %s draw uatom 1000usdx --from myKeyName
$ %s tx %s draw atom-a 1000usdx --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -181,7 +181,7 @@ func GetCmdRepay(cdc *codec.Codec) *cobra.Command {
fmt.Sprintf(`Cancel out debt in an existing cdp.

Example:
$ %s tx %s repay uatom 1000usdx --from myKeyName
$ %s tx %s repay atom-a 1000usdx --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
24 changes: 12 additions & 12 deletions x/cdp/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/cdp/accounts", getAccountsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/cdp/parameters", getParamsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/cdps/cdp/{%s}/{%s}", types.RestOwner, types.RestCollateralDenom), queryCdpHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/cdps/denom/{%s}", types.RestCollateralDenom), queryCdpsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/cdps/ratio/{%s}/{%s}", types.RestCollateralDenom, types.RestRatio), queryCdpsByRatioHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/cdps/cdp/deposits/{%s}/{%s}", types.RestOwner, types.RestCollateralDenom), queryCdpDepositsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/cdps/cdp/{%s}/{%s}", types.RestOwner, types.RestCollateralType), queryCdpHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/cdps/collateralType/{%s}", types.RestCollateralType), queryCdpsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/cdps/ratio/{%s}/{%s}", types.RestCollateralType, types.RestRatio), queryCdpsByRatioHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/cdps/cdp/deposits/{%s}/{%s}", types.RestOwner, types.RestCollateralType), queryCdpDepositsHandlerFn(cliCtx)).Methods("GET")
}

func queryCdpHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
Expand All @@ -31,15 +31,15 @@ func queryCdpHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {

vars := mux.Vars(r)
ownerBech32 := vars[types.RestOwner]
collateralDenom := vars[types.RestCollateralDenom]
collateralType := vars[types.RestCollateralType]

owner, err := sdk.AccAddressFromBech32(ownerBech32)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

params := types.NewQueryCdpParams(owner, collateralDenom)
params := types.NewQueryCdpParams(owner, collateralType)

bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
Expand All @@ -66,9 +66,9 @@ func queryCdpsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}

vars := mux.Vars(r)
collateralDenom := vars[types.RestCollateralDenom]
collateralType := vars[types.RestCollateralType]

params := types.NewQueryCdpsParams(collateralDenom)
params := types.NewQueryCdpsParams(collateralType)

bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
Expand All @@ -95,7 +95,7 @@ func queryCdpsByRatioHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}

vars := mux.Vars(r)
collateralDenom := vars[types.RestCollateralDenom]
collateralType := vars[types.RestCollateralType]
ratioStr := vars[types.RestRatio]

ratioDec, sdkError := sdk.NewDecFromStr(ratioStr)
Expand All @@ -104,7 +104,7 @@ func queryCdpsByRatioHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

params := types.NewQueryCdpsByRatioParams(collateralDenom, ratioDec)
params := types.NewQueryCdpsByRatioParams(collateralType, ratioDec)
bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
Expand All @@ -131,15 +131,15 @@ func queryCdpDepositsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {

vars := mux.Vars(r)
ownerBech32 := vars[types.RestOwner]
collateralDenom := vars[types.RestCollateralDenom]
collateralType := vars[types.RestCollateralType]

owner, err := sdk.AccAddressFromBech32(ownerBech32)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

params := types.NewQueryCdpDeposits(owner, collateralDenom)
params := types.NewQueryCdpDeposits(owner, collateralType)

bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
Expand Down