Skip to content

Commit

Permalink
Feature/refactor asset (paritytech#184)
Browse files Browse the repository at this point in the history
* add set_balance
  • Loading branch information
Aton committed Jan 4, 2019
1 parent 1858b2b commit bb85964
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
Binary file not shown.
66 changes: 49 additions & 17 deletions xrml/xassets/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ pub trait OnAssetChanged<AccountId, Balance> {
fn on_destroy(who: &AccountId, token: &Token, value: Balance);
fn on_reserve(_who: &AccountId, _token: &Token, _value: Balance) {}
fn on_unreserve(_who: &AccountId, _token: &Token, _value: Balance) {}
fn on_set_free(_who: &AccountId, _token: &Token, _value: Balance) {}
fn on_set_reserved(_who: &AccountId, _token: &Token, _value: Balance) {}
fn on_set_balance(_who: &AccountId, _token: &Token, _type: AssetType, _value: Balance) {}
}

impl<AccountId, Balance> OnAssetChanged<AccountId, Balance> for () {
Expand Down Expand Up @@ -128,24 +127,19 @@ decl_module! {
Ok(())
}

/// cancel asset, mark this asset is invalid
fn cancel_asset(token: Token) -> Result {
is_valid_token(&token)?;
Self::remove_asset(&token)?;
Ok(())
}

// /// set free token for an account
// fn set_asset_free_balance(who: Address<T::AccountId, T::AccountIndex>, token: Token, free: T::Balance) -> Result {
// let who = balances::Module::<T>::lookup(who)?;
// Self::set_free_balance(&who, &token, free)?;
// Ok(())
// }
// /// set reserved token for an account
// fn set_asset_reserved_balance(who: Address<T::AccountId, T::AccountIndex>, token: Token, reserved: T::Balance, res_type: ReservedType) -> Result {
// let who = balances::Module::<T>::lookup(who)?;
// Self::set_reserved_balance(&who, &token, reserved, res_type)?;
// Ok(())
// }
/// set free token for an account
fn set_balance(who: Address<T::AccountId, T::AccountIndex>, token: Token, balances: CodecBTreeMap<AssetType, T::Balance>) -> Result {
let who = balances::Module::<T>::lookup(who)?;
Self::set_balance_by_root(&who, &token, balances)?;
Ok(())
}

/// transfer between account
fn transfer(origin, dest: Address<T::AccountId, T::AccountIndex>, token: Token, value: T::Balance, memo: Vec<u8>) -> Result {
Expand All @@ -171,8 +165,9 @@ decl_storage! {
/// asset list of a account
pub CrossChainAssetsOf get(crosschain_assets_of): map T::AccountId => Vec<Token>;

/// asset balance for user&token, use btree_map to accept different asset type
pub AssetBalance: map (T::AccountId, Token) => CodecBTreeMap<AssetType, T::Balance>;

/// asset balance for a token, use btree_map to accept different asset type
pub TotalAssetBalance: map Token => CodecBTreeMap<AssetType, T::Balance>;

/// price
Expand Down Expand Up @@ -499,7 +494,7 @@ impl<T: Trait> Module<T> {
Self::should_not_free_type(type_)?;

// get from storage
let total_free_token = Self::total_asset_balance(token, AssetType::Free); //TotalXFreeBalance::<T>::get(token);
let total_free_token = Self::total_asset_balance(token, AssetType::Free);
let total_reserved_token = Self::total_asset_balance(token, type_);
let free_token = Self::asset_balance(who, token, AssetType::Free);
let reserved_token = Self::asset_balance(who, token, type_);
Expand Down Expand Up @@ -540,7 +535,7 @@ impl<T: Trait> Module<T> {
Self::should_not_free_type(type_)?;

// get from storage
let total_free_token = Self::total_asset_balance(token, AssetType::Free); //TotalXFreeBalance::<T>::get(token);
let total_free_token = Self::total_asset_balance(token, AssetType::Free);
let total_reserved_token = Self::total_asset_balance(token, type_);
let free_token = Self::asset_balance(who, token, AssetType::Free);
let reserved_token = Self::asset_balance(who, token, type_);
Expand Down Expand Up @@ -646,6 +641,43 @@ impl<T: Trait> Module<T> {
Ok(())
}

pub fn set_balance_by_root(who: &T::AccountId, token: &Token, balances: CodecBTreeMap<AssetType, T::Balance>) -> Result {
for (type_, val) in balances.0.into_iter() {
let old_val = Self::asset_balance(who, token, type_);
let old_total_val = Self::total_asset_balance(token, type_);
if old_val == val {
continue;
}

let new_total_val = if val > old_val {
match val.checked_sub(&old_val) {
None => return Err("balance too low to sub value"),
Some(b) => match old_total_val.checked_add(&b) {
None => return Err("old total balance too high to add value"),
Some(new) => new,
},
}
} else {
match old_val.checked_sub(&val) {
None => return Err("old balance too low to sub value"),
Some(b) => match old_total_val.checked_sub(&b) {
None => return Err("old total balance too low to sub value"),
Some(new) => new,
},
}
};

Self::set_asset_balance(who, token, type_, val);
if token.as_slice() == <Self as ChainT>::TOKEN && type_ == AssetType::Free {
balances::TotalIssuance::<T>::put(new_total_val)
} else {
Self::set_total_asset_balance(token, type_, val);
}
T::OnAssetChanged::on_set_balance(who, token, type_, val);
}
Ok(())
}

// pub fn set_free_balance(who: &T::AccountId, token: &Token, free: T::Balance) -> Result {
// if token.as_slice() == <Self as ChainT>::TOKEN {
// balances::Module::<T>::set_free_balance(&who, free);
Expand Down
6 changes: 6 additions & 0 deletions xrml/xsupport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
extern crate serde;
#[cfg(feature = "std")]
#[macro_use]
extern crate serde_derive;

extern crate parity_codec as codec;
#[macro_use]
extern crate parity_codec_derive;
Expand Down
4 changes: 2 additions & 2 deletions xrml/xsupport/src/storage/btree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use codec::{Decode, Encode, Input, Output};
use rstd::collections::btree_map::BTreeMap;

#[derive(Default)]
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Default, Eq, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct CodecBTreeMap<K: Ord, V>(pub BTreeMap<K, V>);

impl<K: Encode + Ord, V: Encode> Encode for CodecBTreeMap<K, V> {
Expand Down

0 comments on commit bb85964

Please sign in to comment.