Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package cosmosclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
ctypes "github.com/cometbft/cometbft/rpc/core/types"
|
|
|
|
"github.com/ignite/cli/v29/ignite/pkg/errors"
|
|
)
|
|
|
|
// TX defines a block transaction.
|
|
type TX struct {
|
|
// BlockTime returns the time of the block that contains the transaction.
|
|
BlockTime time.Time
|
|
|
|
// Raw contains the transaction as returned by the Tendermint API.
|
|
Raw *ctypes.ResultTx
|
|
}
|
|
|
|
// GetEvents returns the transaction events.
|
|
func (t TX) GetEvents() (events []TXEvent, err error) {
|
|
for _, e := range t.Raw.TxResult.Events {
|
|
evt := TXEvent{Type: e.Type}
|
|
|
|
for _, a := range e.Attributes {
|
|
// Make sure that the attribute value is a valid JSON encoded string.
|
|
// Tendermint event attribute values contain JSON encoded values without quotes
|
|
// so string values need to be encoded to be quoted and saved as valid JSONB.
|
|
v, err := formatAttributeValue([]byte(a.Value))
|
|
if err != nil {
|
|
return nil, errors.Errorf("error encoding event attr '%s.%s': %w", e.Type, a.Key, err)
|
|
}
|
|
|
|
evt.Attributes = append(evt.Attributes, TXEventAttribute{
|
|
Key: a.Key,
|
|
Value: v,
|
|
})
|
|
}
|
|
|
|
events = append(events, evt)
|
|
}
|
|
|
|
return events, nil
|
|
}
|
|
|
|
// TXEvent defines a transaction event.
|
|
type TXEvent struct {
|
|
Type string `json:"type"`
|
|
Attributes []TXEventAttribute `json:"attributes"`
|
|
}
|
|
|
|
// TXEventAttribute defines a transaction event attribute.
|
|
type TXEventAttribute struct {
|
|
Key string `json:"key"`
|
|
Value []byte `json:"value"`
|
|
}
|
|
|
|
func formatAttributeValue(v []byte) ([]byte, error) {
|
|
if json.Valid(v) {
|
|
return v, nil
|
|
}
|
|
|
|
// Encode all string or invalid values
|
|
return json.Marshal(string(v))
|
|
}
|