github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/testing/events.go (about) 1 package ibctesting 2 3 import ( 4 "fmt" 5 "strconv" 6 7 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 8 clienttypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/types" 9 connectiontypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/03-connection/types" 10 channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types" 11 ) 12 13 // ParseClientIDFromEvents parses events emitted from a MsgCreateClient and returns the 14 // client identifier. 15 func ParseClientIDFromEvents(events sdk.Events) (string, error) { 16 for _, ev := range events { 17 if ev.Type == clienttypes.EventTypeCreateClient { 18 for _, attr := range ev.Attributes { 19 if string(attr.Key) == clienttypes.AttributeKeyClientID { 20 return string(attr.Value), nil 21 } 22 } 23 } 24 } 25 return "", fmt.Errorf("client identifier event attribute not found") 26 } 27 28 // ParseConnectionIDFromEvents parses events emitted from a MsgConnectionOpenInit or 29 // MsgConnectionOpenTry and returns the connection identifier. 30 func ParseConnectionIDFromEvents(events sdk.Events) (string, error) { 31 for _, ev := range events { 32 if ev.Type == connectiontypes.EventTypeConnectionOpenInit || 33 ev.Type == connectiontypes.EventTypeConnectionOpenTry { 34 for _, attr := range ev.Attributes { 35 if string(attr.Key) == connectiontypes.AttributeKeyConnectionID { 36 return string(attr.Value), nil 37 } 38 } 39 } 40 } 41 return "", fmt.Errorf("connection identifier event attribute not found") 42 } 43 44 // ParseChannelIDFromEvents parses events emitted from a MsgChannelOpenInit or 45 // MsgChannelOpenTry and returns the channel identifier. 46 func ParseChannelIDFromEvents(events sdk.Events) (string, error) { 47 for _, ev := range events { 48 if ev.Type == channeltypes.EventTypeChannelOpenInit || ev.Type == channeltypes.EventTypeChannelOpenTry { 49 for _, attr := range ev.Attributes { 50 if string(attr.Key) == channeltypes.AttributeKeyChannelID { 51 return string(attr.Value), nil 52 } 53 } 54 } 55 } 56 return "", fmt.Errorf("channel identifier event attribute not found") 57 } 58 59 // ParseAckFromEvents parses events emitted from a MsgRecvPacket and returns the 60 // acknowledgement. 61 func ParseAckFromEvents(events sdk.Events) ([]byte, error) { 62 for _, ev := range events { 63 if ev.Type == channeltypes.EventTypeWriteAck { 64 for _, attr := range ev.Attributes { 65 if string(attr.Key) == channeltypes.AttributeKeyAck { 66 return attr.Value, nil 67 } 68 } 69 } 70 } 71 return nil, fmt.Errorf("acknowledgement event attribute not found") 72 } 73 74 func ParsePacketFromEvents(events sdk.Events) (channeltypes.Packet, error) { 75 for _, ev := range events { 76 if ev.Type == channeltypes.EventTypeSendPacket { 77 packet := channeltypes.Packet{} 78 for _, attr := range ev.Attributes { 79 switch string(attr.Key) { 80 case channeltypes.AttributeKeyData: 81 packet.Data = attr.Value 82 83 case channeltypes.AttributeKeySequence: 84 seq, err := strconv.ParseUint(string(attr.Value), 10, 64) 85 if err != nil { 86 return channeltypes.Packet{}, err 87 } 88 89 packet.Sequence = seq 90 91 case channeltypes.AttributeKeySrcPort: 92 packet.SourcePort = string(attr.Value) 93 94 case channeltypes.AttributeKeySrcChannel: 95 packet.SourceChannel = string(attr.Value) 96 97 case channeltypes.AttributeKeyDstPort: 98 packet.DestinationPort = string(attr.Value) 99 100 case channeltypes.AttributeKeyDstChannel: 101 packet.DestinationChannel = string(attr.Value) 102 103 case channeltypes.AttributeKeyTimeoutHeight: 104 height, err := clienttypes.ParseHeight(string(attr.Value)) 105 if err != nil { 106 return channeltypes.Packet{}, err 107 } 108 109 packet.TimeoutHeight = height 110 111 case channeltypes.AttributeKeyTimeoutTimestamp: 112 timestamp, err := strconv.ParseUint(string(attr.Value), 10, 64) 113 if err != nil { 114 return channeltypes.Packet{}, err 115 } 116 117 packet.TimeoutTimestamp = timestamp 118 119 default: 120 continue 121 } 122 } 123 124 return packet, nil 125 } 126 } 127 return channeltypes.Packet{}, fmt.Errorf("acknowledgement event attribute not found") 128 }