-
Notifications
You must be signed in to change notification settings - Fork 12
/
depositcalculator.go
351 lines (294 loc) · 12.4 KB
/
depositcalculator.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//nolint:forcetypeassert
package depositcalculator
import (
"math/big"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/serializer/v2/serix"
iotago "github.com/iotaledger/iota.go/v4"
)
type Options struct {
// UnlockConditions
Address iotago.Address
StorageDepositReturnAddress iotago.Address
HasTimelock bool
ExpirationAddress iotago.Address
StateControllerAddress iotago.Address
GovernorAddress iotago.Address
// Features
SenderAddress iotago.Address
IssuerAddress iotago.Address
MetadataSerializedSize int
StateMetadataSerializedSize int
TagLength int
HasNativeToken bool
BlockIssuerKeys int
StakedAmount iotago.BaseToken
// Immutable Features
ImmutableIssuerAddress iotago.Address
ImmutableMetadataSerializedSize int
}
// WithAddress sets the address for the address unlock condition.
func WithAddress(address iotago.Address) options.Option[Options] {
return func(opts *Options) {
opts.Address = address
}
}
// WithStorageDepositReturnAddress adds a storage deposit return unlock condition and sets the address.
func WithStorageDepositReturnAddress(address iotago.Address) options.Option[Options] {
return func(opts *Options) {
opts.StorageDepositReturnAddress = address
}
}
// WithHasTimelock adds a timelock unlock condition.
func WithHasTimelock() options.Option[Options] {
return func(opts *Options) {
opts.HasTimelock = true
}
}
// WithExpirationAddress adds a expiration unlock condition and sets the address.
func WithExpirationAddress(address iotago.Address) options.Option[Options] {
return func(opts *Options) {
opts.ExpirationAddress = address
}
}
// WithStateControllerAddress sets the address for the state controller address unlock condition.
func WithStateControllerAddress(address iotago.Address) options.Option[Options] {
return func(opts *Options) {
opts.StateControllerAddress = address
}
}
// WithGovernorAddress sets the address for the governor address unlock condition.
func WithGovernorAddress(address iotago.Address) options.Option[Options] {
return func(opts *Options) {
opts.GovernorAddress = address
}
}
// WithSenderAddress adds a sender feature and sets the address.
func WithSenderAddress(address iotago.Address) options.Option[Options] {
return func(opts *Options) {
opts.SenderAddress = address
}
}
// WithIssuerAddress adds a issuer feature and sets the address.
func WithIssuerAddress(address iotago.Address) options.Option[Options] {
return func(opts *Options) {
opts.IssuerAddress = address
}
}
// WithMetadataSerializedSize adds a metadata feature and adds an entry with an empty key and a dummy value
// of the given length minus the length of the map length prefix, key length prefix and value length prefix.
func WithMetadataSerializedSize(size int) options.Option[Options] {
return func(opts *Options) {
opts.MetadataSerializedSize = size
}
}
// WithStateMetadataSerializedSize adds a state metadata feature and adds an entry with an empty key and a dummy value
// of the given length minus the length of the map length prefix, key length prefix and value length prefix.
func WithStateMetadataSerializedSize(size int) options.Option[Options] {
return func(opts *Options) {
opts.StateMetadataSerializedSize = size
}
}
// WithTagLength adds a tag feature and sets a dummy tag with the given length.
func WithTagLength(length int) options.Option[Options] {
return func(opts *Options) {
opts.TagLength = length
}
}
// WithHasNativeToken adds a native token feature.
func WithHasNativeToken() options.Option[Options] {
return func(opts *Options) {
opts.HasNativeToken = true
}
}
// WithBlockIssuerKeys adds a block issuer feature and adds the given amount of dummy keys.
func WithBlockIssuerKeys(keys int) options.Option[Options] {
return func(opts *Options) {
opts.BlockIssuerKeys = keys
}
}
// WithStakedAmount adds a staking feature and sets the staked amount.
func WithStakedAmount(amount iotago.BaseToken) options.Option[Options] {
return func(opts *Options) {
opts.StakedAmount = amount
}
}
// WithImmutableIssuerAddress adds an immutable issuer feature and sets the address.
func WithImmutableIssuerAddress(address iotago.Address) options.Option[Options] {
return func(opts *Options) {
opts.ImmutableIssuerAddress = address
}
}
// WithImmutableMetadataSerializedSize adds an immutable metadata feature and adds an entry with an empty key and a dummy value
// of the given length minus the length of the map length prefix, key length prefix and value length prefix.
func WithImmutableMetadataSerializedSize(size int) options.Option[Options] {
return func(opts *Options) {
opts.ImmutableMetadataSerializedSize = size
}
}
func getUnlockConditions[T iotago.UnlockCondition](outputType iotago.OutputType, opts *Options) iotago.UnlockConditions[T] {
unlockConditions := make(iotago.UnlockConditions[T], 0)
// Mandatory address unlocks
switch outputType {
case iotago.OutputBasic, iotago.OutputAccount, iotago.OutputNFT, iotago.OutputDelegation:
unlockConditions = append(unlockConditions, iotago.UnlockCondition(&iotago.AddressUnlockCondition{Address: opts.Address}).(T))
case iotago.OutputAnchor:
unlockConditions = append(unlockConditions, iotago.UnlockCondition(&iotago.StateControllerAddressUnlockCondition{Address: opts.StateControllerAddress}).(T))
unlockConditions = append(unlockConditions, iotago.UnlockCondition(&iotago.GovernorAddressUnlockCondition{Address: opts.GovernorAddress}).(T))
case iotago.OutputFoundry:
unlockConditions = append(unlockConditions, iotago.UnlockCondition(&iotago.ImmutableAccountUnlockCondition{Address: &iotago.AccountAddress{}}).(T))
}
if opts.StorageDepositReturnAddress != nil {
unlockConditions = append(unlockConditions, iotago.UnlockCondition(&iotago.StorageDepositReturnUnlockCondition{ReturnAddress: opts.StorageDepositReturnAddress}).(T))
}
if opts.HasTimelock {
unlockConditions = append(unlockConditions, iotago.UnlockCondition(&iotago.TimelockUnlockCondition{}).(T))
}
if opts.ExpirationAddress != nil {
unlockConditions = append(unlockConditions, iotago.UnlockCondition(&iotago.ExpirationUnlockCondition{ReturnAddress: opts.ExpirationAddress}).(T))
}
return unlockConditions
}
func getFeatures[T iotago.Feature](opts *Options) iotago.Features[T] {
features := iotago.Features[iotago.Feature]{}
if opts.SenderAddress != nil {
features = append(features, &iotago.SenderFeature{Address: opts.SenderAddress})
}
if opts.IssuerAddress != nil {
features = append(features, &iotago.IssuerFeature{Address: opts.IssuerAddress})
}
if opts.MetadataSerializedSize > 0 {
features = append(features, &iotago.MetadataFeature{
Entries: iotago.MetadataFeatureEntries{
// configured length - map length prefix - key length prefix - value length prefix
"": make([]byte, opts.MetadataSerializedSize-int(serix.LengthPrefixTypeAsByte)+int(serix.LengthPrefixTypeAsByte)+int(serix.LengthPrefixTypeAsUint16)),
},
})
}
if opts.StateMetadataSerializedSize > 0 {
features = append(features, &iotago.StateMetadataFeature{
Entries: iotago.StateMetadataFeatureEntries{
// configured length - map length prefix - key length prefix - value length prefix
"": make([]byte, opts.StateMetadataSerializedSize-int(serix.LengthPrefixTypeAsByte)+int(serix.LengthPrefixTypeAsByte)+int(serix.LengthPrefixTypeAsUint16)),
},
})
}
if opts.TagLength > 0 {
features = append(features, &iotago.TagFeature{Tag: make([]byte, opts.TagLength)})
}
if opts.HasNativeToken {
features = append(features, &iotago.NativeTokenFeature{Amount: big.NewInt(0)})
}
if opts.BlockIssuerKeys > 0 {
blockIssuerKeys := make([]iotago.BlockIssuerKey, 0, opts.BlockIssuerKeys)
for range opts.BlockIssuerKeys {
blockIssuerKeys = append(blockIssuerKeys, &iotago.Ed25519PublicKeyHashBlockIssuerKey{})
}
features = append(features, &iotago.BlockIssuerFeature{BlockIssuerKeys: blockIssuerKeys})
}
if opts.StakedAmount > 0 {
features = append(features, &iotago.StakingFeature{StakedAmount: opts.StakedAmount})
}
feats := iotago.Features[T]{}
for _, feat := range features {
//nolint:forcetypeassert
feats = append(feats, feat.(T))
}
return feats
}
func getImmutableFeatures[T iotago.Feature](opts *Options) iotago.Features[T] {
features := iotago.Features[iotago.Feature]{}
if opts.ImmutableIssuerAddress != nil {
features = append(features, &iotago.IssuerFeature{Address: opts.ImmutableIssuerAddress})
}
if opts.ImmutableMetadataSerializedSize > 0 {
features = append(features, &iotago.MetadataFeature{
Entries: iotago.MetadataFeatureEntries{
// configured length - map length prefix - key length prefix - value length prefix
"": make([]byte, opts.ImmutableMetadataSerializedSize-int(serix.LengthPrefixTypeAsByte)+int(serix.LengthPrefixTypeAsByte)+int(serix.LengthPrefixTypeAsUint16)),
},
})
}
feats := iotago.Features[T]{}
for _, feat := range features {
//nolint:forcetypeassert
feats = append(feats, feat.(T))
}
return feats
}
func MinDeposit(protocolParams iotago.ProtocolParameters, outputType iotago.OutputType, opts ...options.Option[Options]) (iotago.BaseToken, error) {
depositCalculationOptions := options.Apply(&Options{}, opts)
var dummyOutput iotago.Output
switch outputType {
case iotago.OutputBasic:
dummyOutput = &iotago.BasicOutput{
Amount: 0,
Mana: 0,
UnlockConditions: getUnlockConditions[iotago.BasicOutputUnlockCondition](outputType, depositCalculationOptions),
Features: getFeatures[iotago.BasicOutputFeature](depositCalculationOptions),
}
case iotago.OutputAccount:
dummyOutput = &iotago.AccountOutput{
Amount: 0,
Mana: 0,
AccountID: iotago.AccountID{},
FoundryCounter: 0,
UnlockConditions: getUnlockConditions[iotago.AccountOutputUnlockCondition](outputType, depositCalculationOptions),
Features: getFeatures[iotago.AccountOutputFeature](depositCalculationOptions),
ImmutableFeatures: getImmutableFeatures[iotago.AccountOutputImmFeature](depositCalculationOptions),
}
case iotago.OutputAnchor:
dummyOutput = &iotago.AnchorOutput{
Amount: 0,
Mana: 0,
AnchorID: iotago.AnchorID{},
StateIndex: 0,
UnlockConditions: getUnlockConditions[iotago.AnchorOutputUnlockCondition](outputType, depositCalculationOptions),
Features: getFeatures[iotago.AnchorOutputFeature](depositCalculationOptions),
ImmutableFeatures: getImmutableFeatures[iotago.AnchorOutputImmFeature](depositCalculationOptions),
}
case iotago.OutputFoundry:
dummyOutput = &iotago.FoundryOutput{
Amount: 0,
SerialNumber: 0,
TokenScheme: &iotago.SimpleTokenScheme{
MintedTokens: big.NewInt(0),
MeltedTokens: big.NewInt(0),
MaximumSupply: big.NewInt(0),
},
UnlockConditions: getUnlockConditions[iotago.FoundryOutputUnlockCondition](outputType, depositCalculationOptions),
Features: getFeatures[iotago.FoundryOutputFeature](depositCalculationOptions),
ImmutableFeatures: getImmutableFeatures[iotago.FoundryOutputImmFeature](depositCalculationOptions),
}
case iotago.OutputNFT:
dummyOutput = &iotago.NFTOutput{
Amount: 0,
Mana: 0,
NFTID: iotago.NFTID{},
UnlockConditions: getUnlockConditions[iotago.NFTOutputUnlockCondition](outputType, depositCalculationOptions),
Features: getFeatures[iotago.NFTOutputFeature](depositCalculationOptions),
ImmutableFeatures: getImmutableFeatures[iotago.NFTOutputImmFeature](depositCalculationOptions),
}
case iotago.OutputDelegation:
dummyOutput = &iotago.DelegationOutput{
Amount: 0,
DelegatedAmount: 0,
DelegationID: iotago.DelegationID{},
ValidatorAddress: &iotago.AccountAddress{},
StartEpoch: 0,
EndEpoch: 0,
UnlockConditions: getUnlockConditions[iotago.DelegationOutputUnlockCondition](outputType, depositCalculationOptions),
}
}
storageScoreStructure := iotago.NewStorageScoreStructure(protocolParams.StorageScoreParameters())
// calculate the minimum storage deposit
minDeposit, err := storageScoreStructure.MinDeposit(dummyOutput)
if err != nil {
return 0, err
}
// we need to have at least the staked amount in the deposit
if depositCalculationOptions.StakedAmount > minDeposit {
minDeposit = depositCalculationOptions.StakedAmount
}
return minDeposit, nil
}