-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth_call.go
98 lines (81 loc) · 2.42 KB
/
eth_call.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package transformer
import (
"context"
"math/big"
"github.com/labstack/echo"
"github.com/revolutionchain/charon/pkg/eth"
"github.com/revolutionchain/charon/pkg/revo"
"github.com/revolutionchain/charon/pkg/utils"
)
// ProxyETHCall implements ETHProxy
type ProxyETHCall struct {
*revo.Revo
}
func (p *ProxyETHCall) Method() string {
return "eth_call"
}
func (p *ProxyETHCall) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, eth.JSONRPCError) {
var req eth.CallRequest
if err := unmarshalRequest(rawreq.Params, &req); err != nil {
// TODO: Is this correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
return p.request(c.Request().Context(), &req)
}
func (p *ProxyETHCall) request(ctx context.Context, ethreq *eth.CallRequest) (interface{}, eth.JSONRPCError) {
// eth req -> revo req
revoreq, jsonErr := p.ToRequest(ethreq)
if jsonErr != nil {
return nil, jsonErr
}
if revoreq.GasLimit != nil && revoreq.GasLimit.Cmp(big.NewInt(40000000)) > 0 {
revoresp := eth.CallResponse("0x")
p.Revo.GetLogger().Log("msg", "Caller gas above allowance, capping", "requested", revoreq.GasLimit.Int64(), "cap", "40,000,000")
return &revoresp, nil
}
revoresp, err := p.CallContract(ctx, revoreq)
if err != nil {
if err == revo.ErrInvalidAddress {
revoresp := eth.CallResponse("0x")
return &revoresp, nil
}
return nil, eth.NewCallbackError(err.Error())
}
// revo res -> eth res
return p.ToResponse(revoresp), nil
}
func (p *ProxyETHCall) ToRequest(ethreq *eth.CallRequest) (*revo.CallContractRequest, eth.JSONRPCError) {
from := ethreq.From
var err error
if utils.IsEthHexAddress(from) {
from, err = p.FromHexAddress(from)
if err != nil {
return nil, eth.NewCallbackError(err.Error())
}
}
var gasLimit *big.Int
if ethreq.Gas != nil {
gasLimit = ethreq.Gas.Int
}
if gasLimit != nil && gasLimit.Int64() < MinimumGasLimit {
p.GetLogger().Log("msg", "Gas limit is too low", "gasLimit", gasLimit.String())
}
return &revo.CallContractRequest{
To: ethreq.To,
From: from,
Data: ethreq.Data,
GasLimit: gasLimit,
}, nil
}
func (p *ProxyETHCall) ToResponse(qresp *revo.CallContractResponse) interface{} {
if qresp.ExecutionResult.Output == "" {
return eth.NewJSONRPCError(
-32000,
"Revert: executionResult output is empty",
nil,
)
}
data := utils.AddHexPrefix(qresp.ExecutionResult.Output)
revoresp := eth.CallResponse(data)
return &revoresp
}