Skip to content

Commit

Permalink
Merge pull request #108 from lcnem/feature/debug-cdp-list
Browse files Browse the repository at this point in the history
chore: debug get cdp list
  • Loading branch information
YasunoriMATSUOKA authored Oct 5, 2021
2 parents dca1a95 + 617d553 commit f59edf7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions x/cdp/client/cli/query_cdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -28,6 +29,10 @@ func CmdListCdp() *cobra.Command {
}

res, err := queryClient.CdpAll(context.Background(), params)

fmt.Printf("yakitori list res, %s\n", res)
fmt.Printf("yakitori list err, %s\n", err)

if err != nil {
return err
}
Expand Down Expand Up @@ -57,6 +62,10 @@ func CmdShowCdp() *cobra.Command {
}

res, err := queryClient.Cdp(context.Background(), params)

fmt.Printf("yakitori res, %s\n", res)
fmt.Printf("yakitori err, %s\n", err)

if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions x/cdp/keeper/cdps.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,20 @@ func (k Keeper) GetCdpIdsByOwner(ctx sdk.Context, owner sdk.AccAddress) (cdpIDs
// GetCdpByOwnerAndCollateralType queries cdps owned by owner and returns the cdp with matching denom
func (k Keeper) GetCdpByOwnerAndCollateralType(ctx sdk.Context, owner sdk.AccAddress, collateralType string) (types.Cdp, bool) {
cdpIDs, found := k.GetCdpIdsByOwner(ctx, owner)
fmt.Println(cdpIDs)
if !found {
fmt.Println("a")
return types.Cdp{}, false
}
for _, id := range cdpIDs {
fmt.Printf("collateralType %s\n", collateralType)
fmt.Printf("id %d\n", id)
cdp, found := k.GetCdp(ctx, collateralType, id)
if found {
return cdp, true
}
}
fmt.Println("b")
return types.Cdp{}, false
}

Expand Down
18 changes: 18 additions & 0 deletions x/cdp/keeper/cdps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ func (suite *CdpTestSuite) TestAddCdp() {
suite.Require().True(errors.Is(err, cdptypes.ErrCdpAlreadyExists))
}

func (suite *CdpTestSuite) TestAddGetCdp() {
_, addrs := app.GeneratePrivKeyAddressPairs(2)
ak := suite.app.GetAccountKeeper()
sk := suite.app.GetBankKeeper()
acc := ak.NewAccountWithAddress(suite.ctx, addrs[0])
sk.GetAllBalances(suite.ctx, acc.GetAddress())
sk.AddCoins(suite.ctx, acc.GetAddress(), cs(c("xrp", 200000000), c("btc", 500000000)))
ak.SetAccount(suite.ctx, acc)
err := suite.keeper.AddCdp(suite.ctx, addrs[0], c("btc", 100000000), c("jpyx", 10000000), "btc-a")
suite.NoError(err)
id := suite.keeper.GetNextCdpID(suite.ctx)
suite.Equal(uint64(2), id)
_, found := suite.keeper.GetCdp(suite.ctx, "btc-a", 1)
suite.True(found)
_, found2 := suite.keeper.GetCdpByOwnerAndCollateralType(suite.ctx, addrs[0], "btc-a")
suite.True(found2)
}

func (suite *CdpTestSuite) TestGetSetCollateralTypeByte() {
_, found := suite.keeper.GetCollateralTypePrefix(suite.ctx, "lol-a")
suite.False(found)
Expand Down
2 changes: 2 additions & 0 deletions x/cdp/keeper/grpc_query_cdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"fmt"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
Expand Down Expand Up @@ -42,6 +43,7 @@ func (k Keeper) CdpAll(c context.Context, req *types.QueryAllCdpRequest) (*types
}

func (k Keeper) Cdp(c context.Context, req *types.QueryGetCdpRequest) (*types.QueryGetCdpResponse, error) {
fmt.Printf("yakitori res, %s\n", req)
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
Expand Down

0 comments on commit f59edf7

Please sign in to comment.