Skip to content

Commit 8c1f915

Browse files
authored
chore(bind): return structs instead of interfaces (#1167)
1 parent 431e370 commit 8c1f915

13 files changed

Lines changed: 101 additions & 185 deletions

File tree

thorclient/bind/bind_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
type testEnv struct {
2929
t *testing.T
3030
client *thorclient.Client
31-
bindContract Contract
31+
bindContract *Contract
3232
owner *PrivateKeySigner
3333
user *PrivateKeySigner
3434
testNode testnode.Node

thorclient/bind/call.go

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,33 @@ import (
99
"errors"
1010
"fmt"
1111

12-
"github.com/vechain/thor/v2/api"
13-
1412
"github.com/ethereum/go-ethereum/common/hexutil"
1513
"github.com/ethereum/go-ethereum/common/math"
14+
"github.com/vechain/thor/v2/api"
1615
"github.com/vechain/thor/v2/thor"
1716
"github.com/vechain/thor/v2/thorclient"
1817
)
1918

20-
// CallBuilder is the interface for read operations.
21-
type CallBuilder interface {
22-
// AtRevision sets the revision for the call.
23-
AtRevision(rev string) CallBuilder
24-
25-
// Caller sets the caller for the simulation.
26-
Caller(caller *thor.Address) CallBuilder
27-
28-
// ExecuteInto unpacks the result into the provided interface.
29-
ExecuteInto(result any) error
30-
31-
// Execute performs the call and returns the raw result.
32-
Execute() (*api.CallResult, error)
33-
}
34-
35-
// callBuilder is the concrete implementation of CallBuilder.
36-
type callBuilder struct {
37-
op *methodBuilder
19+
type CallBuilder struct {
20+
op *MethodBuilder
3821
rev string
3922
caller *thor.Address
4023
}
4124

4225
// AtRevision implements CallBuilder.AtRevision.
43-
func (b *callBuilder) AtRevision(rev string) CallBuilder {
26+
func (b *CallBuilder) AtRevision(rev string) *CallBuilder {
4427
b.rev = rev
4528
return b
4629
}
4730

4831
// Caller implements CallBuilder.AtRevision.
49-
func (b *callBuilder) Caller(caller *thor.Address) CallBuilder {
32+
func (b *CallBuilder) Caller(caller *thor.Address) *CallBuilder {
5033
b.caller = caller
5134
return b
5235
}
5336

5437
// ExecuteInto implements CallBuilder.Into.
55-
func (b *callBuilder) ExecuteInto(result any) error {
38+
func (b *CallBuilder) ExecuteInto(result any) error {
5639
method, ok := b.op.contract.abi.Methods[b.op.method]
5740
if !ok {
5841
return errors.New("method not found: " + b.op.method)
@@ -72,7 +55,7 @@ func (b *callBuilder) ExecuteInto(result any) error {
7255
}
7356

7457
// Execute implements CallBuilder.Execute.
75-
func (b *callBuilder) Execute() (*api.CallResult, error) {
58+
func (b *CallBuilder) Execute() (*api.CallResult, error) {
7659
// Build the clause
7760
clause, err := b.op.Clause()
7861
if err != nil {
@@ -100,24 +83,25 @@ func (b *callBuilder) Execute() (*api.CallResult, error) {
10083
return nil, fmt.Errorf("expected 1 result, got %d", len(res))
10184
}
10285

103-
if res[0].Reverted {
86+
result := res[0]
87+
if result.Reverted {
10488
message := "contract call reverted"
105-
if res[0].Data != "" {
106-
decoded, err := hexutil.Decode(res[0].Data)
89+
if result.Data != "" {
90+
decoded, err := hexutil.Decode(result.Data)
10791
if err != nil {
108-
return nil, fmt.Errorf("failed to decode revert data: %w", err)
92+
return result, fmt.Errorf("failed to decode revert data: %w", err)
10993
}
11094
revertReason, err := UnpackRevert(decoded)
11195
if err == nil {
11296
message = fmt.Sprintf("contract call reverted: %s", revertReason)
11397
}
11498
}
115-
return nil, errors.New(message)
99+
return result, errors.New(message)
116100
}
117101

118-
if res[0].VMError != "" {
119-
return nil, fmt.Errorf("VM error: %s", res[0].VMError)
102+
if result.VMError != "" {
103+
return nil, fmt.Errorf("VM error: %s", result.VMError)
120104
}
121105

122-
return res[0], nil
106+
return result, nil
123107
}

thorclient/bind/contract.go

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,30 @@ import (
2121
"github.com/vechain/thor/v2/tx"
2222
)
2323

24-
// Contract is the main interface for contract interactions.
25-
// It provides a unified entry point for all contract operations.
26-
type Contract interface {
27-
// Method creates a new method builder for the specified method and arguments.
28-
Method(method string, args ...any) MethodBuilder
29-
30-
// FilterEvent creates a new filter builder for the specified event.
31-
FilterEvent(eventName string) FilterBuilder
32-
33-
// Address returns the contract address.
34-
Address() *thor.Address
35-
36-
// ABI returns the contract ABI.
37-
ABI() *abi.ABI
38-
39-
// Client returns the underlying client.
40-
Client() *thorclient.Client
41-
}
42-
43-
// contract is the concrete implementation of Contract.
44-
type contract struct {
24+
type Contract struct {
4525
client *thorclient.Client
4626
abi *abi.ABI
4727
addr *thor.Address
4828
}
4929

5030
// NewContract creates a new contract instance with the given client, ABI data and address.
51-
func NewContract(client *thorclient.Client, abiData []byte, address *thor.Address) (Contract, error) {
31+
func NewContract(client *thorclient.Client, abiData []byte, address *thor.Address) (*Contract, error) {
5232
if address == nil {
5333
return nil, fmt.Errorf("empty contract address")
5434
}
5535
contractABI, err := abi.JSON(bytes.NewReader(abiData))
5636
if err != nil {
5737
return nil, err
5838
}
59-
return &contract{
39+
return &Contract{
6040
client: client,
6141
abi: &contractABI,
6242
addr: address,
6343
}, nil
6444
}
6545

6646
// DeployContract deploys a contract and creates a new contract instance with the given client, ABI data and address.
67-
func DeployContract(client *thorclient.Client, signer Signer, abiData []byte, bytecode string) (Contract, error) {
47+
func DeployContract(client *thorclient.Client, signer Signer, abiData []byte, bytecode string) (*Contract, error) {
6848
bc, err := hexutil.Decode(bytecode)
6949
if err != nil {
7050
return nil, err
@@ -120,8 +100,8 @@ func DeployContract(client *thorclient.Client, signer Signer, abiData []byte, by
120100
}
121101

122102
// Method implements Contract.Method.
123-
func (c *contract) Method(method string, args ...any) MethodBuilder {
124-
return &methodBuilder{
103+
func (c *Contract) Method(method string, args ...any) *MethodBuilder {
104+
return &MethodBuilder{
125105
contract: c,
126106
method: method,
127107
args: args,
@@ -130,26 +110,26 @@ func (c *contract) Method(method string, args ...any) MethodBuilder {
130110
}
131111

132112
// FilterEvent implements Contract.Event.
133-
func (c *contract) FilterEvent(eventName string) FilterBuilder {
134-
return &filterBuilder{
135-
op: &methodBuilder{
113+
func (c *Contract) FilterEvent(eventName string) *FilterBuilder {
114+
return &FilterBuilder{
115+
op: &MethodBuilder{
136116
contract: c,
137117
method: eventName,
138118
},
139119
}
140120
}
141121

142122
// Address returns the contract address.
143-
func (c *contract) Address() *thor.Address {
123+
func (c *Contract) Address() *thor.Address {
144124
return c.addr
145125
}
146126

147127
// ABI returns the contract ABI.
148-
func (c *contract) ABI() *abi.ABI {
128+
func (c *Contract) ABI() *abi.ABI {
149129
return c.abi
150130
}
151131

152132
// Client returns the underlying HTTP client.
153-
func (c *contract) Client() *thorclient.Client {
133+
func (c *Contract) Client() *thorclient.Client {
154134
return c.client
155135
}

thorclient/bind/filter.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,38 @@ import (
99
"errors"
1010

1111
"github.com/vechain/thor/v2/api"
12-
1312
"github.com/vechain/thor/v2/logdb"
1413
"github.com/vechain/thor/v2/thor"
1514
)
1615

17-
// FilterBuilder is the interface for event filtering.
18-
type FilterBuilder interface {
19-
// InRange sets the range for event filtering.
20-
InRange(r *api.Range) FilterBuilder
21-
22-
// WithOptions sets the filter options.
23-
WithOptions(opts *api.Options) FilterBuilder
24-
25-
// OrderBy sets the order for event filtering.
26-
OrderBy(order logdb.Order) FilterBuilder
27-
28-
// Execute performs the event filtering.
29-
Execute() ([]api.FilteredEvent, error)
30-
}
31-
32-
// filterBuilder is the concrete implementation of FilterBuilder.
33-
type filterBuilder struct {
34-
op *methodBuilder
16+
// FilterBuilder is the concrete implementation of FilterBuilder.
17+
type FilterBuilder struct {
18+
op *MethodBuilder
3519
evRange *api.Range
3620
opts *api.Options
3721
order logdb.Order
3822
}
3923

4024
// InRange implements FilterBuilder.InRange.
41-
func (b *filterBuilder) InRange(r *api.Range) FilterBuilder {
25+
func (b *FilterBuilder) InRange(r *api.Range) *FilterBuilder {
4226
b.evRange = r
4327
return b
4428
}
4529

4630
// WithOptions implements FilterBuilder.WithOptions.
47-
func (b *filterBuilder) WithOptions(opts *api.Options) FilterBuilder {
31+
func (b *FilterBuilder) WithOptions(opts *api.Options) *FilterBuilder {
4832
b.opts = opts
4933
return b
5034
}
5135

5236
// OrderBy implements FilterBuilder.OrderBy.
53-
func (b *filterBuilder) OrderBy(order logdb.Order) FilterBuilder {
37+
func (b *FilterBuilder) OrderBy(order logdb.Order) *FilterBuilder {
5438
b.order = order
5539
return b
5640
}
5741

5842
// Execute implements FilterBuilder.Execute.
59-
func (b *filterBuilder) Execute() ([]api.FilteredEvent, error) {
43+
func (b *FilterBuilder) Execute() ([]api.FilteredEvent, error) {
6044
event, ok := b.op.contract.abi.Events[b.op.method]
6145
if !ok {
6246
return nil, errors.New("event not found: " + b.op.method)

thorclient/bind/method.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,35 @@ import (
1313
"github.com/vechain/thor/v2/tx"
1414
)
1515

16-
// MethodBuilder is the interface that routes to specific operation types.
17-
type MethodBuilder interface {
18-
// WithValue sets the VET value for the operation.
19-
WithValue(vet *big.Int) MethodBuilder
20-
21-
// Call returns a CallBuilder for read operations.
22-
Call() CallBuilder
23-
24-
// Send returns a SendBuilder for write operations.
25-
Send() SendBuilder
26-
27-
// Clause returns a ClauseBuilder for building transaction clauses.
28-
Clause() (*tx.Clause, error)
29-
}
30-
31-
// methodBuilder is the concrete implementation of MethodBuilder.
32-
type methodBuilder struct {
33-
contract *contract
16+
type MethodBuilder struct {
17+
contract *Contract
3418
method string
3519
args []any
3620
vet *big.Int
3721
}
3822

3923
// WithValue implements MethodBuilder.WithValue.
40-
func (b *methodBuilder) WithValue(vet *big.Int) MethodBuilder {
24+
func (b *MethodBuilder) WithValue(vet *big.Int) *MethodBuilder {
4125
b.vet = vet
4226
return b
4327
}
4428

4529
// Call implements MethodBuilder.Call.
46-
func (b *methodBuilder) Call() CallBuilder {
47-
return &callBuilder{
30+
func (b *MethodBuilder) Call() *CallBuilder {
31+
return &CallBuilder{
4832
op: b,
4933
}
5034
}
5135

5236
// Send implements MethodBuilder.Send.
53-
func (b *methodBuilder) Send() SendBuilder {
54-
return &sendBuilder{
37+
func (b *MethodBuilder) Send() *SendBuilder {
38+
return &SendBuilder{
5539
op: b,
5640
}
5741
}
5842

5943
// Clause implements Clause build.
60-
func (b *methodBuilder) Clause() (*tx.Clause, error) {
44+
func (b *MethodBuilder) Clause() (*tx.Clause, error) {
6145
method, ok := b.contract.abi.Methods[b.method]
6246
if !ok {
6347
return nil, fmt.Errorf("method not found: %s", b.method)

0 commit comments

Comments
 (0)