forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_asset.go
284 lines (266 loc) · 8.66 KB
/
export_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
// 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 = (*ExportAsset)(nil)
type ExportAsset struct {
To codec.Address `json:"to"`
Asset ids.ID `json:"asset"`
Value uint64 `json:"value"`
Return bool `json:"return"`
Reward uint64 `json:"reward"`
SwapIn uint64 `json:"swapIn"`
AssetOut ids.ID `json:"assetOut"`
SwapOut uint64 `json:"swapOut"`
SwapExpiry int64 `json:"swapExpiry"`
Destination ids.ID `json:"destination"`
}
func (*ExportAsset) GetTypeID() uint8 {
return exportAssetID
}
func (e *ExportAsset) StateKeys(actor codec.Address, _ ids.ID) state.Keys {
if e.Return {
return state.Keys{
string(storage.AssetKey(e.Asset)): state.Read | state.Write,
string(storage.BalanceKey(actor, e.Asset)): state.Read | state.Write,
}
}
return state.Keys{
string(storage.AssetKey(e.Asset)): state.Read | state.Write,
string(storage.LoanKey(e.Asset, e.Destination)): state.Read | state.Write,
string(storage.BalanceKey(actor, e.Asset)): state.Read | state.Write,
}
}
func (e *ExportAsset) StateKeysMaxChunks() []uint16 {
if e.Return {
return []uint16{storage.AssetChunks, storage.BalanceChunks}
}
return []uint16{storage.AssetChunks, storage.LoanChunks, storage.BalanceChunks}
}
func (*ExportAsset) OutputsWarpMessage() bool {
return true
}
func (e *ExportAsset) executeReturn(
ctx context.Context,
mu state.Mutable,
actor codec.Address,
txID ids.ID,
) (bool, uint64, []byte, *warp.UnsignedMessage, error) {
exists, symbol, decimals, metadata, supply, _, isWarp, err := storage.GetAsset(ctx, mu, e.Asset)
if err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if !exists {
return false, ExportAssetComputeUnits, OutputAssetMissing, nil, nil
}
if !isWarp {
return false, ExportAssetComputeUnits, OutputNotWarpAsset, nil, nil
}
allowedDestination, err := ids.ToID(metadata[consts.IDLen:])
if err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if allowedDestination != e.Destination {
return false, ExportAssetComputeUnits, OutputWrongDestination, nil, nil
}
newSupply, err := smath.Sub(supply, e.Value)
if err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
newSupply, err = smath.Sub(newSupply, e.Reward)
if err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if newSupply > 0 {
if err := storage.SetAsset(ctx, mu, e.Asset, symbol, decimals, metadata, newSupply, codec.EmptyAddress, true); err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
} else {
if err := storage.DeleteAsset(ctx, mu, e.Asset); err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
}
if err := storage.SubBalance(ctx, mu, actor, e.Asset, e.Value); err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if e.Reward > 0 {
if err := storage.SubBalance(ctx, mu, actor, e.Asset, e.Reward); err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
}
originalAsset, err := ids.ToID(metadata[:consts.IDLen])
if err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
wt := &WarpTransfer{
To: e.To,
Symbol: symbol,
Decimals: decimals,
Asset: originalAsset,
Value: e.Value,
Return: e.Return,
Reward: e.Reward,
SwapIn: e.SwapIn,
AssetOut: e.AssetOut,
SwapOut: e.SwapOut,
SwapExpiry: e.SwapExpiry,
TxID: txID,
DestinationChainID: e.Destination,
}
payload, err := wt.Marshal()
if err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
wm := &warp.UnsignedMessage{
// NetworkID + SourceChainID is populated by hypersdk
Payload: payload,
}
return true, ExportAssetComputeUnits, nil, wm, nil
}
func (e *ExportAsset) executeLoan(
ctx context.Context,
mu state.Mutable,
actor codec.Address,
txID ids.ID,
) (bool, uint64, []byte, *warp.UnsignedMessage, error) {
exists, symbol, decimals, _, _, _, isWarp, err := storage.GetAsset(ctx, mu, e.Asset)
if err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if !exists {
return false, ExportAssetComputeUnits, OutputAssetMissing, nil, nil
}
if isWarp {
// Cannot export an asset if it was warped in and not returning
return false, ExportAssetComputeUnits, OutputWarpAsset, nil, nil
}
if err := storage.AddLoan(ctx, mu, e.Asset, e.Destination, e.Value); err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if err := storage.SubBalance(ctx, mu, actor, e.Asset, e.Value); err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if e.Reward > 0 {
if err := storage.AddLoan(ctx, mu, e.Asset, e.Destination, e.Reward); err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
if err := storage.SubBalance(ctx, mu, actor, e.Asset, e.Reward); err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
}
wt := &WarpTransfer{
To: e.To,
Symbol: symbol,
Decimals: decimals,
Asset: e.Asset,
Value: e.Value,
Return: e.Return,
Reward: e.Reward,
SwapIn: e.SwapIn,
AssetOut: e.AssetOut,
SwapOut: e.SwapOut,
SwapExpiry: e.SwapExpiry,
TxID: txID,
DestinationChainID: e.Destination,
}
payload, err := wt.Marshal()
if err != nil {
return false, ExportAssetComputeUnits, utils.ErrBytes(err), nil, nil
}
wm := &warp.UnsignedMessage{
// NetworkID + SourceChainID is populated by hypersdk
Payload: payload,
}
return true, ExportAssetComputeUnits, nil, wm, nil
}
func (e *ExportAsset) Execute(
ctx context.Context,
_ chain.Rules,
mu state.Mutable,
_ int64,
actor codec.Address,
txID ids.ID,
_ bool,
) (bool, uint64, []byte, *warp.UnsignedMessage, error) {
if e.Value == 0 {
return false, ExportAssetComputeUnits, OutputValueZero, nil, nil
}
if e.Destination == ids.Empty {
// This would result in multiplying balance export by whoever imports the
// transaction.
return false, ExportAssetComputeUnits, OutputAnycast, nil, nil
}
// TODO: check if destination is ourselves
if e.Return {
return e.executeReturn(ctx, mu, actor, txID)
}
return e.executeLoan(ctx, mu, actor, txID)
}
func (*ExportAsset) MaxComputeUnits(chain.Rules) uint64 {
return ExportAssetComputeUnits
}
func (*ExportAsset) Size() int {
return codec.AddressLen + consts.IDLen +
consts.Uint64Len + consts.BoolLen +
consts.Uint64Len + /* op bits */
consts.Uint64Len + consts.Uint64Len + consts.IDLen + consts.Uint64Len +
consts.Int64Len + consts.IDLen
}
func (e *ExportAsset) Marshal(p *codec.Packer) {
p.PackAddress(e.To)
p.PackID(e.Asset)
p.PackUint64(e.Value)
p.PackBool(e.Return)
op := codec.NewOptionalWriter(consts.Uint64Len*3 + consts.Int64Len + consts.IDLen)
op.PackUint64(e.Reward)
op.PackUint64(e.SwapIn)
op.PackID(e.AssetOut)
op.PackUint64(e.SwapOut)
op.PackInt64(e.SwapExpiry)
p.PackOptional(op)
p.PackID(e.Destination)
}
func UnmarshalExportAsset(p *codec.Packer, _ *warp.Message) (chain.Action, error) {
var export ExportAsset
p.UnpackAddress(&export.To)
p.UnpackID(false, &export.Asset) // may export native
export.Value = p.UnpackUint64(true)
export.Return = p.UnpackBool()
op := p.NewOptionalReader()
export.Reward = op.UnpackUint64() // reward not required
export.SwapIn = op.UnpackUint64() // optional
op.UnpackID(&export.AssetOut)
export.SwapOut = op.UnpackUint64()
export.SwapExpiry = op.UnpackInt64()
op.Done()
p.UnpackID(true, &export.Destination)
if err := p.Err(); err != nil {
return nil, err
}
// Handle swap checks
if !ValidSwapParams(
export.Value,
export.SwapIn,
export.AssetOut,
export.SwapOut,
export.SwapExpiry,
) {
return nil, chain.ErrInvalidObject
}
return &export, nil
}
func (*ExportAsset) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}