Skip to content

Commit 11eddc0

Browse files
No longer exposed unit_price to front-end (#195)
* ♻️ ($PALLET) No longer exposed unit_price to front-end * 🎨 ($PALLET) Format the code
1 parent ac9b7ba commit 11eddc0

2 files changed

Lines changed: 50 additions & 40 deletions

File tree

pallets/vsbond-auction/src/lib.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,27 @@ pub struct OrderInfo<T: Config> {
4545
supply: BalanceOf<T>,
4646
/// The quantity of vsbond has not be sold
4747
remain: BalanceOf<T>,
48-
unit_price: U64F64,
48+
total_price: BalanceOf<T>,
4949
order_id: OrderId,
5050
order_state: OrderState,
5151
}
5252

53+
impl<T: Config> OrderInfo<T> {
54+
pub fn unit_price(&self) -> U64F64 {
55+
let supply: u128 = self.supply.saturated_into();
56+
let total_price: u128 = self.total_price.saturated_into();
57+
58+
U64F64::from_num(total_price) / supply
59+
}
60+
}
61+
5362
impl<T: Config> core::fmt::Debug for OrderInfo<T> {
5463
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
5564
f.debug_tuple("")
5665
.field(&self.owner)
5766
.field(&self.vsbond)
5867
.field(&self.supply)
59-
.field(&self.unit_price)
68+
.field(&self.unit_price())
6069
.field(&self.order_id)
6170
.field(&self.order_state)
6271
.finish()
@@ -175,7 +184,7 @@ pub mod pallet {
175184
#[pallet::compact] first_slot: LeasePeriodOf<T>,
176185
#[pallet::compact] last_slot: LeasePeriodOf<T>,
177186
#[pallet::compact] supply: BalanceOf<T>,
178-
unit_price: U64F64,
187+
#[pallet::compact] total_price: BalanceOf<T>,
179188
) -> DispatchResultWithPostInfo {
180189
// Check origin
181190
let owner = ensure_signed(origin)?;
@@ -212,7 +221,7 @@ pub mod pallet {
212221
vsbond,
213222
supply,
214223
remain: supply,
215-
unit_price,
224+
total_price,
216225
order_id,
217226
order_state: OrderState::InTrade,
218227
};
@@ -343,11 +352,15 @@ pub mod pallet {
343352
// Calculate the real quantity to clinch
344353
let quantity_clinchd = min(order_info.remain, quantity);
345354
// Calculate the total price that buyer need to pay
346-
let total_price = Self::total_price(quantity_clinchd, order_info.unit_price);
355+
let price_to_pay = Self::price_to_pay(quantity_clinchd, order_info.unit_price());
347356

348357
// Check the balance of buyer
349-
T::MultiCurrency::ensure_can_withdraw(T::InvoicingCurrency::get(), &buyer, total_price)
350-
.map_err(|_| Error::<T>::CantPayThePrice)?;
358+
T::MultiCurrency::ensure_can_withdraw(
359+
T::InvoicingCurrency::get(),
360+
&buyer,
361+
price_to_pay,
362+
)
363+
.map_err(|_| Error::<T>::CantPayThePrice)?;
351364

352365
// Get the new OrderInfo
353366
let new_order_info = if quantity_clinchd == order_info.remain {
@@ -381,7 +394,7 @@ pub mod pallet {
381394
T::InvoicingCurrency::get(),
382395
&buyer,
383396
&new_order_info.owner,
384-
total_price,
397+
price_to_pay,
385398
)?;
386399

387400
// Move order_id from InTrade to Clinchd if meets condition
@@ -434,7 +447,8 @@ pub mod pallet {
434447
next_order_id
435448
}
436449

437-
pub(crate) fn total_price(quantity: BalanceOf<T>, unit_price: U64F64) -> BalanceOf<T> {
450+
/// Get the price(round up) needed to pay.
451+
pub(crate) fn price_to_pay(quantity: BalanceOf<T>, unit_price: U64F64) -> BalanceOf<T> {
438452
let quantity: u128 = quantity.saturated_into();
439453
let total_price = u128::from_fixed((unit_price * quantity).ceil());
440454

pallets/vsbond-auction/src/tests.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{mock::*, *};
2525
#[test]
2626
fn create_order_should_work() {
2727
new_test_ext().execute_with(|| {
28-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 1.to_fixed()));
28+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 100));
2929

3030
assert_eq!(Auction::order_id(), 1);
3131

@@ -46,8 +46,8 @@ fn create_order_should_work() {
4646
#[test]
4747
fn double_create_order_should_work() {
4848
new_test_ext().execute_with(|| {
49-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 1.to_fixed()));
50-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 1.to_fixed()));
49+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 50));
50+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 50));
5151

5252
assert_eq!(Auction::order_id(), 2);
5353

@@ -71,11 +71,11 @@ fn double_create_order_should_work() {
7171
fn create_order_by_origin_illegal_should_fail() {
7272
new_test_ext().execute_with(|| {
7373
assert_noop!(
74-
Auction::create_order(Origin::root(), 3000, 13, 20, 100, 1.to_fixed()),
74+
Auction::create_order(Origin::root(), 3000, 13, 20, 100, 100),
7575
DispatchError::BadOrigin
7676
);
7777
assert_noop!(
78-
Auction::create_order(Origin::none(), 3000, 13, 20, 100, 1.to_fixed()),
78+
Auction::create_order(Origin::none(), 3000, 13, 20, 100, 100),
7979
DispatchError::BadOrigin
8080
);
8181
});
@@ -85,7 +85,7 @@ fn create_order_by_origin_illegal_should_fail() {
8585
fn create_order_under_minimum_supply_should_fail() {
8686
new_test_ext().execute_with(|| {
8787
assert_noop!(
88-
Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 0, 1.to_fixed()),
88+
Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 0, 0),
8989
Error::<Test>::NotEnoughSupply
9090
);
9191
});
@@ -95,14 +95,14 @@ fn create_order_under_minimum_supply_should_fail() {
9595
fn create_order_without_enough_vsbond_should_fail() {
9696
new_test_ext().execute_with(|| {
9797
assert_noop!(
98-
Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 1000, 1.to_fixed()),
98+
Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 1000, 1000),
9999
Error::<Test>::NotEnoughBalanceToReserve,
100100
);
101101

102102
const LOCK_ID: LockIdentifier = 0u64.to_be_bytes();
103103
assert_ok!(Tokens::set_lock(LOCK_ID, VSBOND, &ALICE, 50));
104104
assert_noop!(
105-
Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 51, 1.to_fixed()),
105+
Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 51, 51),
106106
Error::<Test>::NotEnoughBalanceToReserve,
107107
);
108108
});
@@ -112,11 +112,11 @@ fn create_order_without_enough_vsbond_should_fail() {
112112
fn create_order_exceed_maximum_order_in_trade_should_fail() {
113113
new_test_ext().execute_with(|| {
114114
for _ in 0..MaximumOrderInTrade::get() {
115-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 1, 1.to_fixed()));
115+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 1, 1));
116116
}
117117

118118
assert_noop!(
119-
Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 1, 1.to_fixed()),
119+
Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 1, 1),
120120
Error::<Test>::ExceedMaximumOrderInTrade,
121121
);
122122
});
@@ -125,7 +125,7 @@ fn create_order_exceed_maximum_order_in_trade_should_fail() {
125125
#[test]
126126
fn revoke_order_should_work() {
127127
new_test_ext().execute_with(|| {
128-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 1.to_fixed()));
128+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 100));
129129
assert_ok!(Auction::revoke_order(Some(ALICE).into(), 0));
130130

