Skip to content

Commit

Permalink
add getter for rate providers
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKuczma committed Aug 13, 2024
1 parent be58f48 commit 3108ed9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
9 changes: 9 additions & 0 deletions amm/contracts/stable_pool/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,15 @@ pub mod stable_pool {
.collect()
}

#[ink(message)]
fn token_rates_providers(&self) -> Vec<Option<AccountId>> {
self.pool
.token_rates
.iter()
.map(|rate| rate.get_rate_provider())
.collect()
}

#[ink(message)]
fn get_swap_amount_out(
&mut self,
Expand Down
19 changes: 13 additions & 6 deletions amm/contracts/stable_pool/token_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use traits::RateProvider;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct ExternalTokenRate {
token_rate_contract: AccountId,
pub rate_provider: AccountId,
cached_token_rate: u128,
last_update_block_no: u32,
}
Expand All @@ -22,8 +22,8 @@ impl TokenRate {
Self::Constant(rate)
}

pub fn new_external(token_rate_contract: AccountId) -> Self {
Self::External(ExternalTokenRate::new(token_rate_contract))
pub fn new_external(rate_provider: AccountId) -> Self {
Self::External(ExternalTokenRate::new(rate_provider))
}

/// Get current rate and update the cache.
Expand All @@ -33,12 +33,19 @@ impl TokenRate {
Self::Constant(rate) => *rate,
}
}

pub fn get_rate_provider(&self) -> Option<AccountId> {
match self {
Self::External(external) => Some((*external).rate_provider),
Self::Constant(_) => None,
}
}
}

impl ExternalTokenRate {
pub fn new(token_rate_contract: AccountId) -> Self {
pub fn new(rate_provider: AccountId) -> Self {
Self {
token_rate_contract,
rate_provider,
cached_token_rate: 0,
last_update_block_no: 0,
}
Expand All @@ -54,7 +61,7 @@ impl ExternalTokenRate {

fn query_rate(&self) -> u128 {
let mut rate_provider: contract_ref!(RateProvider, DefaultEnvironment) =
self.token_rate_contract.into();
self.rate_provider.into();
rate_provider.get_rate()
}
}
37 changes: 19 additions & 18 deletions amm/traits/stable_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ pub trait StablePool {
fn fee_receiver(&self) -> Option<AccountId>;

/// Updates cached token rates.
///
///
/// Returns current tokens rates with precision of 12 decimal places.
#[ink(message)]
fn token_rates(&mut self) -> Vec<u128>;

/// Returns list of RateProvider address for each token.
/// If the rate is constant, returns None.
#[ink(message)]
fn token_rates_providers(&self) -> Vec<Option<AccountId>>;

/// Calculate swap amount of `token_out`
/// given `token_in amount`.
///
///
/// Updates cached token rates.
///
///
/// Returns a tuple of (amount out, fee)
/// NOTE: fee is applied on `token_out`
#[ink(message)]
Expand All @@ -50,9 +55,9 @@ pub trait StablePool {

/// Calculate required swap amount of `token_in`
/// to get `token_out_amount`.
///
///
/// Updates cached token rates.
///
///
/// Returns a tuple of (amount in, fee)
/// NOTE: fee is applied on `token_out`
#[ink(message)]
Expand All @@ -65,9 +70,9 @@ pub trait StablePool {

/// Calculate how many lp tokens will be minted
/// given deposit `amounts`.
///
///
/// Updates cached token rates.
///
///
/// Returns a tuple of (lpt amount, fee)
#[ink(message)]
fn get_mint_liquidity_for_amounts(
Expand All @@ -77,9 +82,9 @@ pub trait StablePool {

/// Calculate ideal deposit amounts required
/// to mint `liquidity` amount of lp tokens
///
///
/// Updates cached token rates.
///
///
/// Returns required deposit amounts
#[ink(message)]
fn get_amounts_for_liquidity_mint(
Expand All @@ -89,9 +94,9 @@ pub trait StablePool {

/// Calculate how many lp tokens will be burned
/// given withdraw `amounts`.
///
///
/// Updates cached token rates.
///
///
/// Returns a tuple of (lpt amount, fee part)
#[ink(message)]
fn get_burn_liquidity_for_amounts(
Expand All @@ -101,9 +106,9 @@ pub trait StablePool {

/// Calculate ideal withdraw amounts for
/// burning `liquidity` amount of lp tokens
///
///
/// Updates cached token rates.
///
///
/// Returns withdraw amounts
#[ink(message)]
fn get_amounts_for_liquidity_burn(
Expand Down Expand Up @@ -204,11 +209,7 @@ pub trait StablePool {
/// - trade_fee given as an integer with 1e9 precision. The the maximum is 1% (10000000)
/// - protocol_fee given as an integer with 1e9 precision. The maximum is 50% (500000000)
#[ink(message)]
fn set_fees(
&mut self,
trade_fee: u32,
protocol_fee: u32,
) -> Result<(), StablePoolError>;
fn set_fees(&mut self, trade_fee: u32, protocol_fee: u32) -> Result<(), StablePoolError>;

#[ink(message)]
fn set_amp_coef(&mut self, amp_coef: u128) -> Result<(), StablePoolError>;
Expand Down

0 comments on commit 3108ed9

Please sign in to comment.