diff --git a/x/committee/keeper/proposal.go b/x/committee/keeper/proposal.go index a983e3e3..18ccec82 100644 --- a/x/committee/keeper/proposal.go +++ b/x/committee/keeper/proposal.go @@ -251,13 +251,37 @@ func (k Keeper) enactProposal(ctx sdk.Context, proposal types.Proposal) error { // CloseProposal deletes proposals and their votes, emitting an event denoting the final status of the proposal func (k Keeper) CloseProposal(ctx sdk.Context, proposal types.Proposal, outcome types.ProposalOutcome) { + proposalTally := types.ProposalPollingStatus{} + committee, found := k.GetCommittee(ctx, proposal.CommitteeID) + if found { + switch com := committee.(type) { + case types.MemberCommittee: + currVotes := k.TallyMemberCommitteeVotes(ctx, proposal.ID) + possibleVotes := sdk.NewDec(int64(len(com.Members))) + memberPollingStatus := types.NewProposalPollingStatus(proposal.ID, currVotes, + currVotes, possibleVotes, com.VoteThreshold, sdk.Dec{Int: nil}) + proposalTally = memberPollingStatus + case types.TokenCommittee: + yesVotes, _, currVotes, possibleVotes := k.TallyTokenCommitteeVotes(ctx, proposal.ID, com.TallyDenom) + tokenPollingStatus := types.NewProposalPollingStatus(proposal.ID, yesVotes, + currVotes, possibleVotes, com.VoteThreshold, com.Quorum) + proposalTally = tokenPollingStatus + } + } + k.DeleteProposalAndVotes(ctx, proposal.ID) + bz, err := k.cdc.MarshalJSON(proposalTally) + if err != nil { + fmt.Println("error marshaling proposal tally to bytes:", proposalTally.String()) + } + ctx.EventManager().EmitEvent( sdk.NewEvent( types.EventTypeProposalClose, sdk.NewAttribute(types.AttributeKeyCommitteeID, fmt.Sprintf("%d", proposal.CommitteeID)), sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.ID)), + sdk.NewAttribute(types.AttributeKeyProposalTally, fmt.Sprintf("%s", bz)), sdk.NewAttribute(types.AttributeKeyProposalOutcome, outcome.String()), ), ) diff --git a/x/committee/keeper/proposal_test.go b/x/committee/keeper/proposal_test.go index f0c014f9..5d8c8ac3 100644 --- a/x/committee/keeper/proposal_test.go +++ b/x/committee/keeper/proposal_test.go @@ -818,8 +818,29 @@ func (suite *KeeperTestSuite) TestCloseProposal() { // Confirm proposal exists proposal, found := keeper.GetProposal(ctx, proposalID) suite.True(found) + // Close proposal keeper.CloseProposal(ctx, proposal, types.Passed) + + events := ctx.EventManager().Events() + event := events[0] + suite.Require().Equal("proposal_close", event.Type) + + hasProposalTallyAttr := false + for _, attr := range event.Attributes { + if string(attr.GetKey()) == "proposal_tally" { + hasProposalTallyAttr = true + valueStr := string(attr.GetValue()) + suite.Contains(valueStr, "proposal_id") + suite.Contains(valueStr, "yes_votes") + suite.Contains(valueStr, "current_votes") + suite.Contains(valueStr, "possible_votes") + suite.Contains(valueStr, "vote_threshold") + suite.Contains(valueStr, "quorum") + } + } + suite.Require().True(hasProposalTallyAttr) + // Confirm proposal doesn't exist _, found = keeper.GetProposal(ctx, proposalID) suite.False(found) diff --git a/x/committee/types/events.go b/x/committee/types/events.go index 6794859c..3cefe9f7 100644 --- a/x/committee/types/events.go +++ b/x/committee/types/events.go @@ -14,4 +14,5 @@ const ( AttributeKeyVoter = "voter" AttributeKeyVote = "vote" AttributeKeyProposalOutcome = "proposal_outcome" + AttributeKeyProposalTally = "proposal_tally" ) diff --git a/x/committee/types/querier.go b/x/committee/types/querier.go index 557c6342..a2683bfd 100644 --- a/x/committee/types/querier.go +++ b/x/committee/types/querier.go @@ -1,6 +1,8 @@ package types import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -81,3 +83,16 @@ func NewProposalPollingStatus(proposalID uint64, yesVotes, currentVotes, possibl Quorum: quorum, } } + +// String implements fmt.Stringer +func (p ProposalPollingStatus) String() string { + return fmt.Sprintf(`Proposal ID: %d + Yes votes: %d + Current votes: %d + Possible votes: %d + Vote threshold: %d + Quorum: %d`, + p.ProposalID, p.YesVotes, p.CurrentVotes, + p.PossibleVotes, p.VoteThreshold, p.Quorum, + ) +}