Skip to content

Commit

Permalink
allow only supported assets for streaming (#1762)
Browse files Browse the repository at this point in the history
* set a high limit for the minimum deposit

* should create stream if minimum deposit not set

* it may not cause problems but better to insert first

* remove unused constant

* revert event changes

* chore
  • Loading branch information
0xrjman authored May 17, 2022
1 parent 9effd4f commit 33bb165
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions pallets/streaming/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ benchmarks! {
create {
let caller: T::AccountId = whitelisted_caller();
transfer_initial_balance::<T>(caller.clone());
assert_ok!(Streaming::<T>::set_minimum_deposit(SystemOrigin::Root.into(), KSM, 0));

let recipient: T::AccountId = account("Streaming", 101, SEED);
let deposit_amount: u128 = dollar(5);
let start_time: u64 = 6;
Expand All @@ -64,6 +66,7 @@ benchmarks! {
cancel {
let caller: T::AccountId = whitelisted_caller();
transfer_initial_balance::<T>(caller.clone());
assert_ok!(Streaming::<T>::set_minimum_deposit(SystemOrigin::Root.into(), KSM, 0));
let recipient: T::AccountId = account("Streaming", 101, SEED);
let deposit_amount: u128 = dollar(5);
let start_time: u64 = 6;
Expand All @@ -78,6 +81,8 @@ benchmarks! {
withdraw {
let caller: T::AccountId = whitelisted_caller();
transfer_initial_balance::<T>(caller.clone());
assert_ok!(Streaming::<T>::set_minimum_deposit(SystemOrigin::Root.into(), KSM, 0));

let recipient: T::AccountId = account("Streaming", 101, SEED);

let deposit_amount: u128 = dollar(5);
Expand Down
9 changes: 6 additions & 3 deletions pallets/streaming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pub mod pallet {
pub enum Error<T> {
/// Sender as specified themselves as the recipient
RecipientIsAlsoSender,
/// Asset is not supported to create stream
InvalidAssetId,
/// Insufficient deposit size
DepositLowerThanMinimum,
/// Start time is before current block time
Expand Down Expand Up @@ -219,9 +221,10 @@ pub mod pallet {
let sender = ensure_signed(origin)?;
ensure!(sender != recipient, Error::<T>::RecipientIsAlsoSender);

let minimum_deposit = MinimumDeposits::<T>::get(asset_id);
let minimum_deposit =
Self::minimum_deposit(asset_id).ok_or(Error::<T>::InvalidAssetId)?;
ensure!(
deposit >= minimum_deposit.unwrap_or(1u128),
deposit >= minimum_deposit,
Error::<T>::DepositLowerThanMinimum
);

Expand Down Expand Up @@ -348,12 +351,12 @@ pub mod pallet {

stream.try_deduct(amount)?;
stream.try_complete()?;
Streams::<T>::insert(stream_id, stream.clone());
if stream.has_finished() {
Self::try_push_stream_library(&stream.sender, stream_id, StreamKind::Finish)?;
Self::try_push_stream_library(&recipient, stream_id, StreamKind::Finish)?;
Self::update_finished_stream_library(&stream.sender, &recipient)?;
}
Streams::<T>::insert(stream_id, stream.clone());

// Withdraw deposit from stream
T::Assets::transfer(
Expand Down
3 changes: 3 additions & 0 deletions pallets/streaming/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
// Set block number and time
System::set_block_number(0);
TimestampPallet::set_timestamp(6000);

// Set minimum deposit for DOT
Streaming::set_minimum_deposit(Origin::root(), DOT, dollar(0)).unwrap();
});
ext
}
14 changes: 8 additions & 6 deletions pallets/streaming/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn cancel_works_without_withdrawal() {
true,
));
// rate_per_secs: 101 / (19-6) = 7769230769230
let stream = <Streams<Test>>::get(stream_id_0).unwrap();
let stream = Streams::<Test>::get(stream_id_0).unwrap();
assert_eq!(
stream,
Stream::new(dollar(101), DOT, 7769230769230, ALICE, BOB, 6, 19, true,)
Expand Down Expand Up @@ -194,7 +194,7 @@ fn withdraw_with_slower_rate_works() {
true,
));
// rate_per_secs: 101 / (19-6) = 7769230769230
let stream = <Streams<Test>>::get(stream_id_0).unwrap();
let stream = Streams::<Test>::get(stream_id_0).unwrap();
assert_eq!(
stream,
Stream::new(dollar(101), DOT, 7769230769230, ALICE, BOB, 6, 19, true,)
Expand Down Expand Up @@ -277,7 +277,7 @@ fn cancel_works_with_withdrawal() {
true,
));
// rate_per_secs: 101 / (19-6) = 7769230769230
let stream = <Streams<Test>>::get(stream_id_0).unwrap();
let stream = Streams::<Test>::get(stream_id_0).unwrap();
assert_eq!(
stream,
Stream::new(dollar(101), DOT, 7769230769230, ALICE, BOB, 6, 19, true,)
Expand Down Expand Up @@ -649,10 +649,12 @@ fn create_with_minimum_deposit_works() {
Error::<Test>::DepositLowerThanMinimum
);

// Check with default option
// Asset is not supported to create stream
Assets::force_create(Origin::root(), USDT, ALICE, true, 1).unwrap();
Assets::mint(Origin::signed(ALICE), USDT, ALICE, dollar(10000)).unwrap();
assert_err!(
Streaming::create(Origin::signed(ALICE), BOB, 0, KSM, 6, 10, true),
Error::<Test>::DepositLowerThanMinimum
Streaming::create(Origin::signed(ALICE), BOB, dollar(99), USDT, 6, 10, true),
Error::<Test>::InvalidAssetId
);
})
}

0 comments on commit 33bb165

Please sign in to comment.