Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions pallets/vsbond-auction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub mod module {
pub enum Error<T> {
NotEnoughSupply,
NotFindOrderInfo,
NotEnoughBalanceToUnreserve,
NotEnoughBalanceToReserve,
CantPayThePrice,
ForbidRevokeOrderNotInTrade,
ForbidRevokeOrderWithoutOwnership,
ForbidClinchOrderNotInTrade,
Expand Down Expand Up @@ -179,7 +182,10 @@ pub mod module {
CurrencyId::VSBond(*T::InvoicingCurrency::get(), index, first_slot, last_slot);

// Check the balance of vsbond
T::MultiCurrency::ensure_can_withdraw(vsbond, &owner, supply)?;
ensure!(
T::MultiCurrency::can_reserve(vsbond, &owner, supply),
Error::<T>::NotEnoughBalanceToReserve
);

let order_in_trade_amount = {
if let Some(sets) = Self::in_trade_order_ids(&owner) {
Expand All @@ -205,9 +211,8 @@ pub mod module {
order_state: OrderState::InTrade,
};

// Lock the balance of vsbond_type
let lock_iden = order_id.to_be_bytes();
T::MultiCurrency::set_lock(lock_iden, vsbond, &owner, supply)?;
// Reserve the balance of vsbond_type
T::MultiCurrency::reserve(vsbond, &owner, supply)?;

// Insert OrderInfo to Storage
TotalOrderInfos::<T>::insert(order_id, order_info.clone());
Expand Down Expand Up @@ -249,9 +254,11 @@ pub mod module {
// Check OrderOwner
ensure!(order_info.owner == from, Error::<T>::ForbidRevokeOrderWithoutOwnership);

// Unlock the vsbond
let lock_iden = order_info.order_id.to_be_bytes();
T::MultiCurrency::remove_lock(lock_iden, order_info.vsbond, &from)?;
// Unreserve the vsbond
let reserved_balance =
T::MultiCurrency::reserved_balance(order_info.vsbond, &order_info.owner);
ensure!(reserved_balance >= order_info.remain, Error::<T>::NotEnoughBalanceToUnreserve);
T::MultiCurrency::unreserve(order_info.vsbond, &order_info.owner, order_info.remain);

// Revoke order
TotalOrderInfos::<T>::insert(
Expand Down Expand Up @@ -334,6 +341,10 @@ pub mod module {
.checked_mul(&order_info.unit_price)
.ok_or(Error::<T>::Overflow)?;

// Check the balance of buyer
T::MultiCurrency::ensure_can_withdraw(T::InvoicingCurrency::get(), &buyer, total_price)
.map_err(|_| Error::<T>::CantPayThePrice)?;

// Get the new OrderInfo
let new_order_info = if quantity_clinchd == order_info.remain {
OrderInfo { remain: Zero::zero(), order_state: OrderState::Clinchd, ..order_info }
Expand All @@ -344,17 +355,16 @@ pub mod module {
}
};

// Decrease the locked amount of vsbond
let lock_iden = order_id.to_be_bytes();
T::MultiCurrency::remove_lock(lock_iden, new_order_info.vsbond, &new_order_info.owner)?;
T::MultiCurrency::set_lock(
lock_iden,
// Unreserve the balance of vsbond to transfer
let reserved_balance =
T::MultiCurrency::reserved_balance(new_order_info.vsbond, &new_order_info.owner);
ensure!(reserved_balance >= quantity_clinchd, Error::<T>::NotEnoughBalanceToUnreserve);
T::MultiCurrency::unreserve(
new_order_info.vsbond,
&new_order_info.owner,
new_order_info.remain,
)?;
quantity_clinchd,
);

// TODO: Maybe fail if double lock?
// Exchange: Transfer vsbond from owner to buyer
T::MultiCurrency::transfer(
new_order_info.vsbond,
Expand Down
26 changes: 9 additions & 17 deletions pallets/vsbond-auction/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use sp_runtime::{
generic,
traits::{BlakeTwo256, IdentityLookup},
};
use vsbond_auction::*;

use crate as vsbond_auction;

Expand All @@ -41,8 +40,8 @@ construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
OrmlAssets: orml_tokens::{Pallet, Call, Storage, Event<T>},
VSBondAuction: vsbond_auction::{Pallet, Call, Storage, Event<T>},
Tokens: orml_tokens::{Pallet, Call, Storage, Event<T>},
Auction: vsbond_auction::{Pallet, Call, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -119,10 +118,10 @@ pub(crate) fn new_test_ext() -> sp_io::TestExternalities {

orml_tokens::GenesisConfig::<Test> {
balances: vec![
(ACCOUNT_ALICE, TOKEN, BALANCE_TOKEN),
(ACCOUNT_ALICE, VSBOND, BALANCE_VSBOND),
(ACCOUNT_BRUCE, TOKEN, BALANCE_TOKEN),
(ACCOUNT_BRUCE, VSBOND, BALANCE_VSBOND),
(ALICE, TOKEN, 100),
(ALICE, VSBOND, 100),
(BRUCE, TOKEN, 100),
(BRUCE, VSBOND, 100),
],
}
.assimilate_storage(&mut fs_gc)
Expand All @@ -131,14 +130,7 @@ pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
fs_gc.into()
}

pub(crate) const ALICE: AccountId = 1;
pub(crate) const BRUCE: AccountId = 2;
pub(crate) const TOKEN: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
pub(crate) const PARA_ID: ParaId = 3000;
pub(crate) const FIRST_SLOT: LeasePeriod = 0;
pub(crate) const LAST_SLOT: LeasePeriod = 100;
pub(crate) const VSBOND: CurrencyId =
CurrencyId::VSBond(TokenSymbol::KSM, PARA_ID, FIRST_SLOT, LAST_SLOT);
pub(crate) const ACCOUNT_ALICE: AccountId = 1;
pub(crate) const ACCOUNT_BRUCE: AccountId = 2;
pub(crate) const BALANCE_VSBOND: Balance = 1_000;
pub(crate) const BALANCE_TOKEN: Balance = 1_000;
pub(crate) const UNIT_PRICE: Balance = 1;
pub(crate) const VSBOND: CurrencyId = CurrencyId::VSBond(TokenSymbol::KSM, 3000, 13, 20);
Loading