Skip to content

Commit

Permalink
CLI & REST client updates, Modified GRPC queries, Init Genesis Fixes (#8
Browse files Browse the repository at this point in the history
)

- client updates
- init genesis fixes
  • Loading branch information
harish551 authored Nov 24, 2021
2 parents 8d6cc7e + 2c66261 commit 076e4b0
Show file tree
Hide file tree
Showing 14 changed files with 846 additions and 127 deletions.
13 changes: 12 additions & 1 deletion client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,19 @@ $ %s query onft denoms`, version.AppName)),
if err != nil {
return err
}
owner, err := cmd.Flags().GetString(FlagOwner)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.Denoms(context.Background(), &types.QueryDenomsRequest{Pagination: pagination})
resp, err := queryClient.Denoms(
context.Background(),
&types.QueryDenomsRequest{
Pagination: pagination,
Owner: owner,
},
)
if err != nil {
return err
}
Expand All @@ -187,6 +197,7 @@ $ %s query onft denoms`, version.AppName)),
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "denoms")
cmd.Flags().String(FlagOwner, "", "filter by collection owner address")
return cmd
}

Expand Down
16 changes: 11 additions & 5 deletions client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,24 @@ func queryDenoms(cliCtx client.Context, queryRoute string) http.HandlerFunc {
return
}
pageReq := sdkquery.PageRequest{
Offset: uint64((page - 1) * limit),
Limit: uint64(limit),
Offset: uint64((page - 1) * limit),
Limit: uint64(limit),
CountTotal: true,
}

