-
Notifications
You must be signed in to change notification settings - Fork 1
/
abci.go
48 lines (38 loc) · 1.41 KB
/
abci.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
package posts
import (
"fmt"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gridiron-zone/huddle/x/posts/keeper"
"github.com/gridiron-zone/huddle/x/posts/types"
)
// EndBlocker called every block, process ended polls
func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {
// Iterate over all the active polls that have been ended by the current block time
keeper.IterateActivePollsQueue(ctx, ctx.BlockTime(), func(poll types.Attachment) (stop bool) {
// Compute the poll results
results := keeper.Tally(ctx, poll.SubspaceID, poll.PostID, poll.ID)
// Update the content with the results
content := poll.Content.GetCachedValue().(*types.Poll)
content.FinalTallyResults = results
contentAny, err := codectypes.NewAnyWithValue(content)
if err != nil {
panic(err)
}
poll.Content = contentAny
keeper.SaveAttachment(ctx, poll)
keeper.RemoveFromActivePollQueue(ctx, poll)
// Emit an event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeTallyPoll,
sdk.NewAttribute(types.AttributeKeySubspaceID, fmt.Sprintf("%d", poll.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", poll.PostID)),
sdk.NewAttribute(types.AttributeKeyPollID, fmt.Sprintf("%d", poll.ID)),
),
)
// When poll ends
keeper.AfterPollVotingPeriodEnded(ctx, poll.SubspaceID, poll.PostID, poll.ID)
return false
})
}