forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmint_asset.go
119 lines (101 loc) · 3.26 KB
/
mint_asset.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
// 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"
smath "github.com/ava-labs/avalanchego/utils/math"
"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 = (*MintAsset)(nil)
type MintAsset struct {
// To is the recipient of the [Value].
To codec.Address `json:"to"`
// Asset is the [TxID] that created the asset.
Asset ids.ID `json:"asset"`
// Number of assets to mint to [To].
Value uint64 `json:"value"`
}
func (*MintAsset) GetTypeID() uint8 {
return mintAssetID
}
func (m *MintAsset) StateKeys(codec.Address, ids.ID) state.Keys {
return state.Keys{
string(storage.AssetKey(m.Asset)): state.Read | state.Write,
string(storage.BalanceKey(m.To, m.Asset)): state.Read | state.Write,
}
}
func (*MintAsset) StateKeysMaxChunks() []uint16 {
return []uint16{storage.AssetChunks, storage.BalanceChunks}
}
func (*MintAsset) OutputsWarpMessage() bool {
return false
}
func (m *MintAsset) Execute(
ctx context.Context,
_ chain.Rules,
mu state.Mutable,
_ int64,
actor codec.Address,
_ ids.ID,
_ bool,
) (bool, uint64, []byte, *warp.UnsignedMessage, error) {
if m.Asset == ids.Empty {
return false, MintAssetComputeUnits, OutputAssetIsNative, nil, nil
}
if m.Value == 0 {
return false, MintAssetComputeUnits, OutputValueZero, nil, nil
}
exists, symbol, decimals, metadata, supply, owner, isWarp, err := storage.GetAsset(ctx, mu, m.Asset)
if err != nil {
return false, MintAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if !exists {
return false, MintAssetComputeUnits, OutputAssetMissing, nil, nil
}
if isWarp {
return false, MintAssetComputeUnits, OutputWarpAsset, nil, nil
}
if owner != actor {
return false, MintAssetComputeUnits, OutputWrongOwner, nil, nil
}
newSupply, err := smath.Add64(supply, m.Value)
if err != nil {
return false, MintAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if err := storage.SetAsset(ctx, mu, m.Asset, symbol, decimals, metadata, newSupply, actor, isWarp); err != nil {
return false, MintAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if err := storage.AddBalance(ctx, mu, m.To, m.Asset, m.Value, true); err != nil {
return false, MintAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
return true, MintAssetComputeUnits, nil, nil, nil
}
func (*MintAsset) MaxComputeUnits(chain.Rules) uint64 {
return MintAssetComputeUnits
}
func (*MintAsset) Size() int {
return codec.AddressLen + consts.IDLen + consts.Uint64Len
}
func (m *MintAsset) Marshal(p *codec.Packer) {
p.PackAddress(m.To)
p.PackID(m.Asset)
p.PackUint64(m.Value)
}
func UnmarshalMintAsset(p *codec.Packer, _ *warp.Message) (chain.Action, error) {
var mint MintAsset
p.UnpackAddress(&mint.To)
p.UnpackID(true, &mint.Asset) // empty ID is the native asset
mint.Value = p.UnpackUint64(true)
return &mint, p.Err()
}
func (*MintAsset) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}