pallet_timegraph/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::manual_inspect)]
//! This pallet provides functionality for users to deposit and withdraw funds.
//! It maintains a sequence for deposits and withdrawals to ensure order
//!  and prevent replay attacks.
//!
//!
#![doc = simple_mermaid::mermaid!("../docs/timegraph_flows.mmd")]

pub use pallet::*;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

#[polkadot_sdk::frame_support::pallet]
pub mod pallet {
	use polkadot_sdk::{frame_support, frame_system, sp_runtime};

	use frame_support::pallet_prelude::*;
	use frame_support::traits::{Currency, ExistenceRequirement, ReservableCurrency};
	use frame_system::pallet_prelude::*;
	use sp_runtime::traits::Saturating;

	pub type BalanceOf<T> =
		<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

	pub trait WeightInfo {
		fn deposit() -> Weight;
		fn withdraw() -> Weight;
		fn transfer_to_pool() -> Weight;
		fn transfer_award_to_user() -> Weight;
		fn set_timegraph_account() -> Weight;
		fn set_reward_pool_account() -> Weight;
		fn set_threshold() -> Weight;
	}

	impl WeightInfo for () {
		fn deposit() -> Weight {
			Weight::default()
		}

		fn withdraw() -> Weight {
			Weight::default()
		}

		fn transfer_to_pool() -> Weight {
			Weight::default()
		}

		fn transfer_award_to_user() -> Weight {
			Weight::default()
		}

		fn set_timegraph_account() -> Weight {
			Weight::default()
		}

		fn set_reward_pool_account() -> Weight {
			Weight::default()
		}

		fn set_threshold() -> Weight {
			Weight::default()
		}
	}

	#[pallet::pallet]
	#[pallet::without_storage_info]
	pub struct Pallet<T>(_);

	#[pallet::config]
	pub trait Config: polkadot_sdk::frame_system::Config {
		type RuntimeEvent: From<Event<Self>>
			+ IsType<<Self as polkadot_sdk::frame_system::Config>::RuntimeEvent>;
		type WeightInfo: WeightInfo;
		type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
		#[pallet::constant]
		type InitialThreshold: Get<BalanceOf<Self>>;
		#[pallet::constant]
		type InitialTimegraphAccount: Get<Self::AccountId>;
		#[pallet::constant]
		type InitialRewardPoolAccount: Get<Self::AccountId>;
		type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;
	}

	///Stores the next deposit sequence number for each account.
	#[pallet::storage]
	#[pallet::getter(fn next_deposit_sequence)]
	pub type NextDepositSequence<T: Config> =
		StorageMap<_, Blake2_128Concat, T::AccountId, u64, ValueQuery>;

	///Stores the next withdrawal sequence number for each account.
	#[pallet::storage]
	#[pallet::getter(fn next_withdrawal_sequence)]
	pub type NextWithdrawalSequence<T: Config> =
		StorageMap<_, Blake2_128Concat, T::AccountId, u64, ValueQuery>;

	#[pallet::storage]
	#[pallet::getter(fn timegraph_account)]
	pub type TimegraphAccount<T: Config> =
		StorageValue<_, T::AccountId, ValueQuery, T::InitialTimegraphAccount>;

	#[pallet::storage]
	#[pallet::getter(fn reward_pool_account)]
	pub type RewardPoolAccount<T: Config> =
		StorageValue<_, T::AccountId, ValueQuery, T::InitialRewardPoolAccount>;

	#[pallet::storage]
	#[pallet::getter(fn threshold)]
	pub type Threshold<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery, T::InitialThreshold>;

	#[pallet::event]
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
	pub enum Event<T: Config> {
		/// Deposit event
		///
		/// # Parameters
		/// - `who`: The account ID of the user making the deposit.
		/// - `amount`: The amount of funds deposited.
		/// - `sequence`: The sequence number of the deposit.
		Deposit { who: T::AccountId, amount: BalanceOf<T>, sequence: u64 },
		/// Withdrawal event
		///
		/// # Parameters
		/// - `who`: The account ID of the user making the withdrawal.
		/// - `amount`: The amount of funds withdrawn.
		/// - `sequence`: The sequence number of the withdrawal.
		Withdrawal { who: T::AccountId, amount: BalanceOf<T>, sequence: u64 },

		/// Transfer to pool event
		///
		/// # Parameters
		/// - `from`: The account ID of the user transferring the funds.
		/// - `to`: The account ID of the pool receiving the funds.
		/// - `amount`: The amount of funds transferred.
		TransferToPool { from: T::AccountId, to: T::AccountId, amount: BalanceOf<T> },

		/// Transfer award to user event
		///
		/// # Parameters
		/// - `from`: The account ID of the pool transferring the award.
		/// - `to`: The account ID of the user receiving the award.
		/// - `amount`: The amount of award transferred.
		TransferAwardToUser { from: T::AccountId, to: T::AccountId, amount: BalanceOf<T> },

		/// Timegraph account reset event
		///
		/// # Parameters
		/// - `old`: The old timegraph account ID.
		/// - `new`: The new timegraph account ID.
		TimegraphAccountReset { old: T::AccountId, new: T::AccountId },

