forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.go
94 lines (79 loc) · 2.63 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
// 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/crypto/ed25519"
"github.com/ava-labs/hypersdk/examples/tokenvm/auth"
"github.com/ava-labs/hypersdk/examples/tokenvm/storage"
"github.com/ava-labs/hypersdk/utils"
)
var _ chain.Action = (*Transfer)(nil)
type Transfer struct {
// To is the recipient of the [Value].
To ed25519.PublicKey `json:"to"`
// Asset to transfer to [To].
Asset ids.ID
// Amount are transferred to [To].
Value uint64 `json:"value"`
}
func (*Transfer) GetTypeID() uint8 {
return transferID
}
func (t *Transfer) StateKeys(rauth chain.Auth, _ ids.ID) [][]byte {
return [][]byte{
storage.PrefixBalanceKey(auth.GetActor(rauth), t.Asset),
storage.PrefixBalanceKey(t.To, t.Asset),
}
}
func (t *Transfer) Execute(
ctx context.Context,
r chain.Rules,
db chain.Database,
_ int64,
rauth chain.Auth,
_ ids.ID,
_ bool,
) (*chain.Result, error) {
actor := auth.GetActor(rauth)
unitsUsed := t.MaxUnits(r) // max units == units
if t.Value == 0 {
return &chain.Result{Success: false, Units: unitsUsed, Output: OutputValueZero}, nil
}
if err := storage.SubBalance(ctx, db, actor, t.Asset, t.Value); err != nil {
return &chain.Result{Success: false, Units: unitsUsed, Output: utils.ErrBytes(err)}, nil
}
if err := storage.AddBalance(ctx, db, t.To, t.Asset, t.Value); err != nil {
return &chain.Result{Success: false, Units: unitsUsed, Output: utils.ErrBytes(err)}, nil
}
return &chain.Result{Success: true, Units: unitsUsed}, nil
}
func (*Transfer) MaxUnits(chain.Rules) uint64 {
// We use size as the price of this transaction but we could just as easily
// use any other calculation.
return ed25519.PublicKeyLen + consts.IDLen + consts.Uint64Len
}
func (*Transfer) Size() int {
return ed25519.PublicKeyLen + consts.IDLen + consts.Uint64Len
}
func (t *Transfer) Marshal(p *codec.Packer) {
p.PackPublicKey(t.To)
p.PackID(t.Asset)
p.PackUint64(t.Value)
}
func UnmarshalTransfer(p *codec.Packer, _ *warp.Message) (chain.Action, error) {
var transfer Transfer
p.UnpackPublicKey(false, &transfer.To) // can transfer to blackhole
p.UnpackID(false, &transfer.Asset) // empty ID is the native asset
transfer.Value = p.UnpackUint64(true)
return &transfer, p.Err()
}
func (*Transfer) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}