forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.go
106 lines (88 loc) · 2.79 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
// 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/avalanchego/vms/platformvm/warp"
"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"
"github.com/ava-labs/hypersdk/utils"
)
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"`
}
func (*Transfer) GetTypeID() uint8 {
return transferID
}
func (t *Transfer) StateKeys(actor codec.Address, _ ids.ID) []string {
return []string{
string(storage.BalanceKey(actor, t.Asset)),
string(storage.BalanceKey(t.To, t.Asset)),
}
}
func (*Transfer) StateKeysMaxChunks() []uint16 {
return []uint16{storage.BalanceChunks, storage.BalanceChunks}
}
func (*Transfer) OutputsWarpMessage() bool {
return false
}
func (t *Transfer) Execute(
ctx context.Context,
_ chain.Rules,
mu state.Mutable,
_ int64,
actor codec.Address,
_ ids.ID,
_ bool,
) (bool, uint64, []byte, *warp.UnsignedMessage, error) {
if t.Value == 0 {
return false, TransferComputeUnits, OutputValueZero, nil, nil
}
if len(t.Memo) > MaxMemoSize {
return false, CreateAssetComputeUnits, OutputMemoTooLarge, nil, nil
}
if err := storage.SubBalance(ctx, mu, actor, t.Asset, t.Value); err != nil {
return false, TransferComputeUnits, utils.ErrBytes(err), nil, nil
}
// 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 false, TransferComputeUnits, utils.ErrBytes(err), nil, nil
}
return true, TransferComputeUnits, nil, nil, nil
}
func (*Transfer) MaxComputeUnits(chain.Rules) uint64 {
return TransferComputeUnits
}
func (t *Transfer) Size() int {
return codec.AddressLen + consts.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, _ *warp.Message) (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
}