Skip to content

Commit 1aba892

Browse files
committed
Updated RaDEX to v0.4.0
1 parent fe51993 commit 1aba892

File tree

4 files changed

+107
-97
lines changed

4 files changed

+107
-97
lines changed

1-exchanges/RaDEX/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[package]
22
name = "RaDEX"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55

66
[dependencies]
7-
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.3.0" }
8-
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.3.0" }
7+
sbor = { path = "/Users/omarabdulla/radix/radixdlt-scrypto/sbor" }
8+
scrypto = { path = "/Users/omarabdulla/radix/radixdlt-scrypto/scrypto" }
99

1010
[dev-dependencies]
11-
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.3.0" }
11+
radix-engine = { path = "/Users/omarabdulla/radix/radixdlt-scrypto/radix-engine" }
1212

1313
[profile.release]
1414
opt-level = 's' # Optimize for size.

1-exchanges/RaDEX/src/liquidity_pool.rs

+56-50
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ blueprint!{
1313
/// and more dynamic way of manipulating and making use of the vaults inside the liquidity pool. Keep in mind
1414
/// that this `vaults` hashmap will always have exactly two vaults for the two assets being traded against one
1515
/// another.
16-
vaults: HashMap<Address, Vault>,
16+
vaults: HashMap<ResourceAddress, Vault>,
1717

1818
/// When liquidity providers provide liquidity to the liquidity pool, they are given a number of tokens that is
1919
/// equivalent to the percentage ownership that they have in the liquidity pool. The tracking token is the token
2020
/// 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,
2323

2424
/// The tracking tokens are mutable supply tokens that may be minted and burned when liquidity is supplied or
2525
/// 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!{
6666
token1: Bucket,
6767
token2: Bucket,
6868
fee_to_pool: Decimal
69-
) -> (Component, Bucket) {
69+
) -> (ComponentAddress, Bucket) {
7070
// Performing the checks to see if this liquidity pool may be created or not.
7171
assert_ne!(
7272
token1.resource_address(), token2.resource_address(),
7373
"[Pool Creation]: Liquidity pools may only be created between two different tokens."
7474
);
7575

7676
assert_ne!(
77-
token1.resource_def().resource_type(), ResourceType::NonFungible,
77+
borrow_resource_manager!(token1.resource_address()).resource_type(), ResourceType::NonFungible,
7878
"[Pool Creation]: Both assets must be fungible."
7979
);
8080
assert_ne!(
81-
token2.resource_def().resource_type(), ResourceType::NonFungible,
81+
borrow_resource_manager!(token2.resource_address()).resource_type(), ResourceType::NonFungible,
8282
"[Pool Creation]: Both assets must be fungible."
8383
);
8484

@@ -96,7 +96,7 @@ blueprint!{
9696

9797
// Sorting the buckets and then creating the hashmap of the vaults from the sorted buckets
9898
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());
100100

101101
let lp_id: String = format!("{}-{}", addresses.0, addresses.1);
102102
let pair_name: String = address_pair_symbol(addresses.0, addresses.1);
@@ -106,36 +106,40 @@ blueprint!{
106106
lp_id, pair_name, bucket1.amount(), bucket2.amount()
107107
);
108108

109-
let mut vaults: HashMap<Address, Vault> = HashMap::new();
109+
let mut vaults: HashMap<ResourceAddress, Vault> = HashMap::new();
110110
vaults.insert(bucket1.resource_address(), Vault::with_bucket(bucket1));
111111
vaults.insert(bucket2.resource_address(), Vault::with_bucket(bucket2));
112112

113113
// Creating the admin badge of the liquidity pool which will be given the authority to mint and burn the
114114
// 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)
116117
.metadata("name", "Tracking Token Admin Badge")
117118
.metadata("symbol", "TTAB")
118119
.metadata("description", "This is an admin badge that has the authority to mint and burn tracking tokens")
119120
.metadata("lp_id", format!("{}", lp_id))
120-
.initial_supply_fungible(1);
121+
.initial_supply(1);
121122

122123
// 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)
124126
.metadata("name", format!("{} LP Tracking Token", pair_name))
125127
.metadata("symbol", "TT")
126128
.metadata("description", "A tracking token used to track the percentage ownership of liquidity providers over the liquidity pool")
127129
.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);
131133

132134
// Creating the liquidity pool component and instantiating it
133-
let liquidity_pool: Component = Self {
135+
let liquidity_pool: ComponentAddress = Self {
134136
vaults: vaults,
135-
tracking_token_def: tracking_tokens.resource_def(),
137+
tracking_token_address: tracking_tokens.resource_address(),
136138
tracking_token_admin_badge: Vault::with_bucket(tracking_token_admin_badge),
137139
fee_to_pool: fee_to_pool,
138-
}.instantiate();
140+
}
141+
.instantiate()
142+
.globalize();
139143

