This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtransfer_tx.go
82 lines (70 loc) · 1.83 KB
/
transfer_tx.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chain
import (
"bytes"
"strconv"
"github.com/ava-labs/spacesvm/tdata"
"github.com/ethereum/go-ethereum/common"
)
var _ UnsignedTransaction = &TransferTx{}
type TransferTx struct {
*BaseTx `serialize:"true" json:"baseTx"`
// To is the recipient of the [Units].
To common.Address `serialize:"true" json:"to"`
// Units are transferred to [To].
Units uint64 `serialize:"true" json:"units"`
}
func (t *TransferTx) Execute(c *TransactionContext) error {
// Must transfer to someone
if bytes.Equal(t.To[:], zeroAddress[:]) {
return ErrNonActionable
}
// This prevents someone from transferring to themselves.
if bytes.Equal(t.To[:], c.Sender[:]) {
return ErrNonActionable
}
if t.Units == 0 {
return ErrNonActionable
}
if _, err := ModifyBalance(c.Database, c.Sender, false, t.Units); err != nil {
return err
}
if _, err := ModifyBalance(c.Database, t.To, true, t.Units); err != nil {
return err
}
return nil
}
func (t *TransferTx) Copy() UnsignedTransaction {
to := make([]byte, common.AddressLength)
copy(to, t.To[:])
return &TransferTx{
BaseTx: t.BaseTx.Copy(),
To: common.BytesToAddress(to),
Units: t.Units,
}
}
func (t *TransferTx) TypedData() *tdata.TypedData {
return tdata.CreateTypedData(
t.Magic, Transfer,
[]tdata.Type{
{Name: tdTo, Type: tdAddress},
{Name: tdUnits, Type: tdUint64},
{Name: tdPrice, Type: tdUint64},
{Name: tdBlockID, Type: tdString},
},
tdata.TypedDataMessage{
tdTo: t.To.Hex(),
tdUnits: strconv.FormatUint(t.Units, 10),
tdPrice: strconv.FormatUint(t.Price, 10),
tdBlockID: t.BlockID.String(),
},
)
}
func (t *TransferTx) Activity() *Activity {
return &Activity{
Typ: Transfer,
To: t.To.Hex(),
Units: t.Units,
}
}