-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathproposal.go
More file actions
481 lines (396 loc) · 15.9 KB
/
proposal.go
File metadata and controls
481 lines (396 loc) · 15.9 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package resolvers
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
"context"
"fmt"
"strconv"
"github.com/forbole/bdjuno/database/types"
pkgContext "github.com/oursky/likedao/pkg/context"
"github.com/oursky/likedao/pkg/dataloaders"
servererrors "github.com/oursky/likedao/pkg/errors"
graphql1 "github.com/oursky/likedao/pkg/generated/graphql"
"github.com/oursky/likedao/pkg/models"
)
func (r *proposalResolver) ProposalID(ctx context.Context, obj *models.Proposal) (int, error) {
return obj.ID, nil
}
func (r *proposalResolver) Type(ctx context.Context, obj *models.Proposal) (string, error) {
proposalType := new(models.ProposalType)
err := proposalType.UnmarshalGQL(obj.ProposalType)
if err != nil {
return obj.ProposalType, nil
}
return proposalType.String(), nil
}
func (r *proposalResolver) DepositTotal(ctx context.Context, obj *models.Proposal) ([]types.DbDecCoin, error) {
res, err := pkgContext.GetQueriesFromCtx(ctx).Proposal.QueryProposalDepositTotal(obj.ID)
if err != nil {
return []types.DbDecCoin{}, err
}
return res, nil
}
func (r *proposalResolver) TallyResult(ctx context.Context, obj *models.Proposal) (*models.ProposalTallyResult, error) {
if obj.Status == models.ProposalStatusFailed || obj.Status == models.ProposalStatusInvalid || obj.Status == models.ProposalStatusDepositPeriod {
return nil, nil
}
tally, err := pkgContext.GetDataLoadersFromCtx(ctx).Proposal.LoadProposalTallyResult(obj.ID)
if err != nil {
return nil, servererrors.QueryError.NewError(ctx, fmt.Sprintf("failed to load proposal tally result: %v", err))
}
return tally, nil
}
func (r *proposalResolver) Turnout(ctx context.Context, obj *models.Proposal) (*float64, error) {
turnout, err := pkgContext.GetDataLoadersFromCtx(ctx).Proposal.LoadProposalTurnout(obj.ID)
if err != nil {
return nil, servererrors.QueryError.NewError(ctx, fmt.Sprintf("failed to load proposal turnout: %v", err))
}
if turnout == nil {
return nil, nil
}
return turnout, nil
}
func (r *proposalResolver) VoteByAddress(ctx context.Context, obj *models.Proposal, address string) (*models.ProposalVote, error) {
key := models.ProposalVoteKey{ProposalID: obj.ID, Address: address}
vote, err := pkgContext.GetDataLoadersFromCtx(ctx).Proposal.LoadProposalVote(key)
if err != nil {
return nil, err
}
return vote, nil
}
func (r *proposalResolver) DepositByAddress(ctx context.Context, obj *models.Proposal, address string) (*models.ProposalDeposit, error) {
key := models.ProposalDepositKey{ProposalID: obj.ID, Address: address}
vote, err := pkgContext.GetDataLoadersFromCtx(ctx).Proposal.LoadProposalDeposit(key)
if err != nil {
return nil, err
}
return vote, nil
}
func (r *proposalResolver) Reactions(ctx context.Context, obj *models.Proposal) ([]models.ReactionCount, error) {
reactionCounts, err := pkgContext.GetDataLoadersFromCtx(ctx).Reaction.LoadProposalReactionCount(obj.ID)
if err != nil {
return nil, err
}
var result []models.ReactionCount
for _, reactionCount := range reactionCounts {
result = append(result, models.ReactionCount{Reaction: reactionCount.Reaction, Count: reactionCount.Count})
}
return result, nil
}
func (r *proposalResolver) MyReaction(ctx context.Context, obj *models.Proposal) (*string, error) {
userAddress := pkgContext.GetAuthedUserAddress(ctx)
if userAddress == "" {
return nil, nil
}
reaction, err := pkgContext.GetDataLoadersFromCtx(ctx).Reaction.LoadUserProposalReactions(dataloaders.UserProposalReactionKey{
ProposalID: obj.ID,
UserAddress: userAddress,
})
if err != nil {
return nil, err
}
if reaction == nil {
return nil, nil
}
return &reaction.Reaction, nil
}
func (r *proposalResolver) Votes(ctx context.Context, obj *models.Proposal, input models.QueryProposalVotesInput) (*models.Connection[models.ProposalVote], error) {
result := make([]models.ProposalVote, 0)
validatorLimit := input.First
validatorOffset := input.After
// No need validators if no delegation or already went through all the validators
if len(input.PinnedValidators) == 0 || input.After > len(input.PinnedValidators) {
validatorLimit = 0
validatorOffset = 0
}
validators, err := pkgContext.GetQueriesFromCtx(ctx).Validator.WithProposalVotes().QueryPaginatedValidators(validatorLimit, validatorOffset, input.PinnedValidators)
if err != nil {
return nil, servererrors.QueryError.NewError(ctx, fmt.Sprintf("failed to load validators: %v", err))
}
// Add validators to result
validatorDelegationAddresses := make([]string, 0)
for i := range validators.Items {
validator := validators.Items[i]
// There should only be at most one vote when scoped
if validator.Info != nil && len(validator.Info.ProposalVotes) != 0 {
for _, vote := range validator.Info.ProposalVotes {
if vote.ProposalID == obj.ID {
result = append(result, vote)
break
}
}
} else {
result = append(result, models.ProposalVote{
ProposalID: obj.ID,
VoterAddress: validator.Info.SelfDelegateAddress,
Option: "",
Height: validator.Info.Height,
ValidatorInfo: validator.Info,
})
}
validatorDelegationAddresses = append(validatorDelegationAddresses, validator.Info.SelfDelegateAddress)
}
voteLimit := input.First
// Get the remaining vote items if the number of validators are fewer than the pageSize
if len(validators.Items) < input.First {
voteLimit = input.First - len(validators.Items)
}
// Skip votes if we already have enough items for a page
if len(validators.Items) == input.First {
voteLimit = 0
}
// Start offsetting the votes if we are done with the validators
voteOffset := input.After
if len(input.PinnedValidators) != 0 {
voteOffset -= validators.PaginationInfo.TotalCount
}
if voteOffset < 0 {
voteOffset = 0
}
votes, err := pkgContext.GetQueriesFromCtx(ctx).Proposal.QueryPaginatedProposalVotes(obj.ID, voteLimit, voteOffset, input.Order, validatorDelegationAddresses)
if err != nil {
return nil, servererrors.QueryError.NewError(ctx, fmt.Sprintf("failed to load proposal votes: %v", err))
}
// Add votes to result
for i := range votes.Items {
vote := votes.Items[i]
result = append(result, vote)
}
voteWithValidatorCursorMap := make(map[string]string)
for index, identity := range result {
cursorString := strconv.Itoa(input.After + index + 1)
voteWithValidatorCursorMap[identity.NodeID().String()] = cursorString
}
conn := models.NewConnection(result, func(model models.ProposalVote) string {
return voteWithValidatorCursorMap[model.NodeID().String()]
})
totalCount := votes.PaginationInfo.TotalCount
hasNextPage := votes.PaginationInfo.HasNext
hasPreviousPage := votes.PaginationInfo.HasPrevious
if validatorLimit != 0 {
totalCount = totalCount + validators.PaginationInfo.TotalCount
hasNextPage = validators.PaginationInfo.HasNext
hasPreviousPage = validators.PaginationInfo.HasPrevious
}
conn.TotalCount = totalCount
conn.PageInfo.HasNextPage = hasNextPage
conn.PageInfo.HasPreviousPage = hasPreviousPage
return &conn, nil
}
func (r *proposalResolver) Deposits(ctx context.Context, obj *models.Proposal, input models.QueryProposalDepositsInput) (*models.Connection[models.ProposalDeposit], error) {
result := make([]models.ProposalDeposit, 0)
validatorLimit := input.First
validatorOffset := input.After
// No need validators if no delegation or already went through all the validators
if len(input.PinnedValidators) == 0 || input.After > len(input.PinnedValidators) {
validatorLimit = 0
validatorOffset = 0
}
validators, err := pkgContext.GetQueriesFromCtx(ctx).Validator.WithProposalDeposits().QueryPaginatedValidators(validatorLimit, validatorOffset, input.PinnedValidators)
if err != nil {
return nil, servererrors.QueryError.NewError(ctx, fmt.Sprintf("failed to load validators: %v", err))
}
// Add validators to result
for i := range validators.Items {
validator := validators.Items[i]
if validator.Info != nil && len(validator.Info.ProposalDeposits) != 0 {
for _, deposit := range validator.Info.ProposalDeposits {
if deposit.ProposalID == obj.ID {
result = append(result, deposit)
break
}
}
} else {
result = append(result, models.ProposalDeposit{
ProposalID: obj.ID,
DepositorAddress: validator.Info.SelfDelegateAddress,
Amount: nil,
Height: validator.Info.Height,
ValidatorInfo: validator.Info,
})
}
}
depositLimit := input.First
// Get the remaining vote items if the number of validators are fewer than the pageSize
if len(validators.Items) < input.First {
depositLimit = input.First - len(validators.Items)
}
// Skip votes if we already have enough items for a page
if len(validators.Items) == input.First {
depositLimit = 0
}
// Start offsetting the votes if we are done with the validators
depositOffset := input.After
if len(input.PinnedValidators) != 0 {
depositOffset -= validators.PaginationInfo.TotalCount
}
if depositOffset < 0 {
depositOffset = 0
}
deposits, err := pkgContext.GetQueriesFromCtx(ctx).Proposal.QueryPaginatedProposalDeposits(obj.ID, depositLimit, depositOffset, input.Order, input.PinnedValidators)
if err != nil {
return nil, servererrors.QueryError.NewError(ctx, fmt.Sprintf("failed to load proposal votes: %v", err))
}
// Add votes to result
for i := range deposits.Items {
deposit := deposits.Items[i]
result = append(result, deposit)
}
depositWithValidatorCursorMap := make(map[string]string)
for index, identity := range result {
cursorString := strconv.Itoa(input.After + index + 1)
depositWithValidatorCursorMap[identity.NodeID().String()] = cursorString
}
conn := models.NewConnection(result, func(model models.ProposalDeposit) string {
return depositWithValidatorCursorMap[model.NodeID().String()]
})
totalCount := deposits.PaginationInfo.TotalCount
hasNextPage := deposits.PaginationInfo.HasNext
hasPreviousPage := deposits.PaginationInfo.HasPrevious
if validatorLimit != 0 {
totalCount = totalCount + validators.PaginationInfo.TotalCount
hasNextPage = validators.PaginationInfo.HasNext
hasPreviousPage = validators.PaginationInfo.HasPrevious
}
conn.TotalCount = totalCount
conn.PageInfo.HasNextPage = hasNextPage
conn.PageInfo.HasPreviousPage = hasPreviousPage
return &conn, nil
}
func (r *proposalDepositResolver) Depositor(ctx context.Context, obj *models.ProposalDeposit) (models.ProposalDepositor, error) {
// Deposit is from a validator
validator, err := pkgContext.GetDataLoadersFromCtx(ctx).Validator.LoadValidatorWithInfoBySelfDelegationAddress(obj.DepositorAddress)
if err == nil && validator != nil {
return validator, nil
}
// Deposit is from a depositor
if obj.DepositorAddress != "" {
return models.StringObject{
Value: obj.DepositorAddress,
}, nil
}
// Deposit is initial deposit by the proposer
proposal, err := pkgContext.GetDataLoadersFromCtx(ctx).Proposal.Load(strconv.Itoa(obj.ProposalID))
if err == nil && proposal != nil && proposal.ProposerAddress != "" {
return models.StringObject{
Value: proposal.ProposerAddress,
}, nil
}
return nil, nil
}
func (r *proposalTallyResultResolver) Yes(ctx context.Context, obj *models.ProposalTallyResult) (models.BigInt, error) {
return models.NewBigIntFromBunBigInt(obj.Yes), nil
}
func (r *proposalTallyResultResolver) No(ctx context.Context, obj *models.ProposalTallyResult) (models.BigInt, error) {
return models.NewBigIntFromBunBigInt(obj.No), nil
}
func (r *proposalTallyResultResolver) NoWithVeto(ctx context.Context, obj *models.ProposalTallyResult) (models.BigInt, error) {
return models.NewBigIntFromBunBigInt(obj.NoWithVeto), nil
}
func (r *proposalTallyResultResolver) Abstain(ctx context.Context, obj *models.ProposalTallyResult) (models.BigInt, error) {
return models.NewBigIntFromBunBigInt(obj.Abstain), nil
}
func (r *proposalTallyResultResolver) OutstandingOption(ctx context.Context, obj *models.ProposalTallyResult) (*models.ProposalVoteOption, error) {
option := new(models.ProposalVoteOption)
var votes = int64(0)
// FIXME: Improve this handling
if obj.Yes.ToInt64() > votes {
*option = models.ProposalVoteOptionYes
votes = obj.Yes.ToInt64()
}
if obj.No.ToInt64() > votes {
*option = models.ProposalVoteOptionNo
votes = obj.No.ToInt64()
}
if obj.NoWithVeto.ToInt64() > votes {
*option = models.ProposalVoteOptionNoWithVeto
votes = obj.NoWithVeto.ToInt64()
}
if obj.Abstain.ToInt64() > votes {
*option = models.ProposalVoteOptionAbstain
}
// All the votes are the same, no outstanding option
if *option == "" {
return nil, nil
}
return option, nil
}
func (r *proposalVoteResolver) Voter(ctx context.Context, obj *models.ProposalVote) (models.ProposalVoter, error) {
validator, err := pkgContext.GetDataLoadersFromCtx(ctx).Validator.LoadValidatorWithInfoBySelfDelegationAddress(obj.VoterAddress)
if err == nil && validator != nil {
return validator, nil
}
return models.StringObject{
Value: obj.VoterAddress,
}, nil
}
func (r *proposalVoteResolver) Option(ctx context.Context, obj *models.ProposalVote) (*models.ProposalVoteOption, error) {
if obj.Option == "" {
return nil, nil
}
return &obj.Option, nil
}
func (r *queryResolver) Proposals(ctx context.Context, input models.QueryProposalsInput) (*models.Connection[models.Proposal], error) {
proposalQuery := pkgContext.GetQueriesFromCtx(ctx).Proposal
if input.Address != nil {
if !input.Address.IsDepositor && !input.Address.IsSubmitter && !input.Address.IsVoter {
return nil, servererrors.BadUserInput.NewError(
ctx,
"invalid address filter: at least one of isDepositor, isSubmitter, isVoter in address filter should be true",
)
}
proposalQuery = proposalQuery.ScopeProposalAddress(input.Address)
}
if input.Status != nil {
proposalQuery = proposalQuery.ScopeProposalStatus((*input.Status).ToProposalStatus())
}
if input.Order != nil {
proposalQuery = proposalQuery.ScopeProposalOrder(input.Order)
}
res, err := proposalQuery.QueryPaginatedProposals(input.First, input.After)
if err != nil {
return nil, servererrors.QueryError.NewError(ctx, fmt.Sprintf("failed to load proposals: %v", err))
}
proposalCursorMap := make(map[int]string)
for index, proposal := range res.Items {
cursorString := strconv.Itoa(input.After + index + 1)
proposalCursorMap[proposal.ID] = cursorString
}
conn := models.NewConnection(res.Items, func(model models.Proposal) string {
return proposalCursorMap[model.ID]
})
conn.TotalCount = res.PaginationInfo.TotalCount
conn.PageInfo.HasNextPage = res.PaginationInfo.HasNext
conn.PageInfo.HasPreviousPage = res.PaginationInfo.HasPrevious
return &conn, nil
}
func (r *queryResolver) ProposalByID(ctx context.Context, id models.NodeID) (*models.Proposal, error) {
res, err := pkgContext.GetDataLoadersFromCtx(ctx).Proposal.Load(id.ID)
if err != nil {
return nil, err
}
return res, nil
}
func (r *queryResolver) ProposalVotesDistribution(ctx context.Context, address string) (*models.ProposalTallyResult, error) {
distribution, err := pkgContext.GetQueriesFromCtx(ctx).Proposal.QueryProposalVoteCountByAddress(address)
if err != nil {
return nil, err
}
return distribution, nil
}
// Proposal returns graphql1.ProposalResolver implementation.
func (r *Resolver) Proposal() graphql1.ProposalResolver { return &proposalResolver{r} }
// ProposalDeposit returns graphql1.ProposalDepositResolver implementation.
func (r *Resolver) ProposalDeposit() graphql1.ProposalDepositResolver {
return &proposalDepositResolver{r}
}
// ProposalTallyResult returns graphql1.ProposalTallyResultResolver implementation.
func (r *Resolver) ProposalTallyResult() graphql1.ProposalTallyResultResolver {
return &proposalTallyResultResolver{r}
}
// ProposalVote returns graphql1.ProposalVoteResolver implementation.
func (r *Resolver) ProposalVote() graphql1.ProposalVoteResolver { return &proposalVoteResolver{r} }
type proposalResolver struct{ *Resolver }
type proposalDepositResolver struct{ *Resolver }
type proposalTallyResultResolver struct{ *Resolver }
type proposalVoteResolver struct{ *Resolver }