Skip to content

Commit

Permalink
Use stringer output
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-vball committed Jul 8, 2024
1 parent 25bca0f commit 752b6e3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 51 deletions.
111 changes: 102 additions & 9 deletions abi-bindings/go/Teleporter/TeleporterMessenger/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package teleportermessenger

import (
"encoding/json"
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -81,27 +82,27 @@ func ToEvent(e string) (Event, error) {
}

// FilterTeleporterEvents parses the topics and data of a Teleporter log into the corresponding Teleporter event
func FilterTeleporterEvents(topics []common.Hash, data []byte, event string) (interface{}, error) {
func FilterTeleporterEvents(topics []common.Hash, data []byte, event string) (fmt.Stringer, error) {
e, err := ToEvent(event)
if err != nil {
return nil, err
}
var out interface{}
var out fmt.Stringer
switch e {
case SendCrossChainMessage:
out = new(ReadableTeleporterMessengerSendCrossChainMessage)
out = new(TeleporterMessengerSendCrossChainMessage)
case ReceiveCrossChainMessage:
out = new(ReadableTeleporterMessengerReceiveCrossChainMessage)
out = new(TeleporterMessengerReceiveCrossChainMessage)
case AddFeeAmount:
out = new(ReadableTeleporterMessengerAddFeeAmount)
out = new(TeleporterMessengerAddFeeAmount)
case MessageExecutionFailed:
out = new(ReadableTeleporterMessengerMessageExecutionFailed)
out = new(TeleporterMessengerMessageExecutionFailed)
case MessageExecuted:
out = new(ReadableTeleporterMessengerMessageExecuted)
out = new(TeleporterMessengerMessageExecuted)
case RelayerRewardsRedeemed:
out = new(TeleporterMessengerRelayerRewardsRedeemed)
case ReceiptReceived:
out = new(ReadableTeleporterMessengerReceiptReceived)
out = new(TeleporterMessengerReceiptReceived)
default:
return nil, fmt.Errorf("unknown event %s", e.String())
}
Expand All @@ -111,6 +112,18 @@ func FilterTeleporterEvents(topics []common.Hash, data []byte, event string) (in
return out, nil
}

func (t TeleporterMessengerSendCrossChainMessage) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerSendCrossChainMessage{
MessageID: common.Hash(t.MessageID),
DestinationBlockchainID: ids.ID(t.DestinationBlockchainID),
Message: toReadableTeleporterMessage(t.Message),
FeeInfo: t.FeeInfo,
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerSendCrossChainMessage struct {
MessageID common.Hash
DestinationBlockchainID ids.ID
Expand All @@ -119,6 +132,19 @@ type ReadableTeleporterMessengerSendCrossChainMessage struct {
Raw types.Log
}

func (t TeleporterMessengerReceiveCrossChainMessage) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerReceiveCrossChainMessage{
MessageID: common.Hash(t.MessageID),
SourceBlockchainID: ids.ID(t.SourceBlockchainID),
Deliverer: t.Deliverer,
RewardRedeemer: t.RewardRedeemer,
Message: toReadableTeleporterMessage(t.Message),
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerReceiveCrossChainMessage struct {
MessageID common.Hash
SourceBlockchainID ids.ID
Expand All @@ -128,25 +154,74 @@ type ReadableTeleporterMessengerReceiveCrossChainMessage struct {
Raw types.Log
}

func (t TeleporterMessengerAddFeeAmount) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerAddFeeAmount{
MessageID: common.Hash(t.MessageID),
UpdatedFeeInfo: t.UpdatedFeeInfo,
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerAddFeeAmount struct {
MessageID common.Hash
UpdatedFeeInfo TeleporterFeeInfo
Raw types.Log
}

func (t TeleporterMessengerMessageExecutionFailed) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerMessageExecutionFailed{
MessageID: common.Hash(t.MessageID),
SourceBlockchainID: ids.ID(t.SourceBlockchainID),
Message: toReadableTeleporterMessage(t.Message),
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerMessageExecutionFailed struct {
MessageID common.Hash
SourceBlockchainID ids.ID
Message ReadableTeleporterMessage
Raw types.Log
}

func (t TeleporterMessengerMessageExecuted) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerMessageExecuted{
MessageID: common.Hash(t.MessageID),
SourceBlockchainID: ids.ID(t.SourceBlockchainID),
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerMessageExecuted struct {
MessageID common.Hash
SourceBlockchainID ids.ID
Raw types.Log
}

func (t TeleporterMessengerRelayerRewardsRedeemed) String() string {
outJson, _ := json.MarshalIndent(t, "", " ")

return string(outJson)
}

func (t TeleporterMessengerReceiptReceived) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerReceiptReceived{
MessageID: common.Hash(t.MessageID),
DestinationBlockchainID: ids.ID(t.DestinationBlockchainID),
RelayerRewardAddress: t.RelayerRewardAddress,
FeeInfo: t.FeeInfo,
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerReceiptReceived struct {
MessageID common.Hash
DestinationBlockchainID ids.ID
Expand All @@ -155,7 +230,25 @@ type ReadableTeleporterMessengerReceiptReceived struct {
Raw types.Log
}

// TeleporterMessage is an auto generated low-level Go binding around an user-defined struct.
func toReadableTeleporterMessage(t TeleporterMessage) ReadableTeleporterMessage {
return ReadableTeleporterMessage{
MessageNonce: t.MessageNonce,
OriginSenderAddress: t.OriginSenderAddress,
DestinationBlockchainID: ids.ID(t.DestinationBlockchainID),
DestinationAddress: t.DestinationAddress,
RequiredGasLimit: t.RequiredGasLimit,
AllowedRelayerAddresses: t.AllowedRelayerAddresses,
Receipts: t.Receipts,
Message: t.Message,
}
}

func (t TeleporterMessage) String() string {
outJson, _ := json.MarshalIndent(toReadableTeleporterMessage(t), "", " ")

return string(outJson)
}

type ReadableTeleporterMessage struct {
MessageNonce *big.Int
OriginSenderAddress common.Address
Expand Down
10 changes: 5 additions & 5 deletions abi-bindings/go/Teleporter/TeleporterMessenger/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func TestToEvent(t *testing.T) {
func TestFilterTeleporterEvents(t *testing.T) {
mockBlockchainID := ids.ID{1, 2, 3, 4}
mockMessageNonce := big.NewInt(8)
mockMessageID := common.Hash{9, 10, 11, 12}
message := createTestReadableTeleporterMessage(mockMessageNonce)
mockMessageID := ids.ID{9, 10, 11, 12}
message := createTestTeleporterMessage(mockMessageNonce)
feeInfo := TeleporterFeeInfo{
FeeTokenAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
Amount: big.NewInt(1),
Expand All @@ -93,7 +93,7 @@ func TestFilterTeleporterEvents(t *testing.T) {
message,
feeInfo,
},
expected: &ReadableTeleporterMessengerSendCrossChainMessage{
expected: &TeleporterMessengerSendCrossChainMessage{
MessageID: mockMessageID,
DestinationBlockchainID: mockBlockchainID,
Message: message,
Expand All @@ -109,7 +109,7 @@ func TestFilterTeleporterEvents(t *testing.T) {
deliverer,
message,
},
expected: &ReadableTeleporterMessengerReceiveCrossChainMessage{
expected: &TeleporterMessengerReceiveCrossChainMessage{
MessageID: mockMessageID,
SourceBlockchainID: mockBlockchainID,
Deliverer: deliverer,
Expand All @@ -123,7 +123,7 @@ func TestFilterTeleporterEvents(t *testing.T) {
mockMessageID,
mockBlockchainID,
},
expected: &ReadableTeleporterMessengerMessageExecuted{
expected: &TeleporterMessengerMessageExecuted{
MessageID: mockMessageID,
SourceBlockchainID: mockBlockchainID,
},
Expand Down
35 changes: 7 additions & 28 deletions abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,6 @@ func createTestTeleporterMessage(messageNonce *big.Int) TeleporterMessage {
return m
}

func createTestReadableTeleporterMessage(messageNonce *big.Int) ReadableTeleporterMessage {
m := ReadableTeleporterMessage{
MessageNonce: messageNonce,
OriginSenderAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
DestinationBlockchainID: ids.ID{1, 2, 3, 4},
DestinationAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
RequiredGasLimit: big.NewInt(2),
AllowedRelayerAddresses: []common.Address{
common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
},
Receipts: []TeleporterMessageReceipt{
{
ReceivedMessageNonce: big.NewInt(1),
RelayerRewardAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
},
},
Message: []byte{1, 2, 3, 4},
}
return m
}

func TestPackUnpackTeleporterMessage(t *testing.T) {
message := createTestTeleporterMessage(big.NewInt(4))

Expand Down Expand Up @@ -86,7 +65,7 @@ func TestUnpackEvent(t *testing.T) {
mockBlockchainID := ids.ID{1, 2, 3, 4}
mockMessageNonce := big.NewInt(5)
mockMessageID := common.Hash{9, 10, 11, 12}
message := createTestReadableTeleporterMessage(mockMessageNonce)
message := createTestTeleporterMessage(mockMessageNonce)
feeInfo := TeleporterFeeInfo{
FeeTokenAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
Amount: big.NewInt(1),
Expand All @@ -111,8 +90,8 @@ func TestUnpackEvent(t *testing.T) {
message,
feeInfo,
},
out: new(ReadableTeleporterMessengerSendCrossChainMessage),
expected: &ReadableTeleporterMessengerSendCrossChainMessage{
out: new(TeleporterMessengerSendCrossChainMessage),
expected: &TeleporterMessengerSendCrossChainMessage{
DestinationBlockchainID: mockBlockchainID,
MessageID: mockMessageID,
Message: message,
Expand All @@ -128,8 +107,8 @@ func TestUnpackEvent(t *testing.T) {
deliverer,
message,
},
out: new(ReadableTeleporterMessengerReceiveCrossChainMessage),
expected: &ReadableTeleporterMessengerReceiveCrossChainMessage{
out: new(TeleporterMessengerReceiveCrossChainMessage),
expected: &TeleporterMessengerReceiveCrossChainMessage{
SourceBlockchainID: mockBlockchainID,
MessageID: mockMessageID,
Deliverer: deliverer,
Expand All @@ -143,8 +122,8 @@ func TestUnpackEvent(t *testing.T) {
mockMessageID,
mockBlockchainID,
},
out: new(ReadableTeleporterMessengerMessageExecuted),
expected: &ReadableTeleporterMessengerMessageExecuted{
out: new(TeleporterMessengerMessageExecuted),
expected: &TeleporterMessengerMessageExecuted{
MessageID: mockMessageID,
SourceBlockchainID: mockBlockchainID,
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/teleporter-cli/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func eventRun(cmd *cobra.Command, args []string) {

out, err := teleportermessenger.FilterTeleporterEvents(topics, data, event.Name)
cobra.CheckErr(err)
logger.Info("Parsed Teleporter event", zap.String("name", event.Name), zap.Any("event", out))
logger.Info("Parsed Teleporter event", zap.String("name", event.Name), zap.String("event", out.String()))
cmd.Println("Event command ran successfully for", event.Name)
}

Expand Down
10 changes: 2 additions & 8 deletions cmd/teleporter-cli/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ func printTeleporterLogs(cmd *cobra.Command, log *types.Log) {
out, err := teleportermessenger.FilterTeleporterEvents(log.Topics, log.Data, event.Name)
cobra.CheckErr(err)

outJson, err := json.MarshalIndent(out, "", " ")
cobra.CheckErr(err)

cmd.Println(event.Name + " Log:")
cmd.Println(string(outJson) + "\n")
cmd.Println(out.String() + "\n")
}

func printWarpLogs(cmd *cobra.Command, log *types.Log) {
Expand All @@ -102,11 +99,8 @@ func printWarpLogs(cmd *cobra.Command, log *types.Log) {
teleporterMessage, err := teleportermessenger.UnpackTeleporterMessage(warpPayload.Payload)
cobra.CheckErr(err)

teleporterMessageJson, err := json.MarshalIndent(teleporterMessage, "", " ")
cobra.CheckErr(err)

cmd.Println("Teleporter Message:")
cmd.Println(string(teleporterMessageJson))
cmd.Println(teleporterMessage.String())
}

func traceTransaction(cmd *cobra.Command, txHash common.Hash) {
Expand Down

0 comments on commit 752b6e3

Please sign in to comment.