Skip to content

Commit

Permalink
refactor: refactored 'ExtractValueFromEvents'
Browse files Browse the repository at this point in the history
  • Loading branch information
srdtrk committed Mar 25, 2024
1 parent 4878ea7 commit 8ab8f77
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
37 changes: 12 additions & 25 deletions e2e/tests/interchain_accounts/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,21 @@ func (s *InterchainAccountsQueryTestSuite) TestInterchainAccountsQuery() {
s.Require().NoError(err)
s.Require().Len(txSearchRes.Txs, 1)

// get acknowledgement
ackFound := false
ack := &channeltypes.Acknowledgement_Result{}

search_ack:
for _, event := range txSearchRes.Txs[0].Events {
if event.Type != channeltypes.EventTypeWriteAck {
continue
}

for _, attr := range event.Attributes {
if attr.Key != channeltypes.AttributeKeyAckHex {
continue
}

ackBz, err := hex.DecodeString(attr.Value)
s.Require().NoError(err)
ackHexValue, isFound := s.ExtractValueFromEvents(
txSearchRes.Txs[0].Events,
channeltypes.EventTypeWriteAck,
channeltypes.AttributeKeyAckHex,
)

err = json.Unmarshal(ackBz, ack)
s.Require().NoError(err)
s.Require().True(isFound)
s.Require().NotEmpty(ackHexValue)

ackFound = true
ack := &channeltypes.Acknowledgement_Result{}
ackBz, err := hex.DecodeString(ackHexValue)
s.Require().NoError(err)

break search_ack
}
}
s.Require().True(ackFound)
s.Require().NotZero(ack)
err = json.Unmarshal(ackBz, ack)
s.Require().NoError(err)

// unmarshal the ica response
icaAck := &sdk.TxMsgData{}
Expand Down
23 changes: 23 additions & 0 deletions e2e/testsuite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/ibc-go/e2e/testsuite/sanitize"
"github.com/cosmos/ibc-go/e2e/testvalues"
feetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
Expand Down Expand Up @@ -331,3 +333,24 @@ func (*E2ETestSuite) QueryTxsByEvents(

return result, nil
}

// ExtractValueFromEvents extracts the value of an attribute from a list of events.
// If the attribute is not found, the function returns an empty string and false.
// If the attribute is found, the function returns the value and true.
func (*E2ETestSuite) ExtractValueFromEvents(events []abci.Event, eventType, attrKey string) (string, bool) {
for _, event := range events {
if event.Type != eventType {
continue
}

for _, attr := range event.Attributes {
if attr.Key != attrKey {
continue
}

return attr.Value, true
}
}

return "", false
}

0 comments on commit 8ab8f77

Please sign in to comment.