		/// Reward pool account reset event
		///
		/// # Parameters
		/// - `old`: The old reward pool account ID.
		/// - `new`: The new reward pool account ID.
		RewardPoolAccountReset { old: T::AccountId, new: T::AccountId },

		/// Threshold reset event
		///
		/// # Parameters
		/// - `old`: The old threshold value.
		/// - `new`: The new threshold value.
		ThresholdReset { old: BalanceOf<T>, new: BalanceOf<T> },
	}

	#[pallet::error]
	pub enum Error<T> {
		/// sequence number overflow
		SequenceNumberOverflow,
		/// zero amount
		ZeroAmount,
		/// must keep the threshold until account is removed.
		WithdrawalAmountOverReserve,
		/// The amount to be withdrawn is not required.
		NotWithdrawalRequired,
		/// The reward pool does not have enough balance to complete the operation.
		RewardPoolOutOfBalance,
		/// The reward cannot be transferred to the same account.
		RewardToSameAccount,
		/// The new timegraph account cannot be the same as the old one.
		SameTimegraphAccount,
		/// The new reward pool account cannot be the same as the old one.
		SameRewardPoolAccount,
		/// The new threshold cannot be the same as the old one.
		SameThreshold,
		/// The sender is not a timegraph account.
		SenderIsNotTimegraph,
	}

