forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_asset.go
103 lines (88 loc) · 2.83 KB
/
create_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
// 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 = (*CreateAsset)(nil)
type CreateAsset struct {
Symbol []byte `json:"symbol"`
Decimals uint8 `json:"decimals"`
Metadata []byte `json:"metadata"`
}
func (*CreateAsset) GetTypeID() uint8 {
return createAssetID
}
func (*CreateAsset) StateKeys(_ codec.Address, txID ids.ID) state.Keys {
return state.Keys{
string(storage.AssetKey(txID)): state.Write,
}
}
func (*CreateAsset) StateKeysMaxChunks() []uint16 {
return []uint16{storage.AssetChunks}
}
func (*CreateAsset) OutputsWarpMessage() bool {
return false
}
func (c *CreateAsset) Execute(
ctx context.Context,
_ chain.Rules,
mu state.Mutable,
_ int64,
actor codec.Address,
txID ids.ID,
_ bool,
) (bool, uint64, []byte, *warp.UnsignedMessage, error) {
if len(c.Symbol) == 0 {
return false, CreateAssetComputeUnits, OutputSymbolEmpty, nil, nil
}
if len(c.Symbol) > MaxSymbolSize {
return false, CreateAssetComputeUnits, OutputSymbolTooLarge, nil, nil
}
if c.Decimals > MaxDecimals {
return false, CreateAssetComputeUnits, OutputDecimalsTooLarge, nil, nil
}
if len(c.Metadata) == 0 {
return false, CreateAssetComputeUnits, OutputMetadataEmpty, nil, nil
}
if len(c.Metadata) > MaxMetadataSize {
return false, CreateAssetComputeUnits, OutputMetadataTooLarge, nil, nil
}
// It should only be possible to overwrite an existing asset if there is
// a hash collision.
if err := storage.SetAsset(ctx, mu, txID, c.Symbol, c.Decimals, c.Metadata, 0, actor, false); err != nil {
return false, CreateAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
return true, CreateAssetComputeUnits, nil, nil, nil
}
func (*CreateAsset) MaxComputeUnits(chain.Rules) uint64 {
return CreateAssetComputeUnits
}
func (c *CreateAsset) Size() int {
// TODO: add small bytes (smaller int prefix)
return codec.BytesLen(c.Symbol) + consts.Uint8Len + codec.BytesLen(c.Metadata)
}
func (c *CreateAsset) Marshal(p *codec.Packer) {
p.PackBytes(c.Symbol)
p.PackByte(c.Decimals)
p.PackBytes(c.Metadata)
}
func UnmarshalCreateAsset(p *codec.Packer, _ *warp.Message) (chain.Action, error) {
var create CreateAsset
p.UnpackBytes(MaxSymbolSize, true, &create.Symbol)
create.Decimals = p.UnpackByte()
p.UnpackBytes(MaxMetadataSize, true, &create.Metadata)
return &create, p.Err()
}
func (*CreateAsset) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}