Skip to content

Commit

Permalink
fix: foreign investment amount (#1705)
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli authored Jan 29, 2024
1 parent d781a10 commit a262fe9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
11 changes: 9 additions & 2 deletions pallets/foreign-investments/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cfg_traits::{
};
use cfg_types::investments::CollectedAmount;
use frame_support::pallet_prelude::*;
use sp_runtime::traits::Zero;
use sp_runtime::traits::{EnsureAdd, Zero};
use sp_std::marker::PhantomData;

use crate::{
Expand Down Expand Up @@ -132,7 +132,14 @@ impl<T: Config> ForeignInvestment<T::AccountId> for Pallet<T> {
who: &T::AccountId,
investment_id: T::InvestmentId,
) -> Result<T::Balance, DispatchError> {
T::Investment::investment(who, investment_id)
Ok(T::Investment::investment(who, investment_id)?.ensure_add(
Swaps::<T>::pending_amount_for(
who,
investment_id,
Action::Investment,
pool_currency_of::<T>(investment_id)?,
),
)?)
}

fn redemption(
Expand Down
58 changes: 54 additions & 4 deletions pallets/foreign-investments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,11 @@ mod investment {
})
);

assert_eq!(ForeignInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
assert_eq!(
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT))
);
assert_eq!(MockInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
});
}

Expand Down Expand Up @@ -442,7 +446,11 @@ mod investment {
})
);

assert_eq!(ForeignInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
assert_eq!(
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT + AMOUNT))
);
assert_eq!(MockInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
});
}

Expand Down Expand Up @@ -527,7 +535,11 @@ mod investment {
})
);

assert_eq!(ForeignInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
assert_eq!(
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT * 3 / 4))
);
assert_eq!(MockInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
});
}

Expand Down Expand Up @@ -579,6 +591,10 @@ mod investment {

assert_eq!(
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT))
);
assert_eq!(
MockInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT / 4))
);
});
Expand All @@ -597,6 +613,14 @@ mod investment {
));

util::fulfill_last_swap(Action::Investment, foreign_to_pool(3 * AMOUNT / 4));
assert_eq!(
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT))
);
assert_eq!(
MockInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(3 * AMOUNT / 4))
);

assert_ok!(ForeignInvestment::decrease_foreign_investment(
&USER,
Expand All @@ -617,6 +641,10 @@ mod investment {
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT / 2))
);
assert_eq!(
MockInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT / 2))
);
});
}

Expand Down Expand Up @@ -658,6 +686,10 @@ mod investment {

assert_eq!(
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT * 3 / 2))
);
assert_eq!(
MockInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(3 * AMOUNT / 4))
);
});
Expand Down Expand Up @@ -704,6 +736,7 @@ mod investment {
);

assert_eq!(ForeignInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
assert_eq!(MockInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
});
}

Expand Down Expand Up @@ -764,6 +797,10 @@ mod investment {
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT / 4))
);
assert_eq!(
MockInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT / 4))
);
});
}

Expand Down Expand Up @@ -816,9 +853,12 @@ mod investment {
decrease_swapped_amount: 0,
})
);

assert_eq!(
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(3 * AMOUNT / 4))
);
assert_eq!(
MockInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT / 4))
);
});
Expand Down Expand Up @@ -864,6 +904,7 @@ mod investment {
);

assert_eq!(ForeignInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
assert_eq!(MockInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
});
}

Expand Down Expand Up @@ -910,6 +951,7 @@ mod investment {
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(0))
);
assert_eq!(MockInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
});
}

Expand All @@ -933,6 +975,10 @@ mod investment {
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT))
);
assert_eq!(
MockInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT))
);
});
}

Expand Down Expand Up @@ -972,6 +1018,10 @@ mod investment {
ForeignInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(0))
);
assert_eq!(
MockInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(0))
);

assert_eq!(
ForeignInvestmentInfo::<Runtime>::get(&USER, INVESTMENT_ID),
Expand Down

0 comments on commit a262fe9

Please sign in to comment.