-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcall_inputs.go
170 lines (153 loc) · 4.5 KB
/
call_inputs.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package evm
import(
"encoding/json"
)
func (c *CallInputs) New(txEnv *TxEnv, gasLimit uint64) *CallInputs {
// Check if the transaction kind is Call and extract the target address.
if txEnv.TransactTo.Type != Call2 {
return nil
}
targetAddress := txEnv.TransactTo.Address
// Create and return the CallInputs instance.
return &CallInputs{
Input: txEnv.Data,
GasLimit: gasLimit,
TargetAddress: *targetAddress,
BytecodeAddress: *targetAddress, // Set to target_address as in Rust code.
Caller: txEnv.Caller,
Value: CallValue{ValueType: "transfer", Amount: txEnv.Value},
Scheme: ICall, // Assuming CallScheme.Call is represented by Call.
IsStatic: false,
IsEof: false,
ReturnMemoryOffset: Range{Start: 0, End: 0},
}
}
func (c *CallInputs) NewBoxed(txEnv *TxEnv, gasLimit uint64) *CallInputs {
return c.New(txEnv, gasLimit) // Returns a pointer or nil, similar to Option<Box<Self>> in Rust.
}
func (e EOFCreateInputs) NewTx(tx *TxEnv, gasLimit uint64) EOFCreateInputs {
return EOFCreateInputs{
Caller: tx.Caller,
Value: tx.Value,
GasLimit: gasLimit,
Kind: EOFCreateKind{
Kind: TxK,
Data: tx.Data,
},
}
}
func (c *CreateInputs) New(txEnv *TxEnv, gasLimit uint64) *CreateInputs {
if txEnv.TransactTo.Type != Create2 {
return nil
}
return &CreateInputs{
Caller: txEnv.Caller,
Scheme: CreateScheme{
SchemeType: SchemeTypeCreate,
},
Value: txEnv.Value,
InitCode: txEnv.Data,
GasLimit: gasLimit,
}
}
func (c *CreateInputs) NewBoxed(txEnv *TxEnv, gasLimit uint64) *CreateInputs {
return c.New(txEnv, gasLimit)
}
type CallValue struct {
ValueType string `json:"value_type"`
Amount U256 `json:"amount,omitempty"`
}
// Transfer creates a CallValue representing a transfer.
func Transfer(amount U256) CallValue {
return CallValue{ValueType: "transfer", Amount: amount}
}
// Apparent creates a CallValue representing an apparent value.
func Apparent(amount U256) CallValue {
return CallValue{ValueType: "apparent", Amount: amount}
}
type CallScheme int
const (
ICall CallScheme = iota
ICallCode
IDelegateCall
IStaticCall
IExtCall
IExtStaticCall
IExtDelegateCall
)
type CallInputs struct {
Input Bytes `json:"input"`
ReturnMemoryOffset Range `json:"return_memory_offset"`
GasLimit uint64 `json:"gas_limit"`
BytecodeAddress Address `json:"bytecode_address"`
TargetAddress Address `json:"target_address"`
Caller Address `json:"caller"`
Value CallValue `json:"value"`
Scheme CallScheme `json:"scheme"`
IsStatic bool `json:"is_static"`
IsEof bool `json:"is_eof"`
}
// JSON serialization for CallValue
func (cv CallValue) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
ValueType string `json:"value_type"`
Amount *U256 `json:"amount,omitempty"`
}{
ValueType: cv.ValueType,
Amount: &cv.Amount,
})
}
// JSON deserialization for CallValue
func (cv *CallValue) UnmarshalJSON(data []byte) error {
var aux struct {
ValueType string `json:"value_type"`
Amount *U256 `json:"amount,omitempty"`
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
cv.ValueType = aux.ValueType
if aux.ValueType == "transfer" {
cv.Amount = *aux.Amount
} else if aux.ValueType == "apparent" {
cv.Amount = *aux.Amount
}
return nil
}
type CreateInputs struct {
Caller Address `json:"caller"`
Scheme CreateScheme `json:"scheme"`
Value U256 `json:"value"`
InitCode Bytes `json:"init_code"`
GasLimit uint64 `json:"gas_limit"`
}
type CreateScheme struct {
SchemeType SchemeType `json:"scheme_type"` // can be "create" or "create2"
Salt *U256 `json:"salt,omitempty"` // salt is optional for Create
}
// SchemeType represents the type of creation scheme
type SchemeType int
const (
SchemeTypeCreate SchemeType = iota
SchemeTypeCreate2
)
type EOFCreateKindType int
const (
TxK EOFCreateKindType = iota
OpcodeK
)
// / Inputs for EOF create call.
type EOFCreateInputs struct {
Caller Address `json:"caller"`
Value U256 `json:"value"`
GasLimit uint64 `json:"gas_limit"`
Kind EOFCreateKind `json:"kind"`
}
type EOFCreateKind struct {
Kind EOFCreateKindType
Data interface{} // Use an interface to hold the associated data (Tx or Opcode)
}
// TxKind represents a transaction-based EOF create kind.
type Tx struct {
InitData Bytes `json:"initdata"`
}