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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
use crate::blueprints::pool::v1::constants::*;
use crate::blueprints::pool::v1::errors::multi_resource_pool::*;
use crate::blueprints::pool::v1::events::multi_resource_pool::*;
use crate::blueprints::pool::v1::substates::multi_resource_pool::*;
use crate::internal_prelude::*;
use radix_engine_interface::blueprints::component::*;
use radix_engine_interface::blueprints::pool::*;
use radix_engine_interface::prelude::*;
use radix_engine_interface::*;
use radix_native_sdk::modules::metadata::*;
use radix_native_sdk::modules::role_assignment::*;
use radix_native_sdk::modules::royalty::*;
use radix_native_sdk::resource::*;
use radix_native_sdk::runtime::*;
pub struct MultiResourcePoolBlueprint;
impl MultiResourcePoolBlueprint {
pub fn instantiate<Y: SystemApi<RuntimeError>>(
resource_addresses: IndexSet<ResourceAddress>,
owner_role: OwnerRole,
pool_manager_rule: AccessRule,
address_reservation: Option<GlobalAddressReservation>,
api: &mut Y,
) -> Result<MultiResourcePoolInstantiateOutput, RuntimeError> {
// A pool can't be created where one of the resources is non-fungible - error out if any of
// them are
for resource_address in resource_addresses.iter() {
let resource_manager = ResourceManager(*resource_address);
if let ResourceType::NonFungible { .. } = resource_manager.resource_type(api)? {
return Err(Error::NonFungibleResourcesAreNotAccepted {
resource_address: *resource_address,
}
.into());
}
}
// A multi-resource pool can not be created with no resources - at minimum there should be
// one resource.
if resource_addresses.is_empty() {
return Err(Error::CantCreatePoolWithLessThanOneResource.into());
}
// Allocating the address of the pool - this is going to be needed for the metadata of the
// pool unit resource.
let (address_reservation, address) = {
if let Some(address_reservation) = address_reservation {
let address = api.get_reservation_address(address_reservation.0.as_node_id())?;
(address_reservation, address)
} else {
api.allocate_global_address(BlueprintId {
package_address: POOL_PACKAGE,
blueprint_name: MULTI_RESOURCE_POOL_BLUEPRINT_IDENT.to_string(),
})?
}
};
// Creating the pool unit resource
let pool_unit_resource_manager = {
let component_caller_badge = NonFungibleGlobalId::global_caller_badge(address);
ResourceManager::new_fungible(
owner_role.clone(),
true,
18,
FungibleResourceRoles {
mint_roles: mint_roles! {
minter => rule!(require(component_caller_badge.clone()));
minter_updater => rule!(deny_all);
},
burn_roles: burn_roles! {
burner => rule!(require(component_caller_badge.clone()));
burner_updater => rule!(deny_all);
},
..Default::default()
},
metadata_init! {
"pool" => address, locked;
},
None,
api,
)?
};
// Creating the pool nodes
let role_assignment = RoleAssignment::create(
owner_role,
indexmap! {
ModuleId::Main => roles_init! {
RoleKey { key: POOL_MANAGER_ROLE.to_owned() } => pool_manager_rule;
}
},
api,
)?
.0;
let metadata = Metadata::create_with_data(
metadata_init! {
"pool_vault_number" => resource_addresses.len() as u64, locked;
"pool_resources" => resource_addresses.iter().cloned().map(GlobalAddress::from).collect::<Vec<_>>(), locked;
"pool_unit" => GlobalAddress::from(pool_unit_resource_manager.0), locked;
},
api,
)?;
let royalty = ComponentRoyalty::create(ComponentRoyaltyConfig::default(), api)?;
let object_id = {
let substate = Substate {
vaults: resource_addresses
.into_iter()
.map(|resource_address| {
Vault::create(resource_address, api).map(|vault| (resource_address, vault))
})
.collect::<Result<_, _>>()?,
pool_unit_resource_manager,
};
api.new_simple_object(
MULTI_RESOURCE_POOL_BLUEPRINT_IDENT,
indexmap! {
MultiResourcePoolField::State.field_index() => FieldValue::new(MultiResourcePoolStateFieldPayload::from_content_source(substate)),
},
)?
};
api.globalize(
object_id,
indexmap!(
AttachedModuleId::RoleAssignment => role_assignment.0,
AttachedModuleId::Metadata => metadata.0,
AttachedModuleId::Royalty => royalty.0,
),
Some(address_reservation),
)?;
Ok(Global::new(ComponentAddress::new_or_panic(
address.as_node_id().0,
)))
}
/**
This function calculates the amount of resources that should be contributed to the pool and then
contributes them to the pool returning back a pool unit resource in exchange for the contributed
resources.
The various states that the pool can be in can be outlined within the context laid out in the
docs of the [`TwoResourcePool::contribute`] function, but in a more generalized way.
* State 1: If no pool units exist in circulation then consider the pool to be new. All of the
resources are accepted and an amount of pool units equivalent to the geometric mean of the non
zero contributions is minted.
* State 2: If pool units exist in circulation but all of the reserves are empty then this pool
is in an invalid state, one that requires external intervention from a protected withdraw or
deposit to get out of. This has to do with how we represent the fact that the user owns 100%
of the pool.
* State 3: If pool units exist in circulation and some but not all of the reserves are empty
then contributions will be accepted according to the ratio of resources in the pool and the
contribution of any of the resources with zero reserves will be returned as change.
* State 4: Pool units exist in circulation and none of the vaults are empty, the pool is in
normal operation.
In the case when the pool is operating normally an algorithm is needed to determine the
following:
- Given some resources, how much of these resources can the pool accept.
- Given some resources, how much pool units should the pool mint.
Let r<sub>1</sub>, r<sub>2</sub>, ..., r<sub>n</sub> be the reserves of the resources in the
pool and c<sub>1</sub>, c<sub>2</sub>, ..., c<sub>n</sub> the amounts being contributed to each
of the resources in the pool.
We calculate the ratios of contribution to reserves which is denoted as k<sub>n</sub> where
k<sub>n</sub> = c<sub>n</sub> / r<sub>n</sub> such that we find k<sub>1</sub>, k<sub>2</sub>,
..., k<sub>n</sub>. We then find the minimum value of k denoted as k<sub>min</sub> which gives
us the ratio which can be satisfied by all of the resources provided for contribution.
To determine the amount of resources that should be contributed k<sub>min</sub> is multiplied by
the reserves of each of the resources. This amount is put in the pool's vaults and whatever
amount remains is returned as change.
To determine the amount of pool units to mint k<sub>min</sub> is multiplied by the pool units
total supply.
The following is a minimal example
| n |1 |2 |3 | Note |
|---------------- |:----:|:----:|:----:| -------------------------------- |
| r | 1000 | 2000 | 3000 | |
| c | 2000 | 3000 | 4000 | |
| k | 2 | 1.5 | 1.33 | |
| k<sub>min</sub> | | | 1.33 | |
| ca | 1333 | 2666 | 4000 | Amount of contribution to accept |
[`TwoResourcePool::contribute`]: super::TwoResourcePoolBlueprint::contribute
*/
pub fn contribute<Y: SystemApi<RuntimeError>>(
buckets: Vec<Bucket>,
api: &mut Y,
) -> Result<MultiResourcePoolContributeOutput, RuntimeError> {
Self::with_state(api, |mut substate, api| {
let pool_unit_total_supply = substate
.pool_unit_resource_manager
.total_supply(api)?
.expect("Total supply is always enabled for pool unit resource.");
let pool_unit_total_supply = PreciseDecimal::from(pool_unit_total_supply);
let contribution_information = {
let mut information = substate
.vaults
.iter()
.map(|(resource_address, vault)| -> Result<_, RuntimeError> {
Ok((
*resource_address,
ContributionInformation {
resource_address: *resource_address,
vault: Vault(vault.0),
bucket: Bucket::create(*resource_address, api)?,
reserves: vault.amount(api)?.into(),
contribution: PreciseDecimal::ZERO,
},
))
})
.collect::<Result<IndexMap<_, _>, _>>()?;
for bucket in buckets {
let resource_address = bucket.resource_address(api)?;
if let Some(information) = information.get_mut(&resource_address) {
information.contribution = information
.contribution
.checked_add(bucket.amount(api)?)
.ok_or(Error::DecimalOverflowError)?;
information.bucket.put(bucket, api)?;
} else {
return Err(Error::ResourceDoesNotBelongToPool { resource_address }.into());
}
}
information
};
// New Pool
let mut contributed_resources = index_map_new::<ResourceAddress, Decimal>();
let (pool_units_to_mint, change_buckets) = if pool_unit_total_supply.is_zero() {
// Calculating the pool units to mint through the geometric mean.
let pool_units_to_mint = {
let contributions = contribution_information
.values()
.filter_map(|information| {
if information.contribution.is_zero() {
None
} else {
Some(information.contribution)
}
})
.collect::<Vec<_>>();
let root_order = contributions.len();
// Pool Units to Mint = Geometric Average = root(n, c1 * c2 * ... * cn) Where
// n is the number of non-zero contributions and cn is any non-zero contribution
contributions
.into_iter()
.try_fold(PreciseDecimal::ONE, |accumulator, value| {
value
.checked_nth_root(root_order as u32)
.and_then(|value| value.checked_mul(accumulator))
})
.and_then(|value| value.checked_round(18, RoundingMode::ToPositiveInfinity))
.ok_or(Error::DecimalOverflowError)?
};
for mut information in contribution_information.into_values() {
let amount = information.bucket.amount(api)?;
if !amount.is_zero() {
let entry = contributed_resources
.entry(information.resource_address)
.or_default();
*entry = entry
.checked_add(amount)
.ok_or(Error::DecimalOverflowError)?;
}
information.vault.put(information.bucket, api)?;
}
(pool_units_to_mint, Vec::default())
}
// Not a new Pool
else {
// Calculate the minimum ratio. It is possible for this ratio to be zero. This can
// happen in a number of different cases including very small contributions (e.g.
// 1 atto) with any amount of reserves in the pool. In cases like this, we will just
// end up minting zero pool units and catching that later on.
let minimum_ratio = contribution_information
.values()
.filter_map(|information| {
if !information.reserves.is_zero() {
// There is a possibility that this can overflow if the reserves is not
// zero but is very close to zero (1 atto for example). However, this is
// fine since this would then just return a `None` and would not be one
// of the ratios that we look at to determine the minimum ratio.
information.contribution.checked_div(information.reserves)
} else {
None
}
})
.min()
.ok_or(Error::NoMinimumRatio)?;
// Deposit the buckets into the vaults and then return the change buckets
let change_buckets = contribution_information
.into_values()
.map(|mut information| -> Result<_, RuntimeError> {
let amount_to_contribute = information
.reserves
.checked_mul(minimum_ratio)
.and_then(|value| Decimal::try_from(value).ok())
.ok_or(Error::DecimalOverflowError)?;
let bucket_to_contribute = information.bucket.take_advanced(
amount_to_contribute,
WithdrawStrategy::Rounded(RoundingMode::ToNegativeInfinity),
api,
)?;
let amount_to_contribute = bucket_to_contribute.amount(api)?;
if amount_to_contribute == Decimal::ZERO
&& information.reserves != PreciseDecimal::ZERO
{
return Err(Error::LargerContributionRequiredToMeetRatio.into());
}
information.vault.put(bucket_to_contribute, api)?;
let entry = contributed_resources
.entry(information.resource_address)
.or_default();
*entry = entry
.checked_add(amount_to_contribute)
.ok_or(Error::DecimalOverflowError)?;
let is_empty = information.bucket.is_empty(api)?;
if is_empty {
Bucket(information.bucket.0).drop_empty(api)?;
}
Ok((is_empty, information.bucket))
})
.filter_map(|result| match result {
Ok((is_empty, bucket)) => {
if is_empty {
None
} else {
Some(Ok(bucket))
}
}
Err(error) => Some(Err(error)),
})
.collect::<Result<Vec<_>, _>>()?;
(
pool_unit_total_supply
.checked_mul(minimum_ratio)
.ok_or(Error::DecimalOverflowError)?,
change_buckets,
)
};
let pool_units_to_mint =
Decimal::try_from(pool_units_to_mint).map_err(|_| Error::DecimalOverflowError)?;
if pool_units_to_mint.is_zero() {
return Err(Error::ZeroPoolUnitsMinted.into());
}
let pool_units = substate
.pool_unit_resource_manager
.mint_fungible(pool_units_to_mint, api)?;
Runtime::emit_event(
api,
ContributionEvent {
contributed_resources,
pool_units_minted: pool_units_to_mint,
},
)?;
Ok((pool_units.into(), change_buckets))
})
}
pub fn redeem<Y: SystemApi<RuntimeError>>(
bucket: Bucket,
api: &mut Y,
) -> Result<MultiResourcePoolRedeemOutput, RuntimeError> {
Self::with_state(api, |mut substate, api| {
// Ensure that the passed pool resources are indeed pool resources
let bucket_resource_address = bucket.resource_address(api)?;
if bucket_resource_address != substate.pool_unit_resource_manager.0 {
return Err(Error::InvalidPoolUnitResource {
expected: substate.pool_unit_resource_manager.0,
actual: bucket_resource_address,
}
.into());
}
let pool_units_to_redeem = bucket.amount(api)?;
let pool_units_total_supply = substate
.pool_unit_resource_manager
.total_supply(api)?
.expect("Total supply is always enabled for pool unit resource.");
let mut reserves = index_map_new();
for (resource_address, vault) in substate.vaults.iter() {
let amount = vault.amount(api)?;
let divisibility = ResourceManager(*resource_address).resource_type(api)
.map(|resource_type| {
if let ResourceType::Fungible { divisibility } = resource_type {
divisibility
} else {
panic!("Impossible case, we check for this in the constructor and have a test for this.")
}
})?;
reserves.insert(
*resource_address,
ReserveResourceInformation {
reserves: amount,
divisibility,
},
);
}
let amounts_owed = Self::calculate_amount_owed(
pool_units_to_redeem,
pool_units_total_supply,
reserves,
)?;
bucket.burn(api)?;
Runtime::emit_event(
api,
RedemptionEvent {
redeemed_resources: amounts_owed.clone(),
pool_unit_tokens_redeemed: pool_units_to_redeem,
},
)?;
// The following part does some unwraps and panic-able operations but should never panic
amounts_owed
.into_iter()
.map(|(resource_address, amount)| {
substate
.vaults
.get_mut(&resource_address)
.unwrap()
.take(amount, api)
})
.collect::<Result<Vec<Bucket>, _>>()
})
}
pub fn protected_deposit<Y: SystemApi<RuntimeError>>(
bucket: Bucket,
api: &mut Y,
) -> Result<MultiResourcePoolProtectedDepositOutput, RuntimeError> {
Self::with_state(api, |mut substate, api| {
let resource_address = bucket.resource_address(api)?;
let vault = substate.vaults.get_mut(&resource_address);
if let Some(vault) = vault {
let event = DepositEvent {
amount: bucket.amount(api)?,
resource_address,
};
vault.put(bucket, api)?;
Runtime::emit_event(api, event)?;
Ok(())
} else {
Err(Error::ResourceDoesNotBelongToPool { resource_address }.into())
}
})
}
pub fn protected_withdraw<Y: SystemApi<RuntimeError>>(
resource_address: ResourceAddress,
amount: Decimal,
withdraw_strategy: WithdrawStrategy,
api: &mut Y,
) -> Result<MultiResourcePoolProtectedWithdrawOutput, RuntimeError> {
Self::with_state(api, |mut substate, api| {
let vault = substate.vaults.get_mut(&resource_address);
if let Some(vault) = vault {
let bucket = vault.take_advanced(amount, withdraw_strategy, api)?;
let withdrawn_amount = bucket.amount(api)?;
Runtime::emit_event(
api,
WithdrawEvent {
amount: withdrawn_amount,
resource_address,
},
)?;
Ok(bucket)
} else {
Err(Error::ResourceDoesNotBelongToPool { resource_address }.into())
}
})
}
pub fn get_redemption_value<Y: SystemApi<RuntimeError>>(
amount_of_pool_units: Decimal,
api: &mut Y,
) -> Result<MultiResourcePoolGetRedemptionValueOutput, RuntimeError> {
Self::with_state(api, |substate, api| {
let pool_units_to_redeem = amount_of_pool_units;
let pool_units_total_supply = substate
.pool_unit_resource_manager
.total_supply(api)?
.expect("Total supply is always enabled for pool unit resource.");
if amount_of_pool_units.is_negative()
|| amount_of_pool_units.is_zero()
|| amount_of_pool_units > pool_units_total_supply
{
return Err(Error::InvalidGetRedemptionAmount.into());
}
let mut reserves = index_map_new();
for (resource_address, vault) in substate.vaults.into_iter() {
let amount = vault.amount(api)?;
let divisibility = ResourceManager(resource_address).resource_type(api)
.map(|resource_type| {
if let ResourceType::Fungible { divisibility } = resource_type {
divisibility
} else {
panic!("Impossible case, we check for this in the constructor and have a test for this.")
}
})?;
reserves.insert(
resource_address,
ReserveResourceInformation {
reserves: amount,
divisibility,
},
);
}
Self::calculate_amount_owed(pool_units_to_redeem, pool_units_total_supply, reserves)
})
}
pub fn get_vault_amounts<Y: SystemApi<RuntimeError>>(
api: &mut Y,
) -> Result<TwoResourcePoolGetVaultAmountsOutput, RuntimeError> {
Self::with_state(api, |substate, api| {
substate
.vaults
.into_iter()
.map(|(resource_address, vault)| {
vault.amount(api).map(|amount| (resource_address, amount))
})
.collect::<Result<IndexMap<_, _>, _>>()
})
}
//===================
// Utility Functions
//===================
fn with_state<Y: SystemApi<RuntimeError>, O>(
api: &mut Y,
callback: impl FnOnce(Substate, &mut Y) -> Result<O, RuntimeError>,
) -> Result<O, RuntimeError> {
// Open
let substate_key = MultiResourcePoolField::State.into();
let handle =
api.actor_open_field(ACTOR_STATE_SELF, substate_key, LockFlags::read_only())?;
let substate = api
.field_read_typed::<VersionedMultiResourcePoolState>(handle)?
.fully_update_and_into_latest_version();
// Op
let rtn = callback(substate, api);
// Close
if rtn.is_ok() {
api.field_close(handle)?;
}
rtn
}
fn calculate_amount_owed(
pool_units_to_redeem: Decimal,
pool_units_total_supply: Decimal,
reserves: IndexMap<ResourceAddress, ReserveResourceInformation>,
) -> Result<IndexMap<ResourceAddress, Decimal>, RuntimeError> {
let pool_units_to_redeem = PreciseDecimal::from(pool_units_to_redeem);
let pool_units_total_supply = PreciseDecimal::from(pool_units_total_supply);
reserves
.into_iter()
.map(
|(
resource_address,
ReserveResourceInformation {
divisibility,
reserves,
},
)| {
let reserves = PreciseDecimal::from(reserves);
let amount_owed = pool_units_to_redeem
.checked_div(pool_units_total_supply)
.and_then(|d| d.checked_mul(reserves))
.ok_or(Error::DecimalOverflowError)?;
let amount_owed = Decimal::try_from(amount_owed)
.ok()
.and_then(|value| {
value.checked_round(divisibility, RoundingMode::ToNegativeInfinity)
})
.ok_or(Error::DecimalOverflowError)?;
Ok((resource_address, amount_owed))
},
)
.collect()
}
}
struct ReserveResourceInformation {
reserves: Decimal,
divisibility: u8,
}
#[derive(Debug)]
struct ContributionInformation {
/// The address of the resource.
pub resource_address: ResourceAddress,
/// The vault containing the reserves.
pub vault: Vault,
/// The bucket of the tokens the user wishes to contribute. Might not be contributed in full.
pub bucket: Bucket,
/// The amount of reserves in the vault.
pub reserves: PreciseDecimal,
/// The amount of resources the user wishes to contribute.
pub contribution: PreciseDecimal,
}