-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
128 lines (109 loc) · 3.37 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package feegrant
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant"
"github.com/pkg/errors"
"github.com/bro-n-bro/spacebox-crawler/modules/utils"
"github.com/bro-n-bro/spacebox-crawler/types"
"github.com/bro-n-bro/spacebox/broker/model"
)
func (m *Module) HandleMessage(ctx context.Context, index int, cosmosMsg sdk.Msg, tx *types.Tx) error {
if len(tx.Logs) == 0 {
return nil
}
switch msg := cosmosMsg.(type) {
case *feegranttypes.MsgGrantAllowance:
var allowance feegranttypes.FeeAllowanceI
if err := m.cdc.UnpackAny(msg.Allowance, &allowance); err != nil {
return err
}
ex, err := allowance.ExpiresAt()
if err != nil {
return err
}
data, err := m.cdc.MarshalJSON(msg.Allowance)
if err != nil {
return err
}
if err = m.broker.PublishGrantAllowanceMessage(ctx, model.GrantAllowanceMessage{
Height: tx.Height,
MsgIndex: int64(index),
TxHash: tx.TxHash,
Granter: msg.Granter,
Grantee: msg.Grantee,
Expiration: utils.TimeFromPtr(ex),
Allowance: data,
}); err != nil {
m.log.Err(err).Int64("height", tx.Height).Msg("error while publishing grant allowance message")
return err
}
if err = m.publishFeeAllowance(ctx, tx.Height, msg.Granter, msg.Grantee); err != nil {
m.log.Err(err).
Int64("height", tx.Height).
Str("message", "MsgGrantAllowance").
Msg("error while publishing fee allowance")
return err
}
case *feegranttypes.MsgRevokeAllowance:
if err := m.broker.PublishRevokeAllowanceMessage(ctx, model.RevokeAllowanceMessage{
Height: tx.Height,
MsgIndex: int64(index),
TxHash: tx.TxHash,
Granter: msg.Granter,
Grantee: msg.Grantee,
}); err != nil {
m.log.Err(err).Int64("height", tx.Height).Msg("error while publishing grant allowance message")
return err
}
if err := m.publishFeeAllowance(ctx, tx.Height, msg.Granter, msg.Grantee); err != nil {
m.log.Err(err).
Int64("height", tx.Height).
Str("message", "MsgRevokeAllowance").
Msg("error while publishing fee allowance")
return err
}
}
return nil
}
func (m *Module) publishFeeAllowance(ctx context.Context, height int64, granter, grantee string) error {
respPb, err := m.client.FeegrantQueryClient.Allowance(ctx, &feegranttypes.QueryAllowanceRequest{
Granter: granter,
Grantee: grantee,
})
if err != nil {
// set fee allowance to inactive if it was not found
if err.Error() == "rpc error: code = Internal desc = fee-grant not found: unauthorized" { //nolint:misspell
m.log.Debug().
Str("granter", granter).
Str("grantee", grantee).
Int64("height", height).
Msg("fee allowance not found, setting to inactive")
return m.broker.PublishFeeAllowance(ctx, model.FeeAllowance{
Granter: granter,
Grantee: grantee,
Height: height,
})
}
return errors.Wrap(err, "error while querying fee allowance")
}
allowanceBytes, err := m.cdc.MarshalJSON(respPb.Allowance)
if err != nil {
return err
}
var allowance feegranttypes.FeeAllowanceI
if err = m.cdc.UnpackAny(respPb.Allowance.Allowance, &allowance); err != nil {
return err
}
ex, err := allowance.ExpiresAt()
if err != nil {
return err
}
return m.broker.PublishFeeAllowance(ctx, model.FeeAllowance{
Granter: granter,
Grantee: grantee,
Allowance: allowanceBytes,
Expiration: utils.TimeFromPtr(ex),
Height: height,
})
}