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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pallets/vsbond-auction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2018"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
sp-std = { version = "3.0.0", default-features = false }
substrate-fixed = { git = "https://github.com/encointer/substrate-fixed", default-features = false }
frame-system = { version = "3.0.0", default-features = false }
frame-support = { version = "3.0.0", default-features = false }
node-primitives = { path = "../../node/primitives", default-features = false }
Expand All @@ -25,6 +26,7 @@ default = ["std"]
std = [
"codec/std",
"sp-std/std",
"substrate-fixed/std",
"frame-system/std",
"frame-support/std",
"node-primitives/std",
Expand Down
78 changes: 40 additions & 38 deletions pallets/vsbond-auction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@

use frame_support::{
pallet_prelude::*,
sp_runtime::traits::{CheckedMul, Saturating, Zero},
sp_runtime::traits::{SaturatedConversion, Saturating, Zero},
};
use frame_system::pallet_prelude::*;
use node_primitives::{CurrencyId, LeasePeriod};
use orml_traits::{
MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, MultiReservableCurrency,
};
use orml_traits::{MultiCurrency, MultiReservableCurrency};
pub use pallet::*;
use sp_std::{cmp::min, collections::btree_set::BTreeSet};
use substrate_fixed::{traits::FromFixed, types::U64F64};

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

#[derive(Encode, Decode, Clone, Eq, PartialEq)]
Expand All @@ -43,7 +45,7 @@ pub struct OrderInfo<T: Config> {
supply: BalanceOf<T>,
/// The quantity of vsbond has not be sold
remain: BalanceOf<T>,
unit_price: BalanceOf<T>,
unit_price: U64F64,
order_id: OrderId,
order_state: OrderState,
}
Expand All @@ -62,18 +64,27 @@ impl<T: Config> core::fmt::Debug for OrderInfo<T> {
}

#[derive(Encode, Decode, Copy, Clone, Eq, PartialEq, Debug)]
pub enum OrderState {
enum OrderState {
InTrade,
Revoked,
Clinchd,
}

pub type OrderId = u64;
type OrderId = u64;
type ParaId = u32;

#[allow(type_alias_bounds)]
type AccountIdOf<T: Config> = <T as frame_system::Config>::AccountId;

#[allow(type_alias_bounds)]
type BalanceOf<T: Config> =
<<T as Config>::MultiCurrency as MultiCurrency<AccountIdOf<T>>>::Balance;

pub use module::*;
#[allow(type_alias_bounds)]
type LeasePeriodOf<T: Config> = <T as frame_system::Config>::BlockNumber;

#[frame_support::pallet]
pub mod module {
pub mod pallet {
use super::*;

#[pallet::config]
Expand All @@ -93,8 +104,6 @@ pub mod module {
type MinimumSupply: Get<BalanceOf<Self>>;

type MultiCurrency: MultiCurrency<AccountIdOf<Self>, CurrencyId = CurrencyId>
+ MultiCurrencyExtended<AccountIdOf<Self>, CurrencyId = CurrencyId>
+ MultiLockableCurrency<AccountIdOf<Self>, CurrencyId = CurrencyId>
+ MultiReservableCurrency<AccountIdOf<Self>, CurrencyId = CurrencyId>;
}

Expand Down Expand Up @@ -133,33 +142,30 @@ pub mod module {

#[pallet::storage]
#[pallet::getter(fn order_id)]
pub type NextOrderId<T: Config> = StorageValue<_, OrderId, ValueQuery>;
pub(crate) type NextOrderId<T: Config> = StorageValue<_, OrderId, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn in_trade_order_ids)]
pub type InTradeOrderIds<T: Config> =
pub(crate) type InTradeOrderIds<T: Config> =
StorageMap<_, Twox64Concat, AccountIdOf<T>, BTreeSet<OrderId>>;

#[pallet::storage]
#[pallet::getter(fn revoked_order_ids)]
pub type RevokedOrderIds<T: Config> =
pub(crate) type RevokedOrderIds<T: Config> =
StorageMap<_, Twox64Concat, AccountIdOf<T>, BTreeSet<OrderId>>;

#[pallet::storage]
#[pallet::getter(fn clinchd_order_ids)]
pub type ClinchdOrderIds<T: Config> =
pub(crate) type ClinchdOrderIds<T: Config> =
StorageMap<_, Twox64Concat, AccountIdOf<T>, BTreeSet<OrderId>>;

#[pallet::storage]
#[pallet::getter(fn order_info)]
pub type TotalOrderInfos<T: Config> = StorageMap<_, Twox64Concat, OrderId, OrderInfo<T>>;
pub(crate) type TotalOrderInfos<T: Config> = StorageMap<_, Twox64Concat, OrderId, OrderInfo<T>>;

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

#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(1_000)]
Expand All @@ -169,7 +175,7 @@ pub mod module {
#[pallet::compact] first_slot: LeasePeriodOf<T>,
#[pallet::compact] last_slot: LeasePeriodOf<T>,
#[pallet::compact] supply: BalanceOf<T>,
#[pallet::compact] unit_price: BalanceOf<T>,
unit_price: U64F64,
) -> DispatchResultWithPostInfo {
// Check origin
let owner = ensure_signed(origin)?;
Expand Down Expand Up @@ -337,9 +343,7 @@ pub mod module {
// Calculate the real quantity to clinch
let quantity_clinchd = min(order_info.remain, quantity);
// Calculate the total price that buyer need to pay
let total_price = quantity_clinchd
.checked_mul(&order_info.unit_price)
.ok_or(Error::<T>::Overflow)?;
let total_price = Self::total_price(quantity_clinchd, order_info.unit_price);

// Check the balance of buyer
T::MultiCurrency::ensure_can_withdraw(T::InvoicingCurrency::get(), &buyer, total_price)
Expand Down Expand Up @@ -422,23 +426,21 @@ pub mod module {
Ok(().into())
}
}
}

impl<T: Config> Pallet<T> {
pub(crate) fn next_order_id() -> OrderId {
let next_order_id = Self::order_id();
NextOrderId::<T>::mutate(|current| *current += 1);
next_order_id
impl<T: Config> Pallet<T> {
pub(crate) fn next_order_id() -> OrderId {
let next_order_id = Self::order_id();
NextOrderId::<T>::mutate(|current| *current += 1);
next_order_id
}

pub(crate) fn total_price(quantity: BalanceOf<T>, unit_price: U64F64) -> BalanceOf<T> {
let quantity: u128 = quantity.saturated_into();
let total_price = u128::from_fixed((unit_price * quantity).ceil());

BalanceOf::<T>::saturated_from(total_price)
}
}
}

// TODO: Maybe impl Auction trait for vsbond-auction

#[allow(type_alias_bounds)]
type AccountIdOf<T: Config> = <T as frame_system::Config>::AccountId;
#[allow(type_alias_bounds)]
type BalanceOf<T: Config> =
<<T as Config>::MultiCurrency as MultiCurrency<AccountIdOf<T>>>::Balance;
#[allow(type_alias_bounds)]
type LeasePeriodOf<T: Config> = <T as frame_system::Config>::BlockNumber;
type ParaId = u32;
2 changes: 0 additions & 2 deletions pallets/vsbond-auction/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg(test)]

use frame_support::{construct_runtime, parameter_types, traits::GenesisBuild};
use node_primitives::{Amount, Balance, CurrencyId, TokenSymbol};
use sp_core::H256;
Expand Down
Loading