@@ -13,13 +13,13 @@ blueprint!{
13
13
/// and more dynamic way of manipulating and making use of the vaults inside the liquidity pool. Keep in mind
14
14
/// that this `vaults` hashmap will always have exactly two vaults for the two assets being traded against one
15
15
/// another.
16
- vaults: HashMap <Address , Vault >,
16
+ vaults: HashMap <ResourceAddress , Vault >,
17
17
18
18
/// When liquidity providers provide liquidity to the liquidity pool, they are given a number of tokens that is
19
19
/// equivalent to the percentage ownership that they have in the liquidity pool. The tracking token is the token
20
20
/// that the liquidity providers are given when they provide liquidity to the pool and this is the resource
21
- /// definition of the token.
22
- tracking_token_def : ResourceDef ,
21
+ /// address of the token.
22
+ tracking_token_address : ResourceAddress ,
23
23
24
24
/// The tracking tokens are mutable supply tokens that may be minted and burned when liquidity is supplied or
25
25
/// removed from the liquidity pool. This badge is the badge that has the authority to mint and burn the tokens
@@ -66,19 +66,19 @@ blueprint!{
66
66
token1: Bucket ,
67
67
token2: Bucket ,
68
68
fee_to_pool: Decimal
69
- ) -> ( Component , Bucket ) {
69
+ ) -> ( ComponentAddress , Bucket ) {
70
70
// Performing the checks to see if this liquidity pool may be created or not.
71
71
assert_ne!(
72
72
token1. resource_address( ) , token2. resource_address( ) ,
73
73
"[Pool Creation]: Liquidity pools may only be created between two different tokens."
74
74
) ;
75
75
76
76
assert_ne!(
77
- token1. resource_def ( ) . resource_type( ) , ResourceType :: NonFungible ,
77
+ borrow_resource_manager! ( token1. resource_address ( ) ) . resource_type( ) , ResourceType :: NonFungible ,
78
78
"[Pool Creation]: Both assets must be fungible."
79
79
) ;
80
80
assert_ne!(
81
- token2. resource_def ( ) . resource_type( ) , ResourceType :: NonFungible ,
81
+ borrow_resource_manager! ( token2. resource_address ( ) ) . resource_type( ) , ResourceType :: NonFungible ,
82
82
"[Pool Creation]: Both assets must be fungible."
83
83
) ;
84
84
@@ -96,7 +96,7 @@ blueprint!{
96
96
97
97
// Sorting the buckets and then creating the hashmap of the vaults from the sorted buckets
98
98
let ( bucket1, bucket2) : ( Bucket , Bucket ) = sort_buckets( token1, token2) ;
99
- let addresses: ( Address , Address ) = ( bucket1. resource_address( ) , bucket2. resource_address( ) ) ;
99
+ let addresses: ( ResourceAddress , ResourceAddress ) = ( bucket1. resource_address( ) , bucket2. resource_address( ) ) ;
100
100
101
101
let lp_id: String = format!( "{}-{}" , addresses. 0 , addresses. 1 ) ;
102
102
let pair_name: String = address_pair_symbol( addresses. 0 , addresses. 1 ) ;
@@ -106,36 +106,40 @@ blueprint!{
106
106
lp_id, pair_name, bucket1. amount( ) , bucket2. amount( )
107
107
) ;
108
108
109
- let mut vaults: HashMap <Address , Vault > = HashMap :: new( ) ;
109
+ let mut vaults: HashMap <ResourceAddress , Vault > = HashMap :: new( ) ;
110
110
vaults. insert( bucket1. resource_address( ) , Vault :: with_bucket( bucket1) ) ;
111
111
vaults. insert( bucket2. resource_address( ) , Vault :: with_bucket( bucket2) ) ;
112
112
113
113
// Creating the admin badge of the liquidity pool which will be given the authority to mint and burn the
114
114
// tracking tokens issued to the liquidity providers.
115
- let tracking_token_admin_badge: Bucket = ResourceBuilder :: new_fungible( DIVISIBILITY_NONE )
115
+ let tracking_token_admin_badge: Bucket = ResourceBuilder :: new_fungible( )
116
+ . divisibility( DIVISIBILITY_NONE )
116
117
. metadata( "name" , "Tracking Token Admin Badge" )
117
118
. metadata( "symbol" , "TTAB" )
118
119
. metadata( "description" , "This is an admin badge that has the authority to mint and burn tracking tokens" )
119
120
. metadata( "lp_id" , format!( "{}" , lp_id) )
120
- . initial_supply_fungible ( 1 ) ;
121
+ . initial_supply ( 1 ) ;
121
122
122
123
// Creating the tracking tokens and minting the amount owed to the initial liquidity provider
123
- let tracking_tokens: Bucket = ResourceBuilder :: new_fungible( DIVISIBILITY_MAXIMUM )
124
+ let tracking_tokens: Bucket = ResourceBuilder :: new_fungible( )
125
+ . divisibility( DIVISIBILITY_MAXIMUM )
124
126
. metadata( "name" , format!( "{} LP Tracking Token" , pair_name) )
125
127
. metadata( "symbol" , "TT" )
126
128
. metadata( "description" , "A tracking token used to track the percentage ownership of liquidity providers over the liquidity pool" )
127
129
. metadata( "lp_id" , format!( "{}" , lp_id) )
128
- . flags ( MINTABLE | BURNABLE )
129
- . badge ( tracking_token_admin_badge. resource_address( ) , MAY_MINT | MAY_BURN )
130
- . initial_supply_fungible ( 100 ) ;
130
+ . mintable ( rule! ( require ( tracking_token_admin_badge . resource_address ( ) ) ) , LOCKED )
131
+ . burnable ( rule! ( require ( tracking_token_admin_badge. resource_address( ) ) ) , LOCKED )
132
+ . initial_supply ( 100 ) ;
131
133
132
134
// Creating the liquidity pool component and instantiating it
133
- let liquidity_pool: Component = Self {
135
+ let liquidity_pool: ComponentAddress = Self {
134
136
vaults: vaults,
135
- tracking_token_def : tracking_tokens. resource_def ( ) ,
137
+ tracking_token_address : tracking_tokens. resource_address ( ) ,
136
138
tracking_token_admin_badge: Vault :: with_bucket( tracking_token_admin_badge) ,
137
139
fee_to_pool: fee_to_pool,
138
- } . instantiate( ) ;
140
+ }
141
+ . instantiate( )
142
+ . globalize( ) ;
139
143
140
144
return ( liquidity_pool, tracking_tokens) ;
141
145
}
@@ -147,14 +151,14 @@ blueprint!{
147
151
///
148
152
/// # Arguments:
149
153
///
150
- /// * `address` (Address ) - The address of the resource that we wish to check if it belongs to the pool.
154
+ /// * `address` (ResourceAddress ) - The address of the resource that we wish to check if it belongs to the pool.
151
155
///
152
156
/// # Returns:
153
157
///
154
158
/// * `bool` - A boolean of whether the address belongs to this pool or not.
155
159
pub fn belongs_to_pool(
156
160
& self ,
157
- address: Address
161
+ address: ResourceAddress
158
162
) -> bool {
159
163
return self . vaults. contains_key( & address) ;
160
164
}
@@ -166,13 +170,13 @@ blueprint!{
166
170
///
167
171
/// # Arguments:
168
172
///
169
- /// * `address` (Address ) - The address of the resource that we wish to check if it belongs to the pool.
173
+ /// * `address` (ResourceAddress ) - The address of the resource that we wish to check if it belongs to the pool.
170
174
/// * `label` (String) - The label of the method that called this assert method. As an example, if the swap
171
175
/// method were to call this method, then the label would be `Swap` so that it's clear where the assertion error
172
176
/// took place.
173
177
pub fn assert_belongs_to_pool(
174
178
& self ,
175
- address: Address ,
179
+ address: ResourceAddress ,
176
180
label: String
177
181
) {
178
182
assert!(
@@ -182,13 +186,13 @@ blueprint!{
182
186
) ;
183
187
}
184
188
185
- /// Gets the resource addresses of the tokens in this liquidity pool and returns them as a `Vec<Address >`.
189
+ /// Gets the resource addresses of the tokens in this liquidity pool and returns them as a `Vec<ResourceAddress >`.
186
190
///
187
191
/// # Returns:
188
192
///
189
- /// `Vec<Address >` - A vector of the resource addresses of the tokens in this liquidity pool.
190
- pub fn addresses( & self ) -> Vec <Address > {
191
- return self . vaults. keys( ) . cloned( ) . collect:: <Vec <Address >>( ) ;
193
+ /// `Vec<ResourceAddress >` - A vector of the resource addresses of the tokens in this liquidity pool.
194
+ pub fn addresses( & self ) -> Vec <ResourceAddress > {
195
+ return self . vaults. keys( ) . cloned( ) . collect:: <Vec <ResourceAddress >>( ) ;
192
196
}
193
197
194
198
/// Gets the name of the given liquidity pool from the symbols of the two tokens.
@@ -197,7 +201,7 @@ blueprint!{
197
201
///
198
202
/// `String` - A string of the pair symbol
199
203
pub fn name( & self ) -> String {
200
- let addresses: Vec <Address > = self . addresses( ) ;
204
+ let addresses: Vec <ResourceAddress > = self . addresses( ) ;
201
205
return address_pair_symbol( addresses[ 0 ] , addresses[ 1 ] ) ;
202
206
}
203
207
@@ -212,20 +216,20 @@ blueprint!{
212
216
///
213
217
/// # Arguments
214
218
///
215
- /// * `resource_address` (Address ) - The resource address for a token from the pool.
219
+ /// * `resource_address` (ResourceAddress ) - The resource address for a token from the pool.
216
220
///
217
221
/// # Returns:
218
222
///
219
- /// * `Address ` - The address of the other token in this pool.
223
+ /// * `ResourceAddress ` - The address of the other token in this pool.
220
224
pub fn other_resource_address(
221
225
& self ,
222
- resource_address: Address
223
- ) -> Address {
226
+ resource_address: ResourceAddress
227
+ ) -> ResourceAddress {
224
228
// Checking if the passed resource address belongs to this pool.
225
- self . assert_belongs_to_pool( resource_address, String :: from( "Other Resource Address " ) ) ;
229
+ self . assert_belongs_to_pool( resource_address, String :: from( "Other Resource ResourceAddress " ) ) ;
226
230
227
231
// Checking which of the addresses was provided as an argument and returning the other address.
228
- let addresses: Vec <Address > = self . addresses( ) ;
232
+ let addresses: Vec <ResourceAddress > = self . addresses( ) ;
229
233
return if addresses[ 0 ] == resource_address { addresses[ 1 ] } else { addresses[ 0 ] } ;
230
234
}
231
235
@@ -235,7 +239,7 @@ blueprint!{
235
239
///
236
240
/// `Decimal` - A decimal value of the reserves amount of Token A and Token B multiplied by one another.
237
241
pub fn k( & self ) -> Decimal {
238
- let addresses: Vec <Address > = self . addresses( ) ;
242
+ let addresses: Vec <ResourceAddress > = self . addresses( ) ;
239
243
return self . vaults[ & addresses[ 0 ] ] . amount( ) * self . vaults[ & addresses[ 1 ] ] . amount( )
240
244
}
241
245
@@ -250,7 +254,7 @@ blueprint!{
250
254
///
251
255
/// # Arguments:
252
256
///
253
- /// * `input_resource_address` (Address ) - The resource address of the input token.
257
+ /// * `input_resource_address` (ResourceAddress ) - The resource address of the input token.
254
258
/// * `input_amount` (Decimal) - The amount of input tokens to calculate the output for.
255
259
///
256
260
/// # Returns:
@@ -269,7 +273,7 @@ blueprint!{
269
273
/// * `r` - The fee modifier where `r = (100 - fee) / 100`
270
274
pub fn calculate_output_amount(
271
275
& self ,
272
- input_resource_address: Address ,
276
+ input_resource_address: ResourceAddress ,
273
277
input_amount: Decimal
274
278
) -> Decimal {
275
279
// Checking if the passed resource address belongs to this pool.
@@ -295,7 +299,7 @@ blueprint!{
295
299
///
296
300
/// # Arguments:
297
301
///
298
- /// * `output_resource_address` (Address ) - The resource address of the output token.
302
+ /// * `output_resource_address` (ResourceAddress ) - The resource address of the output token.
299
303
/// * `output_amount` (Decimal) - The amount of output tokens to calculate the input for.
300
304
///
301
305
/// # Returns:
@@ -314,7 +318,7 @@ blueprint!{
314
318
/// * `r` - The fee modifier where `r = (100 - fee) / 100`
315
319
pub fn calculate_input_amount(
316
320
& self ,
317
- output_resource_address: Address ,
321
+ output_resource_address: ResourceAddress ,
318
322
output_amount: Decimal
319
323
) -> Decimal {
320
324
// Checking if the passed resource address belongs to this pool.
@@ -363,15 +367,15 @@ blueprint!{
363
367
///
364
368
/// # Arguments:
365
369
///
366
- /// * `resource_address` (Address ) - The address of the resource to withdraw from the liquidity pool.
370
+ /// * `resource_address` (ResourceAddress ) - The address of the resource to withdraw from the liquidity pool.
367
371
/// * `amount` (Decimal) - The amount of tokens to withdraw from the liquidity pool.
368
372
///
369
373
/// # Returns:
370
374
///
371
375
/// * `Bucket` - A bucket of the withdrawn tokens.
372
376
fn withdraw(
373
377
& mut self ,
374
- resource_address: Address ,
378
+ resource_address: ResourceAddress ,
375
379
amount: Decimal
376
380
) -> Bucket {
377
381
// Performing the checks to ensure tha the withdraw can actually go through
@@ -480,15 +484,16 @@ blueprint!{
480
484
self . deposit( bucket2. take( amount2) ) ;
481
485
482
486
// Computing the amount of tracking tokens that the liquidity provider is owed and minting them. In the case
483
- // that the liquidity pool has been completely emptied out (tracking_token_def.total_supply() == 0) then the
484
- // first person to supply liquidity back into the pool again would be given 100 tracking tokens.
485
- let tracking_amount: Decimal = if self . tracking_token_def. total_supply( ) == Decimal :: zero( ) {
487
+ // that the liquidity pool has been completely emptied out (tracking_tokens_manager.total_supply() == 0)
488
+ // then the first person to supply liquidity back into the pool again would be given 100 tracking tokens.
489
+ let tracking_tokens_manager: & ResourceManager = borrow_resource_manager!( self . tracking_token_address) ;
490
+ let tracking_amount: Decimal = if tracking_tokens_manager. total_supply( ) == Decimal :: zero( ) {
486
491
dec!( "100.00" )
487
492
} else {
488
- amount1 * self . tracking_token_def . total_supply( ) / m
493
+ amount1 * tracking_tokens_manager . total_supply( ) / m
489
494
} ;
490
- let tracking_tokens: Bucket = self . tracking_token_admin_badge. authorize( |x | {
491
- self . tracking_token_def . mint( tracking_amount, x )
495
+ let tracking_tokens: Bucket = self . tracking_token_admin_badge. authorize( || {
496
+ tracking_tokens_manager . mint( tracking_amount)
492
497
} ) ;
493
498
info!( "[Add Liquidity]: Owed amount of tracking tokens: {}" , tracking_amount) ;
494
499
@@ -525,20 +530,21 @@ blueprint!{
525
530
// Checking the resource address of the tracking tokens passed to ensure that they do indeed belong to this
526
531
// liquidity pool.
527
532
assert_eq!(
528
- tracking_tokens. resource_address( ) , self . tracking_token_def . address ( ) ,
533
+ tracking_tokens. resource_address( ) , self . tracking_token_address ,
529
534
"[Remove Liquidity]: The tracking tokens given do not belong to this liquidity pool."
530
535
) ;
531
536
532
537
// Calculating the percentage ownership that the tracking tokens amount corresponds to
533
- let percentage: Decimal = tracking_tokens. amount( ) / self . tracking_token_def. total_supply( ) ;
538
+ let tracking_tokens_manager: & ResourceManager = borrow_resource_manager!( self . tracking_token_address) ;
539
+ let percentage: Decimal = tracking_tokens. amount( ) / tracking_tokens_manager. total_supply( ) ;
534
540
535
541
// Burning the tracking tokens
536
- self . tracking_token_admin_badge. authorize( |x | {
537
- tracking_tokens. burn_with_auth ( x ) ;
542
+ self . tracking_token_admin_badge. authorize( || {
543
+ tracking_tokens. burn ( ) ;
538
544
} ) ;
539
545
540
546
// Withdrawing the amount of tokens owed to this liquidity provider
541
- let addresses: Vec <Address > = self . addresses( ) ;
547
+ let addresses: Vec <ResourceAddress > = self . addresses( ) ;
542
548
let bucket1: Bucket = self . withdraw( addresses[ 0 ] , self . vaults[ & addresses[ 0 ] ] . amount( ) * percentage) ;
543
549
let bucket2: Bucket = self . withdraw( addresses[ 1 ] , self . vaults[ & addresses[ 1 ] ] . amount( ) * percentage) ;
544
550
0 commit comments