140144
return (liquidity_pool, tracking_tokens);
141145
}
@@ -147,14 +151,14 @@ blueprint!{
147151
///
148152
/// # Arguments:
149153
///
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.
151155
///
152156
/// # Returns:
153157
///
154158
/// * `bool` - A boolean of whether the address belongs to this pool or not.
155159
pub fn belongs_to_pool(
156160
&self,
157-
address: Address
161+
address: ResourceAddress
158162
) -> bool {
159163
return self.vaults.contains_key(&address);
160164
}
@@ -166,13 +170,13 @@ blueprint!{
166170
///
167171
/// # Arguments:
168172
///
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.
170174
/// * `label` (String) - The label of the method that called this assert method. As an example, if the swap
171175
/// method were to call this method, then the label would be `Swap` so that it's clear where the assertion error
172176
/// took place.
173177
pub fn assert_belongs_to_pool(
174178
&self,
175-
address: Address,
179+
address: ResourceAddress,
176180
label: String
177181
) {
178182
assert!(
@@ -182,13 +186,13 @@ blueprint!{
182186
);
183187
}
184188

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>`.
186190
///
187191
/// # Returns:
188192
///
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>>();
192196
}
193197

194198
/// Gets the name of the given liquidity pool from the symbols of the two tokens.
@@ -197,7 +201,7 @@ blueprint!{
197201
///
198202
/// `String` - A string of the pair symbol
199203
pub fn name(&self) -> String {
200-
let addresses: Vec<Address> = self.addresses();
204+
let addresses: Vec<ResourceAddress> = self.addresses();
201205
return address_pair_symbol(addresses[0], addresses[1]);
202206
}
203207

@@ -212,20 +216,20 @@ blueprint!{
212216
///
213217
/// # Arguments
214218
///
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.
216220
///
217221
/// # Returns:
218222
///
219-
/// * `Address` - The address of the other token in this pool.
223+
/// * `ResourceAddress` - The address of the other token in this pool.
220224
pub fn other_resource_address(
221225
&self,
222-
resource_address: Address
223-
) -> Address {
226+
resource_address: ResourceAddress
227+
) -> ResourceAddress {
224228
// 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"));
226230

227231
// 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();
229233
return if addresses[0] == resource_address {addresses[1]} else {addresses[0]};
230234
}
231235

@@ -235,7 +239,7 @@ blueprint!{
235239
///
236240
/// `Decimal` - A decimal value of the reserves amount of Token A and Token B multiplied by one another.
237241
pub fn k(&self) -> Decimal {
238-
let addresses: Vec<Address> = self.addresses();
242+
let addresses: Vec<ResourceAddress> = self.addresses();
239243
return self.vaults[&addresses[0]].amount() * self.vaults[&addresses[1]].amount()
240244
}
241245

@@ -250,7 +254,7 @@ blueprint!{
250254
///
251255
/// # Arguments:
252256
///
253-
/// * `input_resource_address` (Address) - The resource address of the input token.
257+
/// * `input_resource_address` (ResourceAddress) - The resource address of the input token.
254258
/// * `input_amount` (Decimal) - The amount of input tokens to calculate the output for.
255259
///
256260
/// # Returns:
@@ -269,7 +273,7 @@ blueprint!{
269273
/// * `r` - The fee modifier where `r = (100 - fee) / 100`
270274
pub fn calculate_output_amount(
271275
&self,
272-
input_resource_address: Address,
276+
input_resource_address: ResourceAddress,
273277
input_amount: Decimal
274278
) -> Decimal {
275279
// Checking if the passed resource address belongs to this pool.
@@ -295,7 +299,7 @@ blueprint!{
295299
///
296300
/// # Arguments:
297301
///
298-
/// * `output_resource_address` (Address) - The resource address of the output token.
302+
/// * `output_resource_address` (ResourceAddress) - The resource address of the output token.
299303
/// * `output_amount` (Decimal) - The amount of output tokens to calculate the input for.
300304
///
301305
/// # Returns:
@@ -314,7 +318,7 @@ blueprint!{
314318
/// * `r` - The fee modifier where `r = (100 - fee) / 100`
315319
pub fn calculate_input_amount(
316320
&self,
317-
output_resource_address: Address,
321+
output_resource_address: ResourceAddress,
318322
output_amount: Decimal
319323
) -> Decimal {
320324
// Checking if the passed resource address belongs to this pool.
@@ -363,15 +367,15 @@ blueprint!{
363367
///
364368
/// # Arguments:
365369
///
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.
367371
/// * `amount` (Decimal) - The amount of tokens to withdraw from the liquidity pool.
368372
///
369373
/// # Returns:
370374
///
371375
/// * `Bucket` - A bucket of the withdrawn tokens.
372376
fn withdraw(
373377
&mut self,
374-
resource_address: Address,
378+
resource_address: ResourceAddress,
375379
amount: Decimal
376380
) -> Bucket {
377381
// Performing the checks to ensure tha the withdraw can actually go through
@@ -480,15 +484,16 @@ blueprint!{
480484
self.deposit(bucket2.take(amount2));
481485

482486
// 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() {
486491
dec!("100.00")
487492
} else {
488-
amount1 * self.tracking_token_def.total_supply() / m
493+
amount1 * tracking_tokens_manager.total_supply() / m
489494
};
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)
492497
});
493498
info!("[Add Liquidity]: Owed amount of tracking tokens: {}", tracking_amount);
494499

@@ -525,20 +530,21 @@ blueprint!{
525530
// Checking the resource address of the tracking tokens passed to ensure that they do indeed belong to this
526531
// liquidity pool.
527532
assert_eq!(
528-
tracking_tokens.resource_address(), self.tracking_token_def.address(),
533+
tracking_tokens.resource_address(), self.tracking_token_address,
529534
"[Remove Liquidity]: The tracking tokens given do not belong to this liquidity pool."
530535
);
531536

532537
// 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();
534540

535541
// 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();
538544
});
539545

540546
// Withdrawing the amount of tokens owed to this liquidity provider
541-
let addresses: Vec<Address> = self.addresses();
547+
let addresses: Vec<ResourceAddress> = self.addresses();
542548
let bucket1: Bucket = self.withdraw(addresses[0], self.vaults[&addresses[0]].amount() * percentage);
543549
let bucket2: Bucket = self.withdraw(addresses[1], self.vaults[&addresses[1]].amount() * percentage);
544550

0 commit comments

Comments
 (0)