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

feat: add EventOwnerChanged and EventRootChanged #604

Merged
merged 4 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (server/grpc) [\#526](https://github.com/line/lbm-sdk/pull/526) add index field into TxResponse
* (cli) [\#535](https://github.com/line/lbm-sdk/pull/536) updated ostracon to v1.0.5; `unsafe-reset-all` command has been moved to the `ostracon` sub-command.
* (x/foundation) [\#597](https://github.com/line/lbm-sdk/pull/597) tidy up x/foundation
* (x/collection) [\#604](https://github.com/line/lbm-sdk/pull/604) add EventOwnerChanged and EventRootChanged

### Bug Fixes
* (x/wasm) [\#453](https://github.com/line/lbm-sdk/pull/453) modify wasm grpc query api path
Expand Down
38 changes: 38 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,9 @@
- [EventModifiedContract](#lbm.collection.v1.EventModifiedContract)
- [EventModifiedNFT](#lbm.collection.v1.EventModifiedNFT)
- [EventModifiedTokenClass](#lbm.collection.v1.EventModifiedTokenClass)
- [EventOwnerChanged](#lbm.collection.v1.EventOwnerChanged)
- [EventRevokedOperator](#lbm.collection.v1.EventRevokedOperator)
- [EventRootChanged](#lbm.collection.v1.EventRootChanged)
- [EventSent](#lbm.collection.v1.EventSent)

- [AttributeKey](#lbm.collection.v1.AttributeKey)
Expand Down Expand Up @@ -12768,6 +12770,24 @@ Since: 0.46.0 (finschia)



<a name="lbm.collection.v1.EventOwnerChanged"></a>

### EventOwnerChanged
EventOwnerChanged is emitted when the owner of token is changed by operation applied to its ancestor.

Since: 0.46.0 (finschia)


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `contract_id` | [string](#string) | | contract id associated with the contract. |
| `token_id` | [string](#string) | | token id associated with the token. |






<a name="lbm.collection.v1.EventRevokedOperator"></a>

### EventRevokedOperator
Expand All @@ -12787,6 +12807,24 @@ Since: 0.46.0 (finschia)



<a name="lbm.collection.v1.EventRootChanged"></a>

### EventRootChanged
EventRootChanged is emitted when the root of token is changed by operation applied to its ancestor.

Since: 0.46.0 (finschia)


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `contract_id` | [string](#string) | | contract id associated with the contract. |
| `token_id` | [string](#string) | | token id associated with the token. |






<a name="lbm.collection.v1.EventSent"></a>

### EventSent
Expand Down
20 changes: 20 additions & 0 deletions proto/lbm/collection/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,23 @@ message EventDetached {
// token being detached.
string subject = 4;
}

// EventOwnerChanged is emitted when the owner of token is changed by operation applied to its ancestor.
//
// Since: 0.46.0 (finschia)
message EventOwnerChanged {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no changed owner information in this event?

Copy link
Collaborator Author

@0Tech 0Tech Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The address of the new owner was not included in the event on Daphne.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, can I understand that it was not added for backwards compatibility?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new field would not break the compatibility, but there is no reason to add it.

// contract id associated with the contract.
string contract_id = 1;
// token id associated with the token.
string token_id = 2;
}

// EventRootChanged is emitted when the root of token is changed by operation applied to its ancestor.
//
// Since: 0.46.0 (finschia)
message EventRootChanged {
// contract id associated with the contract.
string contract_id = 1;
// token id associated with the token.
string token_id = 2;
}
12 changes: 6 additions & 6 deletions x/collection/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,11 +688,11 @@ func NewEventDetachFrom(event EventDetached, oldRoot string) sdk.Event {
}

// Deprecated: do not use.
func NewEventOperationTransferNFT(contractID string, tokenID string) sdk.Event {
func NewEventOperationTransferNFT(event EventOwnerChanged) sdk.Event {
eventType := EventTypeOperationTransferNFT.String()
attributes := map[AttributeKey]string{
AttributeKeyContractID: contractID,
AttributeKeyTokenID: tokenID,
AttributeKeyContractID: event.ContractId,
AttributeKeyTokenID: event.TokenId,
}

res := sdk.NewEvent(eventType)
Expand Down Expand Up @@ -720,11 +720,11 @@ func NewEventOperationBurnNFT(contractID string, tokenID string) sdk.Event {
}

// Deprecated: do not use.
func NewEventOperationRootChanged(contractID string, tokenID string) sdk.Event {
func NewEventOperationRootChanged(event EventRootChanged) sdk.Event {
eventType := EventTypeOperationRootChanged.String()
attributes := map[AttributeKey]string{
AttributeKeyContractID: contractID,
AttributeKeyTokenID: tokenID,
AttributeKeyContractID: event.ContractId,
AttributeKeyTokenID: event.TokenId,
}

res := sdk.NewEvent(eventType)
Expand Down
652 changes: 553 additions & 99 deletions x/collection/event.pb.go

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions x/collection/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,15 +896,17 @@ func TestNewEventOperationTransferNFT(t *testing.T) {
rand.Seed(time.Now().UnixNano())
str := func() string { return randomString(8) }

contractID := str()
tokenID := str()
legacy := collection.NewEventOperationTransferNFT(contractID, tokenID)
event := collection.EventOwnerChanged{
ContractId: str(),
TokenId: str(),
}
legacy := collection.NewEventOperationTransferNFT(event)

require.Equal(t, collection.EventTypeOperationTransferNFT.String(), legacy.Type)

attributes := map[collection.AttributeKey]string{
collection.AttributeKeyContractID: contractID,
collection.AttributeKeyTokenID: tokenID,
collection.AttributeKeyContractID: event.ContractId,
collection.AttributeKeyTokenID: event.TokenId,
}
for key, value := range attributes {
require.True(t, assertAttribute(legacy, key.String(), value), key)
Expand Down Expand Up @@ -934,15 +936,17 @@ func TestNewEventOperationRootChanged(t *testing.T) {
rand.Seed(time.Now().UnixNano())
str := func() string { return randomString(8) }

contractID := str()
tokenID := str()
legacy := collection.NewEventOperationRootChanged(contractID, tokenID)
event := collection.EventRootChanged{
ContractId: str(),
TokenId: str(),
}
legacy := collection.NewEventOperationRootChanged(event)

require.Equal(t, collection.EventTypeOperationRootChanged.String(), legacy.Type)

attributes := map[collection.AttributeKey]string{
collection.AttributeKeyContractID: contractID,
collection.AttributeKeyTokenID: tokenID,
collection.AttributeKeyContractID: event.ContractId,
collection.AttributeKeyTokenID: event.TokenId,
}
for key, value := range attributes {
require.True(t, assertAttribute(legacy, key.String(), value), key)
Expand Down
33 changes: 22 additions & 11 deletions x/collection/keeper/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,17 @@ func (k Keeper) Attach(ctx sdk.Context, contractID string, owner sdk.AccAddress,
}

// legacy
k.emitEventOnDescendants(ctx, contractID, subject, collection.NewEventOperationRootChanged)
k.iterateDescendants(ctx, contractID, subject, func(descendantID string, _ int) (stop bool) {
event := collection.EventRootChanged{
ContractId: contractID,
TokenId: descendantID,
}
ctx.EventManager().EmitEvent(collection.NewEventOperationRootChanged(event))
if err := ctx.EventManager().EmitTypedEvent(&event); err != nil {
panic(err)
}
return false
})

return nil
}
Expand All @@ -121,7 +131,17 @@ func (k Keeper) Detach(ctx sdk.Context, contractID string, owner sdk.AccAddress,
k.deleteChild(ctx, contractID, *parent, subject)

// legacy
k.emitEventOnDescendants(ctx, contractID, subject, collection.NewEventOperationRootChanged)
k.iterateDescendants(ctx, contractID, subject, func(descendantID string, _ int) (stop bool) {
event := collection.EventRootChanged{
ContractId: contractID,
TokenId: descendantID,
}
ctx.EventManager().EmitEvent(collection.NewEventOperationRootChanged(event))
if err := ctx.EventManager().EmitTypedEvent(&event); err != nil {
panic(err)
}
return false
})

return nil
}
Expand Down Expand Up @@ -275,15 +295,6 @@ func (k Keeper) setLegacyTokenType(ctx sdk.Context, contractID string, tokenType
store.Set(key, []byte{})
}

// Deprecated
func (k Keeper) emitEventOnDescendants(ctx sdk.Context, contractID string, tokenID string, generator func(contractID string, descendantID string) sdk.Event) {
k.iterateDescendants(ctx, contractID, tokenID, func(descendantID string, _ int) (stop bool) {
event := generator(contractID, descendantID)
ctx.EventManager().EmitEvent(event)
return false
})
}

// Deprecated
func (k Keeper) validateDepthAndWidth(ctx sdk.Context, contractID string, tokenID string) error {
widths := map[int]int{0: 1}
Expand Down
20 changes: 17 additions & 3 deletions x/collection/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ func (k Keeper) SendCoins(ctx sdk.Context, contractID string, from, to sdk.AccAd
return err
}

// legacy
for _, coin := range amount {
if err := collection.ValidateNFTID(coin.TokenId); err == nil {
k.iterateDescendants(ctx, contractID, coin.TokenId, func(descendantID string, _ int) (stop bool) {
event := collection.EventOwnerChanged{
ContractId: contractID,
TokenId: descendantID,
}
ctx.EventManager().EmitEvent(collection.NewEventOperationTransferNFT(event))
if err := ctx.EventManager().EmitTypedEvent(&event); err != nil {
panic(err)
}
return false
})
}
}

return nil
}

Expand All @@ -25,9 +42,6 @@ func (k Keeper) addCoins(ctx sdk.Context, contractID string, address sdk.AccAddr

if err := collection.ValidateNFTID(coin.TokenId); err == nil {
k.setOwner(ctx, contractID, coin.TokenId, address)

// legacy
k.emitEventOnDescendants(ctx, contractID, coin.TokenId, collection.NewEventOperationTransferNFT)
}
}

Expand Down
5 changes: 4 additions & 1 deletion x/collection/keeper/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ func (k Keeper) BurnCoins(ctx sdk.Context, contractID string, from sdk.AccAddres
burntAmount = append(burntAmount, coin)
if err := collection.ValidateNFTID(coin.TokenId); err == nil {
// legacy
k.emitEventOnDescendants(ctx, contractID, coin.TokenId, collection.NewEventOperationBurnNFT)
k.iterateDescendants(ctx, contractID, coin.TokenId, func(descendantID string, _ int) (stop bool) {
ctx.EventManager().EmitEvent(collection.NewEventOperationBurnNFT(contractID, descendantID))
return false
})

k.deleteOwner(ctx, contractID, coin.TokenId)
k.deleteNFT(ctx, contractID, coin.TokenId)
Expand Down