forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_asset.go
292 lines (268 loc) · 8.43 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package actions
import (
"bytes"
"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 = (*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(actor codec.Address, _ ids.ID) []string {
var (
keys []string
assetID ids.ID
)
if i.warpTransfer.Return {
assetID = i.warpTransfer.Asset
keys = []string{
string(storage.AssetKey(i.warpTransfer.Asset)),
string(storage.LoanKey(i.warpTransfer.Asset, i.warpMessage.SourceChainID)),
string(storage.BalanceKey(i.warpTransfer.To, i.warpTransfer.Asset)),
}
} else {
assetID = ImportedAssetID(i.warpTransfer.Asset, i.warpMessage.SourceChainID)
keys = []string{
string(storage.AssetKey(assetID)),
string(storage.BalanceKey(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, string(storage.BalanceKey(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, string(storage.BalanceKey(actor, i.warpTransfer.AssetOut)))
keys = append(keys, string(storage.BalanceKey(actor, assetID)))
keys = append(keys, string(storage.BalanceKey(i.warpTransfer.To, i.warpTransfer.AssetOut)))
}
return keys
}
func (i *ImportAsset) StateKeysMaxChunks() []uint16 {
// Can't use [warpTransfer] because it may not be populated yet
chunks := []uint16{}
chunks = append(chunks, storage.LoanChunks)
chunks = append(chunks, storage.AssetChunks)
chunks = append(chunks, storage.BalanceChunks)
// If the [warpTransfer] specified a reward, we add the state key to make
// sure it is paid.
chunks = append(chunks, storage.BalanceChunks)
// If the [warpTransfer] requests a swap, we add the state keys to transfer
// the required balances.
if i.Fill {
chunks = append(chunks, storage.BalanceChunks)
chunks = append(chunks, storage.BalanceChunks)
chunks = append(chunks, storage.BalanceChunks)
}
return chunks
}
func (*ImportAsset) OutputsWarpMessage() bool {
return false
}
func (i *ImportAsset) executeMint(
ctx context.Context,
mu state.Mutable,
actor codec.Address,
) []byte {
asset := ImportedAssetID(i.warpTransfer.Asset, i.warpMessage.SourceChainID)
exists, symbol, decimals, metadata, supply, _, warp, err := storage.GetAsset(ctx, mu, asset)
if err != nil {
return utils.ErrBytes(err)
}
if exists && !warp {
// Should not be possible
return OutputConflictingAsset
}
if !exists {
symbol = i.warpTransfer.Symbol
decimals = i.warpTransfer.Decimals
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, mu, asset, symbol, decimals, metadata, newSupply, codec.EmptyAddress, true); err != nil {
return utils.ErrBytes(err)
}
if err := storage.AddBalance(ctx, mu, i.warpTransfer.To, asset, i.warpTransfer.Value, true); err != nil {
return utils.ErrBytes(err)
}
if i.warpTransfer.Reward > 0 {
if err := storage.AddBalance(ctx, mu, actor, asset, i.warpTransfer.Reward, true); err != nil {
return utils.ErrBytes(err)
}
}
return nil
}
func (i *ImportAsset) executeReturn(
ctx context.Context,
mu state.Mutable,
actor codec.Address,
) []byte {
exists, symbol, decimals, _, _, _, warp, err := storage.GetAsset(ctx, mu, i.warpTransfer.Asset)
if err != nil {
return utils.ErrBytes(err)
}
if !exists {
return OutputAssetMissing
}
if !bytes.Equal(i.warpTransfer.Symbol, symbol) {
return OutputSymbolIncorrect
}
if i.warpTransfer.Decimals != decimals {
return OutputDecimalsIncorrect
}
if warp {
return OutputWarpAsset
}
if err := storage.SubLoan(
ctx, mu, i.warpTransfer.Asset,
i.warpMessage.SourceChainID, i.warpTransfer.Value,
); err != nil {
return utils.ErrBytes(err)
}
if err := storage.AddBalance(
ctx, mu, i.warpTransfer.To,
i.warpTransfer.Asset, i.warpTransfer.Value,
true,
); err != nil {
return utils.ErrBytes(err)
}
if i.warpTransfer.Reward > 0 {
if err := storage.SubLoan(
ctx, mu, i.warpTransfer.Asset,
i.warpMessage.SourceChainID, i.warpTransfer.Reward,
); err != nil {
return utils.ErrBytes(err)
}
if err := storage.AddBalance(
ctx, mu, actor,
i.warpTransfer.Asset, i.warpTransfer.Reward,
true,
); err != nil {
return utils.ErrBytes(err)
}
}
return nil
}
func (i *ImportAsset) Execute(
ctx context.Context,
r chain.Rules,
mu state.Mutable,
t int64,
actor codec.Address,
_ ids.ID,
warpVerified bool,
) (bool, uint64, []byte, *warp.UnsignedMessage, error) {
if !warpVerified {
return false, ImportAssetComputeUnits, OutputWarpVerificationFailed, nil, nil
}
if i.warpTransfer.DestinationChainID != r.ChainID() {
return false, ImportAssetComputeUnits, OutputInvalidDestination, nil, nil
}
if i.warpTransfer.Value == 0 {
return false, ImportAssetComputeUnits, OutputValueZero, nil, nil
}
var output []byte
if i.warpTransfer.Return {
output = i.executeReturn(ctx, mu, actor)
} else {
output = i.executeMint(ctx, mu, actor)
}
if len(output) > 0 {
return false, ImportAssetComputeUnits, output, nil, nil
}
if i.warpTransfer.SwapIn == 0 {
// We are ensured that [i.Fill] is false here because of logic in unmarshal
return true, ImportAssetComputeUnits, nil, nil, nil
}
if !i.Fill {
if i.warpTransfer.SwapExpiry > t {
return false, ImportAssetComputeUnits, OutputMustFill, nil, nil
}
return true, ImportAssetComputeUnits, nil, nil, 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, mu, i.warpTransfer.To, assetIn, i.warpTransfer.SwapIn); err != nil {
return false, ImportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if err := storage.AddBalance(ctx, mu, actor, assetIn, i.warpTransfer.SwapIn, true); err != nil {
return false, ImportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if err := storage.SubBalance(ctx, mu, actor, i.warpTransfer.AssetOut, i.warpTransfer.SwapOut); err != nil {
return false, ImportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if err := storage.AddBalance(ctx, mu, i.warpTransfer.To, i.warpTransfer.AssetOut, i.warpTransfer.SwapOut, true); err != nil {
return false, ImportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
return true, ImportAssetComputeUnits, nil, nil, nil
}
func (*ImportAsset) MaxComputeUnits(chain.Rules) uint64 {
return ImportAssetComputeUnits
}
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
}