Skip to content

Commit

Permalink
Merge branch 'develop' into feature/local-staking-pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
iStrike7 committed Apr 20, 2021
2 parents 3fa1553 + dc0d477 commit 6aa135a
Show file tree
Hide file tree
Showing 7 changed files with 2,360 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pallets/tokens/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "orml-tokens"
description = "Fungible tokens module that implements `MultiCurrency` trait."
repository = "https://github.com/open-web3-stack/open-runtime-module-library/tree/master/tokens"
license = "Apache-2.0"
version = "0.3.1"
authors = ["Laminar Developers <[email protected]>"]
edition = "2018"

[dependencies]
serde = { version = "1.0.111", optional = true }
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false }
sp-io = { version = "2.0.0", default-features = false }
sp-std = { version = "2.0.0", default-features = false }

frame-support = { version = "2.0.0", default-features = false }
frame-system = { version = "2.0.0", default-features = false }

orml-traits = { version = "0.3.1", default-features = false }


[dev-dependencies]
sp-core = { version = "2.0.0", default-features = false }
pallet-treasury = { version = "2.0.0" }
pallet-elections-phragmen = { version = "2.0.0" }

clear_on_drop = { version = "0.2.4", features = ["no_cc"] } # https://github.com/paritytech/substrate/issues/4179

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"sp-runtime/std",
"sp-std/std",
"sp-io/std",
"frame-support/std",
"frame-system/std",
"orml-traits/std",
]
2 changes: 2 additions & 0 deletions pallets/tokens/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is Fork of the `orml_tokens` crate because Origin crate does not support minting

20 changes: 20 additions & 0 deletions pallets/tokens/src/default_weight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Weights for the Tokens Module
use frame_support::weights::{
constants::{RocksDbWeight as DbWeight, WEIGHT_PER_MICROS},
Weight,
};

impl crate::WeightInfo for () {
fn transfer() -> Weight {
WEIGHT_PER_MICROS
.saturating_mul(84)
.saturating_add(DbWeight::get().reads_writes(4, 2))
}

fn transfer_all() -> Weight {
WEIGHT_PER_MICROS
.saturating_mul(88)
.saturating_add(DbWeight::get().reads_writes(4, 2))
}
}
155 changes: 155 additions & 0 deletions pallets/tokens/src/imbalances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// wrapping these imbalances in a private module is necessary to ensure absolute
// privacy of the inner member.
use crate::{TotalIssuance, Trait};
use frame_support::storage::StorageMap;
use frame_support::traits::{Get, Imbalance, TryDrop};
use sp_runtime::traits::{Saturating, Zero};
use sp_std::{marker, mem, result};

/// Opaque, move-only struct with private fields that serves as a token
/// denoting that funds have been created without any equal and opposite
/// accounting.
#[must_use]
pub struct PositiveImbalance<T: Trait, GetCurrencyId: Get<T::CurrencyId>>(
T::Balance,
marker::PhantomData<GetCurrencyId>,
);

impl<T: Trait, GetCurrencyId: Get<T::CurrencyId>> PositiveImbalance<T, GetCurrencyId> {
/// Create a new positive imbalance from a balance.
pub fn new(amount: T::Balance) -> Self {
PositiveImbalance(amount, marker::PhantomData::<GetCurrencyId>)
}
}

/// Opaque, move-only struct with private fields that serves as a token
/// denoting that funds have been destroyed without any equal and opposite
/// accounting.
#[must_use]
pub struct NegativeImbalance<T: Trait, GetCurrencyId: Get<T::CurrencyId>>(
T::Balance,
marker::PhantomData<GetCurrencyId>,
);

impl<T: Trait, GetCurrencyId: Get<T::CurrencyId>> NegativeImbalance<T, GetCurrencyId> {
/// Create a new negative imbalance from a balance.
pub fn new(amount: T::Balance) -> Self {
NegativeImbalance(amount, marker::PhantomData::<GetCurrencyId>)
}
}

impl<T: Trait, GetCurrencyId: Get<T::CurrencyId>> TryDrop for PositiveImbalance<T, GetCurrencyId> {
fn try_drop(self) -> result::Result<(), Self> {
self.drop_zero()
}
}

impl<T: Trait, GetCurrencyId: Get<T::CurrencyId>> Imbalance<T::Balance> for PositiveImbalance<T, GetCurrencyId> {
type Opposite = NegativeImbalance<T, GetCurrencyId>;

fn zero() -> Self {
Self::new(Zero::zero())
}
fn drop_zero(self) -> result::Result<(), Self> {
if self.0.is_zero() {
Ok(())
} else {
Err(self)
}
}
fn split(self, amount: T::Balance) -> (Self, Self) {
let first = self.0.min(amount);
let second = self.0 - first;

mem::forget(self);
(Self::new(first), Self::new(second))
}
fn merge(mut self, other: Self) -> Self {
self.0 = self.0.saturating_add(other.0);
mem::forget(other);

self
}
fn subsume(&mut self, other: Self) {
self.0 = self.0.saturating_add(other.0);
mem::forget(other);
}
fn offset(self, other: Self::Opposite) -> result::Result<Self, Self::Opposite> {
let (a, b) = (self.0, other.0);
mem::forget((self, other));

if a >= b {
Ok(Self::new(a - b))
} else {
Err(NegativeImbalance::new(b - a))
}
}
fn peek(&self) -> T::Balance {
self.0
}
}

impl<T: Trait, GetCurrencyId: Get<T::CurrencyId>> TryDrop for NegativeImbalance<T, GetCurrencyId> {
fn try_drop(self) -> result::Result<(), Self> {
self.drop_zero()
}
}

impl<T: Trait, GetCurrencyId: Get<T::CurrencyId>> Imbalance<T::Balance> for NegativeImbalance<T, GetCurrencyId> {
type Opposite = PositiveImbalance<T, GetCurrencyId>;

fn zero() -> Self {
Self::new(Zero::zero())
}
fn drop_zero(self) -> result::Result<(), Self> {
if self.0.is_zero() {
Ok(())
} else {
Err(self)
}
}
fn split(self, amount: T::Balance) -> (Self, Self) {
let first = self.0.min(amount);
let second = self.0 - first;

mem::forget(self);
(Self::new(first), Self::new(second))
}
fn merge(mut self, other: Self) -> Self {
self.0 = self.0.saturating_add(other.0);
mem::forget(other);

self
}
fn subsume(&mut self, other: Self) {
self.0 = self.0.saturating_add(other.0);
mem::forget(other);
}
fn offset(self, other: Self::Opposite) -> result::Result<Self, Self::Opposite> {
let (a, b) = (self.0, other.0);
mem::forget((self, other));

if a >= b {
Ok(Self::new(a - b))
} else {
Err(PositiveImbalance::new(b - a))
}
}
fn peek(&self) -> T::Balance {
self.0
}
}

impl<T: Trait, GetCurrencyId: Get<T::CurrencyId>> Drop for PositiveImbalance<T, GetCurrencyId> {
/// Basic drop handler will just square up the total issuance.
fn drop(&mut self) {
<TotalIssuance<T>>::mutate(GetCurrencyId::get(), |v| *v = v.saturating_add(self.0));
}
}

impl<T: Trait, GetCurrencyId: Get<T::CurrencyId>> Drop for NegativeImbalance<T, GetCurrencyId> {
/// Basic drop handler will just square up the total issuance.
fn drop(&mut self) {
<TotalIssuance<T>>::mutate(GetCurrencyId::get(), |v| *v = v.saturating_sub(self.0));
}
}
Loading

0 comments on commit 6aa135a

Please sign in to comment.