-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathimport_asset.go
253 lines (233 loc) · 7.45 KB
/
import_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// 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/crypto"
"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 = (*ImportAsset)(nil)
type ImportAsset struct {
// Fill indicates if the actor wishes to fill the order request in the warp
// message. This must be true if the warp message is in a block with
// a timestamp < [SwapExpiry].
Fill bool `json:"fill"`
// warpTransfer is parsed from the inner *warp.Message
warpTransfer *WarpTransfer
// warpMessage is the full *warp.Message parsed from [chain.Transaction]
warpMessage *warp.Message
}
func (*ImportAsset) GetTypeID() uint8 {
return importAssetID
}
func (i *ImportAsset) StateKeys(rauth chain.Auth, _ ids.ID) [][]byte {
var (
keys [][]byte
assetID ids.ID
actor = auth.GetActor(rauth)
)
if i.warpTransfer.Return {
assetID = i.warpTransfer.Asset
keys = [][]byte{
storage.PrefixLoanKey(i.warpTransfer.Asset, i.warpMessage.SourceChainID),
storage.PrefixBalanceKey(i.warpTransfer.To, i.warpTransfer.Asset),
}
} else {
assetID = ImportedAssetID(i.warpTransfer.Asset, i.warpMessage.SourceChainID)
keys = [][]byte{
storage.PrefixAssetKey(assetID),
storage.PrefixBalanceKey(i.warpTransfer.To, assetID),
}
}
// If the [warpTransfer] specified a reward, we add the state key to make
// sure it is paid.
if i.warpTransfer.Reward > 0 {
keys = append(keys, storage.PrefixBalanceKey(actor, assetID))
}
// If the [warpTransfer] requests a swap, we add the state keys to transfer
// the required balances.
if i.Fill && i.warpTransfer.SwapIn > 0 {
keys = append(keys, storage.PrefixBalanceKey(actor, i.warpTransfer.AssetOut))
keys = append(keys, storage.PrefixBalanceKey(actor, assetID))
keys = append(keys, storage.PrefixBalanceKey(i.warpTransfer.To, i.warpTransfer.AssetOut))
}
return keys
}
func (i *ImportAsset) executeMint(
ctx context.Context,
db chain.Database,
actor crypto.PublicKey,
) []byte {
asset := ImportedAssetID(i.warpTransfer.Asset, i.warpMessage.SourceChainID)
exists, metadata, supply, _, warp, err := storage.GetAsset(ctx, db, asset)
if err != nil {
return utils.ErrBytes(err)
}
if exists && !warp {
// Should not be possible
return OutputConflictingAsset
}
if !exists {
metadata = ImportedAssetMetadata(i.warpTransfer.Asset, i.warpMessage.SourceChainID)
}
newSupply, err := smath.Add64(supply, i.warpTransfer.Value)
if err != nil {
return utils.ErrBytes(err)
}
newSupply, err = smath.Add64(newSupply, i.warpTransfer.Reward)
if err != nil {
return utils.ErrBytes(err)
}
if err := storage.SetAsset(ctx, db, asset, metadata, newSupply, crypto.EmptyPublicKey, true); err != nil {
return utils.ErrBytes(err)
}
if err := storage.AddBalance(ctx, db, i.warpTransfer.To, asset, i.warpTransfer.Value); err != nil {
return utils.ErrBytes(err)
}
if i.warpTransfer.Reward > 0 {
if err := storage.AddBalance(ctx, db, actor, asset, i.warpTransfer.Reward); err != nil {
return utils.ErrBytes(err)
}
}
return nil
}
func (i *ImportAsset) executeReturn(
ctx context.Context,
db chain.Database,
actor crypto.PublicKey,
) []byte {
if err := storage.SubLoan(
ctx, db, i.warpTransfer.Asset,
i.warpMessage.SourceChainID, i.warpTransfer.Value,
); err != nil {
return utils.ErrBytes(err)
}
if err := storage.AddBalance(
ctx, db, i.warpTransfer.To,
i.warpTransfer.Asset, i.warpTransfer.Value,
); err != nil {
return utils.ErrBytes(err)
}
if i.warpTransfer.Reward > 0 {
if err := storage.SubLoan(
ctx, db, i.warpTransfer.Asset,
i.warpMessage.SourceChainID, i.warpTransfer.Reward,
); err != nil {
return utils.ErrBytes(err)
}
if err := storage.AddBalance(
ctx, db, actor,
i.warpTransfer.Asset, i.warpTransfer.Reward,
); err != nil {
return utils.ErrBytes(err)
}
}
return nil
}
func (i *ImportAsset) Execute(
ctx context.Context,
r chain.Rules,
db chain.Database,
t int64,
rauth chain.Auth,
_ ids.ID,
warpVerified bool,
) (*chain.Result, error) {
actor := auth.GetActor(rauth)
unitsUsed := i.MaxUnits(r) // max units == units
if !warpVerified {
return &chain.Result{
Success: false,
Units: unitsUsed,
Output: OutputWarpVerificationFailed,
}, nil
}
if i.warpTransfer.DestinationChainID != r.ChainID() {
return &chain.Result{Success: false, Units: unitsUsed, Output: OutputInvalidDestination}, nil
}
if i.warpTransfer.Value == 0 {
return &chain.Result{Success: false, Units: unitsUsed, Output: OutputValueZero}, nil
}
var output []byte
if i.warpTransfer.Return {
output = i.executeReturn(ctx, db, actor)
} else {
output = i.executeMint(ctx, db, actor)
}
if len(output) > 0 {
return &chain.Result{Success: false, Units: unitsUsed, Output: output}, nil
}
if i.warpTransfer.SwapIn == 0 {
// We are ensured that [i.Fill] is false here because of logic in unmarshal
return &chain.Result{Success: true, Units: unitsUsed}, nil
}
if !i.Fill {
if i.warpTransfer.SwapExpiry > t {
return &chain.Result{Success: false, Units: unitsUsed, Output: OutputMustFill}, nil
}
return &chain.Result{Success: true, Units: unitsUsed}, nil
}
// TODO: charge more if swap is performed
var assetIn ids.ID
if i.warpTransfer.Return {
assetIn = i.warpTransfer.Asset
} else {
assetIn = ImportedAssetID(i.warpTransfer.Asset, i.warpMessage.SourceChainID)
}
if err := storage.SubBalance(ctx, db, i.warpTransfer.To, assetIn, i.warpTransfer.SwapIn); err != nil {
return &chain.Result{Success: false, Units: unitsUsed, Output: utils.ErrBytes(err)}, nil
}
if err := storage.AddBalance(ctx, db, actor, assetIn, i.warpTransfer.SwapIn); err != nil {
return &chain.Result{Success: false, Units: unitsUsed, Output: utils.ErrBytes(err)}, nil
}
if err := storage.SubBalance(ctx, db, actor, i.warpTransfer.AssetOut, i.warpTransfer.SwapOut); err != nil {
return &chain.Result{Success: false, Units: unitsUsed, Output: utils.ErrBytes(err)}, nil
}
if err := storage.AddBalance(ctx, db, i.warpTransfer.To, i.warpTransfer.AssetOut, i.warpTransfer.SwapOut); err != nil {
return &chain.Result{Success: false, Units: unitsUsed, Output: utils.ErrBytes(err)}, nil
}
return &chain.Result{Success: true, Units: unitsUsed}, nil
}
func (i *ImportAsset) MaxUnits(chain.Rules) uint64 {
return uint64(len(i.warpMessage.Payload)) + consts.BoolLen
}
func (*ImportAsset) Size() int {
return consts.BoolLen
}
// All we encode that is action specific for now is the type byte from the
// registry.
func (i *ImportAsset) Marshal(p *codec.Packer) {
p.PackBool(i.Fill)
}
func UnmarshalImportAsset(p *codec.Packer, wm *warp.Message) (chain.Action, error) {
var (
imp ImportAsset
err error
)
imp.Fill = p.UnpackBool()
if err := p.Err(); err != nil {
return nil, err
}
imp.warpMessage = wm
imp.warpTransfer, err = UnmarshalWarpTransfer(imp.warpMessage.Payload)
if err != nil {
return nil, err
}
// Ensure we can fill the swap if it exists
if imp.Fill && imp.warpTransfer.SwapIn == 0 {
return nil, ErrNoSwapToFill
}
return &imp, nil
}
func (*ImportAsset) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}