Skip to content

Commit

Permalink
Stop using Vec<> in CurveAmm/Uniswap pallet storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekvpandya committed Dec 28, 2021
1 parent d9801e0 commit d99a6c7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 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.

24 changes: 15 additions & 9 deletions frame/curve-amm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,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, (T::AssetId, T::AssetId)>;

/// Balance of asset for given pool excluding admin_fee
#[pallet::storage]
Expand Down Expand Up @@ -338,7 +339,7 @@ pub mod pallet {
let pool = Self::get_pool_info(pool_id).ok_or(Error::<T>::PoolNotFound)?;
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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let mut balances = Vec::new();
for asset_id in &assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
Expand Down Expand Up @@ -525,7 +526,7 @@ 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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let n_coins = assets.len();
let mut balances = Vec::new();
for asset_id in &assets {
Expand Down Expand Up @@ -621,7 +622,7 @@ 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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let mut balances = Vec::new();
for asset_id in &assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
Expand Down Expand Up @@ -711,7 +712,7 @@ 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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let mut balances = Vec::new();
for asset_id in &assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
Expand Down Expand Up @@ -818,7 +819,7 @@ 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((assets[0], assets[1]));
Ok(())
})?;

Expand All @@ -834,6 +835,11 @@ pub mod pallet {
Ok(pool_id)
}

pub fn get_assets(pool_id: &T::PoolId) -> Option<Vec<T::AssetId>> {
let assets_pair = PoolAssets::<T>::get(pool_id)?;
Some(vec![assets_pair.0, assets_pair.1])
}

/// Return pool information for given pool_id.
pub fn get_pool_info(pool_id: T::PoolId) -> Option<StableSwapPoolInfo<T::AccountId>> {
Pools::<T>::get(pool_id)
Expand Down Expand Up @@ -1075,7 +1081,7 @@ 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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let asset_id = assets[destination_asset_index];
T::LpToken::transfer(asset_id, source, pool_account_id, amount, true)?;
PoolAssetTotalBalance::<T>::mutate(
Expand All @@ -1097,7 +1103,7 @@ pub mod pallet {
destination: &T::AccountId,
amount: T::Balance,
) -> DispatchResult {
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let asset_id = assets[source_asset_index];
T::LpToken::transfer(asset_id, pool_account_id, destination, amount, true)?;

Expand Down
24 changes: 15 additions & 9 deletions frame/uniswap-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,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, (T::AssetId, T::AssetId)>;

/// Balance of asset for given pool excluding admin_fee
#[pallet::storage]
Expand Down Expand Up @@ -339,7 +340,7 @@ 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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let mut balances = Vec::new();
for asset_id in &assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
Expand Down Expand Up @@ -491,7 +492,7 @@ 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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let n_coins = assets.len();
let mut balances = Vec::new();
for asset_id in &assets {
Expand Down Expand Up @@ -588,7 +589,7 @@ 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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let mut balances = Vec::new();
for asset_id in &assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
Expand Down Expand Up @@ -658,7 +659,7 @@ 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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let mut balances = Vec::new();
for asset_id in &assets {
balances.push(PoolAssetBalance::<T>::get(pool_id, asset_id));
Expand Down Expand Up @@ -760,7 +761,7 @@ 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((assets[0], assets[1]));
Ok(())
})?;

Expand All @@ -776,6 +777,11 @@ pub mod pallet {
Ok(pool_id)
}

pub fn get_assets(pool_id: &T::PoolId) -> Option<Vec<T::AssetId>> {
let assets_pair = PoolAssets::<T>::get(pool_id)?;
Some(vec![assets_pair.0, assets_pair.1])
}

/// Return pool information for given pool_id.
pub fn get_pool_info(pool_id: T::PoolId) -> Option<ConstantProductPoolInfo<T::AccountId>> {
Pools::<T>::get(pool_id)
Expand Down Expand Up @@ -815,7 +821,7 @@ 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 = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
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 +843,7 @@ pub mod pallet {
destination: &T::AccountId,
amount: T::Balance,
) -> DispatchResult {
let assets = PoolAssets::<T>::get(pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let assets = Self::get_assets(&pool_id).ok_or(Error::<T>::InconsistentStorage)?;
let asset_id = assets[source_asset_index];
T::LpToken::transfer(asset_id, pool_account_id, destination, amount, true)?;

Expand Down

0 comments on commit d99a6c7

Please sign in to comment.