	#[pallet::call]
	impl<T: Config> Pallet<T> {
		/// The extrinsic from timegraph allows user to deposit funds into the timegraph account
		///
		/// #  Flow
		/// 1. Ensure the origin is a signed account.
		/// 2. Validate the amount is greater than zero.
		/// 3. Ensure the sender .
		/// 4. Transfer the funds.
		/// 5. Increment the deposit sequence number for origin.
		/// 6. Emit a [`Event::Deposit`] event.
		#[pallet::call_index(0)]
		#[pallet::weight(T::WeightInfo::deposit())]
		pub fn deposit(origin: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResult {
			let who = ensure_signed(origin)?;
			ensure!(amount > 0_u32.into(), Error::<T>::ZeroAmount);

			T::Currency::reserve(&who, amount)?;

			NextDepositSequence::<T>::try_mutate(&who, |x| -> DispatchResult {
				*x = x.checked_add(1).ok_or(Error::<T>::SequenceNumberOverflow)?;
				Ok(())
			})?;

			Self::deposit_event(Event::Deposit {
				who: who.clone(),
				amount,
				sequence: NextDepositSequence::<T>::get(who),
			});

			Ok(())
		}

		/// The extrinsic from timegraph allows account to refund the token to the user
		///
		/// # Flow
		/// 1. Ensure the origin is a signed account.
		/// 2. Validate the amount is greater than zero.
		/// 3. Ensure the sender and receiver are not the same.
		/// 4. Validate the withdrawal sequence number.
		/// 5. Transfer the funds.
		/// 6. Increment the withdrawal sequence number.
		/// 7. Emit a [`Event::Withdrawal`] event.
		#[pallet::call_index(1)]
		#[pallet::weight(T::WeightInfo::withdraw())]
		pub fn withdraw(origin: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResult {
			let who = ensure_signed(origin)?;
			ensure!(amount > 0_u32.into(), Error::<T>::ZeroAmount);

			let current_reserve = T::Currency::reserved_balance(&who);

			let threshold = Threshold::<T>::get();

			ensure!(
				amount.saturating_add(threshold) <= current_reserve,
				Error::<T>::WithdrawalAmountOverReserve
			);

			ensure!(
				T::Currency::unreserve(&who, amount) == 0_u32.into(),
				Error::<T>::NotWithdrawalRequired
			);

			NextWithdrawalSequence::<T>::try_mutate(&who, |x| -> DispatchResult {
				*x = x.checked_add(1).ok_or(Error::<T>::SequenceNumberOverflow)?;
				Ok(())
			})?;

			Self::deposit_event(Event::Withdrawal {
				who: who.clone(),
				amount,
				sequence: NextWithdrawalSequence::<T>::get(&who),
			});
			Ok(())
		}

		/// The extrinsic from timegraph allows transferring funds to the reward pool
		///
		/// # Flow
		/// 1. Ensure the origin is the timegraph account.
		/// 2. Unreserve the specified amount from the given account.
		/// 3. Transfer the unreserved funds to the reward pool account.
		/// 4. Emit a [`Event::TransferToPool`] event.
		#[pallet::call_index(2)]
		#[pallet::weight(T::WeightInfo::transfer_to_pool())]
		pub fn transfer_to_pool(
			origin: OriginFor<T>,
			account: T::AccountId,
			amount: BalanceOf<T>,
		) -> DispatchResult {
			Self::ensure_timegraph(origin)?;
			let unserved = T::Currency::unreserve(&account, amount);
			ensure!(unserved == 0_u32.into(), Error::<T>::NotWithdrawalRequired);

			T::Currency::transfer(
				&account,
				&RewardPoolAccount::<T>::get(),
				amount,
				ExistenceRequirement::KeepAlive,
			)?;

			Self::deposit_event(Event::TransferToPool {
				from: account.clone(),
				to: RewardPoolAccount::<T>::get(),
				amount,
			});

			Ok(())
		}

		/// The extrinsic from timegraph allows transferring awards to a user
		///
		/// # Flow
		/// 1. Ensure the origin is the timegraph account.
		/// 2. Ensure the account is not the reward pool account.
		/// 3. Check if the reward pool has enough balance.
		/// 4. Transfer the specified amount from the reward pool account to the given account.
		/// 5. Reserve the transferred amount in the given account.
		/// 6. Emit a [`Event::TransferAwardToUser`] event.
		#[pallet::call_index(3)]
		#[pallet::weight(T::WeightInfo::transfer_award_to_user())]
		pub fn transfer_award_to_user(
			origin: OriginFor<T>,
			account: T::AccountId,
			amount: BalanceOf<T>,
		) -> DispatchResult {
			Self::ensure_timegraph(origin)?;
			ensure!(account != RewardPoolAccount::<T>::get(), Error::<T>::RewardToSameAccount);

			let pool_account = RewardPoolAccount::<T>::get();
			let pool_balance = T::Currency::free_balance(&pool_account);
			ensure!(pool_balance > amount, Error::<T>::RewardPoolOutOfBalance);

			T::Currency::transfer(
				&pool_account,
				&account,
				amount,
				ExistenceRequirement::KeepAlive,
			)?;

			T::Currency::reserve(&account, amount)?;

			Self::deposit_event(Event::TransferAwardToUser {
				from: RewardPoolAccount::<T>::get(),
				to: account,
				amount,
			});

			Ok(())
		}

		/// The extrinsic allows setting a new timegraph account
		///
		/// # Flow
		/// 1. Ensure the origin is the root account.
		/// 2. Ensure the new account is different from the current timegraph account.
		/// 3. Emit a [`Event::TimegraphAccountReset`] event.
		/// 4. Set the new timegraph account.
		#[pallet::call_index(4)]
		#[pallet::weight(T::WeightInfo::withdraw())]
		pub fn set_timegraph_account(
			origin: OriginFor<T>,
			account: T::AccountId,
		) -> DispatchResult {
			T::AdminOrigin::ensure_origin(origin)?;
			ensure!(
				account.clone() != TimegraphAccount::<T>::get(),
				Error::<T>::SameTimegraphAccount
			);

			Self::deposit_event(Event::TimegraphAccountReset {
				old: TimegraphAccount::<T>::get(),
				new: account.clone(),
			});

			TimegraphAccount::<T>::set(account);

			Ok(())
		}

		/// The extrinsic allows setting a new reward pool account
		///
		/// # Flow
		/// 1. Ensure the origin is the root account.
		/// 2. Ensure the new account is different from the current reward pool account.
		/// 3. Emit a [`Event::RewardPoolAccountReset`] event.
		/// 4. Set the new reward pool account.
		#[pallet::call_index(5)]
		#[pallet::weight(T::WeightInfo::withdraw())]
		pub fn set_reward_pool_account(
			origin: OriginFor<T>,
			account: T::AccountId,
		) -> DispatchResult {
			T::AdminOrigin::ensure_origin(origin)?;

			ensure!(
				account.clone() != RewardPoolAccount::<T>::get(),
				Error::<T>::SameRewardPoolAccount
			);

			Self::deposit_event(Event::RewardPoolAccountReset {
				old: RewardPoolAccount::<T>::get(),
				new: account.clone(),
			});

			RewardPoolAccount::<T>::set(account);

			Ok(())
		}

		/// The extrinsic allows setting a new threshold
		///
		/// # Flow
		/// 1. Ensure the origin is the root account.
		/// 2. Ensure the new threshold amount is different from the current threshold.
		/// 3. Emit a [`Event::ThresholdReset`] event.
		/// 4. Set the new threshold amount.
		#[pallet::call_index(6)]
		#[pallet::weight(T::WeightInfo::withdraw())]
		pub fn set_threshold(origin: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResult {
			T::AdminOrigin::ensure_origin(origin)?;

			ensure!(amount != Threshold::<T>::get(), Error::<T>::SameThreshold);

			Self::deposit_event(Event::ThresholdReset {
				old: Threshold::<T>::get(),
				new: amount,
			});

			Threshold::<T>::set(amount);

			Ok(())
		}
	}

	impl<T: Config> Pallet<T> {
		/// Ensures that the origin is the current timegraph account.
		///
		/// # Parameters
		/// - `origin`: The origin of the call, which must be a signed account.
		///
		/// # Errors
		/// - Returns `Error::<T>::SenderIsNotTimegraph` if the origin is not the current timegraph account.
		pub fn ensure_timegraph(origin: OriginFor<T>) -> DispatchResult {
			let who = ensure_signed(origin)?;
			let current_account = TimegraphAccount::<T>::get();
			ensure!(who == current_account, Error::<T>::SenderIsNotTimegraph);
			Ok(())
		}
	}
}