Skip to content

Commit

Permalink
Stop using Vec<> in CurveAmm/Uniswap pallet storage. (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekvpandya committed Jan 7, 2022
1 parent f195745 commit ac4177e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 25 additions & 15 deletions frame/curve-amm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub mod pallet {
use codec::{Codec, FullCodec};
use composable_traits::{
currency::CurrencyFactory,
defi::CurrencyPair,
dex::{CurveAmm, StableSwapPoolInfo},
math::LiftedFixedBalance,
};
Expand Down Expand Up @@ -118,10 +119,11 @@ pub mod pallet {
pub type Pools<T: Config> =
StorageMap<_, Blake2_128Concat, T::PoolId, StableSwapPoolInfo<T::AccountId, T::AssetId>>;

/// List of assets supported by the pool
/// Pair of assets supported by the pool
#[pallet::storage]
#[pallet::getter(fn pool_assets)]
pub type PoolAssets<T: Config> = StorageMap<_, Blake2_128Concat, T::PoolId, Vec<T::AssetId>>;
pub type PoolAssets<T: Config> =
StorageMap<_, Blake2_128Concat, T::PoolId, CurrencyPair<T::AssetId>>;

/// Balance of asset for given pool excluding admin_fee
#[pallet::storage]
Expand Down Expand Up @@ -284,7 +286,8 @@ pub mod pallet {

let pool = Self::get_pool_info(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let pool_lp_asset = pool.lp_token;
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let old_balances = assets
.iter()
.map(|asset_id| {
Expand Down Expand Up @@ -425,7 +428,7 @@ pub mod pallet {
token_supply.checked_add(&mint_amount).ok_or(Error::<T>::Math)?;

// Ensure that for all tokens user has sufficient amount
for (amount, asset) in amounts.iter().zip(&assets) {
for (amount, asset) in amounts.iter().zip(assets) {
ensure!(T::LpToken::balance(*asset, who) >= *amount, Error::<T>::InsufficientFunds);
}

Expand Down Expand Up @@ -487,8 +490,8 @@ pub mod pallet {

let pool = Self::get_pool_info(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let pool_lp_asset = pool.lp_token;
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;

let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let balances: Vec<<T as Config>::Balance> = assets
.iter()
.copied()
Expand Down Expand Up @@ -595,9 +598,10 @@ pub mod pallet {
let i = i_token.into() as usize;
let j = j_token.into() as usize;

let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let mut balances = Vec::new();
for asset_id in &assets {
for asset_id in assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
}
let n_coins = assets.len();
Expand Down Expand Up @@ -687,13 +691,14 @@ pub mod pallet {
pool_id: T::PoolId,
admin_fee_account: &Self::AccountId,
) -> Result<(), DispatchError> {
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let mut balances = Vec::new();
for asset_id in &assets {
for asset_id in assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
}
let mut total_balances = Vec::new();
for asset_id in &assets {
for asset_id in assets {
total_balances.push(PoolAssetTotalBalance::<T>::get(pool_id, asset_id));
}
let n_coins = assets.len();
Expand Down Expand Up @@ -729,7 +734,7 @@ pub mod pallet {

for (asset, amount) in assets.into_iter().zip(admin_fees.iter().copied()) {
T::LpToken::transfer(
asset,
*asset,
&Self::account_id(&pool_id),
admin_fee_account,
amount,
Expand Down Expand Up @@ -790,7 +795,10 @@ pub mod pallet {

PoolAssets::<T>::try_mutate(pool_id, |pool_assets| -> DispatchResult {
ensure!(pool_assets.is_none(), Error::<T>::InconsistentStorage);
*pool_assets = Some(assets);
*pool_assets = Some(CurrencyPair::new(
*assets.get(0).ok_or(Error::<T>::IndexOutOfRange)?,
*assets.get(1).ok_or(Error::<T>::IndexOutOfRange)?,
));
Ok(())
})?;

Expand Down Expand Up @@ -1049,7 +1057,8 @@ pub mod pallet {
destination_asset_index: usize,
amount: T::Balance,
) -> DispatchResult {
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let asset_id =
assets.get(destination_asset_index).ok_or(Error::<T>::IndexOutOfRange)?;
T::LpToken::transfer(*asset_id, source, pool_account_id, amount, true)?;
Expand All @@ -1072,7 +1081,8 @@ pub mod pallet {
destination: &T::AccountId,
amount: T::Balance,
) -> DispatchResult {
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let asset_id = assets.get(source_asset_index).ok_or(Error::<T>::IndexOutOfRange)?;
T::LpToken::transfer(*asset_id, pool_account_id, destination, amount, true)?;

Expand Down
43 changes: 27 additions & 16 deletions frame/uniswap-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub mod pallet {
use codec::{Codec, FullCodec};
use composable_traits::{
currency::CurrencyFactory,
defi::CurrencyPair,
dex::{ConstantProductPoolInfo, CurveAmm},
math::LiftedFixedBalance,
};
Expand Down Expand Up @@ -158,10 +159,11 @@ pub mod pallet {
#[pallet::getter(fn pool_lp_asset)]
pub type PoolLPAsset<T: Config> = StorageMap<_, Blake2_128Concat, T::PoolId, T::AssetId>;

/// List of assets supported by the pool
/// Pair of assets supported by the pool
#[pallet::storage]
#[pallet::getter(fn pool_assets)]
pub type PoolAssets<T: Config> = StorageMap<_, Blake2_128Concat, T::PoolId, Vec<T::AssetId>>;
pub type PoolAssets<T: Config> =
StorageMap<_, Blake2_128Concat, T::PoolId, CurrencyPair<T::AssetId>>;

/// Balance of asset for given pool excluding admin_fee
#[pallet::storage]
Expand Down Expand Up @@ -339,9 +341,10 @@ pub mod pallet {

let pool_lp_asset =
PoolLPAsset::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let mut balances = Vec::new();
for asset_id in &assets {
for asset_id in assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
}

Expand Down Expand Up @@ -452,7 +455,7 @@ pub mod pallet {
T::LpToken::mint_into(pool_lp_asset, who, mint_amount)?;

let mut invariant = T::Balance::one();
for asset_id in &assets {
for asset_id in assets {
invariant *= PoolAssetBalance::<T>::get(pool_id, asset_id);
}

Expand Down Expand Up @@ -491,10 +494,11 @@ pub mod pallet {

let pool_lp_asset =
PoolLPAsset::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let n_coins = assets.len();
let mut balances = Vec::new();
for asset_id in &assets {
for asset_id in assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
}

Expand Down Expand Up @@ -588,9 +592,10 @@ pub mod pallet {
let i: usize = i_token.into() as usize;
let j: usize = j_token.into() as usize;

let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let mut balances = Vec::new();
for asset_id in &assets {
for asset_id in assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
}
let n_coins = assets.len();
Expand Down Expand Up @@ -658,13 +663,14 @@ pub mod pallet {
pool_id: T::PoolId,
admin_fee_account: &Self::AccountId,
) -> Result<(), DispatchError> {
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let mut balances = Vec::new();
for asset_id in &assets {
for asset_id in assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
}
let mut total_balances = Vec::new();
for asset_id in &assets {
for asset_id in assets {
total_balances.push(PoolAssetTotalBalance::<T>::get(pool_id, asset_id));
}
let n_coins = assets.len();
Expand Down Expand Up @@ -699,7 +705,7 @@ pub mod pallet {

for (asset, amount) in assets.into_iter().zip(admin_fees.iter().copied()) {
T::LpToken::transfer(
asset,
*asset,
&Self::account_id(&pool_id),
admin_fee_account,
amount,
Expand Down Expand Up @@ -760,7 +766,10 @@ pub mod pallet {

PoolAssets::<T>::try_mutate(pool_id, |pool_assets| -> DispatchResult {
ensure!(pool_assets.is_none(), Error::<T>::InconsistentStorage);
*pool_assets = Some(assets);
*pool_assets = Some(CurrencyPair::new(
*assets.get(0).ok_or(Error::<T>::IndexOutOfRange)?,
*assets.get(1).ok_or(Error::<T>::IndexOutOfRange)?,
));
Ok(())
})?;

Expand Down Expand Up @@ -815,7 +824,8 @@ pub mod pallet {
destination_asset_index: usize,
amount: T::Balance,
) -> DispatchResult {
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let asset_id = assets[destination_asset_index];
T::LpToken::transfer(asset_id, source, pool_account_id, amount, true)?;
PoolAssetTotalBalance::<T>::mutate(
Expand All @@ -837,7 +847,8 @@ pub mod pallet {
destination: &T::AccountId,
amount: T::Balance,
) -> DispatchResult {
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets_pair = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let assets = assets_pair.as_slice();
let asset_id = assets[source_asset_index];
T::LpToken::transfer(asset_id, pool_account_id, destination, amount, true)?;

Expand Down

0 comments on commit ac4177e

Please sign in to comment.