From 658b00f024a5877da58d579e838cb762d5a71f4c Mon Sep 17 00:00:00 2001 From: Nguyen Thanh Nhan Date: Wed, 25 Oct 2023 15:21:24 +0700 Subject: [PATCH] Add ParseProposalIDFromEvents helper function (#4942) * add ParseProposalIDFromEvents * update with damian suggest --- testing/endpoint.go | 12 ++---------- testing/events.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/testing/endpoint.go b/testing/endpoint.go index d83b0625d67..23f5c55e486 100644 --- a/testing/endpoint.go +++ b/testing/endpoint.go @@ -2,7 +2,6 @@ package ibctesting import ( "fmt" - "strconv" "strings" "github.com/stretchr/testify/require" @@ -619,15 +618,8 @@ func (endpoint *Endpoint) ChanUpgradeInit() error { return err } - events := res.Events - for _, event := range events { - for _, attribute := range event.Attributes { - if attribute.Key == "proposal_id" { - proposalID, err = strconv.ParseUint(attribute.Value, 10, 64) - require.NoError(endpoint.Chain.TB, err) - } - } - } + proposalID, err = ParseProposalIDFromEvents(res.Events) + require.NoError(endpoint.Chain.TB, err) return VoteAndCheckProposalStatus(endpoint, proposalID) } diff --git a/testing/events.go b/testing/events.go index 9ccfaea96fa..b26b970659e 100644 --- a/testing/events.go +++ b/testing/events.go @@ -135,6 +135,19 @@ func ParseAckFromEvents(events []abci.Event) ([]byte, error) { return nil, fmt.Errorf("acknowledgement event attribute not found") } +// ParseProposalIDFromEvents parses events emitted from MsgSubmitProposal and returns proposalID +func ParseProposalIDFromEvents(events []abci.Event) (uint64, error) { + for _, event := range events { + for _, attribute := range event.Attributes { + if attribute.Key == "proposal_id" { + return strconv.ParseUint(attribute.Value, 10, 64) + } + } + } + + return 0, fmt.Errorf("proposalID event attribute not found") +} + // AssertEventsLegacy asserts that expected events are present in the actual events. // Expected map needs to be a subset of actual events to pass. func AssertEventsLegacy(