Skip to content

Commit

Permalink
Fix/claimed balance under ed in stream (#1866)
Browse files Browse the repository at this point in the history
* add ExistentialDeposit in Streaming

* add test

* add runtime

* fix

* add config NativeCurrencyId

* add docker stake-client-fast-match-unstake
  • Loading branch information
mclyk authored Sep 6, 2022
1 parent e33105c commit 3a01928
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ services:
image: parallelfinance/stake-client:latest
command: sync ledger --relay-ws=ws://relaychain-bob:9944 --para-ws=ws://parachain-${PARA_ID}-0:9944 --derivative-index 1
restart: always
stake-client-fast-match-unstake:
image: parallelfinance/stake-client:latest
command: fast-match-unstake --para-ws=ws://parachain-${PARA_ID}-0:9944 --batch-size 30
restart: always
liquidation-client:
image: parallelfinance/liquidation-client:latest
command: --endpoint ws://parachain-${PARA_ID}-0:9944
Expand Down
16 changes: 15 additions & 1 deletion pallets/streaming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ pub mod pallet {
#[pallet::constant]
type MaxFinishedStreamsCount: Get<u32>;

/// Currency id of the native token
#[pallet::constant]
type NativeCurrencyId: Get<AssetIdOf<Self>>;

/// The essential balance for an existed account
#[pallet::constant]
type NativeExistentialDeposit: Get<Balance>;

/// The Unix time
type UnixTime: UnixTime;

Expand Down Expand Up @@ -335,7 +343,7 @@ pub mod pallet {
pub fn withdraw(
origin: OriginFor<T>,
stream_id: StreamId,
amount: BalanceOf<T>,
mut amount: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let recipient = ensure_signed(origin)?;

Expand All @@ -349,6 +357,12 @@ pub mod pallet {
Error::<T>::InsufficientStreamBalance
);

if stream.asset_id == T::NativeCurrencyId::get()
&& amount.saturating_add(T::NativeExistentialDeposit::get()) >= recipient_balance
{
amount = recipient_balance
}

stream.try_deduct(amount)?;
stream.try_complete()?;
Streams::<T>::insert(stream_id, stream.clone());
Expand Down
4 changes: 3 additions & 1 deletion pallets/streaming/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl pallet_timestamp::Config for Test {
}

parameter_types! {
pub const ExistentialDeposit: Balance = 1;
pub const ExistentialDeposit: Balance = 1_000;
pub const MaxLocks: u32 = 50;
}

Expand Down Expand Up @@ -158,6 +158,8 @@ impl Config for Test {
type Assets = CurrencyAdapter;
type UpdateOrigin = EnsureRoot<AccountId>;
type WeightInfo = ();
type NativeCurrencyId = NativeCurrencyId;
type NativeExistentialDeposit = ExistentialDeposit;
}

pub fn dollar(d: u128) -> u128 {
Expand Down
96 changes: 91 additions & 5 deletions pallets/streaming/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ fn withdraw_with_slower_rate_works() {
assert_eq!(stream.sender_balance().unwrap(), 7769230769240);
assert_eq!(stream.recipient_balance().unwrap(), 93230769230760);

// Bob withdraw all available balance (93230769230759 + 1 = 93230769230760)
// Bob withdraw all available balance (93230769229759 + 1 + 1000 = 93230769230760)
assert_ok!(Streaming::withdraw(
Origin::signed(BOB),
stream_id_0,
93230769230759
93230769229759
));
// withdraw a small value should be ok
assert_ok!(Streaming::withdraw(Origin::signed(BOB), stream_id_0, 1));
assert_ok!(Streaming::withdraw(Origin::signed(BOB), stream_id_0, 1001));

stream = Streams::<Test>::get(stream_id_0).unwrap();
assert_eq!(stream.sender_balance().unwrap(), 7769230769240);
Expand All @@ -249,10 +249,10 @@ fn withdraw_with_slower_rate_works() {
assert_ok!(Streaming::withdraw(
Origin::signed(BOB),
stream_id_0,
7769230769239
7769230768239
));

assert_ok!(Streaming::withdraw(Origin::signed(BOB), stream_id_0, 1));
assert_ok!(Streaming::withdraw(Origin::signed(BOB), stream_id_0, 1001));

// Stream is removed as balance goes zero
assert_eq!(
Expand All @@ -262,6 +262,92 @@ fn withdraw_with_slower_rate_works() {
});
}

#[test]
fn withdraw_under_ed_works() {
new_test_ext().execute_with(|| {
assert_ok!(Streaming::set_minimum_deposit(
Origin::root(),
HKO,
dollar(10)
));
let before_bob = <Test as Config>::Assets::balance(HKO, &BOB);
assert_eq!(TimestampPallet::now(), 6000);
// Alice creates stream 101 dollars to Bob
let stream_id_0 = NextStreamId::<Test>::get();
assert_ok!(Streaming::create(
Origin::signed(ALICE),
BOB,
dollar(101),
HKO,
6,
19,
true,
));
// rate_per_secs: 101 / (19-6) = 7769230769230
let stream = Streams::<Test>::get(stream_id_0).unwrap();
assert_eq!(
stream,
Stream::new(dollar(101), HKO, 7769230769230, ALICE, BOB, 6, 19, true,)
);

// Dave cannot access
assert_err!(
Streaming::withdraw(Origin::signed(DAVE), 0, 1),
Error::<Test>::NotTheRecipient
);

// passed 11 seconds
TimestampPallet::set_timestamp(17000);
assert_eq!(stream.delta_of(), Ok(11));
// Should be 15538461538460, but add 10(amount) dut to accuracy loss
assert_eq!(stream.sender_balance().unwrap(), 15538461538470);
// per_rate_secs * 11 = 85461538461530
assert_eq!(stream.recipient_balance().unwrap(), 85461538461530);

// passed 12 seconds
TimestampPallet::set_timestamp(18000);
let mut stream = Streams::<Test>::get(stream_id_0).unwrap();
// delta of should only increase until end_time
assert_eq!(stream.delta_of(), Ok(12));
// Should be 7769230769230, but add 10(amount) dut to accuracy loss
assert_eq!(stream.sender_balance().unwrap(), 7769230769240);
assert_eq!(stream.recipient_balance().unwrap(), 93230769230760);

// Bob withdraw balance
let ed = <Test as Config>::NativeExistentialDeposit::get();
assert_ok!(Streaming::withdraw(
Origin::signed(BOB),
stream_id_0,
93230769230760 - ed + 1
));

stream = Streams::<Test>::get(stream_id_0).unwrap();
assert_eq!(stream.sender_balance().unwrap(), 7769230769240);
assert_eq!(stream.recipient_balance().unwrap(), 0);

// passed 14 seconds
TimestampPallet::set_timestamp(20000);
stream = Streams::<Test>::get(stream_id_0).unwrap();
assert_eq!(stream.delta_of(), Ok(13));
assert_eq!(stream.sender_balance().unwrap(), 0);
// Reaches the end_time, returned amount should contains the accuracy loss(10)
// recipient_balance = 7769230769230 + 10
assert_eq!(stream.recipient_balance().unwrap(), 7769230769240);

// Bob withdraw remaining_balance
assert_ok!(Streaming::withdraw(
Origin::signed(BOB),
stream_id_0,
7769230769240 - ed + 1
));
// Stream is removed as balance goes zero
assert_eq!(
<Test as Config>::Assets::balance(HKO, &BOB) - before_bob,
dollar(101)
);
});
}

#[test]
fn cancel_works_with_withdrawal() {
new_test_ext().execute_with(|| {
Expand Down
2 changes: 2 additions & 0 deletions runtime/heiko/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,8 @@ impl pallet_streaming::Config for Runtime {
type UnixTime = Timestamp;
type UpdateOrigin = EnsureRootOrMoreThanHalfGeneralCouncil;
type WeightInfo = weights::pallet_streaming::WeightInfo<Runtime>;
type NativeCurrencyId = NativeCurrencyId;
type NativeExistentialDeposit = ExistentialDeposit;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions runtime/kerria/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,8 @@ impl pallet_streaming::Config for Runtime {
type UnixTime = Timestamp;
type UpdateOrigin = EnsureRootOrMoreThanHalfGeneralCouncil;
type WeightInfo = weights::pallet_streaming::WeightInfo<Runtime>;
type NativeCurrencyId = NativeCurrencyId;
type NativeExistentialDeposit = ExistentialDeposit;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions runtime/parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,8 @@ impl pallet_streaming::Config for Runtime {
type UnixTime = Timestamp;
type UpdateOrigin = EnsureRootOrMoreThanHalfGeneralCouncil;
type WeightInfo = weights::pallet_streaming::WeightInfo<Runtime>;
type NativeCurrencyId = NativeCurrencyId;
type NativeExistentialDeposit = ExistentialDeposit;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions runtime/vanilla/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2176,6 +2176,8 @@ impl pallet_streaming::Config for Runtime {
type UnixTime = Timestamp;
type UpdateOrigin = EnsureRootOrMoreThanHalfGeneralCouncil;
type WeightInfo = weights::pallet_streaming::WeightInfo<Runtime>;
type NativeCurrencyId = NativeCurrencyId;
type NativeExistentialDeposit = ExistentialDeposit;
}

parameter_types! {
Expand Down

0 comments on commit 3a01928

Please sign in to comment.