forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.go
103 lines (84 loc) · 2.5 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
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package actions
import (
"context"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/examples/tokenvm/storage"
"github.com/ava-labs/hypersdk/state"
)
var _ chain.Action = (*Transfer)(nil)
type Transfer struct {
// To is the recipient of the [Value].
To codec.Address `json:"to"`
// Asset to transfer to [To].
Asset ids.ID `json:"asset"`
// Amount are transferred to [To].
Value uint64 `json:"value"`
// Optional message to accompany transaction.
Memo []byte `json:"memo"`
// TODO: add boolean to indicate whether sender will
// create recipient account
}
func (*Transfer) GetTypeID() uint8 {
return transferID
}
func (t *Transfer) StateKeys(actor codec.Address, _ ids.ID) state.Keys {
return state.Keys{
string(storage.BalanceKey(actor, t.Asset)): state.Read | state.Write,
string(storage.BalanceKey(t.To, t.Asset)): state.All,
}
}
func (*Transfer) StateKeysMaxChunks() []uint16 {
return []uint16{storage.BalanceChunks, storage.BalanceChunks}
}
func (t *Transfer) Execute(
ctx context.Context,
_ chain.Rules,
mu state.Mutable,
_ int64,
actor codec.Address,
_ ids.ID,
) ([][]byte, error) {
if t.Value == 0 {
return nil, ErrOutputValueZero
}
if len(t.Memo) > MaxMemoSize {
return nil, ErrOutputMemoTooLarge
}
if err := storage.SubBalance(ctx, mu, actor, t.Asset, t.Value); err != nil {
return nil, err
}
// TODO: allow sender to configure whether they will pay to create
if err := storage.AddBalance(ctx, mu, t.To, t.Asset, t.Value, true); err != nil {
return nil, err
}
return nil, nil
}
func (*Transfer) ComputeUnits(chain.Rules) uint64 {
return TransferComputeUnits
}
func (t *Transfer) Size() int {
return codec.AddressLen + ids.IDLen + consts.Uint64Len + codec.BytesLen(t.Memo)
}
func (t *Transfer) Marshal(p *codec.Packer) {
p.PackAddress(t.To)
p.PackID(t.Asset)
p.PackUint64(t.Value)
p.PackBytes(t.Memo)
}
func UnmarshalTransfer(p *codec.Packer) (chain.Action, error) {
var transfer Transfer
p.UnpackAddress(&transfer.To)
p.UnpackID(false, &transfer.Asset) // empty ID is the native asset
transfer.Value = p.UnpackUint64(true)
p.UnpackBytes(MaxMemoSize, false, &transfer.Memo)
return &transfer, p.Err()
}
func (*Transfer) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}