-
Notifications
You must be signed in to change notification settings - Fork 6
/
transfer.go
executable file
·377 lines (311 loc) · 10 KB
/
transfer.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
373
374
375
376
377
package actions
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/hacash/core/fields"
"github.com/hacash/core/interfaces"
"github.com/hacash/core/interfacev2"
"github.com/hacash/core/sys"
)
type Action_1_SimpleToTransfer struct {
ToAddress fields.Address
Amount fields.Amount
// data ptr
belong_trs interfacev2.Transaction
belong_trs_v3 interfaces.Transaction
}
func NewAction_1_SimpleToTransfer(addr fields.Address, amt *fields.Amount) *Action_1_SimpleToTransfer {
return &Action_1_SimpleToTransfer{
ToAddress: addr,
Amount: *amt,
}
}
func (elm *Action_1_SimpleToTransfer) Kind() uint16 {
return 1
}
// json api
func (elm *Action_1_SimpleToTransfer) Describe() map[string]interface{} {
var data = map[string]interface{}{}
return data
}
func (elm *Action_1_SimpleToTransfer) Serialize() ([]byte, error) {
var kindByte = make([]byte, 2)
binary.BigEndian.PutUint16(kindByte, elm.Kind())
var addrBytes, _ = elm.ToAddress.Serialize()
var amtBytes, _ = elm.Amount.Serialize()
var buffer bytes.Buffer
buffer.Write(kindByte)
buffer.Write(addrBytes)
buffer.Write(amtBytes)
return buffer.Bytes(), nil
}
func (elm *Action_1_SimpleToTransfer) Parse(buf []byte, seek uint32) (uint32, error) {
moveseek, e := elm.ToAddress.Parse(buf, seek)
if e != nil {
return 0, e
}
moveseek2, e := elm.Amount.Parse(buf, moveseek)
if e != nil {
return 0, e
}
return moveseek2, nil
}
func (elm *Action_1_SimpleToTransfer) Size() uint32 {
return 2 + elm.ToAddress.Size() + elm.Amount.Size()
}
func (*Action_1_SimpleToTransfer) RequestSignAddresses() []fields.Address {
return []fields.Address{} // not sign
}
func (act *Action_1_SimpleToTransfer) WriteInChainState(state interfaces.ChainStateOperation) error {
if act.belong_trs_v3 == nil {
panic("Action belong to transaction not be nil !")
}
// check amount value
if !act.Amount.IsPositive() {
return fmt.Errorf("Amount is not positive.")
}
// transfer
return DoSimpleTransferFromChainState(state, act.belong_trs_v3.GetAddress(), act.ToAddress, act.Amount)
}
func (act *Action_1_SimpleToTransfer) WriteinChainState(state interfacev2.ChainStateOperation) error {
panic("WriteinChainState be deprecated")
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
// check amount value
if !act.Amount.IsPositive() {
return fmt.Errorf("Amount is not positive.")
}
// transfer
return DoSimpleTransferFromChainStateV2(state, act.belong_trs.GetAddress(), act.ToAddress, act.Amount)
}
func (act *Action_1_SimpleToTransfer) RecoverChainState(state interfacev2.ChainStateOperation) error {
panic("RecoverChainState be deprecated")
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
// Fallback
return DoSimpleTransferFromChainStateV2(state, act.ToAddress, act.belong_trs.GetAddress(), act.Amount)
}
// Set belongs to long_ trs
func (act *Action_1_SimpleToTransfer) SetBelongTransaction(trs interfacev2.Transaction) {
act.belong_trs = trs
}
func (act *Action_1_SimpleToTransfer) SetBelongTrs(trs interfaces.Transaction) {
act.belong_trs_v3 = trs
}
// burning fees // 是否销毁本笔交易的 90% 的交易费用
func (act *Action_1_SimpleToTransfer) IsBurning90PersentTxFees() bool {
///////////// TEST CODE START /////////////
//if act.Amount.ToMeiString() == "2" { // 测试转账为 2 枚时,手续费减半
// return true
//}
///////////// TEST CODE END /////////////
return false
}
//////////////////////////////////////////
type Action_13_FromTransfer struct {
FromAddress fields.Address
Amount fields.Amount
// data ptr
belong_trs interfacev2.Transaction
belong_trs_v3 interfaces.Transaction
}
func NewAction_13_FromTransfer(addr fields.Address, amt *fields.Amount) *Action_13_FromTransfer {
return &Action_13_FromTransfer{
FromAddress: addr,
Amount: *amt,
}
}
func (elm *Action_13_FromTransfer) Kind() uint16 {
return 13
}
// json api
func (elm *Action_13_FromTransfer) Describe() map[string]interface{} {
var data = map[string]interface{}{}
return data
}
func (elm *Action_13_FromTransfer) Serialize() ([]byte, error) {
var kindByte = make([]byte, 2)
binary.BigEndian.PutUint16(kindByte, elm.Kind())
var addrBytes, _ = elm.FromAddress.Serialize()
var amtBytes, _ = elm.Amount.Serialize()
var buffer bytes.Buffer
buffer.Write(kindByte)
buffer.Write(addrBytes)
buffer.Write(amtBytes)
return buffer.Bytes(), nil
}
func (elm *Action_13_FromTransfer) Parse(buf []byte, seek uint32) (uint32, error) {
moveseek, e := elm.FromAddress.Parse(buf, seek)
if e != nil {
return 0, e
}
moveseek2, e := elm.Amount.Parse(buf, moveseek)
if e != nil {
return 0, e
}
return moveseek2, nil
}
func (elm *Action_13_FromTransfer) Size() uint32 {
return 2 + elm.FromAddress.Size() + elm.Amount.Size()
}
func (elm *Action_13_FromTransfer) RequestSignAddresses() []fields.Address {
return []fields.Address{
elm.FromAddress,
} // from sign
}
func (act *Action_13_FromTransfer) WriteInChainState(state interfaces.ChainStateOperation) error {
if !sys.TestDebugLocalDevelopmentMark {
return fmt.Errorf("mainnet not yet") // Waiting for review is not enabled yet
}
if act.belong_trs_v3 == nil {
panic("Action belong to transaction not be nil !")
}
// check amount value
if !act.Amount.IsPositive() {
return fmt.Errorf("Amount is not positive.")
}
// transfer
return DoSimpleTransferFromChainState(state, act.FromAddress, act.belong_trs_v3.GetAddress(), act.Amount)
}
func (act *Action_13_FromTransfer) WriteinChainState(state interfacev2.ChainStateOperation) error {
if !sys.TestDebugLocalDevelopmentMark {
return fmt.Errorf("mainnet not yet") // Waiting for review is not enabled yet
}
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
// check amount value
if !act.Amount.IsPositive() {
return fmt.Errorf("Amount is not positive.")
}
// transfer
return DoSimpleTransferFromChainStateV2(state, act.FromAddress, act.belong_trs.GetAddress(), act.Amount)
}
func (act *Action_13_FromTransfer) RecoverChainState(state interfacev2.ChainStateOperation) error {
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
// Fallback
return DoSimpleTransferFromChainStateV2(state, act.belong_trs.GetAddress(), act.FromAddress, act.Amount)
}
// Set belongs to long_ trs
func (act *Action_13_FromTransfer) SetBelongTransaction(trs interfacev2.Transaction) {
act.belong_trs = trs
}
func (act *Action_13_FromTransfer) SetBelongTrs(trs interfaces.Transaction) {
act.belong_trs_v3 = trs
}
// burning fees // 是否销毁本笔交易的 90% 的交易费用
func (act *Action_13_FromTransfer) IsBurning90PersentTxFees() bool {
return false
}
////////////////////////////////////////
type Action_14_FromToTransfer struct {
FromAddress fields.Address
ToAddress fields.Address
Amount fields.Amount
// data ptr
belong_trs interfacev2.Transaction
belong_trs_v3 interfaces.Transaction
}
func NewAction_14_FromToTransfer(fromaddr fields.Address, toaddr fields.Address, amt *fields.Amount) *Action_14_FromToTransfer {
return &Action_14_FromToTransfer{
FromAddress: fromaddr,
ToAddress: toaddr,
Amount: *amt,
}
}
func (elm *Action_14_FromToTransfer) Kind() uint16 {
return 14
}
// json api
func (elm *Action_14_FromToTransfer) Describe() map[string]interface{} {
var data = map[string]interface{}{}
return data
}
func (elm *Action_14_FromToTransfer) Serialize() ([]byte, error) {
var kindByte = make([]byte, 2)
binary.BigEndian.PutUint16(kindByte, elm.Kind())
var addr1Bytes, _ = elm.FromAddress.Serialize()
var addr2Bytes, _ = elm.ToAddress.Serialize()
var amtBytes, _ = elm.Amount.Serialize()
var buffer bytes.Buffer
buffer.Write(kindByte)
buffer.Write(addr1Bytes)
buffer.Write(addr2Bytes)
buffer.Write(amtBytes)
return buffer.Bytes(), nil
}
func (elm *Action_14_FromToTransfer) Parse(buf []byte, seek uint32) (uint32, error) {
var e error
seek, e = elm.FromAddress.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = elm.ToAddress.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = elm.Amount.Parse(buf, seek)
if e != nil {
return 0, e
}
return seek, nil
}
func (elm *Action_14_FromToTransfer) Size() uint32 {
return 2 + elm.FromAddress.Size() + elm.ToAddress.Size() + elm.Amount.Size()
}
func (elm *Action_14_FromToTransfer) RequestSignAddresses() []fields.Address {
return []fields.Address{
elm.FromAddress,
} // from sign
}
func (act *Action_14_FromToTransfer) WriteInChainState(state interfaces.ChainStateOperation) error {
if !sys.TestDebugLocalDevelopmentMark {
return fmt.Errorf("mainnet not yet") // Waiting for review is not enabled yet
}
if act.belong_trs_v3 == nil {
panic("Action belong to transaction not be nil !")
}
// check amount value
if !act.Amount.IsPositive() {
return fmt.Errorf("Amount is not positive.")
}
// transfer
return DoSimpleTransferFromChainState(state, act.FromAddress, act.ToAddress, act.Amount)
}
func (act *Action_14_FromToTransfer) WriteinChainState(state interfacev2.ChainStateOperation) error {
if !sys.TestDebugLocalDevelopmentMark {
return fmt.Errorf("mainnet not yet") // Waiting for review is not enabled yet
}
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
// check amount value
if !act.Amount.IsPositive() {
return fmt.Errorf("Amount is not positive.")
}
// transfer
return DoSimpleTransferFromChainStateV2(state, act.FromAddress, act.ToAddress, act.Amount)
}
func (act *Action_14_FromToTransfer) RecoverChainState(state interfacev2.ChainStateOperation) error {
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
// Fallback
return DoSimpleTransferFromChainStateV2(state, act.ToAddress, act.FromAddress, act.Amount)
}
// Set belongs to long_ trs
func (act *Action_14_FromToTransfer) SetBelongTransaction(trs interfacev2.Transaction) {
act.belong_trs = trs
}
func (act *Action_14_FromToTransfer) SetBelongTrs(trs interfaces.Transaction) {
act.belong_trs_v3 = trs
}
// burning fees // 是否销毁本笔交易的 90% 的交易费用
func (act *Action_14_FromToTransfer) IsBurning90PersentTxFees() bool {
return false
}