if query.Get("total_count") == "true" {
pageReq.CountTotal = true
var owner sdk.AccAddress
if query.Get("owner") != "" {
owner, err = sdk.AccAddressFromBech32(query.Get("owner"))
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
}

denoms, err := qc.Denoms(
context.Background(),
&types.QueryDenomsRequest{
Pagination: &pageReq,
Owner: owner.String(),
},
)
if rest.CheckInternalServerError(w, err) {
Expand Down
5 changes: 1 addition & 4 deletions genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) {
}

for _, c := range data.Collections {
if err := k.SetDenom(ctx, c.Denom); err != nil {
panic(err)
}
if err := k.SetCollection(ctx, c); err != nil {
panic(err)
}
Expand All @@ -27,4 +24,4 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {

func DefaultGenesisState() *types.GenesisState {
return types.NewGenesisState([]types.Collection{})
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/cosmos/cosmos-sdk v0.44.3
github.com/cosmos/ibc-go v1.1.0
github.com/cosmos/ibc-go v1.2.2
github.com/gin-gonic/gin v1.7.0 // indirect
github.com/gogo/protobuf v1.3.3
github.com/golang/protobuf v1.5.2
Expand All @@ -15,7 +15,7 @@ require (
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/tendermint/spm v0.1.4
github.com/tendermint/spm v0.1.8
github.com/tendermint/tendermint v0.34.14
github.com/tendermint/tm-db v0.6.4
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c
Expand Down
297 changes: 272 additions & 25 deletions go.sum

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion keeper/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ import (
)

func (k Keeper) SetCollection(ctx sdk.Context, collection types.Collection) error {
denom := collection.Denom
creator, err := sdk.AccAddressFromBech32(denom.Creator)
if err != nil {
return err
}
if err := k.CreateDenom(
ctx,
denom.Id,
denom.Symbol,
denom.Name,
denom.Schema,
creator,
denom.Description,
denom.PreviewURI,
); err != nil {
return err
}

for _, onft := range collection.ONFTs {
metadata := types.Metadata{
Name: onft.GetName(),
Expand All @@ -28,7 +46,7 @@ func (k Keeper) SetCollection(ctx sdk.Context, collection types.Collection) erro
onft.GetData(),
onft.IsTransferable(),
onft.IsExtensible(),
onft.GetOwner(),
creator,
onft.GetOwner(),
); err != nil {
return err
Expand Down
31 changes: 31 additions & 0 deletions keeper/denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ func (k Keeper) GetDenoms(ctx sdk.Context) (denoms []types.Denom) {
}
return denoms
}

func (k Keeper) GetDenomsByOwner(ctx sdk.Context, owner sdk.AccAddress) (denoms []types.Denom) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyDenomCreator(owner, ""))
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
denomId := types.MustUnMarshalDenomID(k.cdc, iterator.Value())
denom, _ := k.GetDenom(ctx, denomId)
denoms = append(denoms, denom)
}
return denoms
}

func (k Keeper) AuthorizeDenomCreator(ctx sdk.Context, id string, creator sdk.AccAddress) (types.Denom, error) {
denom, err := k.GetDenom(ctx, id)
if err != nil {
Expand All @@ -77,3 +91,20 @@ func (k Keeper) HasPermissionToMint(ctx sdk.Context, denomID string, sender sdk.
}
return false
}

func (k Keeper) deleteDenomOwner(ctx sdk.Context, denomID string, owner sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.KeyDenomCreator(owner, denomID))
}

func (k Keeper) setDenomOwner(ctx sdk.Context, denomId string, owner sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)

bz := types.MustMarshalDenomID(k.cdc, denomId)
store.Set(types.KeyDenomCreator(owner, denomId), bz)
}

func (k Keeper) swapDenomOwner(ctx sdk.Context, denomID string, srcOwner, dstOwner sdk.AccAddress) {
k.deleteDenomOwner(ctx, denomID, srcOwner)
k.setDenomOwner(ctx, denomID, dstOwner)
}
62 changes: 49 additions & 13 deletions keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,40 @@ func (k Keeper) Denom(c context.Context, request *types.QueryDenomRequest) (*typ

func (k Keeper) Denoms(c context.Context, request *types.QueryDenomsRequest) (*types.QueryDenomsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var denoms []types.Denom
var (
denoms []types.Denom
pagination *query.PageResponse
err error
)
store := ctx.KVStore(k.storeKey)
denomStore := prefix.NewStore(store, types.KeyDenomID(""))
pagination, err := query.Paginate(denomStore, request.Pagination, func(key []byte, value []byte) error {
var denom types.Denom
k.cdc.MustUnmarshal(value, &denom)
denoms = append(denoms, denom)
return nil
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)

if request.Owner != "" {
owner, err := sdk.AccAddressFromBech32(request.Owner)
if err != nil {
return nil, err
}
denomStore := prefix.NewStore(store, types.KeyDenomCreator(owner, ""))
pagination, err = query.Paginate(denomStore, request.Pagination, func(key []byte, value []byte) error {
denomId := types.MustUnMarshalDenomID(k.cdc, value)
denom, _ := k.GetDenom(ctx, denomId)
denoms = append(denoms, denom)
return nil
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}

} else {
denomStore := prefix.NewStore(store, types.KeyDenomID(""))
pagination, err = query.Paginate(denomStore, request.Pagination, func(key []byte, value []byte) error {
var denom types.Denom
k.cdc.MustUnmarshal(value, &denom)
denoms = append(denoms, denom)
return nil
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}
}
return &types.QueryDenomsResponse{
Denoms: denoms,
Expand All @@ -95,7 +118,7 @@ func (k Keeper) ONFT(c context.Context, request *types.QueryONFTRequest) (*types

oNFT, ok := nft.(types.ONFT)
if !ok {
return nil, sdkerrors.Wrapf(types.ErrUnknownONFT, "invalid type NFT %s from collection %s", request.Id, request.DenomId)
return nil, sdkerrors.Wrapf(types.ErrUnknownONFT, "invalid type ONFT %s from collection %s", request.Id, request.DenomId)
}

return &types.QueryONFTResponse{
Expand All @@ -114,6 +137,7 @@ func (k Keeper) OwnerONFTs(c context.Context, request *types.QueryOwnerONFTsRequ
Address: address.String(),
IDCollections: types.IDCollections{},
}
var ownerCollections []types.OwnerONFTCollection
idsMap := make(map[string][]string)
store := ctx.KVStore(k.storeKey)
onftStore := prefix.NewStore(store, types.KeyOwner(address, request.DenomId, ""))
Expand All @@ -136,9 +160,21 @@ func (k Keeper) OwnerONFTs(c context.Context, request *types.QueryOwnerONFTsRequ
})
for i := 0; i < len(owner.IDCollections); i++ {
owner.IDCollections[i].OnftIds = idsMap[owner.IDCollections[i].DenomId]
denom, _ := k.GetDenom(ctx, owner.IDCollections[i].DenomId)
var onfts []types.ONFT
for _, onftid := range owner.IDCollections[i].OnftIds {
onft, _ := k.GetONFT(ctx, denom.Id, onftid)
onfts = append(onfts, onft.(types.ONFT))
}
ownerCollection := types.OwnerONFTCollection{
Denom: denom,
Onfts: onfts,
}
ownerCollections = append(ownerCollections, ownerCollection)
}
return &types.QueryOwnerONFTsResponse{
Owner: &owner,
Pagination: pagination,
Owner: address.String(),
Collections: ownerCollections,
Pagination: pagination,
}, nil
}
8 changes: 7 additions & 1 deletion keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func (k Keeper) CreateDenom(
if k.HasDenomSymbol(ctx, symbol) {
return sdkerrors.Wrapf(types.ErrInvalidDenom, "denomSymbol %s has already exists", symbol)
}
return k.SetDenom(ctx, types.NewDenom(id, symbol, name, schema, creator, description, previewUri))
err := k.SetDenom(ctx, types.NewDenom(id, symbol, name, schema, creator, description, previewUri))
if err != nil {
return err
}
k.setDenomOwner(ctx, id, creator)
return nil
}

func (k Keeper) UpdateDenom(ctx sdk.Context, id, name, description, previewURI string, sender sdk.AccAddress) error {
Expand Down Expand Up @@ -79,6 +84,7 @@ func (k Keeper) TransferDenomOwner(ctx sdk.Context, id string, curOwner, newOwne
if err != nil {
return err
}
k.swapDenomOwner(ctx, id, curOwner, newOwner)
return nil
}

Expand Down
11 changes: 9 additions & 2 deletions proto/onft/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ message QueryDenomResponse {
message QueryDenomsRequest {
// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 1;
string owner = 2;
}

message QueryDenomsResponse {
Expand All @@ -76,8 +77,9 @@ message QueryOwnerONFTsRequest {
}

message QueryOwnerONFTsResponse {
Owner owner = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
string owner = 1;
repeated OwnerONFTCollection collections = 2 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 3;
}

message QuerySupplyRequest {
Expand All @@ -88,3 +90,8 @@ message QuerySupplyRequest {
message QuerySupplyResponse {
uint64 amount = 1;
}

message OwnerONFTCollection {
Denom denom = 1 [(gogoproto.nullable) = false];
repeated ONFT onfts = 2 [(gogoproto.nullable) = false];
}
13 changes: 9 additions & 4 deletions types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ func MustMarshalONFTID(cdc codec.BinaryCodec, onftID string) []byte {
return cdc.MustMarshal(&onftIDWrap)
}

func MustUnMarshalONFTID(cdc codec.BinaryCodec, value []byte) string {
var onftIDWrap gogotypes.StringValue
cdc.MustUnmarshal(value, &onftIDWrap)
return onftIDWrap.Value
func MustMarshalDenomID(cdc codec.BinaryCodec, denomID string) []byte {
denomIDWrap := gogotypes.StringValue{Value: denomID}
return cdc.MustMarshal(&denomIDWrap)
}

func MustUnMarshalDenomID(cdc codec.BinaryCodec, value []byte) string {
var denomIDWrap gogotypes.StringValue
cdc.MustUnmarshal(value, &denomIDWrap)
return denomIDWrap.Value
}
5 changes: 2 additions & 3 deletions types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ func NewGenesisState(collections []Collection) *GenesisState {
}
}


func ValidateGenesis(data GenesisState) error {
for _, c := range data.Collections {
if err := ValidateDenomID(c.Denom.Name); err != nil {
if err := ValidateDenomID(c.Denom.Id); err != nil {
return err
}

Expand All @@ -30,4 +29,4 @@ func ValidateGenesis(data GenesisState) error {
}
}
return nil
}
}
14 changes: 14 additions & 0 deletions types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
PrefixCollection = []byte{0x03}
PrefixDenom = []byte{0x04}
PrefixDenomSymbol = []byte{0x05}
PrefixCreator = []byte{0x06}

delimiter = []byte("/")
)
Expand Down Expand Up @@ -90,6 +91,19 @@ func KeyDenomID(id string) []byte {
key := append(PrefixDenom, delimiter...)
return append(key, []byte(id)...)
}
func KeyDenomCreator(address sdk.AccAddress, denomId string) []byte {

key := append(PrefixCreator, delimiter...)
if address != nil {
key = append(key, []byte(address)...)
key = append(key, delimiter...)
}
if address != nil && len(denomId) > 0 {
key = append(key, []byte(denomId)...)
key = append(key, delimiter...)
}
return key
}

func KeyDenomSymbol(symbol string) []byte {
key := append(PrefixDenomSymbol, delimiter...)
Expand Down
Loading

0 comments on commit 076e4b0

Please sign in to comment.