-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontract.go
57 lines (52 loc) · 1.35 KB
/
contract.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package evmc
import (
"context"
"github.com/bbaktaeho/evmc/evmctypes"
"github.com/ethereum/go-ethereum/rpc"
)
type contract struct {
c caller
}
func (c *contract) Query(ctx context.Context, queryParams *evmctypes.QueryParams) (*evmctypes.QueryResp, error) {
var (
result = new(string)
params = []interface{}{queryParams, evmctypes.ParseBlockAndTag(queryParams.NumOrTag)}
)
if err := c.c.call(ctx, result, EthCall, params...); err != nil {
return nil, err
}
return &evmctypes.QueryResp{
To: queryParams.To,
Data: queryParams.Data,
Result: *result,
}, nil
}
func (c *contract) BatchQuery(
ctx context.Context,
batchQueryParams []*evmctypes.QueryParams,
) ([]*evmctypes.QueryResp, error) {
var (
size = len(batchQueryParams)
elements = make([]rpc.BatchElem, size)
results = make([]*evmctypes.QueryResp, size)
)
for i := range elements {
results[i] = &evmctypes.QueryResp{To: batchQueryParams[i].To, Data: batchQueryParams[i].Data}
numOrTag := evmctypes.ParseBlockAndTag(batchQueryParams[i].NumOrTag)
elements[i] = rpc.BatchElem{
Method: EthCall.String(),
Args: []interface{}{
batchQueryParams[i],
numOrTag,
},
Result: &results[i].Result,
}
}
if err := c.c.batchCall(ctx, elements); err != nil {
return nil, err
}
for i, el := range elements {
results[i].Error = el.Error
}
return results, nil
}