-
Notifications
You must be signed in to change notification settings - Fork 0
/
grants.go
182 lines (151 loc) · 5.28 KB
/
grants.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package operations
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/cosmos/cosmos-sdk/x/feegrant"
wallettypes "github.com/desmos-labs/cosmos-go-wallet/types"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"
"github.com/desmos-labs/caerus/scheduler"
"github.com/desmos-labs/caerus/types"
)
func RegisterGrantsOperations(ctx scheduler.Context, scheduler *gocron.Scheduler) {
scheduler.Every(10).Seconds().Do(func() {
err := GrantAuthorizations(ctx)
if err != nil {
log.Error().Err(err).Msg("error while granting authorizations")
}
})
}
// GrantAuthorizations grants the authorizations to the users that have requested them
func GrantAuthorizations(ctx scheduler.Context) error {
// Get at most 100 authorizations requests
feeGrantRequests, err := ctx.Database.GetNotGrantedFeeGrantRequests(100)
if err != nil {
return err
}
// If there are no fee grant requests, return
if len(feeGrantRequests) == 0 {
return nil
}
// Group the authorizations based on the application that is associated to them
appsFeeGrantRequests := map[string][]types.FeeGrantRequest{}
for _, feeGrantRequest := range feeGrantRequests {
appsFeeGrantRequests[feeGrantRequest.AppID] = append(appsFeeGrantRequests[feeGrantRequest.AppID], feeGrantRequest)
}
// Get the grant requests ids.
// This will be used later on to mark the fee requests as granted
var grantedGrantRequestsIDs []string
// Get the list of users to whom the fee allowances have been granted.
// This will be used later on to notify applications about which users the grants have been given to
grantedUsers := map[string][]string{}
// Build the messages to be sent
var msgExecMsgs []sdk.Msg
for appID, appGrantRequests := range appsFeeGrantRequests {
// Get the application wallet address
app, found, err := ctx.Database.GetApp(appID)
if err != nil {
return err
}
if !found {
log.Error().Str("application id", appID).Msg("application not found while trying to grant a fee allowance")
// Skip this application since we are not going to be able to properly execute the message
continue
}
// Check if the app still has the on-chain MsgExec authorization
// If it does not have an authorization, send a notification
hasOnChainAuthorization, err := ctx.ChainClient.HasGrantedMsgGrantAllowanceAuthorization(app.WalletAddress)
if err != nil {
return err
}
if !hasOnChainAuthorization {
err = ctx.FirebaseClient.SendNotificationToApp(appID, types.Notification{
Data: map[string]string{
types.NotificationTypeKey: "missing_authorization",
types.NotificationMessageKey: "Your application is missing the on-chain authorization to be able to send fee allowances",
},
})
if err != nil {
return err
}
// Skip this application since we are not going to be able to properly execute the message
continue
}
// Build the MsgExec instances
var grantAllowanceMsgs []sdk.Msg
for _, grantRequest := range appGrantRequests {
// Parse the addresses
feeGrantGranterAddress, err := sdk.AccAddressFromBech32(app.WalletAddress)
if err != nil {
return err
}
feeGrantGranteeAddress, err := sdk.AccAddressFromBech32(grantRequest.DesmosAddress)
if err != nil {
return err
}
msgGrantAllowance, err := feegrant.NewMsgGrantAllowance(grantRequest.Allowance, feeGrantGranterAddress, feeGrantGranteeAddress)
if err != nil {
return err
}
grantedGrantRequestsIDs = append(grantedGrantRequestsIDs, grantRequest.ID)
grantedUsers[appID] = append(grantedUsers[appID], grantRequest.DesmosAddress)
// Only add the message if the fee allowance has not been granted yet
hasFeeGrant, err := ctx.ChainClient.HasFeeGrant(feeGrantGranteeAddress.String(), feeGrantGranterAddress.String())
if err != nil {
return err
}
if !hasFeeGrant {
grantAllowanceMsgs = append(grantAllowanceMsgs, msgGrantAllowance)
}
}
if len(grantAllowanceMsgs) == 0 {
continue
}
// Parse the messages
authzGranteeAddress, err := sdk.AccAddressFromBech32(ctx.ChainClient.AccAddress())
if err != nil {
return err
}
// Build the MsgExec instance
msgExec := authz.NewMsgExec(authzGranteeAddress, grantAllowanceMsgs)
msgExecMsgs = append(msgExecMsgs, &msgExec)
}
// If there are no messages, just return
if len(msgExecMsgs) == 0 {
return nil
}
// Broadcast the transaction
res, err := ctx.ChainClient.BroadcastTxCommit(&wallettypes.TransactionData{
Messages: msgExecMsgs,
Memo: "Sent from Caerus",
GasAuto: true,
FeeAuto: true,
})
if err != nil {
return err
}
if res.Code != 0 {
return fmt.Errorf("error while broadcasting fee allowance transactions: %s", res.RawLog)
}
// Set the grant requests as granted
err = ctx.Database.SetFeeGrantRequestsGranted(grantedGrantRequestsIDs)
if err != nil {
return err
}
// Send a notification to the applications
for appID, usersAddresses := range grantedUsers {
err = ctx.FirebaseClient.SendNotificationToApp(appID, types.Notification{
Data: map[string]string{
types.NotificationTypeKey: "fee_allowances_granted",
types.NotificationMessageKey: "Your fee allowances have been granted",
"users": strings.Join(usersAddresses, ","),
},
})
if err != nil {
return err
}
}
return nil
}