131131
assert_eq!(Auction::order_id(), 1);
@@ -150,7 +150,7 @@ fn revoke_order_should_work() {
150150
#[test]
151151
fn revoke_order_which_be_partial_clinchd_should_work() {
152152
new_test_ext().execute_with(|| {
153-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 1.to_fixed()));
153+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 50));
154154
assert_ok!(Auction::partial_clinch_order(Some(BRUCE).into(), 0, 25));
155155
assert_ok!(Auction::revoke_order(Some(ALICE).into(), 0));
156156

@@ -177,18 +177,14 @@ fn revoke_order_which_be_partial_clinchd_should_work() {
177177
#[test]
178178
fn revoke_order_not_exist_should_fail() {
179179
new_test_ext().execute_with(|| {
180-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 1.to_fixed()));
181-
assert_noop!(
182-
Auction::partial_clinch_order(Some(BRUCE).into(), 1, 25),
183-
Error::<Test>::NotFindOrderInfo
184-
);
180+
assert_noop!(Auction::revoke_order(Some(ALICE).into(), 0), Error::<Test>::NotFindOrderInfo);
185181
});
186182
}
187183

188184
#[test]
189185
fn revoke_order_without_enough_reserved_vsbond_should_fail() {
190186
new_test_ext().execute_with(|| {
191-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 1.to_fixed()));
187+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 50));
192188
assert_ok!(Tokens::repatriate_reserved(
193189
VSBOND,
194190
&ALICE,
@@ -206,7 +202,7 @@ fn revoke_order_without_enough_reserved_vsbond_should_fail() {
206202
#[test]
207203
fn revoke_order_by_origin_illegal_should_fail() {
208204
new_test_ext().execute_with(|| {
209-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 1.to_fixed()));
205+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 50));
210206
assert_noop!(
211207
Auction::revoke_order(Some(BRUCE).into(), 0),
212208
Error::<Test>::ForbidRevokeOrderWithoutOwnership
@@ -219,14 +215,14 @@ fn revoke_order_by_origin_illegal_should_fail() {
219215
#[test]
220216
fn revoke_order_not_in_trade_should_fail() {
221217
new_test_ext().execute_with(|| {
222-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 1.to_fixed()));
218+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 50));
223219
assert_ok!(Auction::revoke_order(Some(ALICE).into(), 0));
224220
assert_noop!(
225221
Auction::revoke_order(Some(ALICE).into(), 0),
226222
Error::<Test>::ForbidRevokeOrderNotInTrade
227223
);
228224

229-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 1.to_fixed()));
225+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 50, 50));
230226
assert_ok!(Auction::clinch_order(Some(BRUCE).into(), 1));
231227
assert_noop!(
232228
Auction::revoke_order(Some(ALICE).into(), 1),
@@ -238,7 +234,7 @@ fn revoke_order_not_in_trade_should_fail() {
238234
#[test]
239235
fn partial_clinch_order_should_work() {
240236
new_test_ext().execute_with(|| {
241-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 0.33.to_fixed()));
237+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 33));
242238
assert_ok!(Auction::partial_clinch_order(Some(BRUCE).into(), 0, 33));
243239

244240
assert_eq!(Auction::order_id(), 1);
@@ -304,7 +300,7 @@ fn partial_clinch_order_should_work() {
304300
#[test]
305301
fn partial_clinch_order_not_exist_should_fail() {
306302
new_test_ext().execute_with(|| {
307-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 1.to_fixed()));
303+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 100));
308304
assert_noop!(Auction::clinch_order(Some(BRUCE).into(), 1), Error::<Test>::NotFindOrderInfo);
309305
assert_noop!(
310306
Auction::partial_clinch_order(Some(BRUCE).into(), 1, 50),
@@ -316,7 +312,7 @@ fn partial_clinch_order_not_exist_should_fail() {
316312
#[test]
317313
fn clinch_order_by_origin_illegal_should_fail() {
318314
new_test_ext().execute_with(|| {
319-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 1.to_fixed()));
315+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 100));
320316
assert_noop!(
321317
Auction::clinch_order(Some(ALICE).into(), 0),
322318
Error::<Test>::ForbidClinchOrderWithinOwnership
@@ -340,14 +336,14 @@ fn clinch_order_by_origin_illegal_should_fail() {
340336
#[test]
341337
fn clinch_order_not_in_trade_should_fail() {
342338
new_test_ext().execute_with(|| {
343-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 1.to_fixed()));
339+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 100));
344340
assert_ok!(Auction::revoke_order(Some(ALICE).into(), 0));
345341
assert_noop!(
346342
Auction::partial_clinch_order(Some(BRUCE).into(), 0, 50),
347343
Error::<Test>::ForbidClinchOrderNotInTrade
348344
);
349345

350-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 1.to_fixed()));
346+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 100));
351347
assert_ok!(Auction::clinch_order(Some(BRUCE).into(), 1));
352348
assert_noop!(
353349
Auction::partial_clinch_order(Some(BRUCE).into(), 1, 50),
@@ -359,20 +355,20 @@ fn clinch_order_not_in_trade_should_fail() {
359355
#[test]
360356
fn clinch_order_without_enough_token_should_fail() {
361357
new_test_ext().execute_with(|| {
362-
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 2.to_fixed()));
358+
assert_ok!(Auction::create_order(Some(ALICE).into(), 3000, 13, 20, 100, 200));
363359
assert_ok!(Auction::partial_clinch_order(Some(BRUCE).into(), 0, 50));
364360
assert_noop!(Auction::clinch_order(Some(BRUCE).into(), 0), Error::<Test>::CantPayThePrice);
365361
});
366362
}
367363

368364
// Test Utilities
369365
#[test]
370-
fn check_total_price() {
366+
fn check_price_to_pay() {
371367
let unit_price: U64F64 = 0.333f64.to_fixed();
372368
let quantities: [BalanceOf<Test>; 4] = [3, 33, 333, 3333];
373-
let total_prices: [BalanceOf<Test>; 4] = [1, 11, 111, 1110];
369+
let price_to_pays: [BalanceOf<Test>; 4] = [1, 11, 111, 1110];
374370

375-
for (quantity, total_price) in quantities.iter().zip(total_prices.iter()) {
376-
assert_eq!(Auction::total_price(*quantity, unit_price), *total_price);
371+
for (quantity, price_to_pay) in quantities.iter().zip(price_to_pays.iter()) {
372+
assert_eq!(Auction::price_to_pay(*quantity, unit_price), *price_to_pay);
377373
}
378374
}

0 commit comments

Comments
 (0)