|
| 1 | +use scrypto::prelude::*; |
| 2 | + |
| 3 | +blueprint! { |
| 4 | + struct Radiswap { |
| 5 | + /// The resource address of LP token. |
| 6 | + lp_resource_address: ResourceAddress, |
| 7 | + /// LP tokens mint badge. |
| 8 | + lp_mint_badge: Vault, |
| 9 | + /// The reserve for token A. |
| 10 | + a_pool: Vault, |
| 11 | + /// The reserve for token B. |
| 12 | + b_pool: Vault, |
| 13 | + /// The fee to apply for every swap |
| 14 | + fee: Decimal, |
| 15 | + /// The standard (Uniswap-like) DEX follows the X*Y=K rule. Since we enable a user defined 'lp_initial_supply', we need to store this value to recover incase all liquidity is removed from the system. |
| 16 | + /// Adding and removing liquidity does not change this ratio, this ratio is only changed upon swaps. |
| 17 | + lp_per_asset_ratio: Decimal, |
| 18 | + } |
| 19 | + |
| 20 | + impl Radiswap { |
| 21 | + /// Creates a Radiswap component for token pair A/B and returns the component address |
| 22 | + /// along with the initial LP tokens. |
| 23 | + pub fn instantiate_pool( |
| 24 | + a_tokens: Bucket, |
| 25 | + b_tokens: Bucket, |
| 26 | + lp_initial_supply: Decimal, |
| 27 | + lp_symbol: String, |
| 28 | + lp_name: String, |
| 29 | + lp_url: String, |
| 30 | + fee: Decimal, |
| 31 | + ) -> (ComponentAddress, Bucket) { |
| 32 | + // Check arguments |
| 33 | + assert!( |
| 34 | + !a_tokens.is_empty() && !b_tokens.is_empty(), |
| 35 | + "You must pass in an initial supply of each token" |
| 36 | + ); |
| 37 | + assert!( |
| 38 | + fee >= dec!("0") && fee <= dec!("1"), |
| 39 | + "Invalid fee in thousandths" |
| 40 | + ); |
| 41 | + |
| 42 | + // Instantiate our LP token and mint an initial supply of them |
| 43 | + let lp_mint_badge = ResourceBuilder::new_fungible() |
| 44 | + .divisibility(DIVISIBILITY_NONE) |
| 45 | + .metadata("name", "LP Token Mint Auth") |
| 46 | + .initial_supply(1); |
| 47 | + let lp_resource_address = ResourceBuilder::new_fungible() |
| 48 | + .divisibility(DIVISIBILITY_MAXIMUM) |
| 49 | + .metadata("symbol", lp_symbol) |
| 50 | + .metadata("name", lp_name) |
| 51 | + .metadata("url", lp_url) |
| 52 | + .mintable(rule!(require(lp_mint_badge.resource_address())), LOCKED) |
| 53 | + .burnable(rule!(require(lp_mint_badge.resource_address())), LOCKED) |
| 54 | + .no_initial_supply(); |
| 55 | + |
| 56 | + let lp_tokens = lp_mint_badge.authorize(|| { |
| 57 | + borrow_resource_manager!(lp_resource_address).mint(lp_initial_supply) |
| 58 | + }); |
| 59 | + |
| 60 | + // ratio = initial supply / (x * y) = initial supply / k |
| 61 | + let lp_per_asset_ratio = lp_initial_supply / (a_tokens.amount() * b_tokens.amount()); |
| 62 | + |
| 63 | + // Instantiate our Radiswap component |
| 64 | + let radiswap = Self { |
| 65 | + lp_resource_address, |
| 66 | + lp_mint_badge: Vault::with_bucket(lp_mint_badge), |
| 67 | + a_pool: Vault::with_bucket(a_tokens), |
| 68 | + b_pool: Vault::with_bucket(b_tokens), |
| 69 | + fee, |
| 70 | + lp_per_asset_ratio, |
| 71 | + } |
| 72 | + .instantiate() |
| 73 | + .globalize(); |
| 74 | + |
| 75 | + // Return the new Radiswap component, as well as the initial supply of LP tokens |
| 76 | + (radiswap, lp_tokens) |
| 77 | + } |
| 78 | + |
| 79 | + /// Adds liquidity to this pool and return the LP tokens representing pool shares |
| 80 | + /// along with any remainder. |
| 81 | + pub fn add_liquidity(&mut self, mut a_tokens: Bucket, mut b_tokens: Bucket) -> (Bucket, Bucket) { |
| 82 | + // Get the resource manager of the lp tokens |
| 83 | + let lp_resource_manager = borrow_resource_manager!(self.lp_resource_address); |
| 84 | + |
| 85 | + // Differentiate LP calculation based on whether pool is empty or not. |
| 86 | + let (supply_to_mint, remainder) = if lp_resource_manager.total_supply() == 0.into() { |
| 87 | + // Set initial LP tokens based on previous LP per K ratio. |
| 88 | + let supply_to_mint = |
| 89 | + self.lp_per_asset_ratio * a_tokens.amount() * b_tokens.amount(); |
| 90 | + self.a_pool.put(a_tokens.take(a_tokens.amount())); |
| 91 | + self.b_pool.put(b_tokens); |
| 92 | + (supply_to_mint, a_tokens) |
| 93 | + } else { |
| 94 | + // The ratio of added liquidity in existing liquidty. |
| 95 | + let a_ratio = a_tokens.amount() / self.a_pool.amount(); |
| 96 | + let b_ratio = b_tokens.amount() / self.b_pool.amount(); |
| 97 | + |
| 98 | + let (actual_ratio, remainder) = if a_ratio <= b_ratio { |
| 99 | + // We will claim all input token A's, and only the correct amount of token B |
| 100 | + self.a_pool.put(a_tokens); |
| 101 | + self.b_pool |
| 102 | + .put(b_tokens.take(self.b_pool.amount() * a_ratio)); |
| 103 | + (a_ratio, b_tokens) |
| 104 | + } else { |
| 105 | + // We will claim all input token B's, and only the correct amount of token A |
| 106 | + self.b_pool.put(b_tokens); |
| 107 | + self.a_pool |
| 108 | + .put(a_tokens.take(self.a_pool.amount() * b_ratio)); |
| 109 | + (b_ratio, a_tokens) |
| 110 | + }; |
| 111 | + ( |
| 112 | + lp_resource_manager.total_supply() * actual_ratio, |
| 113 | + remainder, |
| 114 | + ) |
| 115 | + }; |
| 116 | + |
| 117 | + // Mint LP tokens according to the share the provider is contributing |
| 118 | + let lp_tokens = self.lp_mint_badge.authorize(|| { |
| 119 | + lp_resource_manager.mint(supply_to_mint) |
| 120 | + }); |
| 121 | + |
| 122 | + // Return the LP tokens along with any remainder |
| 123 | + (lp_tokens, remainder) |
| 124 | + } |
| 125 | + |
| 126 | + /// Removes liquidity from this pool. |
| 127 | + pub fn remove_liquidity(&mut self, lp_tokens: Bucket) -> (Bucket, Bucket) { |
| 128 | + assert!( |
| 129 | + self.lp_resource_address == lp_tokens.resource_address(), |
| 130 | + "Wrong token type passed in" |
| 131 | + ); |
| 132 | + |
| 133 | + // Get the resource manager of the lp tokens |
| 134 | + let lp_resource_manager = borrow_resource_manager!(self.lp_resource_address); |
| 135 | + |
| 136 | + // Calculate the share based on the input LP tokens. |
| 137 | + let share = lp_tokens.amount() / lp_resource_manager.total_supply(); |
| 138 | + |
| 139 | + // Withdraw the correct amounts of tokens A and B from reserves |
| 140 | + let a_withdrawn = self.a_pool.take(self.a_pool.amount() * share); |
| 141 | + let b_withdrawn = self.b_pool.take(self.b_pool.amount() * share); |
| 142 | + |
| 143 | + // Burn the LP tokens received |
| 144 | + self.lp_mint_badge.authorize(|| { |
| 145 | + lp_tokens.burn(); |
| 146 | + }); |
| 147 | + |
| 148 | + // Return the withdrawn tokens |
| 149 | + (a_withdrawn, b_withdrawn) |
| 150 | + } |
| 151 | + |
| 152 | + /// Swaps token A for B, or vice versa. |
| 153 | + pub fn swap(&mut self, input_tokens: Bucket) -> Bucket { |
| 154 | + // Get the resource manager of the lp tokens |
| 155 | + let lp_resource_manager = borrow_resource_manager!(self.lp_resource_address); |
| 156 | + |
| 157 | + // Calculate the swap fee |
| 158 | + let fee_amount = input_tokens.amount() * self.fee; |
| 159 | + |
| 160 | + let output_tokens = if input_tokens.resource_address() == self.a_pool.resource_address() { |
| 161 | + // Calculate how much of token B we will return |
| 162 | + let b_amount = self.b_pool.amount() |
| 163 | + - self.a_pool.amount() * self.b_pool.amount() |
| 164 | + / (input_tokens.amount() - fee_amount + self.a_pool.amount()); |
| 165 | + |
| 166 | + // Put the input tokens into our pool |
| 167 | + self.a_pool.put(input_tokens); |
| 168 | + |
| 169 | + // Return the tokens owed |
| 170 | + self.b_pool.take(b_amount) |
| 171 | + } else { |
| 172 | + // Calculate how much of token A we will return |
| 173 | + let a_amount = self.a_pool.amount() |
| 174 | + - self.a_pool.amount() * self.b_pool.amount() |
| 175 | + / (input_tokens.amount() - fee_amount + self.b_pool.amount()); |
| 176 | + |
| 177 | + // Put the input tokens into our pool |
| 178 | + self.b_pool.put(input_tokens); |
| 179 | + |
| 180 | + // Return the tokens owed |
| 181 | + self.a_pool.take(a_amount) |
| 182 | + }; |
| 183 | + |
| 184 | + // Accrued fees change the raio |
| 185 | + self.lp_per_asset_ratio = |
| 186 | + lp_resource_manager.total_supply() / (self.a_pool.amount() * self.b_pool.amount()); |
| 187 | + |
| 188 | + output_tokens |
| 189 | + } |
| 190 | + |
| 191 | + /// Returns the resource addresses of the pair. |
| 192 | + pub fn get_pair(&self) -> (ResourceAddress, ResourceAddress) { |
| 193 | + ( |
| 194 | + self.a_pool.resource_address(), |
| 195 | + self.b_pool.resource_address(), |
| 196 | + ) |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments