-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
372 lines (305 loc) · 10.4 KB
/
transaction.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/********************************************************************************
This file is part of go-web3.
go-web3 is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
go-web3 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with go-web3. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
/**
* @file transaction.go
* @authors:
* Reginaldo Costa <ethereum@gmail.com>
* @date 2017
*/
package dto
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-web3/complex/types"
)
// TransactionParameters GO transaction to make more easy controll the parameters
type TransactionParameters struct {
From string
To string
Nonce *big.Int
Gas *big.Int
GasPrice *big.Int
Value *big.Int
Data types.ComplexString
}
// RequestTransactionParameters JSON
type RequestTransactionParameters struct {
From string `json:"from"`
To string `json:"to,omitempty"`
Nonce string `json:"nonce,omitempty"`
Gas string `json:"gas,omitempty"`
GasPrice string `json:"gasPrice,omitempty"`
Value string `json:"value,omitempty"`
Data string `json:"data,omitempty"`
}
// Transform the GO transactions parameters to json style
func (params *TransactionParameters) Transform() *RequestTransactionParameters {
request := new(RequestTransactionParameters)
request.From = params.From
if params.To != "" {
request.To = params.To
}
if params.Nonce != nil {
request.Nonce = "0x" + params.Nonce.Text(16)
}
if params.Gas != nil {
request.Gas = "0x" + params.Gas.Text(16)
}
if params.GasPrice != nil {
request.GasPrice = "0x" + params.GasPrice.Text(16)
}
if params.Value != nil {
request.Value = "0x" + params.Value.Text(16)
}
if params.Data != "" {
request.Data = params.Data.ToHex()
}
return request
}
type SignTransactionResponse struct {
Raw types.ComplexString `json:"raw"`
Transaction SignedTransactionParams `json:"tx"`
}
type SignedTransactionParams struct {
Gas *big.Int `json:gas`
GasPrice *big.Int `json:gasPrice`
Hash string `json:hash`
Input string `json:input`
Nonce *big.Int `json:nonce`
S string `json:s`
R string `json:r`
V *big.Int `json:v`
To string `json:to`
Value *big.Int `json:value`
}
type TransactionResponse struct {
Hash string `json:"hash"`
Nonce *big.Int `json:"nonce"`
BlockHash string `json:"blockHash"`
BlockNumber *big.Int `json:"blockNumber"`
TransactionIndex *big.Int `json:"transactionIndex"`
From string `json:"from"`
To string `json:"to"`
Input string `json:"input"`
Value *big.Int `json:"value"`
GasPrice *big.Int `json:"gasPrice,omitempty"`
Gas *big.Int `json:"gas,omitempty"`
Data types.ComplexString `json:"data,omitempty"`
}
type TransactionReceipt struct {
TransactionHash string `json:"transactionHash"`
TransactionIndex *big.Int `json:"transactionIndex"`
BlockHash string `json:"blockHash"`
BlockNumber *big.Int `json:"blockNumber"`
From string `json:"from"`
To string `json:"to"`
CumulativeGasUsed *big.Int `json:"cumulativeGasUsed"`
GasUsed *big.Int `json:"gasUsed"`
ContractAddress string `json:"contractAddress"`
Logs []TransactionLogs `json:"logs"`
LogsBloom string `json:"logsBloom"`
Root string `json:"string"`
Status bool `json:"status"`
}
type TransactionLogs struct {
Address string `json:"address"`
Topics []string `json:"topics"`
Data string `json:"data"`
BlockNumber *big.Int `json:"blockNumber"`
TransactionHash string `json:"transactionHash"`
TransactionIndex *big.Int `json:"transactionIndex"`
BlockHash string `json:"blockHash"`
LogIndex *big.Int `json:"logIndex"`
Removed bool `json:"removed"`
}
func (t *TransactionResponse) UnmarshalJSON(data []byte) error {
type Alias TransactionResponse
temp := &struct {
Nonce string `json:"nonce"`
BlockNumber string `json:"blockNumber"`
TransactionIndex string `json:"transactionIndex"`
Value string `json:"value"`
GasPrice string `json:"gasPrice,omitempty"`
Gas string `json:"gas,omitempty"`
*Alias
}{
Alias: (*Alias)(t),
}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
nonce, success := big.NewInt(0).SetString(temp.Nonce[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.Nonce))
}
if len(temp.BlockNumber) == 0 {
temp.BlockNumber = "0x"
}
blockNum, success := big.NewInt(0).SetString(temp.BlockNumber[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.BlockNumber))
}
if len(temp.TransactionIndex) == 0 {
temp.TransactionIndex = "0x"
}
txIndex, success := big.NewInt(0).SetString(temp.TransactionIndex[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.TransactionIndex))
}
gas, success := big.NewInt(0).SetString(temp.Gas[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.Gas))
}
gasPrice, success := big.NewInt(0).SetString(temp.GasPrice[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.GasPrice))
}
value, success := big.NewInt(0).SetString(temp.Value[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.Value))
}
t.Nonce = nonce
t.BlockNumber = blockNum
t.TransactionIndex = txIndex
t.Gas = gas
t.GasPrice = gasPrice
t.Value = value
return nil
}
func (r *TransactionLogs) UnmarshalJSON(data []byte) error {
type Alias TransactionLogs
log := &struct {
TransactionIndex string `json:"transactionIndex"`
BlockNumber string `json:"blockNumber"`
LogIndex string `json:"logIndex"`
*Alias
}{
Alias: (*Alias)(r),
}
if err := json.Unmarshal(data, &log); err != nil {
return err
}
blockNumLog, success := big.NewInt(0).SetString(log.BlockNumber[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", log.BlockNumber))
}
txIndexLogs, success := big.NewInt(0).SetString(log.TransactionIndex[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", log.TransactionIndex))
}
logIndex, success := big.NewInt(0).SetString(log.LogIndex[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", log.LogIndex))
}
r.BlockNumber = blockNumLog
r.TransactionIndex = txIndexLogs
r.LogIndex = logIndex
return nil
}
func (r *TransactionReceipt) UnmarshalJSON(data []byte) error {
type Alias TransactionReceipt
temp := &struct {
TransactionIndex string `json:"transactionIndex"`
BlockNumber string `json:"blockNumber"`
CumulativeGasUsed string `json:"cumulativeGasUsed"`
GasUsed string `json:"gasUsed"`
Status string `json:"status"`
*Alias
}{
Alias: (*Alias)(r),
}
fmt.Println(string(data))
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
if temp == nil {
return errors.New(fmt.Sprintf("Recept is nil"))
}
if len(temp.BlockNumber) < 2 {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.BlockNumber))
}
blockNum, success := big.NewInt(0).SetString(temp.BlockNumber[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.BlockNumber))
}
txIndex, success := big.NewInt(0).SetString(temp.TransactionIndex[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.TransactionIndex))
}
gasUsed, success := big.NewInt(0).SetString(temp.GasUsed[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.GasUsed))
}
cumulativeGas, success := big.NewInt(0).SetString(temp.CumulativeGasUsed[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.CumulativeGasUsed))
}
status, success := big.NewInt(0).SetString(temp.Status[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.Status))
}
r.TransactionIndex = txIndex
r.BlockNumber = blockNum
r.CumulativeGasUsed = cumulativeGas
r.GasUsed = gasUsed
r.Status = false
if status.Cmp(big.NewInt(1)) == 0 {
r.Status = true
}
return nil
}
func (sp *SignedTransactionParams) UnmarshalJSON(data []byte) error {
type Alias SignedTransactionParams
temp := &struct {
Gas string `json:gas`
GasPrice string `json:gasPrice`
Nonce string `json:nonce`
V string `json:v`
Value string `json:value`
*Alias
}{
Alias: (*Alias)(sp),
}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
gas, success := big.NewInt(0).SetString(temp.Gas[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.Gas))
}
gasPrice, success := big.NewInt(0).SetString(temp.GasPrice[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.GasPrice))
}
nonce, success := big.NewInt(0).SetString(temp.Nonce[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.Nonce))
}
v, success := big.NewInt(0).SetString(temp.V[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.V))
}
val, success := big.NewInt(0).SetString(temp.Value[2:], 16)
if !success {
return errors.New(fmt.Sprintf("Error converting %s to BigInt", temp.Value))
}
sp.Gas = gas
sp.GasPrice = gasPrice
sp.Nonce = nonce
sp.V = v
sp.Value = val
return nil
}