Skip to content

Commit

Permalink
improve logging around AddABISignature
Browse files Browse the repository at this point in the history
  • Loading branch information
xiam committed Feb 11, 2025
1 parent 3e86bba commit 5bd7497
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
27 changes: 14 additions & 13 deletions ethcoder/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ func (d *ABI) AddMethod(methodSig string) (string, error) {
if err != nil {
return "", nil
}

return d.AddABISignature(abiSig, false)
}

func (d *ABI) AddABISignature(abiSig ABISignature, isEvent bool) (string, error) {
contractABI, name, err := abiSig.ToABI(isEvent)
if err != nil {
return name, err
return name, fmt.Errorf("to abi: %w", err)
}

if isEvent {
Expand Down Expand Up @@ -141,7 +142,7 @@ func (d *ABI) AddABIBySigOrJSON(abiSigOrJSON string, isEvent bool) ([]string, er
if strings.HasPrefix(abiSigOrJSON, "[") || strings.HasPrefix(abiSigOrJSON, "{") {
err := d.AddABIFromJSON(abiSigOrJSON)
if err != nil {
return nil, err
return nil, fmt.Errorf("add abi from JSON: %w", err)
}
names := []string{}
if isEvent {
Expand All @@ -158,13 +159,13 @@ func (d *ABI) AddABIBySigOrJSON(abiSigOrJSON string, isEvent bool) ([]string, er
if isEvent {
event, err := d.AddEvent(abiSigOrJSON)
if err != nil {
return nil, err
return nil, fmt.Errorf("add event: %w", err)
}
return []string{event}, nil
} else {
method, err := d.AddMethod(abiSigOrJSON)
if err != nil {
return nil, err
return nil, fmt.Errorf("add method: %w", err)
}
return []string{method}, nil
}
Expand Down Expand Up @@ -241,47 +242,47 @@ type ContractCallDef struct {

// EncodeContractCall encodes a contract call as a hex encoded calldata.
func EncodeContractCall(callDef ContractCallDef) (string, error) {
abi := NewABI()
abiobj := NewABI()

names, err := abi.AddABIBySigOrJSON(callDef.ABI, false)
names, err := abiobj.AddABIBySigOrJSON(callDef.ABI, false)
if err != nil {
return "", err
return "", fmt.Errorf("add abi: %w", err)
}
if len(names) == 0 {
return "", fmt.Errorf("no method names added")
}
methodName := names[0]

abiSig, ok := abi.GetMethodABISignature(methodName)
abiSig, ok := abiobj.GetMethodABISignature(methodName)
if !ok {
return "", fmt.Errorf("method %s not found", methodName)
}

// Prepare the arguments, which may be nested
argStringValues, err := prepareContractCallArgs(callDef.Args)
if err != nil {
return "", err
return "", fmt.Errorf("prepare contract call args: %w", err)
}

// Decode argument string values into runtime types based on the abi argument types.
argValues, err := ABIUnmarshalStringValuesAny(abiSig.ArgTypes, argStringValues)
if err != nil {
return "", err
return "", fmt.Errorf("decode argument string values: %w", err)
}

rawABI := abi.RawABI()
rawABI := abiobj.RawABI()

// Pre-process all argValues to be in the format that the geth abi encoder expects.
// argValues are runtime types.
args, err := packableArgValues(rawABI, methodName, argValues)
if err != nil {
return "", err
return "", fmt.Errorf("pre-process arg values: %w", err)
}

// Encode abi method arguments into calldata bytes
packed, err := rawABI.Pack(methodName, args...)
if err != nil {
return "", err
return "", fmt.Errorf("encode method arguments: %w", err)
}

// Return as hex encoded string, with 0x prefix
Expand Down
2 changes: 1 addition & 1 deletion ethcoder/abi_sig.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s ABISignature) ToABI(isEvent bool) (abi.ABI, string, error) {
abiArgs := abi.Arguments{}
selector, err := abi.ParseSelector(s.Signature)
if err != nil {
return abi.ABI{}, "", err
return abi.ABI{}, "", fmt.Errorf("parse selector error: %w", err)
}

for i, argType := range s.ArgTypes {
Expand Down
9 changes: 5 additions & 4 deletions ethcoder/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,11 @@ func TestEncodeContractCall(t *testing.T) {

func TestEncodeContractCall2(t *testing.T) {
jsonContractCall := `{
"abi": "testTuple((address,string),(string,address,bytes,uint),int)",
"abi": "testTuple((address,string),(string,address,bytes,uint256),int256)",
"args": [
["0x8f408550720b268b0ea0969c527ac997d969a638", "firstWord"],
["secondWord", "0x38104f7bb130756dcdd24d804e3e2d2e9df25d7d", "U29tZXRoaW5n", "3"],
"7"
["secondWord", "0x38104f7bb130756dcdd24d804e3e2d2e9df25d7d", "0x0", "1"],
"1"
]
}`

Expand All @@ -655,5 +655,6 @@ func TestEncodeContractCall2(t *testing.T) {

res, err := EncodeContractCall(contractCall)
require.Nil(t, err)
require.Equal(t, "0xxx", res)

require.Equal(t, "0xeeaffbb7000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000010000000000000000000000008f408550720b268b0ea0969c527ac997d969a638000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000096669727374576f72640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000038104f7bb130756dcdd24d804e3e2d2e9df25d7d00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a7365636f6e64576f7264000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", res)
}

0 comments on commit 5bd7497

Please sign in to comment.