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
pub use super::*;
impl<T: Config> Pallet<T> {
pub fn balance_to_u128_option(input: BalanceOf<T>) -> Option<u128> {
input.try_into().ok()
}
pub fn u128_to_balance_option(input: u128) -> Option<BalanceOf<T>> {
input.try_into().ok()
}
}
impl<T: Config> PaymentHandler<T> for Pallet<T> {
#[require_transactional]
fn create_payment(
from: &T::AccountId,
recipient: &T::AccountId,
amount: BalanceOf<T>,
payment_state: PaymentState<T>,
incentive_percentage: Percent,
remark: Option<&[u8]>,
) -> Result<PaymentDetail<T>, sp_runtime::DispatchError> {
Payment::<T>::try_mutate(
from,
recipient,
|maybe_payment| -> Result<PaymentDetail<T>, sp_runtime::DispatchError> {
if let Some(payment) = maybe_payment {
ensure!(
payment.state == PaymentState::PaymentRequested,
Error::<T>::PaymentAlreadyInProcess
);
}
let incentive_amount = incentive_percentage.mul_floor(amount);
let mut new_payment = PaymentDetail {
amount,
incentive_amount,
state: payment_state,
resolver_account: T::DisputeResolver::get_resolver_account(),
fee_detail: None,
};
let (fee_recipient, fee_percent) =
T::FeeHandler::apply_fees(from, recipient, &new_payment, remark);
let fee_amount = fee_percent.mul_floor(amount);
new_payment.fee_detail = Some((fee_recipient, fee_amount));
*maybe_payment = Some(new_payment.clone());
Ok(new_payment)
},
)
}
#[require_transactional]
fn reserve_payment_amount(
from: &T::AccountId,
to: &T::AccountId,
payment: PaymentDetail<T>,
) -> DispatchResult {
let fee_amount = payment.fee_detail.map(|(_, f)| f).unwrap_or_else(|| 0u32.into());
let total_fee_amount = payment.incentive_amount.saturating_add(fee_amount);
let total_amount = total_fee_amount.saturating_add(payment.amount);
T::Currency::reserve(from, total_amount)?;
T::Currency::repatriate_reserved(from, to, payment.amount, BalanceStatus::Reserved)?;
Ok(())
}
fn settle_payment(
from: &T::AccountId,
to: &T::AccountId,
recipient_share: Percent,
) -> DispatchResult {
Payment::<T>::try_mutate(from, to, |maybe_payment| -> DispatchResult {
let payment = maybe_payment.take().ok_or(Error::<T>::InvalidPayment)?;
match payment.fee_detail {
Some((fee_recipient, fee_amount)) => {
T::Currency::unreserve(from, payment.incentive_amount + fee_amount);
if recipient_share != Percent::zero() {
T::Currency::transfer(
from, &fee_recipient, fee_amount, AllowDeath,
)?;
}
},
None => {
T::Currency::unreserve(from, payment.incentive_amount);
},
};
T::Currency::unreserve(to, payment.amount);
let amount_to_recipient = recipient_share.mul_floor(payment.amount);
let amount_to_sender = payment.amount.saturating_sub(amount_to_recipient);
T::Currency::transfer(to, from, amount_to_sender, AllowDeath)?;
Ok(())
})?;
Ok(())
}
fn get_payment_details(from: &T::AccountId, to: &T::AccountId) -> Option<PaymentDetail<T>> {
Payment::<T>::get(from, to)
}
}
impl<T: Config> Pallet<T> {
pub fn check_task(now: T::BlockNumber, remaining_weight: Weight) -> Weight {
const MAX_TASKS_TO_PROCESS: usize = 5;
remaining_weight.saturating_sub(T::WeightInfo::remove_task());
let cancel_weight = T::WeightInfo::cancel();
let possible_task_count: usize = remaining_weight
.ref_time()
.saturating_div(cancel_weight.ref_time())
.try_into()
.unwrap_or(MAX_TASKS_TO_PROCESS);
ScheduledTasks::<T>::mutate(|tasks| {
let mut task_list: Vec<_> = tasks
.clone()
.into_iter()
.take(possible_task_count)
.filter(|(_, ScheduledTask { when, task })| {
when <= &now && matches!(task, Task::Cancel)
})
.collect();
task_list.sort_by(|(_, t), (_, x)| x.when.cmp(&t.when));
while !task_list.is_empty() && remaining_weight >= cancel_weight {
if let Some((account_pair, _)) = task_list.pop() {
remaining_weight.saturating_sub(cancel_weight);
tasks.remove(&account_pair);
if <Self as PaymentHandler<T>>::settle_payment(
&account_pair.0,
&account_pair.1,
Percent::from_percent(0),
)
.is_err()
{
log::warn!(
target: "runtime::payments",
"Warning: Unable to process payment refund!"
);
} else {
Self::deposit_event(Event::PaymentCancelled {
from: account_pair.0,
to: account_pair.1,
});
}
}
}
});
remaining_weight
}
}