Skip to content

Commit

Permalink
Revisit x/collection Attach, Detach, AttachFrom & DetachFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
0Tech committed Dec 2, 2022
1 parent 0b9755f commit c5c8aee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
40 changes: 10 additions & 30 deletions x/collection/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,7 @@ func (s msgServer) Attach(c context.Context, req *collection.MsgAttach) (*collec
panic(err)
}

fromAddr, err := sdk.AccAddressFromBech32(req.From)
if err != nil {
return nil, err
}
fromAddr := sdk.MustAccAddressFromBech32(req.From)

if err := s.keeper.Attach(ctx, req.ContractId, fromAddr, req.TokenId, req.ToTokenId); err != nil {
return nil, err
Expand All @@ -694,7 +691,7 @@ func (s msgServer) Detach(c context.Context, req *collection.MsgDetach) (*collec
// for the additional field of the event
parent, err := s.keeper.GetParent(ctx, req.ContractId, req.TokenId)
if err != nil {
return nil, err
return nil, collection.ErrTokenNotAChild.Wrap(err.Error())
}
event := collection.EventDetached{
ContractId: req.ContractId,
Expand All @@ -708,10 +705,7 @@ func (s msgServer) Detach(c context.Context, req *collection.MsgDetach) (*collec
panic(err)
}

fromAddr, err := sdk.AccAddressFromBech32(req.From)
if err != nil {
return nil, err
}
fromAddr := sdk.MustAccAddressFromBech32(req.From)

if err := s.keeper.Detach(ctx, req.ContractId, fromAddr, req.TokenId); err != nil {
return nil, err
Expand All @@ -723,18 +717,11 @@ func (s msgServer) Detach(c context.Context, req *collection.MsgDetach) (*collec
func (s msgServer) AttachFrom(c context.Context, req *collection.MsgAttachFrom) (*collection.MsgAttachFromResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

fromAddr, err := sdk.AccAddressFromBech32(req.From)
if err != nil {
return nil, err
}

proxyAddr, err := sdk.AccAddressFromBech32(req.Proxy)
if err != nil {
return nil, err
}
fromAddr := sdk.MustAccAddressFromBech32(req.From)
proxyAddr := sdk.MustAccAddressFromBech32(req.Proxy)

if _, err := s.keeper.GetAuthorization(ctx, req.ContractId, fromAddr, proxyAddr); err != nil {
return nil, sdkerrors.ErrUnauthorized.Wrap(err.Error())
return nil, collection.ErrCollectionNotApproved.Wrap(err.Error())
}

event := collection.EventAttached{
Expand All @@ -760,30 +747,23 @@ func (s msgServer) AttachFrom(c context.Context, req *collection.MsgAttachFrom)
func (s msgServer) DetachFrom(c context.Context, req *collection.MsgDetachFrom) (*collection.MsgDetachFromResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

fromAddr, err := sdk.AccAddressFromBech32(req.From)
if err != nil {
return nil, err
}

proxyAddr, err := sdk.AccAddressFromBech32(req.Proxy)
if err != nil {
return nil, err
}
fromAddr := sdk.MustAccAddressFromBech32(req.From)
proxyAddr := sdk.MustAccAddressFromBech32(req.Proxy)

if _, err := s.keeper.GetAuthorization(ctx, req.ContractId, fromAddr, proxyAddr); err != nil {
return nil, sdkerrors.ErrUnauthorized.Wrap(err.Error())
}

// legacy
if err := s.keeper.hasNFT(ctx, req.ContractId, req.TokenId); err != nil {
return nil, err
return nil, collection.ErrTokenNotNFT.Wrap(err.Error())
}
oldRoot := s.keeper.GetRoot(ctx, req.ContractId, req.TokenId)

// for the additional field of the event
parent, err := s.keeper.GetParent(ctx, req.ContractId, req.TokenId)
if err != nil {
return nil, err
return nil, collection.ErrTokenNotAChild.Wrap(err.Error())
}
event := collection.EventDetached{
ContractId: req.ContractId,
Expand Down
18 changes: 9 additions & 9 deletions x/collection/keeper/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ func (k Keeper) pruneNFT(ctx sdk.Context, contractID string, tokenID string) []s
func (k Keeper) Attach(ctx sdk.Context, contractID string, owner sdk.AccAddress, subject, target string) error {
// validate subject
if !k.GetBalance(ctx, contractID, owner, subject).IsPositive() {
return sdkerrors.ErrInvalidRequest.Wrapf("%s is not owner of %s", owner, subject)
return collection.ErrTokenNotOwnedBy.Wrapf("%s is not owner of %s", owner, subject)
}

// validate target
if err := k.hasNFT(ctx, contractID, target); err != nil {
return err
return collection.ErrTokenNotNFT.Wrap(err.Error())
}

root := k.GetRoot(ctx, contractID, target)
if !owner.Equals(k.getOwner(ctx, contractID, root)) {
return sdkerrors.ErrInvalidRequest.Wrapf("%s is not owner of %s", owner, target)
return collection.ErrTokenNotOwnedBy.Wrapf("%s is not owner of %s", owner, target)
}
if root == subject {
return sdkerrors.ErrInvalidRequest.Wrap("cycles not allowed")
return collection.ErrCannotAttachToADescendant.Wrap("cycles not allowed")
}

// update subject
Expand Down Expand Up @@ -113,16 +113,16 @@ func (k Keeper) Attach(ctx sdk.Context, contractID string, owner sdk.AccAddress,

func (k Keeper) Detach(ctx sdk.Context, contractID string, owner sdk.AccAddress, subject string) error {
if err := k.hasNFT(ctx, contractID, subject); err != nil {
return err
return collection.ErrTokenNotNFT.Wrap(err.Error())
}

parent, err := k.GetParent(ctx, contractID, subject)
if err != nil {
return err
return collection.ErrTokenNotAChild.Wrap(err.Error())
}

if !owner.Equals(k.GetRootOwner(ctx, contractID, subject)) {
return sdkerrors.ErrInvalidRequest.Wrapf("%s is not owner of %s", owner, subject)
return collection.ErrTokenNotOwnedBy.Wrapf("%s is not owner of %s", owner, subject)
}

// update subject
Expand Down Expand Up @@ -312,12 +312,12 @@ func (k Keeper) validateDepthAndWidth(ctx sdk.Context, contractID string, tokenI

depth := len(widths)
if legacyDepth := depth - 1; legacyDepth > int(params.DepthLimit) {
return sdkerrors.ErrInvalidRequest.Wrapf("resulting depth exceeds its limit: %d", params.DepthLimit)
return collection.ErrCompositionTooDeep.Wrapf("resulting depth exceeds its limit: %d", params.DepthLimit)
}

for _, width := range widths {
if width > int(params.WidthLimit) {
return sdkerrors.ErrInvalidRequest.Wrapf("resulting width exceeds its limit: %d", params.WidthLimit)
return collection.ErrCompositionTooWide.Wrapf("resulting width exceeds its limit: %d", params.WidthLimit)
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/collection/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ func (m MsgAttach) ValidateBasic() error {
}

if m.TokenId == m.ToTokenId {
return sdkerrors.ErrInvalidRequest.Wrap("cannot attach token to itself")
return ErrCannotAttachToItself.Wrap("cannot attach token to itself")
}

return nil
Expand Down Expand Up @@ -1183,7 +1183,7 @@ func (m MsgAttachFrom) ValidateBasic() error {
}

if m.TokenId == m.ToTokenId {
return sdkerrors.ErrInvalidRequest.Wrap("cannot attach token to itself")
return ErrCannotAttachToItself.Wrap("cannot attach token to itself")
}

return nil
Expand Down

0 comments on commit c5c8aee

Please sign in to comment.