/
request_evmcall.go
138 lines (111 loc) · 3.41 KB
/
request_evmcall.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package isc
import (
"encoding/json"
"fmt"
"io"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
iotago "github.com/iotaledger/iota.go/v3"
"github.com/iotaledger/wasp/packages/evm/evmtypes"
"github.com/iotaledger/wasp/packages/hashing"
"github.com/iotaledger/wasp/packages/kv/dict"
"github.com/iotaledger/wasp/packages/util/rwutil"
"github.com/iotaledger/wasp/packages/vm/core/evm/evmnames"
)
// ----------------------------------------------------------------
// evmOffLedgerCallRequest is used to wrap an EVM call (for the eth_call or eth_estimateGas jsonrpc methods)
type evmOffLedgerCallRequest struct {
chainID ChainID
callMsg ethereum.CallMsg
}
var _ OffLedgerRequest = &evmOffLedgerCallRequest{}
func NewEVMOffLedgerCallRequest(chainID ChainID, callMsg ethereum.CallMsg) OffLedgerRequest {
return &evmOffLedgerCallRequest{
chainID: chainID,
callMsg: callMsg,
}
}
func (req *evmOffLedgerCallRequest) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
rr.ReadKindAndVerify(rwutil.Kind(requestKindOffLedgerEVMTx))
rr.Read(&req.chainID)
data := rr.ReadBytes()
if rr.Err == nil {
req.callMsg, rr.Err = evmtypes.DecodeCallMsg(data)
}
return rr.Err
}
func (req *evmOffLedgerCallRequest) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.WriteKind(rwutil.Kind(requestKindOffLedgerEVMTx))
ww.Write(&req.chainID)
if ww.Err == nil {
data := evmtypes.EncodeCallMsg(req.callMsg)
ww.WriteBytes(data)
}
return ww.Err
}
func (req *evmOffLedgerCallRequest) Allowance() *Assets {
return NewEmptyAssets()
}
func (req *evmOffLedgerCallRequest) Assets() *Assets {
return NewEmptyAssets()
}
func (req *evmOffLedgerCallRequest) Bytes() []byte {
return rwutil.WriteToBytes(req)
}
func (req *evmOffLedgerCallRequest) CallTarget() CallTarget {
return CallTarget{
Contract: Hn(evmnames.Contract),
EntryPoint: Hn(evmnames.FuncCallContract),
}
}
func (req *evmOffLedgerCallRequest) ChainID() ChainID {
return req.chainID
}
func (req *evmOffLedgerCallRequest) GasBudget() (gas uint64, isEVM bool) {
return req.callMsg.Gas, true
}
func (req *evmOffLedgerCallRequest) ID() RequestID {
return NewRequestID(iotago.TransactionID(hashing.HashData(req.Bytes())), 0)
}
func (req *evmOffLedgerCallRequest) IsOffLedger() bool {
return true
}
func (req *evmOffLedgerCallRequest) NFT() *NFT {
return nil
}
func (req *evmOffLedgerCallRequest) Nonce() uint64 {
return 0
}
func (req *evmOffLedgerCallRequest) Params() dict.Dict {
return dict.Dict{evmnames.FieldCallMsg: evmtypes.EncodeCallMsg(req.callMsg)}
}
func (req *evmOffLedgerCallRequest) SenderAccount() AgentID {
return NewEthereumAddressAgentID(req.chainID, req.callMsg.From)
}
func (req *evmOffLedgerCallRequest) String() string {
// ignore error so String does not crash the app
data, _ := json.MarshalIndent(req.callMsg, " ", " ")
return fmt.Sprintf("%T::{ ID: %s, callMsg: %s }",
req,
req.ID(),
data,
)
}
func (req *evmOffLedgerCallRequest) TargetAddress() iotago.Address {
return req.chainID.AsAddress()
}
func (req *evmOffLedgerCallRequest) VerifySignature() error {
return fmt.Errorf("%T should never be used to send regular requests", req)
}
func (*evmOffLedgerCallRequest) EVMTransaction() *types.Transaction {
return nil
}
func (req *evmOffLedgerCallRequest) EVMCallData() *EVMCallData {
return EVMCallDataFromCallMsg(req.callMsg)
}
func (req *evmOffLedgerCallRequest) TxValue() *big.Int {
return req.callMsg.Value
}