Skip to content

Commit 361ad04

Browse files
authored
Add ethersjs testcases (#193)
* Add ethersjs testcases * Update changelog
1 parent 41020fa commit 361ad04

File tree

955 files changed

+953
-374512
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

955 files changed

+953
-374512
lines changed

.github/workflows/pr.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Setup go
1515
uses: actions/setup-go@v1
1616
with:
17-
go-version: '1.15.10'
17+
go-version: '1.18.1'
1818
- name: "Setup"
1919
run: ./scripts/setup-ci.sh
2020
- name: Go test

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# 0.1.2 (Unreleased)
33

4+
- Add `testcases` for contract signature and transaction signing [[GH-193](https://github.com/umbracle/ethgo/issues/193)]
45
- Add `eth_feeHistory` rpc endpoint [[GH-192](https://github.com/umbracle/ethgo/issues/192)]
56
- Update `testserver` to `go-ethereum:v1.10.15` [[GH-191](https://github.com/umbracle/ethgo/issues/191)]
67
- Do not decode `to` in `Transaction` if not exists [[GH-190](https://github.com/umbracle/ethgo/issues/190)]

abi/type.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ func (t *Type) Format(includeArgs bool) string {
165165
name += " indexed"
166166
}
167167
if includeArgs {
168-
name += " " + i.Name
168+
if i.Name != "" {
169+
name += " " + i.Name
170+
}
169171
}
170172
rawAux = append(rawAux, name)
171173
}
@@ -328,11 +330,17 @@ func readType(l *lexer) (*Type, error) {
328330
var tt *Type
329331

330332
tok := l.nextToken()
333+
334+
isTuple := false
331335
if tok.typ == tupleToken {
332336
if l.nextToken().typ != lparenToken {
333337
return nil, expectedToken(lparenToken)
334338
}
335-
339+
isTuple = true
340+
} else if tok.typ == lparenToken {
341+
isTuple = true
342+
}
343+
if isTuple {
336344
var next token
337345
elems := []*TupleElem{}
338346
for {

abi/type_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,45 @@ func TestType(t *testing.T) {
264264
tuple: []*TupleElem{},
265265
},
266266
},
267+
{
268+
// hidden tuple token
269+
s: "tuple((int32))",
270+
a: &ArgumentStr{
271+
Type: "tuple",
272+
Components: []*ArgumentStr{
273+
{
274+
Type: "tuple",
275+
Components: []*ArgumentStr{
276+
{
277+
Type: "int32",
278+
},
279+
},
280+
},
281+
},
282+
},
283+
t: &Type{
284+
kind: KindTuple,
285+
t: tupleT,
286+
tuple: []*TupleElem{
287+
{
288+
Elem: &Type{
289+
kind: KindTuple,
290+
t: tupleT,
291+
tuple: []*TupleElem{
292+
{
293+
Elem: &Type{
294+
kind: KindInt,
295+
size: 32,
296+
t: int32T,
297+
},
298+
},
299+
},
300+
},
301+
},
302+
},
303+
},
304+
r: "tuple(tuple(int32))",
305+
},
267306
{
268307
s: "int[[",
269308
err: true,

encoding.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package ethgo
2+
3+
import (
4+
"encoding/hex"
5+
"math/big"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type ArgBig big.Int
11+
12+
func (a *ArgBig) UnmarshalText(input []byte) error {
13+
buf, err := decodeToHex(input)
14+
if err != nil {
15+
return err
16+
}
17+
b := new(big.Int)
18+
b.SetBytes(buf)
19+
*a = ArgBig(*b)
20+
return nil
21+
}
22+
23+
func (a ArgBig) MarshalText() ([]byte, error) {
24+
b := (*big.Int)(&a)
25+
return []byte("0x" + b.Text(16)), nil
26+
}
27+
28+
type ArgUint64 uint64
29+
30+
func (b ArgUint64) MarshalText() ([]byte, error) {
31+
buf := make([]byte, 2, 10)
32+
copy(buf, `0x`)
33+
buf = strconv.AppendUint(buf, uint64(b), 16)
34+
return buf, nil
35+
}
36+
37+
func (u *ArgUint64) UnmarshalText(input []byte) error {
38+
str := strings.TrimPrefix(string(input), "0x")
39+
if str == "" {
40+
str = "0"
41+
}
42+
num, err := strconv.ParseUint(str, 16, 64)
43+
if err != nil {
44+
return err
45+
}
46+
*u = ArgUint64(num)
47+
return nil
48+
}
49+
50+
func (u *ArgUint64) Uint64() uint64 {
51+
return uint64(*u)
52+
}
53+
54+
type ArgBytes []byte
55+
56+
func (b ArgBytes) MarshalText() ([]byte, error) {
57+
return encodeToHex(b), nil
58+
}
59+
60+
func (b *ArgBytes) UnmarshalText(input []byte) error {
61+
hh, err := decodeToHex(input)
62+
if err != nil {
63+
return nil
64+
}
65+
aux := make([]byte, len(hh))
66+
copy(aux[:], hh[:])
67+
*b = aux
68+
return nil
69+
}
70+
71+
func (b *ArgBytes) Bytes() []byte {
72+
return *b
73+
}
74+
75+
func decodeToHex(b []byte) ([]byte, error) {
76+
str := string(b)
77+
str = strings.TrimPrefix(str, "0x")
78+
if len(str)%2 != 0 {
79+
str = "0" + str
80+
}
81+
return hex.DecodeString(str)
82+
}
83+
84+
func encodeToHex(b []byte) []byte {
85+
str := hex.EncodeToString(b)
86+
if len(str)%2 != 0 {
87+
str = "0" + str
88+
}
89+
return []byte("0x" + str)
90+
}

go.mod

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11
module github.com/umbracle/ethgo
22

3-
go 1.12
3+
go 1.18
44

55
require (
6-
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
7-
github.com/Microsoft/go-winio v0.4.13 // indirect
8-
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
96
github.com/boltdb/bolt v1.3.1
107
github.com/btcsuite/btcd v0.21.0-beta
118
github.com/btcsuite/btcutil v1.0.2
9+
github.com/gorilla/websocket v1.4.1
10+
github.com/jmoiron/sqlx v1.2.0
11+
github.com/lib/pq v1.2.0
12+
github.com/mitchellh/mapstructure v1.1.2
13+
github.com/ory/dockertest v3.3.5+incompatible
14+
github.com/stretchr/testify v1.4.0
15+
github.com/tyler-smith/go-bip39 v1.1.0
16+
github.com/umbracle/fastrlp v0.0.0-20211229195328-c1416904ae17
17+
github.com/valyala/fasthttp v1.4.0
18+
github.com/valyala/fastjson v1.4.1
19+
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
20+
golang.org/x/text v0.3.2
21+
)
22+
23+
require (
24+
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
25+
github.com/Microsoft/go-winio v0.4.13 // indirect
26+
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
1227
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
1328
github.com/containerd/continuity v0.0.0-20191214063359-1097c8bae83b // indirect
29+
github.com/davecgh/go-spew v1.1.1 // indirect
1430
github.com/docker/go-connections v0.4.0 // indirect
1531
github.com/docker/go-units v0.4.0 // indirect
1632
github.com/go-sql-driver/mysql v1.4.1 // indirect
1733
github.com/google/go-cmp v0.3.1 // indirect
18-
github.com/gorilla/websocket v1.4.1
34+
github.com/google/gofuzz v1.2.0 // indirect
1935
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
20-
github.com/jmoiron/sqlx v1.2.0
2136
github.com/klauspost/compress v1.4.1 // indirect
2237
github.com/klauspost/cpuid v1.2.0 // indirect
38+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
2339
github.com/kr/pretty v0.1.0 // indirect
24-
github.com/lib/pq v1.2.0
25-
github.com/mitchellh/mapstructure v1.1.2
40+
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
2641
github.com/opencontainers/image-spec v1.0.1 // indirect
2742
github.com/opencontainers/runc v0.1.1 // indirect
28-
github.com/ory/dockertest v3.3.5+incompatible
43+
github.com/pkg/errors v0.8.1 // indirect
44+
github.com/pmezard/go-difflib v1.0.0 // indirect
2945
github.com/sirupsen/logrus v1.4.2 // indirect
30-
github.com/stretchr/testify v1.4.0
31-
github.com/tyler-smith/go-bip39 v1.1.0
32-
github.com/umbracle/fastrlp v0.0.0-20211229195328-c1416904ae17
33-
github.com/valyala/fasthttp v1.4.0
34-
github.com/valyala/fastjson v1.4.1
35-
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
46+
github.com/valyala/bytebufferpool v1.0.0 // indirect
3647
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect
37-
golang.org/x/text v0.3.2
48+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
3849
google.golang.org/appengine v1.6.5 // indirect
3950
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
51+
gopkg.in/yaml.v2 v2.2.2 // indirect
4052
gotest.tools v2.2.0+incompatible // indirect
4153
)

testcases/accounts_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package testcases
2+
3+
import (
4+
"encoding/hex"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/umbracle/ethgo"
10+
"github.com/umbracle/ethgo/wallet"
11+
)
12+
13+
func TestAccounts(t *testing.T) {
14+
var walletSpec []struct {
15+
Address string `json:"address"`
16+
Checksum string `json:"checksumAddress"`
17+
Name string `json:"name"`
18+
PrivateKey *string `json:"privateKey,omitempty"`
19+
}
20+
ReadTestCase(t, "accounts", &walletSpec)
21+
22+
for _, spec := range walletSpec {
23+
if spec.PrivateKey != nil {
24+
// test that we can decode the private key
25+
priv, err := hex.DecodeString(strings.TrimPrefix(*spec.PrivateKey, "0x"))
26+
assert.NoError(t, err)
27+
28+
key, err := wallet.NewWalletFromPrivKey(priv)
29+
assert.NoError(t, err)
30+
31+
assert.Equal(t, key.Address().String(), spec.Checksum)
32+
}
33+
34+
// test that an string address can be checksumed
35+
addr := ethgo.HexToAddress(spec.Address)
36+
assert.Equal(t, addr.String(), spec.Checksum)
37+
}
38+
}

testcases/contract_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package testcases
2+
3+
import (
4+
"encoding/hex"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/umbracle/ethgo"
9+
"github.com/umbracle/ethgo/abi"
10+
"github.com/umbracle/ethgo/testutil"
11+
)
12+
13+
func TestContract_Signatures(t *testing.T) {
14+
var signatures []struct {
15+
Name string `json:"name"`
16+
Signature string `json:"signature"`
17+
SigHash string `json:"sigHash"`
18+
Abi string `json:"abi"`
19+
}
20+
ReadTestCase(t, "contract-signatures", &signatures)
21+
22+
for _, c := range signatures {
23+
t.Run(c.Name, func(t *testing.T) {
24+
m, err := abi.NewMethod(c.Signature)
25+
assert.NoError(t, err)
26+
27+
sigHash := "0x" + hex.EncodeToString(m.ID())
28+
assert.Equal(t, sigHash, c.SigHash)
29+
})
30+
}
31+
}
32+
33+
func TestContract_Interface(t *testing.T) {
34+
t.Skip()
35+
36+
server := testutil.NewTestServer(t, nil)
37+
defer server.Close()
38+
39+
var calls []struct {
40+
Name string `json:"name"`
41+
Interface string `json:"interface"`
42+
Bytecode ethgo.ArgBytes `json:"bytecode"`
43+
Result ethgo.ArgBytes `json:"result"`
44+
Values string `json:"values"`
45+
}
46+
ReadTestCase(t, "contract-interface", &calls)
47+
48+
for _, c := range calls {
49+
t.Run(c.Name, func(t *testing.T) {
50+
a, err := abi.NewABI(c.Interface)
51+
assert.NoError(t, err)
52+
53+
method := a.GetMethod("test")
54+
55+
receipt, err := server.SendTxn(&ethgo.Transaction{
56+
Input: c.Bytecode.Bytes(),
57+
})
58+
assert.NoError(t, err)
59+
60+
outputRaw, err := server.Call(&ethgo.CallMsg{
61+
To: &receipt.ContractAddress,
62+
Data: method.ID(),
63+
})
64+
assert.NoError(t, err)
65+
66+
output, err := hex.DecodeString(outputRaw[2:])
67+
assert.NoError(t, err)
68+
69+
_, err = method.Decode(output)
70+
assert.NoError(t, err)
71+
})
72+
}
73+
74+
}

0 commit comments

Comments
 (0)