/
fmt.go
36 lines (31 loc) · 943 Bytes
/
fmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package util
import (
"fmt"
"reflect"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)
func StringifyArg(arg interface{}) string {
val := reflect.ValueOf(arg)
typ := val.Type()
// If the argument is a slice, iterate over its elements and stringify each one
if typ.Kind() == reflect.Slice {
elemCount := val.Len()
elemStrings := make([]string, elemCount)
for i := 0; i < elemCount; i++ {
elem := val.Index(i).Interface()
elemStrings[i] = StringifyArg(elem)
}
return fmt.Sprintf("[%s]", strings.Join(elemStrings, ", "))
}
// Handle other types
switch arg.(type) {
case *bind.TransactOpts:
return fmt.Sprintf("TransactOpts{From: %s, Nonce: %s}", val.FieldByName("From").Interface(), val.FieldByName("Nonce").Interface())
case common.Address:
return fmt.Sprintf("Address: %s", arg.(common.Address).Hex())
default:
return fmt.Sprintf("%v", arg)
}
}