From 40840e11586e6bdb321174ae59ca8dc55ff6c9bb Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Wed, 13 Jul 2022 11:54:39 +0000 Subject: [PATCH 1/4] test: add tests on event.go --- x/token/event.go | 52 ++++-- x/token/event_test.go | 382 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 424 insertions(+), 10 deletions(-) create mode 100644 x/token/event_test.go diff --git a/x/token/event.go b/x/token/event.go index 7616ea3292..5e94917d89 100644 --- a/x/token/event.go +++ b/x/token/event.go @@ -51,7 +51,7 @@ func NewEventIssueToken(e EventIssue, grantee, to sdk.AccAddress, amount sdk.Int new := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new.AppendAttributes(attribute) + new = new.AppendAttributes(attribute) } return new } @@ -68,7 +68,7 @@ func NewEventMintToken(e EventMinted) sdk.Event { new := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new.AppendAttributes(attribute) + new = new.AppendAttributes(attribute) } return new } @@ -84,7 +84,7 @@ func NewEventBurnToken(e EventBurned) sdk.Event { new := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new.AppendAttributes(attribute) + new = new.AppendAttributes(attribute) } return new } @@ -101,7 +101,7 @@ func NewEventBurnTokenFrom(e EventBurned) sdk.Event { new := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new.AppendAttributes(attribute) + new = new.AppendAttributes(attribute) } return new } @@ -134,7 +134,7 @@ func NewEventTransfer(e EventSent) sdk.Event { new := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new.AppendAttributes(attribute) + new = new.AppendAttributes(attribute) } return new } @@ -152,7 +152,7 @@ func NewEventTransferFrom(e EventSent) sdk.Event { new := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new.AppendAttributes(attribute) + new = new.AppendAttributes(attribute) } return new } @@ -164,14 +164,46 @@ func NewEventGrantPermToken(e EventGrant) sdk.Event { AttributeKeyTo: e.Grantee, AttributeKeyPerm: LegacyPermission(e.Permission).String(), } - if e.Granter != e.Grantee { + if len(e.Granter) != 0 { attributes[AttributeKeyFrom] = e.Granter } new := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new.AppendAttributes(attribute) + new = new.AppendAttributes(attribute) + } + return new +} + +func NewEventGrantPermTokenHead(e EventGrant) sdk.Event { + eventType := EventTypeGrantPermToken.String() + attributes := map[AttributeKey]string{ + AttributeKeyContractID: e.ContractId, + AttributeKeyTo: e.Grantee, + } + if len(e.Granter) != 0 { + attributes[AttributeKeyFrom] = e.Granter + } + + new := sdk.NewEvent(eventType) + for key, value := range attributes { + attribute := sdk.NewAttribute(key.String(), value) + new = new.AppendAttributes(attribute) + } + return new +} + +func NewEventGrantPermTokenBody(e EventGrant) sdk.Event { + eventType := EventTypeGrantPermToken.String() + attributes := map[AttributeKey]string{ + AttributeKeyPerm: LegacyPermission(e.Permission).String(), + } + + new := sdk.NewEvent(eventType) + for key, value := range attributes { + attribute := sdk.NewAttribute(key.String(), value) + new = new.AppendAttributes(attribute) } return new } @@ -187,7 +219,7 @@ func NewEventRevokePermToken(e EventAbandon) sdk.Event { new := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new.AppendAttributes(attribute) + new = new.AppendAttributes(attribute) } return new } @@ -203,7 +235,7 @@ func NewEventApproveToken(e EventAuthorizedOperator) sdk.Event { new := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new.AppendAttributes(attribute) + new = new.AppendAttributes(attribute) } return new } diff --git a/x/token/event_test.go b/x/token/event_test.go new file mode 100644 index 0000000000..15a20ce00b --- /dev/null +++ b/x/token/event_test.go @@ -0,0 +1,382 @@ +package token_test + +import ( + "fmt" + "math/rand" + "testing" + "time" + + "github.com/stretchr/testify/require" + + sdk "github.com/line/lbm-sdk/types" + "github.com/line/lbm-sdk/x/token" +) + +func TestEventTypeStringer(t *testing.T) { + for _, name := range token.EventType_name { + value := token.EventType(token.EventType_value[name]) + customName := value.String() + require.EqualValues(t, value, token.EventTypeFromString(customName), name) + } +} + +func TestAttributeKeyStringer(t *testing.T) { + for _, name := range token.AttributeKey_name { + value := token.AttributeKey(token.AttributeKey_value[name]) + customName := value.String() + require.EqualValues(t, value, token.AttributeKeyFromString(customName), name) + } +} + +func randomString(length int) string { + letters := []rune("0123456789abcdef") + res := make([]rune, length) + for i := range res { + res[i] = letters[rand.Intn(len(letters))] + } + return string(res) +} + +func assertAttribute(e sdk.Event, key, value string) bool { + for _, attr := range e.Attributes { + if string(attr.Key) == key { + return string(attr.Value) == value + } + } + return false +} + +func TestNewEventIssueToken(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + + event := token.EventIssue{ + ContractId: str(), + Name: str(), + Symbol: str(), + Uri: str(), + Meta: str(), + Decimals: 0, + Mintable: true, + } + operator := sdk.AccAddress(str()) + to := sdk.AccAddress(str()) + amount := sdk.OneInt() + legacy := token.NewEventIssueToken(event, operator, to, amount) + + require.Equal(t, token.EventTypeIssueToken.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyName: event.Name, + token.AttributeKeySymbol: event.Symbol, + token.AttributeKeyImageURI: event.Uri, + token.AttributeKeyMeta: event.Meta, + token.AttributeKeyMintable: fmt.Sprintf("%v", event.Mintable), + token.AttributeKeyDecimals: fmt.Sprintf("%d", event.Decimals), + token.AttributeKeyAmount: amount.String(), + token.AttributeKeyOwner: operator.String(), + token.AttributeKeyTo: to.String(), + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventMintToken(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + + event := token.EventMinted{ + ContractId: str(), + Operator: str(), + To: str(), + Amount: sdk.OneInt(), + } + legacy := token.NewEventMintToken(event) + + require.Equal(t, token.EventTypeMintToken.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyFrom: event.Operator, + token.AttributeKeyTo: event.To, + token.AttributeKeyAmount: event.Amount.String(), + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventBurnToken(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + + from := str() + event := token.EventBurned{ + ContractId: str(), + Operator: from, + From: from, + Amount: sdk.OneInt(), + } + legacy := token.NewEventBurnToken(event) + require.NotNil(t, legacy) + + require.Equal(t, token.EventTypeBurnToken.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyFrom: event.From, + token.AttributeKeyAmount: event.Amount.String(), + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventBurnTokenFrom(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + + event := token.EventBurned{ + ContractId: str(), + Operator: str(), + From: str(), + Amount: sdk.OneInt(), + } + legacy := token.NewEventBurnTokenFrom(event) + require.NotNil(t, legacy) + + require.Equal(t, token.EventTypeBurnTokenFrom.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyProxy: event.Operator, + token.AttributeKeyFrom: event.From, + token.AttributeKeyAmount: event.Amount.String(), + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventModifyToken(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + + event := token.EventModified{ + ContractId: str(), + Operator: str(), + Changes: []token.Pair{{ + Field: token.AttributeKeyName.String(), + Value: str(), + }}, + } + legacies := token.NewEventModifyToken(event) + require.Greater(t, len(legacies), 1) + + require.Equal(t, token.EventTypeModifyToken.String(), legacies[0].Type) + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + } + for key, value := range attributes { + require.True(t, assertAttribute(legacies[0], key.String(), value), key) + } + + for i, legacy := range legacies[1:] { + require.Equal(t, token.EventTypeModifyToken.String(), legacy.Type) + + attributes := map[string]string{ + event.Changes[i].Field: event.Changes[i].Value, + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key, value), key) + } + } +} + +func TestNewEventTransfer(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + + from := str() + event := token.EventSent{ + ContractId: str(), + Operator: from, + From: from, + Amount: sdk.OneInt(), + } + legacy := token.NewEventTransfer(event) + require.NotNil(t, legacy) + + require.Equal(t, token.EventTypeTransfer.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyFrom: event.From, + token.AttributeKeyTo: event.To, + token.AttributeKeyAmount: event.Amount.String(), + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventTransferFrom(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + + event := token.EventSent{ + ContractId: str(), + Operator: str(), + From: str(), + Amount: sdk.OneInt(), + } + legacy := token.NewEventTransferFrom(event) + require.NotNil(t, legacy) + + require.Equal(t, token.EventTypeTransferFrom.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyProxy: event.Operator, + token.AttributeKeyFrom: event.From, + token.AttributeKeyTo: event.To, + token.AttributeKeyAmount: event.Amount.String(), + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventGrantPermToken(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + permission := func() token.Permission { + n := len(token.Permission_value) - 1 + return token.Permission(1 + rand.Intn(n)) + } + + event := token.EventGrant{ + ContractId: str(), + Granter: str(), + Grantee: str(), + Permission: permission(), + } + legacy := token.NewEventGrantPermToken(event) + + require.Equal(t, token.EventTypeGrantPermToken.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyFrom: event.Granter, + token.AttributeKeyTo: event.Grantee, + token.AttributeKeyPerm: token.LegacyPermission(event.Permission).String(), + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventGrantPermTokenHead(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + permission := func() token.Permission { + n := len(token.Permission_value) - 1 + return token.Permission(1 + rand.Intn(n)) + } + + event := token.EventGrant{ + ContractId: str(), + Granter: str(), + Grantee: str(), + Permission: permission(), + } + legacy := token.NewEventGrantPermTokenHead(event) + + require.Equal(t, token.EventTypeGrantPermToken.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyTo: event.Grantee, + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventGrantPermTokenBody(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + permission := func() token.Permission { + n := len(token.Permission_value) - 1 + return token.Permission(1 + rand.Intn(n)) + } + + event := token.EventGrant{ + ContractId: str(), + Granter: str(), + Grantee: str(), + Permission: permission(), + } + legacy := token.NewEventGrantPermTokenBody(event) + + require.Equal(t, token.EventTypeGrantPermToken.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyPerm: token.LegacyPermission(event.Permission).String(), + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventRevokePermToken(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + permission := func() token.Permission { + n := len(token.Permission_value) - 1 + return token.Permission(1 + rand.Intn(n)) + } + + event := token.EventAbandon{ + ContractId: str(), + Grantee: str(), + Permission: permission(), + } + legacy := token.NewEventRevokePermToken(event) + + require.Equal(t, token.EventTypeRevokePermToken.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyFrom: event.Grantee, + token.AttributeKeyPerm: token.LegacyPermission(event.Permission).String(), + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} + +func TestNewEventApproveToken(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + str := func() string { return randomString(8) } + + event := token.EventAuthorizedOperator{ + ContractId: str(), + Holder: str(), + Operator: str(), + } + legacy := token.NewEventApproveToken(event) + + require.Equal(t, token.EventTypeApproveToken.String(), legacy.Type) + + attributes := map[token.AttributeKey]string{ + token.AttributeKeyContractID: event.ContractId, + token.AttributeKeyApprover: event.Holder, + token.AttributeKeyProxy: event.Operator, + } + for key, value := range attributes { + require.True(t, assertAttribute(legacy, key.String(), value), key) + } +} From ed1fc35219598dee84ac39f1e294b54952d6898b Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Wed, 13 Jul 2022 12:24:17 +0000 Subject: [PATCH 2/4] fix: fix the order of events --- x/token/keeper/msg_server.go | 30 +++++++++++++++++++++++++++++ x/token/keeper/supply.go | 37 ++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/x/token/keeper/msg_server.go b/x/token/keeper/msg_server.go index 38bfc95991..d33bd99a31 100644 --- a/x/token/keeper/msg_server.go +++ b/x/token/keeper/msg_server.go @@ -192,6 +192,14 @@ func (s msgServer) Grant(c context.Context, req *token.MsgGrant) (*token.MsgGran s.keeper.Grant(ctx, req.ContractId, granter, grantee, req.Permission) + event := token.EventGrant{ + ContractId: req.ContractId, + Granter: req.Granter, + Grantee: req.Grantee, + Permission: req.Permission, + } + ctx.EventManager().EmitEvent(token.NewEventGrantPermToken(event)) + return &token.MsgGrantResponse{}, nil } @@ -207,6 +215,13 @@ func (s msgServer) Abandon(c context.Context, req *token.MsgAbandon) (*token.Msg s.keeper.Abandon(ctx, req.ContractId, grantee, req.Permission) + event := token.EventAbandon{ + ContractId: req.ContractId, + Grantee: req.Grantee, + Permission: req.Permission, + } + ctx.EventManager().EmitEvent(token.NewEventRevokePermToken(event)) + return &token.MsgAbandonResponse{}, nil } @@ -226,6 +241,14 @@ func (s msgServer) GrantPermission(c context.Context, req *token.MsgGrantPermiss s.keeper.Grant(ctx, req.ContractId, granter, grantee, permission) + event := token.EventGrant{ + ContractId: req.ContractId, + Granter: req.From, + Grantee: req.To, + Permission: permission, + } + ctx.EventManager().EmitEvent(token.NewEventGrantPermToken(event)) + return &token.MsgGrantPermissionResponse{}, nil } @@ -241,6 +264,13 @@ func (s msgServer) RevokePermission(c context.Context, req *token.MsgRevokePermi s.keeper.Abandon(ctx, req.ContractId, grantee, permission) + event := token.EventAbandon{ + ContractId: req.ContractId, + Grantee: req.From, + Permission: permission, + } + ctx.EventManager().EmitEvent(token.NewEventRevokePermToken(event)) + return &token.MsgRevokePermissionResponse{}, nil } diff --git a/x/token/keeper/supply.go b/x/token/keeper/supply.go index 0a370d102f..b92ce22f69 100644 --- a/x/token/keeper/supply.go +++ b/x/token/keeper/supply.go @@ -9,6 +9,20 @@ import ( func (k Keeper) Issue(ctx sdk.Context, class token.TokenClass, owner, to sdk.AccAddress, amount sdk.Int) { k.issue(ctx, class) + event := token.EventIssue{ + ContractId: class.ContractId, + Name: class.Name, + Symbol: class.Symbol, + Uri: class.ImageUri, + Meta: class.Meta, + Decimals: class.Decimals, + Mintable: class.Mintable, + } + ctx.EventManager().EmitEvent(token.NewEventIssueToken(event, owner, to, amount)) // deprecated + if err := ctx.EventManager().EmitTypedEvent(&event); err != nil { + panic(err) + } + permissions := []token.Permission{ token.PermissionModify, } @@ -18,7 +32,16 @@ func (k Keeper) Issue(ctx sdk.Context, class token.TokenClass, owner, to sdk.Acc token.PermissionBurn, ) } + + // legacy + eventGrant := token.EventGrant{ + ContractId: class.ContractId, + Grantee: to.String(), + } + ctx.EventManager().EmitEvent(token.NewEventGrantPermTokenHead(eventGrant)) for _, permission := range permissions { + eventGrant.Permission = permission + ctx.EventManager().EmitEvent(token.NewEventGrantPermTokenBody(eventGrant)) k.Grant(ctx, class.ContractId, "", owner, permission) } @@ -32,20 +55,6 @@ func (k Keeper) Issue(ctx sdk.Context, class token.TokenClass, owner, to sdk.Acc }); err != nil { panic(err) } - - event := token.EventIssue{ - ContractId: class.ContractId, - Name: class.Name, - Symbol: class.Symbol, - Uri: class.ImageUri, - Meta: class.Meta, - Decimals: class.Decimals, - Mintable: class.Mintable, - } - ctx.EventManager().EmitEvent(token.NewEventIssueToken(event, owner, to, amount)) // deprecated - if err := ctx.EventManager().EmitTypedEvent(&event); err != nil { - panic(err) - } } func (k Keeper) issue(ctx sdk.Context, class token.TokenClass) { From c0050bd781970d7510603462ac5d7a12a65ad934 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Mon, 18 Jul 2022 06:59:03 +0000 Subject: [PATCH 3/4] docs: update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e8315ed21..2e5a44efb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/auth) [\#563](https://github.com/line/lbm-sdk/pull/563) fix unmarshal bug of `BaseAccountJSON` * (client) [\#565](https://github.com/line/lbm-sdk/pull/565) fix the data race problem in `TestQueryABCIHeight` * (x/token) [\#589](https://github.com/line/lbm-sdk/pull/589) fix naming collision in x/token enums +* (x/token) [\#599](https://github.com/line/lbm-sdk/pull/599) fix the order of events ### Breaking Changes * (proto) [\#564](https://github.com/line/lbm-sdk/pull/564) change gRPC path to original cosmos path From 4048634073b2941046c7b614528c53dd6181b347 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Mon, 18 Jul 2022 11:36:52 +0000 Subject: [PATCH 4/4] style: use variable name other than 'name' --- x/token/event.go | 186 +++++++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 93 deletions(-) diff --git a/x/token/event.go b/x/token/event.go index 5e94917d89..c2a352261f 100644 --- a/x/token/event.go +++ b/x/token/event.go @@ -32,210 +32,210 @@ func AttributeKeyFromString(name string) AttributeKey { return AttributeKey(AttributeKey_value[attributeKeyName]) } -func NewEventIssueToken(e EventIssue, grantee, to sdk.AccAddress, amount sdk.Int) sdk.Event { +func NewEventIssueToken(event EventIssue, grantee, to sdk.AccAddress, amount sdk.Int) sdk.Event { eventType := EventTypeIssueToken.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyName: e.Name, - AttributeKeySymbol: e.Symbol, - AttributeKeyImageURI: e.Uri, - AttributeKeyMeta: e.Meta, - AttributeKeyDecimals: fmt.Sprintf("%d", e.Decimals), - AttributeKeyMintable: fmt.Sprintf("%t", e.Mintable), + AttributeKeyContractID: event.ContractId, + AttributeKeyName: event.Name, + AttributeKeySymbol: event.Symbol, + AttributeKeyImageURI: event.Uri, + AttributeKeyMeta: event.Meta, + AttributeKeyDecimals: fmt.Sprintf("%d", event.Decimals), + AttributeKeyMintable: fmt.Sprintf("%t", event.Mintable), AttributeKeyOwner: grantee.String(), AttributeKeyTo: to.String(), AttributeKeyAmount: amount.String(), } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventMintToken(e EventMinted) sdk.Event { +func NewEventMintToken(event EventMinted) sdk.Event { eventType := EventTypeMintToken.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyFrom: e.Operator, - AttributeKeyTo: e.To, - AttributeKeyAmount: e.Amount.String(), + AttributeKeyContractID: event.ContractId, + AttributeKeyFrom: event.Operator, + AttributeKeyTo: event.To, + AttributeKeyAmount: event.Amount.String(), } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventBurnToken(e EventBurned) sdk.Event { +func NewEventBurnToken(event EventBurned) sdk.Event { eventType := EventTypeBurnToken.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyFrom: e.From, - AttributeKeyAmount: e.Amount.String(), + AttributeKeyContractID: event.ContractId, + AttributeKeyFrom: event.From, + AttributeKeyAmount: event.Amount.String(), } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventBurnTokenFrom(e EventBurned) sdk.Event { +func NewEventBurnTokenFrom(event EventBurned) sdk.Event { eventType := EventTypeBurnTokenFrom.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyProxy: e.Operator, - AttributeKeyFrom: e.From, - AttributeKeyAmount: e.Amount.String(), + AttributeKeyContractID: event.ContractId, + AttributeKeyProxy: event.Operator, + AttributeKeyFrom: event.From, + AttributeKeyAmount: event.Amount.String(), } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventModifyToken(e EventModified) []sdk.Event { +func NewEventModifyToken(event EventModified) []sdk.Event { eventType := EventTypeModifyToken.String() - new := []sdk.Event{ + res := []sdk.Event{ sdk.NewEvent(eventType, - sdk.NewAttribute(AttributeKeyContractID.String(), e.ContractId), + sdk.NewAttribute(AttributeKeyContractID.String(), event.ContractId), ), } - for _, pair := range e.Changes { + for _, pair := range event.Changes { attribute := sdk.NewAttribute(pair.Field, pair.Value) event := sdk.NewEvent(eventType, attribute) - new = append(new, event) + res = append(res, event) } - return new + return res } -func NewEventTransfer(e EventSent) sdk.Event { +func NewEventTransfer(event EventSent) sdk.Event { eventType := EventTypeTransfer.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyFrom: e.From, - AttributeKeyTo: e.To, - AttributeKeyAmount: e.Amount.String(), + AttributeKeyContractID: event.ContractId, + AttributeKeyFrom: event.From, + AttributeKeyTo: event.To, + AttributeKeyAmount: event.Amount.String(), } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventTransferFrom(e EventSent) sdk.Event { +func NewEventTransferFrom(event EventSent) sdk.Event { eventType := EventTypeTransferFrom.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyProxy: e.Operator, - AttributeKeyFrom: e.From, - AttributeKeyTo: e.To, - AttributeKeyAmount: e.Amount.String(), + AttributeKeyContractID: event.ContractId, + AttributeKeyProxy: event.Operator, + AttributeKeyFrom: event.From, + AttributeKeyTo: event.To, + AttributeKeyAmount: event.Amount.String(), } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventGrantPermToken(e EventGrant) sdk.Event { +func NewEventGrantPermToken(event EventGrant) sdk.Event { eventType := EventTypeGrantPermToken.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyTo: e.Grantee, - AttributeKeyPerm: LegacyPermission(e.Permission).String(), + AttributeKeyContractID: event.ContractId, + AttributeKeyTo: event.Grantee, + AttributeKeyPerm: LegacyPermission(event.Permission).String(), } - if len(e.Granter) != 0 { - attributes[AttributeKeyFrom] = e.Granter + if len(event.Granter) != 0 { + attributes[AttributeKeyFrom] = event.Granter } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventGrantPermTokenHead(e EventGrant) sdk.Event { +func NewEventGrantPermTokenHead(event EventGrant) sdk.Event { eventType := EventTypeGrantPermToken.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyTo: e.Grantee, + AttributeKeyContractID: event.ContractId, + AttributeKeyTo: event.Grantee, } - if len(e.Granter) != 0 { - attributes[AttributeKeyFrom] = e.Granter + if len(event.Granter) != 0 { + attributes[AttributeKeyFrom] = event.Granter } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventGrantPermTokenBody(e EventGrant) sdk.Event { +func NewEventGrantPermTokenBody(event EventGrant) sdk.Event { eventType := EventTypeGrantPermToken.String() attributes := map[AttributeKey]string{ - AttributeKeyPerm: LegacyPermission(e.Permission).String(), + AttributeKeyPerm: LegacyPermission(event.Permission).String(), } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventRevokePermToken(e EventAbandon) sdk.Event { +func NewEventRevokePermToken(event EventAbandon) sdk.Event { eventType := EventTypeRevokePermToken.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyFrom: e.Grantee, - AttributeKeyPerm: LegacyPermission(e.Permission).String(), + AttributeKeyContractID: event.ContractId, + AttributeKeyFrom: event.Grantee, + AttributeKeyPerm: LegacyPermission(event.Permission).String(), } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res } -func NewEventApproveToken(e EventAuthorizedOperator) sdk.Event { +func NewEventApproveToken(event EventAuthorizedOperator) sdk.Event { eventType := EventTypeApproveToken.String() attributes := map[AttributeKey]string{ - AttributeKeyContractID: e.ContractId, - AttributeKeyApprover: e.Holder, - AttributeKeyProxy: e.Operator, + AttributeKeyContractID: event.ContractId, + AttributeKeyApprover: event.Holder, + AttributeKeyProxy: event.Operator, } - new := sdk.NewEvent(eventType) + res := sdk.NewEvent(eventType) for key, value := range attributes { attribute := sdk.NewAttribute(key.String(), value) - new = new.AppendAttributes(attribute) + res = res.AppendAttributes(attribute) } - return new + return res }