diff --git a/xrml/xassets/assets/src/lib.rs b/xrml/xassets/assets/src/lib.rs index 0a1974f424ff4..86d26f9491cde 100644 --- a/xrml/xassets/assets/src/lib.rs +++ b/xrml/xassets/assets/src/lib.rs @@ -25,10 +25,10 @@ extern crate srml_system as system; extern crate xrml_xsupport as xsupport; -//#[cfg(test)] -//mod mock; -//#[cfg(test)] -//mod tests; +#[cfg(test)] +mod mock; +#[cfg(test)] +mod tests; pub mod assetdef; pub mod memo; @@ -245,11 +245,11 @@ impl Module { } fn set_free_balance_creating(who: &T::AccountId, token: &Token, value: T::Balance) { - let need_create = balances::FreeBalance::::exists(who); if token.as_slice() == ::TOKEN { balances::Module::::set_free_balance_creating(who, value); } else { - if need_create { + let need_create = balances::FreeBalance::::exists(who); + if need_create == false { balances::Module::::set_free_balance_creating(who, Zero::zero()); } Self::set_free_balance(who, token, value) @@ -468,11 +468,11 @@ impl Module { // check let new_free_token = match free_token.checked_add(&value) { Some(b) => b, - None => return Err("free token too high to issue"), + None => return Err("free balance too high to issue"), }; let new_total_free_token = match total_free_token.checked_add(&value) { Some(b) => b, - None => return Err("total free token too high to issue"), + None => return Err("total free balance too high to issue"), }; // set to storage Self::init_asset_for(who, token); @@ -501,19 +501,19 @@ impl Module { // test overflow let new_free_token = match free_token.checked_sub(&value) { Some(b) => b, - None => return Err("free token too low to reserve"), + None => return Err("free balance too low to reserve"), }; let new_reserved_token = match reserved_token.checked_add(&value) { Some(b) => b, - None => return Err("reserved token too high to reserve"), + None => return Err("reserved balance too high to reserve"), }; let new_total_free_token = match total_free_token.checked_sub(&value) { Some(b) => b, - None => return Err("total free token too low to reserve"), + None => return Err("total free balance too low to reserve"), }; let new_total_reserved_token = match total_reserved_token.checked_add(&value) { Some(b) => b, - None => return Err("total reserved token too high to reserve"), + None => return Err("total reserved balance too high to reserve"), }; // set to storage Self::set_total_asset_balance(token, AssetType::Free, new_total_free_token); @@ -543,19 +543,19 @@ impl Module { // test overflow let new_free_token = match free_token.checked_add(&value) { Some(b) => b, - None => return Err("free token too high to unreserve"), + None => return Err("free balance too high to unreserve"), }; let new_reserved_token = match reserved_token.checked_sub(&value) { Some(b) => b, - None => return Err("reserved token too low to unreserve"), + None => return Err("reserved balance too low to unreserve"), }; let new_total_free_token = match total_free_token.checked_add(&value) { Some(b) => b, - None => return Err("total free token too high to unreserve"), + None => return Err("total free balance too high to unreserve"), }; let new_total_reserved_token = match total_reserved_token.checked_sub(&value) { Some(b) => b, - None => return Err("total reserved token too low to unreserve"), + None => return Err("total reserved balance too low to unreserve"), }; // set to storage Self::set_total_asset_balance(token, AssetType::Free, new_total_free_token); @@ -581,13 +581,13 @@ impl Module { let total_reserved_token = Self::total_asset_balance(token, type_); let reserved_token = Self::asset_balance(who, token, type_); // check - let new_total_reserved_token = match total_reserved_token.checked_sub(&value) { + let new_reserved_token = match reserved_token.checked_sub(&value) { Some(b) => b, - None => return Err("total reserved token too low to destroy"), + None => return Err("reserved balance too low to destroy"), }; - let new_reserved_token = match reserved_token.checked_sub(&value) { + let new_total_reserved_token = match total_reserved_token.checked_sub(&value) { Some(b) => b, - None => return Err("reserved token too low to destroy"), + None => return Err("total reserved balance too low to destroy"), }; // set to storage @@ -666,84 +666,12 @@ impl Module { if token.as_slice() == ::TOKEN && type_ == AssetType::Free { balances::TotalIssuance::::put(new_total_val) } else { - Self::set_total_asset_balance(token, type_, val); + Self::set_total_asset_balance(token, type_, new_total_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() == ::TOKEN { - // balances::Module::::set_free_balance(&who, free); - // } else { - // let key = (who.clone(), token.clone()); - // let old_free = XFreeBalance::::get(&key); - // let old_total_free = TotalXFreeBalance::::get(token); - // if old_free == free { - // return Err("some value for free token"); - // } - // let new_total_free = if free > old_free { - // match free.checked_sub(&old_free) { - // None => return Err("free token too low to sub value"), - // Some(b) => match old_total_free.checked_add(&b) { - // None => return Err("old total free token too high to add value"), - // Some(new) => new, - // }, - // } - // } else { - // match old_free.checked_sub(&free) { - // None => return Err("old free token too low to sub value"), - // Some(b) => match old_total_free.checked_sub(&b) { - // None => return Err("old total free token too low to sub value"), - // Some(new) => new, - // }, - // } - // }; - // TotalXFreeBalance::::insert(token, new_total_free); - // XFreeBalance::::insert(key, free); - // } - // T::OnAssetChanged::on_set_free(who, token, free); - // Ok(()) - // } - // - // pub fn set_reserved_balance( - // who: &T::AccountId, - // token: &Token, - // reserved: T::Balance, - // res_type: ReservedType, - // ) -> Result { - // let key = (who.clone(), token.clone(), res_type); - // let old_reserved = XReservedBalance::::get(&key); - // let old_total_reserved = TotalXReservedBalance::::get(token); - // - // if old_reserved == reserved { - // return Err("some value for reserved token"); - // } - // - // let new_total_reserved = if reserved > old_reserved { - // match reserved.checked_sub(&old_reserved) { - // None => return Err("reserved token too low to sub value"), - // Some(b) => match old_total_reserved.checked_add(&b) { - // None => return Err("old total reserved token too high to add value"), - // Some(new) => new, - // }, - // } - // } else { - // match old_reserved.checked_sub(&reserved) { - // None => return Err("old reserved token too low to sub value"), - // Some(b) => match old_total_reserved.checked_sub(&b) { - // None => return Err("old total reserved token too high to sub value"), - // Some(new) => new, - // }, - // } - // }; - // TotalXReservedBalance::::insert(token, new_total_reserved); - // XReservedBalance::::insert(key, reserved); - // - // T::OnAssetChanged::on_set_reserved(who, token, reserved); - // Ok(()) - // } } #[derive(PartialEq, Eq, Clone, Copy, Encode, Decode)] @@ -759,7 +687,7 @@ pub enum TokenErr { impl TokenErr { pub fn info(&self) -> &'static str { match *self { - TokenErr::NotEnough => "free token too low", + TokenErr::NotEnough => "free balance too low", TokenErr::OverFlow => "overflow for this value", TokenErr::SomeAccount => "from and to are same account", TokenErr::InvalidToken => "not a valid token for this account", diff --git a/xrml/xassets/assets/src/mock.rs b/xrml/xassets/assets/src/mock.rs index 53281adccffac..ade0e91bbad1a 100644 --- a/xrml/xassets/assets/src/mock.rs +++ b/xrml/xassets/assets/src/mock.rs @@ -37,159 +37,70 @@ impl balances::Trait for Test { type Event = (); } -impl cxsystem::Trait for Test {} - -impl associations::Trait for Test { - type OnCalcFee = cxsupport::Module; - type Event = (); -} - -impl cxsupport::Trait for Test {} - -// define tokenbalances module type -pub type TokenBalance = u128; - impl Trait for Test { - const CHAINX_SYMBOL: SymbolString = b"pcx"; - const CHAINX_TOKEN_DESC: DescString = b"this is pcx for mock"; - type TokenBalance = TokenBalance; + /// Event type Event = (); - type OnMoveToken = (); + type OnAssetChanged = (); + type OnAssetRegistration = (); } -pub type TokenBalances = Module; +pub type XAssets = Module; pub type Balances = balances::Module; +pub type Balance = u64; // This function basically just builds a genesis storage key/value store according to // our desired mockup. pub fn new_test_ext() -> runtime_io::TestExternalities { let mut r = system::GenesisConfig::::default() .build_storage() - .unwrap(); + .unwrap() + .0; // balance -<<<<<<< HEAD - r.extend(balances::GenesisConfig:: { - balances: vec![(1, 1000), (2, 510), (3, 1000)], - transaction_base_fee: 0, - transaction_byte_fee: 0, - existential_deposit: 500, - transfer_fee: 0, - creation_fee: 0, - reclaim_rebate: 0, - }.build_storage().unwrap()); -======= r.extend( balances::GenesisConfig:: { balances: vec![(1, 1000), (2, 510), (3, 1000)], transaction_base_fee: 0, transaction_byte_fee: 0, - existential_deposit: 500, + existential_deposit: 0, transfer_fee: 0, creation_fee: 0, reclaim_rebate: 0, } .build_storage() - .unwrap(), - ); ->>>>>>> develop - // token - let t: Token = Token::new(b"x-btc".to_vec(), b"btc token".to_vec(), 8); - let t2: Token = Token::new(b"x-eth".to_vec(), b"eth token".to_vec(), 4); - -<<<<<<< HEAD - r.extend(GenesisConfig:: { - token_list: vec![ - (t, [(3, 100)].to_vec()), - (t2, [(3, 100)].to_vec()), - ], - transfer_token_fee: 10, - }.build_storage().unwrap()); -======= - r.extend( - GenesisConfig:: { - chainx_precision: 8, - token_list: vec![(t, [(3, 100)].to_vec()), (t2, [(3, 100)].to_vec())], - transfer_token_fee: 10, - } - .build_storage() - .unwrap(), + .unwrap() + .0, ); ->>>>>>> develop - r.into() -} -pub fn err_test_ext() -> runtime_io::TestExternalities { - let mut r = system::GenesisConfig::::default() - .build_storage() - .unwrap(); - // balance -<<<<<<< HEAD - r.extend(balances::GenesisConfig:: { - balances: vec![(1, 1000), (2, 510), (3, 1000)], - transaction_base_fee: 0, - transaction_byte_fee: 0, - existential_deposit: 500, - transfer_fee: 0, - creation_fee: 0, - reclaim_rebate: 0, - }.build_storage().unwrap()); -======= - r.extend( - balances::GenesisConfig:: { - balances: vec![(1, 1000), (2, 510), (3, 1000)], - transaction_base_fee: 0, - transaction_byte_fee: 0, - existential_deposit: 500, - transfer_fee: 0, - creation_fee: 0, - reclaim_rebate: 0, - } - .build_storage() - .unwrap(), - ); ->>>>>>> develop - // token - let t: Token = Token::new(b"x-btc?...".to_vec(), b"btc token".to_vec(), 8); - let t2: Token = Token::new(b"x-eth".to_vec(), b"eth token".to_vec(), 4); - -<<<<<<< HEAD - r.extend(GenesisConfig:: { - token_list: vec![ - (t, [(3, 100)].to_vec()), - (t2, [(3, 100)].to_vec()), - ], - transfer_token_fee: 10, - }.build_storage().unwrap()); -======= + let btc_asset = Asset::new( + b"BTC".to_vec(), // token + Chain::Bitcoin, + 8, // bitcoin precision + b"BTC chainx".to_vec(), + ) + .unwrap(); + r.extend( GenesisConfig:: { - chainx_precision: 8, - token_list: vec![(t, [(3, 100)].to_vec()), (t2, [(3, 100)].to_vec())], - transfer_token_fee: 10, + pcx: (3, b"PCX onchain token".to_vec()), + memo_len: 128, + // asset, is_psedu_intention, init for account + // Vec<(Asset, bool, Vec<(T::AccountId, u64)>)>; + asset_list: vec![(btc_asset, true, vec![(3, 100)])], } .build_storage() - .unwrap(), + .unwrap() + .0, ); ->>>>>>> develop + r.into() } -pub fn new_test_ext2() -> runtime_io::TestExternalities { +pub fn err_test_ext() -> runtime_io::TestExternalities { let mut r = system::GenesisConfig::::default() .build_storage() - .unwrap(); + .unwrap() + .0; // balance -<<<<<<< HEAD - r.extend(balances::GenesisConfig:: { - balances: vec![(1, 1000), (2, 510), (3, 1000)], - transaction_base_fee: 0, - transaction_byte_fee: 0, - existential_deposit: 0, - transfer_fee: 0, - creation_fee: 0, - reclaim_rebate: 0, - }.build_storage().unwrap()); -======= r.extend( balances::GenesisConfig:: { balances: vec![(1, 1000), (2, 510), (3, 1000)], @@ -201,31 +112,30 @@ pub fn new_test_ext2() -> runtime_io::TestExternalities { reclaim_rebate: 0, } .build_storage() - .unwrap(), + .unwrap() + .0, ); ->>>>>>> develop - // token - let t: Token = Token::new(b"x-btc".to_vec(), b"btc token".to_vec(), 8); - let t2: Token = Token::new(b"x-eth".to_vec(), b"eth token".to_vec(), 4); - -<<<<<<< HEAD - r.extend(GenesisConfig:: { - token_list: vec![ - (t, [(3, 100)].to_vec()), - (t2, [(3, 100)].to_vec()), - ], - transfer_token_fee: 10, - }.build_storage().unwrap()); -======= + + let btc_asset = Asset::new( + b"BTC******".to_vec(), // token + Chain::Bitcoin, + 8, // bitcoin precision + b"BTC chainx".to_vec(), + ) + .unwrap(); + r.extend( GenesisConfig:: { - chainx_precision: 8, - token_list: vec![(t, [(3, 100)].to_vec()), (t2, [(3, 100)].to_vec())], - transfer_token_fee: 10, + pcx: (3, b"PCX onchain token".to_vec()), + memo_len: 128, + // asset, is_psedu_intention, init for account + // Vec<(Asset, bool, Vec<(T::AccountId, u64)>)>; + asset_list: vec![(btc_asset, true, vec![(3, 100)])], } .build_storage() - .unwrap(), + .unwrap() + .0, ); ->>>>>>> develop + r.into() } diff --git a/xrml/xassets/assets/src/tests.rs b/xrml/xassets/assets/src/tests.rs index 7f738e30ac9e1..e6d725d923dc3 100644 --- a/xrml/xassets/assets/src/tests.rs +++ b/xrml/xassets/assets/src/tests.rs @@ -2,6 +2,8 @@ use super::*; use mock::*; +use rstd::collections::btree_map::BTreeMap; +use rstd::iter::FromIterator; use runtime_io::with_externalities; #[test] @@ -9,68 +11,32 @@ fn test_genesis() { with_externalities(&mut new_test_ext(), || { // Check that GenesisBuilder works properly. // check token_list - let btc_symbol = b"x-btc".to_vec(); - let eth_symbol = b"x-eth".to_vec(); + let btc_token = b"BTC".to_vec(); assert_eq!( - TokenBalances::token_list(), - vec![ - Test::CHAINX_SYMBOL.to_vec(), - btc_symbol.clone(), - eth_symbol.clone(), - ] + XAssets::assets(), + vec![XAssets::TOKEN.to_vec(), btc_token.clone(),] ); - assert_eq!( - TokenBalances::token_info(btc_symbol.clone()) - .unwrap() - .0 - .precision(), - 8 - ); - assert_eq!( - TokenBalances::token_info(eth_symbol.clone()) - .unwrap() - .0 - .precision(), - 4 - ); - - assert_eq!(TokenBalances::total_free_token(btc_symbol.clone()), 100); - assert_eq!(TokenBalances::total_reserved_token(btc_symbol.clone()), 0); + assert_eq!(XAssets::asset_info(&btc_token).unwrap().0.precision(), 8); - // chainx symbol for every user - assert_eq!( - TokenBalances::token_list_of(&0), - [Test::CHAINX_SYMBOL.to_vec()].to_vec() - ); + // chainx tokenbol for every user + assert_eq!(XAssets::assets_of(&0), vec![XAssets::TOKEN.to_vec()]); }); } #[test] fn test_genesis_token_issue() { with_externalities(&mut new_test_ext(), || { - let btc_symbol = b"x-btc".to_vec(); - let eth_symbol = b"x-eth".to_vec(); -<<<<<<< HEAD - assert_eq!(TokenBalances::free_token(&(3, Test::CHAINX_SYMBOL.to_vec())), 1000); - assert_eq!(TokenBalances::free_token(&(3, btc_symbol.clone())), 100); - assert_eq!(TokenBalances::free_token(&(3, eth_symbol.clone())), 100); - - assert_eq!(TokenBalances::token_list_of(&3), [Test::CHAINX_SYMBOL.to_vec(), btc_symbol, eth_symbol]); -======= + let btc_token = b"BTC".to_vec(); + let chainx_token = XAssets::TOKEN.to_vec(); assert_eq!( - TokenBalances::free_token(&(3, Test::CHAINX_SYMBOL.to_vec())), + XAssets::asset_balance(&3, &chainx_token, AssetType::Free), 1000 ); - assert_eq!(TokenBalances::free_token(&(3, btc_symbol.clone())), 100); - assert_eq!(TokenBalances::free_token(&(3, eth_symbol.clone())), 100); + assert_eq!(XAssets::asset_balance(&3, &btc_token, AssetType::Free), 100); - assert_eq!( - TokenBalances::token_list_of(&3), - [Test::CHAINX_SYMBOL.to_vec(), btc_symbol, eth_symbol] - ); ->>>>>>> develop + assert_eq!(XAssets::assets_of(&3), [chainx_token, btc_token]); }) } @@ -83,44 +49,25 @@ fn test_err_genesis() { #[test] fn test_register() { with_externalities(&mut new_test_ext(), || { - let t_sym: Symbol = b"x-eos".to_vec(); //slice_to_u8_8(b"x-eos"); - let t_desc: TokenDesc = b"eos token".to_vec(); //slice_to_u8_32(b"eos token"); + let token: Token = b"ETH".to_vec(); //slice_to_u8_8(b"x-eos"); + let desc: Desc = b"eth token".to_vec(); //slice_to_u8_32(b"eos token"); let precision = 4; - let t: Token = Token::new(t_sym.clone(), t_desc, precision); - assert_eq!(TokenBalances::register_token(t, 0, 0), Ok(())); + let asset = Asset::new(token.clone(), Chain::Ethereum, precision, desc).unwrap(); + assert_eq!(XAssets::register_asset(asset.clone(), false, 0), Ok(())); - assert_eq!(TokenBalances::token_list_len(), 4); - assert_eq!(TokenBalances::token_list_map(3), t_sym.clone()); + let btc_token = b"BTC".to_vec(); //b"BTC".to_vec(); - let btc_symbol = b"x-btc".to_vec(); //b"x-btc".to_vec(); - let eth_symbol = b"x-eth".to_vec(); //slice_to_u8_8(b"x-eth"); assert_eq!( - TokenBalances::token_list(), - vec![ - Test::CHAINX_SYMBOL.to_vec(), - btc_symbol.clone(), - eth_symbol.clone(), - t_sym.clone(), - ] + XAssets::assets(), + vec![XAssets::TOKEN.to_vec(), btc_token, token.clone()] ); - assert_eq!(TokenBalances::total_free_token(t_sym.clone()), 0); - assert_eq!( - TokenBalances::token_info(t_sym.clone()) - .unwrap() - .0 - .precision(), - 4 - ); - - // test err branch - let btc_t = Token::new(btc_symbol.clone(), b"btc token".to_vec(), 4); + assert_eq!(XAssets::total_asset_balance(&token, AssetType::Free), 0); + assert_eq!(XAssets::asset_info(&token).unwrap().0.precision(), 4); assert_noop!( - TokenBalances::register_token(btc_t, 0, 0), - "already has this token symbol" + XAssets::register_asset(asset, false, 0), + "already has this token" ); - assert_eq!(TokenBalances::token_list_len(), 4); - assert_eq!(TokenBalances::token_list_map(4), b"".to_vec()); }) } @@ -128,55 +75,73 @@ fn test_register() { fn test_remove() { with_externalities(&mut new_test_ext(), || { // register a new token - let t_sym: Symbol = b"x-eos".to_vec(); - let t_desc: TokenDesc = b"eos token".to_vec(); - let precision: Precision = 4; - let t: Token = Token::new(t_sym.clone(), t_desc, precision); - assert_eq!(TokenBalances::register_token(t.clone(), 0, 0), Ok(())); - assert_eq!(TokenBalances::token_list_map(3), t_sym.clone()); + let token: Token = b"ETH".to_vec(); //slice_to_u8_8(b"x-eos"); + let desc: Desc = b"eth token".to_vec(); //slice_to_u8_32(b"eos token"); + let precision = 4; + let asset = Asset::new(token.clone(), Chain::Ethereum, precision, desc).unwrap(); + assert_eq!(XAssets::register_asset(asset.clone(), false, 0), Ok(())); // remove it - assert_eq!(TokenBalances::cancel_token(&t_sym.clone()), Ok(())); - assert_eq!(TokenBalances::token_list_map(3), t_sym.clone()); - assert_eq!(TokenBalances::token_list_len(), 4); // length not modify + assert_eq!(XAssets::cancel_asset(token.clone()), Ok(())); + assert_noop!(XAssets::is_valid_asset(&token), "not a valid token"); // re-register, but must be failed assert_noop!( - TokenBalances::register_token(t.clone(), 0, 0), - "already has this token symbol" + XAssets::register_asset(asset, false, 0), + "already has this token" ); - - // create new token symbol - let t_new: Token = Token { - symbol: b"x-eos2".to_vec(), - ..t - }; - assert_noop!( - TokenBalances::cancel_token(&t_new.symbol), - "this token symbol dose not register yet or is invalid" - ); - assert_eq!(TokenBalances::register_token(t_new.clone(), 0, 0), Ok(())); - assert_eq!(TokenBalances::token_list_map(3), t_sym.clone()); - assert_eq!(TokenBalances::token_list_map(4), t_new.symbol); - assert_eq!(TokenBalances::token_list_len(), 5); }) } #[test] fn test_total_balance() { with_externalities(&mut new_test_ext(), || { - let btc_symbol = b"x-btc".to_vec(); - assert_eq!(TokenBalances::total_token(&btc_symbol.clone()), 100); + let btc_token = b"BTC".to_vec(); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::Free), + 100 + ); - TokenBalances::issue(&0, &btc_symbol, 100).unwrap(); - assert_eq!(TokenBalances::total_token(&btc_symbol.clone()), 200); + XAssets::issue(&0, &btc_token, 100).unwrap(); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::Free), + 200 + ); - TokenBalances::issue(&0, &btc_symbol, 50).unwrap(); - TokenBalances::reserve(&0, &btc_symbol, 50, Default::default()).unwrap(); - assert_eq!(TokenBalances::total_token(&btc_symbol.clone()), 250); + XAssets::issue(&0, &btc_token, 50).unwrap(); + XAssets::reserve(&0, &btc_token, 50, AssetType::ReservedWithdrawal).unwrap(); + assert_eq!(XAssets::all_type_balance(&btc_token), 250); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::ReservedWithdrawal), + 50 + ); - TokenBalances::destroy(&0, &btc_symbol, 25, Default::default()).unwrap(); - assert_eq!(TokenBalances::total_token(&btc_symbol.clone()), 225); + XAssets::destroy(&0, &btc_token, 25, AssetType::ReservedWithdrawal).unwrap(); + assert_eq!(XAssets::all_type_balance(&btc_token), 225); + // chainx total + let token = XAssets::TOKEN.to_vec(); + assert_eq!( + XAssets::total_asset_balance(&token, AssetType::Free), + 1000 + 510 + 1000 + ); + XAssets::reserve(&1, &token, 50, AssetType::ReservedWithdrawal).unwrap(); + assert_eq!( + XAssets::total_asset_balance(&token, AssetType::Free), + 1000 + 510 + 1000 - 50 + ); + assert_eq!( + XAssets::total_asset_balance(&token, AssetType::ReservedWithdrawal), + 50 + ); + XAssets::unreserve(&1, &token, 25, AssetType::ReservedWithdrawal).unwrap(); + assert_eq!( + XAssets::total_asset_balance(&token, AssetType::Free), + 1000 + 510 + 1000 - 50 + 25 + ); + assert_eq!( + XAssets::total_asset_balance(&token, AssetType::ReservedWithdrawal), + 25 + ); }) } @@ -184,21 +149,25 @@ fn test_total_balance() { fn test_account_balance() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_symbol = b"x-btc".to_vec(); - let key = (a, btc_symbol.clone()); - let reserved_key = (a, btc_symbol.clone(), Default::default()); - assert_eq!(TokenBalances::free_token(&key), 0); - assert_eq!(TokenBalances::reserved_token(&reserved_key), 0); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol), 0); - - TokenBalances::issue(&a, &btc_symbol, 100).unwrap(); - assert_eq!(TokenBalances::free_token(&key), 100); - assert_eq!(TokenBalances::reserved_token(&reserved_key), 0); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 100); - - TokenBalances::reserve(&a, &btc_symbol, 50, Default::default()).unwrap(); - TokenBalances::destroy(&a, &btc_symbol, 50, Default::default()).unwrap(); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 50); + let btc_token = b"BTC".to_vec(); + assert_eq!(XAssets::free_balance(&a, &btc_token), 0); + assert_eq!( + XAssets::asset_balance(&a, &btc_token, AssetType::ReservedWithdrawal), + 0 + ); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 0); + + XAssets::issue(&a, &btc_token, 100).unwrap(); + assert_eq!(XAssets::free_balance(&a, &btc_token), 100); + assert_eq!( + XAssets::asset_balance(&a, &btc_token, AssetType::ReservedWithdrawal), + 0 + ); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 100); + + XAssets::reserve(&a, &btc_token, 50, AssetType::ReservedWithdrawal).unwrap(); + XAssets::destroy(&a, &btc_token, 50, AssetType::ReservedWithdrawal).unwrap(); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 50); }) } @@ -206,29 +175,39 @@ fn test_account_balance() { fn test_normal_issue_and_destroy() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_symbol = b"x-btc".to_vec(); - let key = (a, btc_symbol.clone()); - let reserved_key = (a, btc_symbol.clone(), Default::default()); + let btc_token = b"BTC".to_vec(); // issue - TokenBalances::issue(&a, &btc_symbol.clone(), 50).unwrap(); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 50); - assert_eq!(TokenBalances::total_token(&btc_symbol.clone()), 150); + XAssets::issue(&a, &btc_token, 50).unwrap(); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 50); + assert_eq!(XAssets::all_type_balance(&btc_token), 150); // reserve - TokenBalances::reserve(&a, &btc_symbol.clone(), 25, Default::default()).unwrap(); - assert_eq!(TokenBalances::reserved_token(&reserved_key), 25); - assert_eq!(TokenBalances::free_token(&key), 25); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 50); - assert_eq!(TokenBalances::total_reserved_token(&btc_symbol.clone()), 25); + XAssets::reserve(&a, &btc_token, 25, AssetType::ReservedWithdrawal).unwrap(); + assert_eq!( + XAssets::asset_balance(&a, &btc_token, AssetType::ReservedWithdrawal), + 25 + ); + assert_eq!(XAssets::free_balance(&a, &btc_token), 25); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 50); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::ReservedWithdrawal), + 25 + ); // destroy - TokenBalances::destroy(&a, &btc_symbol.clone(), 25, Default::default()).unwrap(); - assert_eq!(TokenBalances::reserved_token(&reserved_key), 0); - assert_eq!(TokenBalances::free_token(&key), 25); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 25); - assert_eq!(TokenBalances::total_reserved_token(&btc_symbol.clone()), 0); - assert_eq!(TokenBalances::total_token(&btc_symbol.clone()), 125); + XAssets::destroy(&a, &btc_token, 25, AssetType::ReservedWithdrawal).unwrap(); + assert_eq!( + XAssets::asset_balance(&a, &btc_token, AssetType::ReservedWithdrawal), + 0 + ); + assert_eq!(XAssets::free_balance(&a, &btc_token), 25); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 25); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::ReservedWithdrawal), + 0 + ); + assert_eq!(XAssets::all_type_balance(&btc_token), 125); }) } @@ -236,28 +215,38 @@ fn test_normal_issue_and_destroy() { fn test_unlock_issue_and_destroy2() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_symbol = b"x-btc".to_vec(); - let key = (a, btc_symbol.clone()); - let reserved_key = (a, btc_symbol.clone(), Default::default()); + let btc_token = b"BTC".to_vec(); // issue - TokenBalances::issue(&a, &btc_symbol.clone(), 50).unwrap(); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 50); - assert_eq!(TokenBalances::total_token(&btc_symbol.clone()), 150); + XAssets::issue(&a, &btc_token, 50).unwrap(); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 50); + assert_eq!(XAssets::all_type_balance(&btc_token), 150); // reserve - TokenBalances::reserve(&a, &btc_symbol.clone(), 25, Default::default()).unwrap(); - assert_eq!(TokenBalances::reserved_token(&reserved_key), 25); - assert_eq!(TokenBalances::free_token(&key), 25); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 50); - assert_eq!(TokenBalances::total_reserved_token(&btc_symbol.clone()), 25); + XAssets::reserve(&a, &btc_token, 25, AssetType::ReservedWithdrawal).unwrap(); + assert_eq!( + XAssets::asset_balance(&a, &btc_token, AssetType::ReservedWithdrawal), + 25 + ); + assert_eq!(XAssets::free_balance(&a, &btc_token), 25); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 50); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::ReservedWithdrawal), + 25 + ); // unreserve - TokenBalances::unreserve(&a, &btc_symbol.clone(), 10, Default::default()).unwrap(); - assert_eq!(TokenBalances::reserved_token(&reserved_key), 15); - assert_eq!(TokenBalances::free_token(&key), 35); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 50); - assert_eq!(TokenBalances::total_reserved_token(&btc_symbol.clone()), 15); + XAssets::unreserve(&a, &btc_token, 10, AssetType::ReservedWithdrawal).unwrap(); + assert_eq!( + XAssets::asset_balance(&a, &btc_token, AssetType::ReservedWithdrawal), + 15 + ); + assert_eq!(XAssets::free_balance(&a, &btc_token), 35); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 50); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::ReservedWithdrawal), + 15 + ); }) } @@ -265,48 +254,40 @@ fn test_unlock_issue_and_destroy2() { fn test_error_issue_and_destroy1() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_symbol = b"x-btc".to_vec(); + let btc_token = b"BTC".to_vec(); // issue - TokenBalances::issue(&a, &btc_symbol.clone(), 50).unwrap(); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 50); - assert_eq!(TokenBalances::total_token(&btc_symbol.clone()), 150); + XAssets::issue(&a, &btc_token, 50).unwrap(); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 50); + assert_eq!(XAssets::all_type_balance(&btc_token), 150); // destroy first // destroy -<<<<<<< HEAD - assert_err!(TokenBalances::destroy(&a, &btc_symbol.clone(), 25, Default::default()), "reserved token too low to destroy"); - // reserve - assert_eq!(TokenBalances::total_free_token(&btc_symbol.clone()), 150); - assert_err!(TokenBalances::reserve(&a, &btc_symbol.clone(), 100, Default::default()), "free token too low to reserve"); - // lock first - assert_ok!(TokenBalances::reserve(&a, &btc_symbol.clone(), 25, Default::default())); - // destroy - assert_ok!(TokenBalances::destroy(&a, &btc_symbol.clone(), 25, Default::default())); -======= assert_err!( - TokenBalances::destroy(&a, &btc_symbol.clone(), 25, Default::default()), - "reserved token too low to destroy" + XAssets::destroy(&a, &btc_token, 25, AssetType::ReservedWithdrawal), + "reserved balance too low to destroy" ); // reserve - assert_eq!(TokenBalances::total_free_token(&btc_symbol.clone()), 150); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::Free), + 150 + ); assert_err!( - TokenBalances::reserve(&a, &btc_symbol.clone(), 100, Default::default()), - "free token too low to reserve" + XAssets::reserve(&a, &btc_token, 100, AssetType::ReservedWithdrawal), + "free balance too low to reserve" ); // lock first - assert_ok!(TokenBalances::reserve( + assert_ok!(XAssets::reserve( &a, - &btc_symbol.clone(), + &btc_token, 25, - Default::default() + AssetType::ReservedWithdrawal )); // destroy - assert_ok!(TokenBalances::destroy( + assert_ok!(XAssets::destroy( &a, - &btc_symbol.clone(), + &btc_token, 25, - Default::default() + AssetType::ReservedWithdrawal )); ->>>>>>> develop }) } @@ -314,31 +295,22 @@ fn test_error_issue_and_destroy1() { fn test_error_issue_and_destroy2() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_symbol = b"x-btc".to_vec(); + let btc_token = b"BTC".to_vec(); // issue - TokenBalances::issue(&a, &btc_symbol.clone(), 50).unwrap(); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol), 50); - assert_eq!(TokenBalances::total_token(&btc_symbol.clone()), 150); + XAssets::issue(&a, &btc_token, 50).unwrap(); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 50); + assert_eq!(XAssets::all_type_balance(&btc_token), 150); // overflow let i: i32 = -1; -<<<<<<< HEAD - assert_err!(TokenBalances::reserve(&a, &btc_symbol.clone(), i as TokenBalance, Default::default()), "free token too low to reserve"); - assert_err!(TokenBalances::issue(&a, &btc_symbol.clone(), i as TokenBalance), "free token too high to issue"); -======= + assert_err!( - TokenBalances::reserve( - &a, - &btc_symbol.clone(), - i as TokenBalance, - Default::default() - ), - "free token too low to reserve" + XAssets::reserve(&a, &btc_token, i as Balance, AssetType::ReservedWithdrawal), + "free balance too low to reserve" ); assert_err!( - TokenBalances::issue(&a, &btc_symbol.clone(), i as TokenBalance), - "free token too high to issue" + XAssets::issue(&a, &btc_token, i as Balance), + "free balance too high to issue" ); ->>>>>>> develop }) } @@ -346,120 +318,106 @@ fn test_error_issue_and_destroy2() { fn test_error_issue_and_destroy3() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_symbol = b"x-btc".to_vec(); + let btc_token = b"BTC".to_vec(); // lock or destroy without init -<<<<<<< HEAD - assert_err!(TokenBalances::destroy(&a, &btc_symbol.clone(), 25, Default::default()), "not a existed token in this account token list"); - assert_err!(TokenBalances::reserve(&a, &btc_symbol.clone(), 25, Default::default()), "not a existed token in this account token list"); - TokenBalances::issue(&a, &btc_symbol.clone(), 0).unwrap(); - assert_err!(TokenBalances::destroy(&a, &btc_symbol.clone(), 25, Default::default()), "reserved token too low to destroy"); - assert_err!(TokenBalances::reserve(&a, &btc_symbol.clone(), 25, Default::default()), "free token too low to reserve"); - - TokenBalances::issue(&a, &btc_symbol.clone(), 100).unwrap(); - assert_ok!(TokenBalances::reserve(&a, &btc_symbol.clone(), 25, Default::default())); - assert_ok!(TokenBalances::destroy(&a, &btc_symbol.clone(), 25, Default::default())); -======= assert_err!( - TokenBalances::destroy(&a, &btc_symbol.clone(), 25, Default::default()), + XAssets::destroy(&a, &btc_token, 25, AssetType::ReservedWithdrawal), "not a existed token in this account token list" ); assert_err!( - TokenBalances::reserve(&a, &btc_symbol.clone(), 25, Default::default()), + XAssets::reserve(&a, &btc_token, 25, AssetType::ReservedWithdrawal), "not a existed token in this account token list" ); - TokenBalances::issue(&a, &btc_symbol.clone(), 0).unwrap(); + XAssets::issue(&a, &btc_token, 0).unwrap(); assert_err!( - TokenBalances::destroy(&a, &btc_symbol.clone(), 25, Default::default()), - "reserved token too low to destroy" + XAssets::destroy(&a, &btc_token, 25, AssetType::ReservedWithdrawal), + "reserved balance too low to destroy" ); assert_err!( - TokenBalances::reserve(&a, &btc_symbol.clone(), 25, Default::default()), - "free token too low to reserve" + XAssets::reserve(&a, &btc_token, 25, AssetType::ReservedWithdrawal), + "free balance too low to reserve" ); - TokenBalances::issue(&a, &btc_symbol.clone(), 100).unwrap(); - assert_ok!(TokenBalances::reserve( + XAssets::issue(&a, &btc_token, 100).unwrap(); + assert_ok!(XAssets::reserve( &a, - &btc_symbol.clone(), + &btc_token, 25, - Default::default() + AssetType::ReservedWithdrawal )); - assert_ok!(TokenBalances::destroy( + assert_ok!(XAssets::destroy( &a, - &btc_symbol.clone(), + &btc_token, 25, - Default::default() + AssetType::ReservedWithdrawal )); ->>>>>>> develop }) } #[test] fn test_transfer_not_init() { - with_externalities(&mut new_test_ext2(), || { + with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let new_id: u64 = 100; - let btc_symbol = b"x-btc".to_vec(); - TokenBalances::issue(&a, &btc_symbol.clone(), 50).unwrap(); - assert_ok!(TokenBalances::transfer( + let new_id: u64 = 1000; + let btc_token = b"BTC".to_vec(); + let chainx_token = XAssets::TOKEN.to_vec(); + XAssets::issue(&a, &btc_token, 50).unwrap(); + assert_ok!(XAssets::transfer( Some(a).into(), new_id.into(), - btc_symbol.clone(), - 25 + btc_token.clone(), + 25, + b"".to_vec() )); assert_eq!(Balances::lookup_index(3), Some(new_id)); - assert_err!( - associations::Module::::init_account(Some(a).into(), new_id.into(),), - "this account is existing" - ); - assert_ok!(TokenBalances::transfer( + assert_ok!(XAssets::transfer( Some(a).into(), new_id.into(), - btc_symbol.clone(), - 25 + btc_token.clone(), + 25, + b"".to_vec() )); - assert_ok!(TokenBalances::transfer( + assert_ok!(XAssets::transfer( Some(a).into(), new_id.into(), - Test::CHAINX_SYMBOL.to_vec(), - 25 + chainx_token.clone(), + 25, + b"".to_vec() )); - assert_eq!( - TokenBalances::free_token(&(a, Test::CHAINX_SYMBOL.to_vec())), - 1000 - 10 - 10 - 25 - 10 - ); - assert_eq!( - TokenBalances::free_token(&(new_id, Test::CHAINX_SYMBOL.to_vec())), - 25 - ); + assert_eq!(XAssets::free_balance(&a, &chainx_token), 1000 - 25); + assert_eq!(XAssets::free_balance(&new_id, &chainx_token), 25); }) } #[test] fn test_transfer_chainx() { - with_externalities(&mut new_test_ext2(), || { + with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid let b: u64 = 2; // accountid - assert_ok!(TokenBalances::transfer( + + let chainx_token = XAssets::TOKEN.to_vec(); + + assert_ok!(XAssets::transfer( Some(a).into(), b.into(), - Test::CHAINX_SYMBOL.to_vec(), - 25 + chainx_token.clone(), + 25, + b"".to_vec() )); - assert_eq!( - TokenBalances::free_token(&(a, Test::CHAINX_SYMBOL.to_vec())), - 1000 - 10 - 25 - ); - assert_eq!( - TokenBalances::free_token(&(b, Test::CHAINX_SYMBOL.to_vec())), - 510 + 25 - ); + assert_eq!(XAssets::free_balance(&a, &chainx_token), 1000 - 25); + assert_eq!(XAssets::free_balance(&b, &chainx_token), 510 + 25); assert_err!( - TokenBalances::transfer(Some(a).into(), b.into(), Test::CHAINX_SYMBOL.to_vec(), 1000), - "balance too low to send value" + XAssets::transfer( + Some(a).into(), + b.into(), + chainx_token.clone(), + 1000, + b"".to_vec() + ), + "free balance too low" ); }) } @@ -469,20 +427,29 @@ fn test_transfer_token() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid let b: u64 = 2; // accountid - let btc_symbol = b"x-btc".to_vec(); + let btc_token = b"BTC".to_vec(); // issue 50 to account 1 - TokenBalances::issue(&a, &btc_symbol.clone(), 50).unwrap(); + XAssets::issue(&a, &btc_token, 50).unwrap(); // transfer - TokenBalances::transfer(Some(a).into(), b.into(), btc_symbol.clone(), 25).unwrap(); + XAssets::transfer( + Some(a).into(), + b.into(), + btc_token.clone(), + 25, + b"".to_vec(), + ) + .unwrap(); // sum not change - assert_eq!(TokenBalances::total_free_token(&btc_symbol.clone()), 150); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 25); - assert_eq!(TokenBalances::free_token(&(b, btc_symbol.clone())), 25); - assert_eq!(Balances::free_balance(&a), 990); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::Free), + 150 + ); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 25); + assert_eq!(XAssets::free_balance(&b, &btc_token), 25); assert_err!( - TokenBalances::transfer(Some(a).into(), b.into(), btc_symbol.clone(), 50), - "free token too low to send value" + XAssets::transfer(Some(a).into(), b.into(), btc_token, 50, b"".to_vec()), + "free balance too low" ) }) } @@ -491,239 +458,187 @@ fn test_transfer_token() { fn test_transfer_to_self() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_symbol = b"x-btc".to_vec(); + let btc_token = b"BTC".to_vec(); // issue 50 to account 1 - TokenBalances::issue(&a, &btc_symbol.clone(), 50).unwrap(); + XAssets::issue(&a, &btc_token, 50).unwrap(); // transfer assert_err!( - TokenBalances::transfer(Some(a).into(), a.into(), btc_symbol.clone(), 25), - "transactor and dest account are same" + XAssets::transfer( + Some(a).into(), + a.into(), + btc_token.clone(), + 25, + b"".to_vec() + ), + "from and to are same account" ); // sum not change - assert_eq!(TokenBalances::total_free_token(&btc_symbol.clone()), 150); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 50); - assert_eq!(Balances::free_balance(&a), 990); - }) -} - -#[test] -fn test_transfer_err() { - with_externalities(&mut new_test_ext(), || { - let a: u64 = 1; // accountid - let b: u64 = 2; // accountid - let btc_symbol = b"x-btc".to_vec(); - // issue 50 to account 2 - TokenBalances::issue(&b, &btc_symbol.clone(), 50).unwrap(); - // transfer - TokenBalances::transfer(Some(b).into(), a.into(), btc_symbol.clone(), 25).unwrap(); - // sum not change - assert_eq!(TokenBalances::total_free_token(&btc_symbol.clone()), 150); - assert_eq!(TokenBalances::free_token(&(b, btc_symbol.clone())), 25); - assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol.clone()), 25); - assert_eq!(Balances::free_balance(&b), 500); - - assert_err!( - TokenBalances::transfer(Some(b).into(), a.into(), btc_symbol.clone(), 1), - "chainx balance is not enough after this tx, not allow to be killed at here" + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::Free), + 150 ); - assert_eq!(Balances::free_balance(&b), 500); + assert_eq!(XAssets::all_type_balance_of(&a, &btc_token), 50); }) } #[test] fn test_set_token() { - with_externalities(&mut new_test_ext2(), || { + with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_symbol = b"x-btc".to_vec(); - TokenBalances::issue(&a, &btc_symbol.clone(), 50).unwrap(); - assert_ok!(TokenBalances::set_free_token( - a.into(), - Test::CHAINX_SYMBOL.to_vec(), - 500 + let btc_token = b"BTC".to_vec(); + XAssets::issue(&a, &btc_token, 50).unwrap(); + let b = CodecBTreeMap::(BTreeMap::from_iter( + vec![(AssetType::Free, 500)].into_iter(), )); + assert_ok!(XAssets::set_balance(a.into(), XAssets::TOKEN.to_vec(), b)); assert_eq!(Balances::free_balance(&a), 500); - assert_ok!(TokenBalances::set_free_token( - a.into(), - btc_symbol.clone(), - 500 + let b = CodecBTreeMap::(BTreeMap::from_iter( + vec![(AssetType::Free, 500)].into_iter(), )); - assert_eq!(TokenBalances::free_token(&(a, btc_symbol.clone())), 500); - assert_eq!(TokenBalances::total_token(&btc_symbol), 500 + 100); + assert_ok!(XAssets::set_balance(a.into(), btc_token.clone(), b)); + assert_eq!(XAssets::free_balance(&a, &btc_token), 500); + assert_eq!( + XAssets::total_asset_balance(&btc_token, AssetType::Free), + 500 + 100 + ); + assert_eq!(XAssets::all_type_balance(&btc_token), 500 + 100); - assert_ok!(TokenBalances::set_free_token( - a.into(), - btc_symbol.clone(), - 600 + let b = CodecBTreeMap::(BTreeMap::from_iter( + vec![(AssetType::Free, 600)].into_iter(), )); - assert_eq!(TokenBalances::free_token(&(a, btc_symbol.clone())), 600); - assert_eq!(TokenBalances::total_token(&btc_symbol), 600 + 100); + assert_ok!(XAssets::set_balance(a.into(), btc_token.clone(), b)); + assert_eq!(XAssets::free_balance(&a, &btc_token), 600); + assert_eq!(XAssets::all_type_balance(&btc_token), 600 + 100); }) } #[test] fn test_char_valid() { with_externalities(&mut new_test_ext(), || { - let to: balances::Address = balances::address::Address::Id(2); - let origin = system::RawOrigin::Signed(1).into(); - let sym = b"".to_vec(); - assert_err!( - TokenBalances::transfer(origin, to.clone(), sym, 10), - "symbol length too long or zero" - ); + let token = b"".to_vec(); + let asset = Asset::new(token, Chain::Ethereum, 1, b"123".to_vec()); + assert_err!(asset, "Token length is zero or too long."); - let origin = system::RawOrigin::Signed(1).into(); - let sym = b"dfasdlfjkalsdjfklasjdflkasjdfklasjklfasjfkdlsajf".to_vec(); - assert_err!( - TokenBalances::transfer(origin, to.clone(), sym, 10), - "symbol length too long or zero" - ); + let token = b"dfasdlfjkalsdjfklasjdflkasjdfklasjklfasjfkdlsajf".to_vec(); + let asset = Asset::new(token, Chain::Ethereum, 1, b"123".to_vec()); + assert_err!(asset, "Token length is zero or too long."); - let origin = system::RawOrigin::Signed(1).into(); - let sym = b"23jfkldae(".to_vec(); + let token = b"23jfkldae(".to_vec(); + let asset = Asset::new(token, Chain::Ethereum, 1, b"123".to_vec()); assert_err!( - TokenBalances::transfer(origin, to.clone(), sym, 10), - "not a valid symbol char for number, capital/small letter or '-', '.', '|', '~'" + asset, + "Token can only use numbers, capital/lowercase letters or \'-\', \'.\', \'|\', \'~\'." ); - let t: Token = Token::new(b"x-btc2".to_vec(), b"btc token fdsfsdfasfasdfasdfasdfasdfasdfasdfjaskldfjalskdjflk;asjdfklasjkldfjalksdjfklasjflkdasjflkjkladsjfkrtewtewrtwertrjhjwretywertwertwerrtwerrtwerrtwertwelasjdfklsajdflkaj".to_vec(), 8); + let asset = Asset::new(b"BTC2".to_vec(), Chain::Ethereum, 1, b"btc token fdsfsdfasfasdfasdfasdfasdfasdfasdfjaskldfjalskdjflk;asjdfklasjkldfjalksdjfklasjflkdasjflkjkladsjfkrtewtewrtwertrjhjwretywertwertwerrtwerrtwerrtwertwelasjdfklsajdflkaj".to_vec()); + assert_err!(asset, "Token desc too long"); + let asset = Asset::new(b"BTC?".to_vec(), Chain::Ethereum, 1, b"123".to_vec()); assert_err!( - TokenBalances::register_token(t, 0, 0), - "token desc length too long" - ); - let t: Token = Token::new(b"x-btc?".to_vec(), b"btc token".to_vec(), 8); - assert_err!( - TokenBalances::register_token(t, 0, 0), - "not a valid symbol char for number, capital/small letter or '-', '.', '|', '~'" + asset, + "Token can only use numbers, capital/lowercase letters or \'-\', \'.\', \'|\', \'~\'." ) }) } #[test] fn test_chainx() { - with_externalities(&mut new_test_ext2(), || { + with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let sym = Test::CHAINX_SYMBOL.to_vec(); + let token = XAssets::TOKEN.to_vec(); assert_err!( - TokenBalances::issue(&a, &sym, 100), - "can't issue chainx token" + XAssets::issue(&a, &token, 100), + "should not use chainx token here" ); - assert_ok!(TokenBalances::reserve(&a, &sym, 100, Default::default())); + assert_ok!(XAssets::reserve( + &a, + &token, + 100, + AssetType::ReservedWithdrawal + )); assert_eq!(Balances::free_balance(&a), 900); - assert_eq!(Balances::reserved_balance(&a), 100); -<<<<<<< HEAD - assert_eq!(TokenBalances::reserved_token(&(a, sym.clone(), Default::default())), 100); - - assert_ok!(TokenBalances::unreserve(&a, &sym, 50, Default::default())); - assert_eq!(Balances::free_balance(&a), 950); - assert_eq!(TokenBalances::reserved_token(&(a, sym.clone(), Default::default())), 50); - assert_eq!(Balances::reserved_balance(&a), 50); - assert_err!(TokenBalances::destroy(&a, &sym, 50, Default::default()), "can't destroy chainx token"); - - assert_err!(TokenBalances::transfer_token(Some(b).into(), a.into(), sym.clone(), 1), "not allow to transfer chainx use transfer_token"); -======= + assert_eq!(Balances::reserved_balance(&a), 0); assert_eq!( - TokenBalances::reserved_token(&(a, sym.clone(), Default::default())), + XAssets::asset_balance(&a, &token, AssetType::ReservedWithdrawal), 100 ); - assert_ok!(TokenBalances::unreserve(&a, &sym, 50, Default::default())); + assert_ok!(XAssets::unreserve( + &a, + &token, + 50, + AssetType::ReservedWithdrawal + )); assert_eq!(Balances::free_balance(&a), 950); assert_eq!( - TokenBalances::reserved_token(&(a, sym.clone(), Default::default())), + XAssets::asset_balance(&a, &token, AssetType::ReservedWithdrawal), 50 ); - assert_eq!(Balances::reserved_balance(&a), 50); + assert_eq!(Balances::reserved_balance(&a), 0); assert_err!( - TokenBalances::destroy(&a, &sym, 50, Default::default()), - "can't destroy chainx token" + XAssets::destroy(&a, &token, 50, AssetType::ReservedWithdrawal), + "should not use chainx token here" ); ->>>>>>> develop }) } #[test] fn test_chainx_err() { - with_externalities(&mut new_test_ext2(), || { + with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let sym = Test::CHAINX_SYMBOL.to_vec(); + let token = XAssets::TOKEN.to_vec(); -<<<<<<< HEAD - assert_err!(TokenBalances::reserve(&a, &sym, 2000, Default::default()), "chainx free token too low to reserve"); - assert_err!(TokenBalances::unreserve(&a, &sym, 10, Default::default()), "chainx reserved token too low to unreserve"); -======= assert_err!( - TokenBalances::reserve(&a, &sym, 2000, Default::default()), - "chainx free token too low to reserve" + XAssets::reserve(&a, &token, 2000, AssetType::ReservedWithdrawal), + "free balance too low to reserve" ); assert_err!( - TokenBalances::unreserve(&a, &sym, 10, Default::default()), - "chainx reserved token too low to unreserve" + XAssets::unreserve(&a, &token, 10, AssetType::ReservedWithdrawal), + "reserved balance too low to unreserve" ); ->>>>>>> develop let i: i32 = -1; - let larger_balance: TokenBalance = (i as u64) as u128 + 2; - - assert_eq!(larger_balance, 18446744073709551617); - assert_eq!(larger_balance as u64, 1); + let larger_balance: Balance = i as u64; -<<<<<<< HEAD - assert_ok!(TokenBalances::reserve(&a, &sym, larger_balance, Default::default())); -======= - assert_ok!(TokenBalances::reserve( - &a, - &sym, - larger_balance, - Default::default() - )); ->>>>>>> develop - assert_eq!(Balances::free_balance(&a), 999); + assert_eq!(larger_balance, 18446744073709551615); - let i: i32 = -1; - let max_balance: TokenBalance = i as u128; - assert_eq!(max_balance as u64, 18446744073709551615); -<<<<<<< HEAD - assert_err!(TokenBalances::reserve(&a, &sym, max_balance, Default::default()), "chainx free token too low to reserve"); -======= assert_err!( - TokenBalances::reserve(&a, &sym, max_balance, Default::default()), - "chainx free token too low to reserve" + XAssets::reserve(&a, &token, larger_balance, AssetType::ReservedWithdrawal), + "free balance too low to reserve" ); ->>>>>>> develop }) } #[test] fn test_move() { - with_externalities(&mut new_test_ext2(), || { + with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid let b: u64 = 2; // accountid - let sym = Test::CHAINX_SYMBOL.to_vec(); - assert_ok!(TokenBalances::move_free_token(&a, &b, &sym, 100)); + let token = XAssets::TOKEN.to_vec(); + assert_ok!(XAssets::move_free_balance(&a, &b, &token, 100)); assert_err!( - TokenBalances::move_free_token(&a, &b, &sym, 1000), + XAssets::move_free_balance(&a, &b, &token, 1000), TokenErr::NotEnough ); assert_eq!(Balances::free_balance(&a), 900); assert_eq!(Balances::free_balance(&b), 510 + 100); - let sym = b"x-btc".to_vec(); + let token = b"BTC".to_vec(); assert_err!( - TokenBalances::move_free_token(&a, &b, &sym, 100), + XAssets::move_free_balance(&a, &b, &token, 100), TokenErr::InvalidToken ); - TokenBalances::issue(&a, &sym, 100).unwrap(); - assert_ok!(TokenBalances::move_free_token(&a, &b, &sym, 100)); + XAssets::issue(&a, &token, 100).unwrap(); + assert_ok!(XAssets::move_free_balance(&a, &b, &token, 100)); assert_err!( - TokenBalances::move_free_token(&a, &b, &sym, 1000), + XAssets::move_free_balance(&a, &b, &token, 1000), TokenErr::NotEnough ); - assert_eq!(TokenBalances::free_token(&(a.clone(), sym.clone())), 0); - assert_eq!(TokenBalances::free_token(&(b.clone(), sym.clone())), 100); + assert_eq!(XAssets::free_balance(&a, &token), 0); + assert_eq!(XAssets::free_balance(&b, &token), 100); }) } diff --git a/xrml/xassets/process/src/lib.rs b/xrml/xassets/process/src/lib.rs index a0b811bb808d5..b31bff522b874 100644 --- a/xrml/xassets/process/src/lib.rs +++ b/xrml/xassets/process/src/lib.rs @@ -46,8 +46,8 @@ extern crate xrml_xbridge_bitcoin as xbitcoin; #[cfg(test)] extern crate base58; -//#[cfg(test)] -//mod tests; +#[cfg(test)] +mod tests; use rstd::prelude::*; //use rstd::result::Result as StdResult; diff --git a/xrml/xassets/process/src/tests.rs b/xrml/xassets/process/src/tests.rs index d9b18c9f1ff0d..c6b1675aed326 100644 --- a/xrml/xassets/process/src/tests.rs +++ b/xrml/xassets/process/src/tests.rs @@ -9,9 +9,7 @@ use runtime_primitives::traits::BlakeTwo256; use runtime_primitives::BuildStorage; use super::*; -use Balances::{DescString, TokenString, Token}; - -use base58::FromBase58; +use xassets::{Asset, Chain}; impl_outer_origin! { pub enum Origin for Test {} @@ -41,45 +39,30 @@ impl balances::Trait for Test { type Event = (); } -impl cxsystem::Trait for Test {} - -impl associations::Trait for Test { - type OnCalcFee = cxsupport::Module; - type Event = (); -} - -impl cxsupport::Trait for Test {} - impl consensus::Trait for Test { const NOTE_OFFLINE_POSITION: u32 = 1; type Log = DigestItem; type SessionKey = u64; - type OnOfflineValidator = (); + type InherentOfflineReport = (); } impl timestamp::Trait for Test { const TIMESTAMP_SET_POSITION: u32 = 0; type Moment = u64; + type OnTimestampSet = (); } -// define Balances module type -pub type Balance = u128; - -impl Balances::Trait for Test { - const CHAINX_Token: TokenString = b"pcx"; - const CHAINX_TOKEN_DESC: DescString = b"this is pcx for mock"; - type Balance = Balance; +impl xassets::Trait for Test { type Event = (); - type OnMoveToken = (); + type OnAssetChanged = (); + type OnAssetRegistration = (); } -impl financialrecords::Trait for Test { +impl xrecords::Trait for Test { type Event = (); - type OnDepositToken = (); - type OnWithdrawToken = (); } -impl btc::Trait for Test { +impl xbitcoin::Trait for Test { type Event = (); } @@ -90,7 +73,8 @@ impl Trait for Test {} pub fn new_test_ext() -> runtime_io::TestExternalities { let mut r = system::GenesisConfig::::default() .build_storage() - .unwrap(); + .unwrap() + .0; // balance r.extend( balances::GenesisConfig:: { @@ -103,29 +87,29 @@ pub fn new_test_ext() -> runtime_io::TestExternalities { reclaim_rebate: 0, } .build_storage() - .unwrap(), + .unwrap() + .0, ); // token balance - let t: Token = Token::new( - btc::Module::::Token.to_vec(), - b"btc token".to_vec(), - 8, - ); + let btc_asset = Asset::new( + b"BTC".to_vec(), // token + Chain::Bitcoin, + 8, // bitcoin precision + b"BTC chainx".to_vec(), + ) + .unwrap(); r.extend( - Balances::GenesisConfig:: { - chainx_precision: 8, - token_list: vec![(t, vec![])], - transfer_token_fee: 10, + xassets::GenesisConfig:: { + pcx: (3, b"PCX onchain token".to_vec()), + memo_len: 128, + // asset, is_psedu_intention, init for account + // Vec<(Asset, bool, Vec<(T::AccountId, u64)>)>; + asset_list: vec![(btc_asset, true, vec![(3, 100)])], } .build_storage() - .unwrap(), - ); - // financialrecords - r.extend( - GenesisConfig:: { withdrawal_fee: 10 } - .build_storage() - .unwrap(), + .unwrap() + .0, ); r.into() @@ -134,9 +118,9 @@ pub fn new_test_ext() -> runtime_io::TestExternalities { #[test] fn test_check_btc_addr() { with_externalities(&mut new_test_ext(), || { - assert_ok!(financialrecords::Module::::deposit( + assert_ok!(xrecords::Module::::deposit( &1, - &b"btc".to_vec(), + &b"BTC".to_vec(), 1000 )); @@ -144,7 +128,7 @@ fn test_check_btc_addr() { assert_err!( Module::::withdraw( origin, - b"btc".to_vec(), + b"BTC".to_vec(), 100, b"sdfds".to_vec(), b"".to_vec() @@ -155,11 +139,26 @@ fn test_check_btc_addr() { let origin = system::RawOrigin::Signed(1).into(); assert_ok!(Module::::withdraw( origin, - b"btc".to_vec(), + b"BTC".to_vec(), 100, - "mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".from_base58().unwrap(), + b"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".to_vec(), b"".to_vec() )); + + assert_eq!( + xassets::Module::::free_balance(&1, &b"BTC".to_vec()), + 900 + ); + + let nums = + xrecords::Module::::withdrawal_application_numbers(Chain::Bitcoin, 10).unwrap(); + for n in nums { + assert_ok!(xrecords::Module::::withdrawal_finish(n)); + } + assert_eq!( + xassets::Module::::all_type_balance_of(&1, &b"BTC".to_vec()), + 900 + ) }) } @@ -167,14 +166,14 @@ fn test_check_btc_addr() { fn test_check_btc_addr2() { with_externalities(&mut new_test_ext(), || { let r = Module::::verify_addr( - &btc::Module::::Token.to_vec(), + &xbitcoin::Module::::TOKEN.to_vec(), b"2N8tR484JD32i1DY2FnRPLwBVaNuXSfzoAv", b"", ); assert_eq!(r, Ok(())); let r = Module::::verify_addr( - &btc::Module::::Token.to_vec(), + &xbitcoin::Module::::TOKEN.to_vec(), b"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b", b"", ); diff --git a/xrml/xassets/records/src/lib.rs b/xrml/xassets/records/src/lib.rs index 28d54e51a1689..ff958da8c1a08 100644 --- a/xrml/xassets/records/src/lib.rs +++ b/xrml/xassets/records/src/lib.rs @@ -35,8 +35,8 @@ extern crate srml_system as system; extern crate xrml_xassets_assets as xassets; extern crate xrml_xsupport as xsupport; -//#[cfg(test)] -//mod tests; +#[cfg(test)] +mod tests; mod withdrawal; diff --git a/xrml/xassets/records/src/tests.rs b/xrml/xassets/records/src/tests.rs index e5bb8c76ea776..43cc9751bc534 100644 --- a/xrml/xassets/records/src/tests.rs +++ b/xrml/xassets/records/src/tests.rs @@ -9,7 +9,6 @@ use runtime_primitives::traits::BlakeTwo256; use runtime_primitives::BuildStorage; use super::*; -use xassets::{DescString, TokenString, Token}; impl_outer_origin! { pub enum Origin for Test {} @@ -39,82 +38,26 @@ impl balances::Trait for Test { type Event = (); } -impl cxsystem::Trait for Test {} - -impl associations::Trait for Test { - type OnCalcFee = cxsupport::Module; - type Event = (); -} - -impl cxsupport::Trait for Test {} - -// define xassets module type -pub type Balance = u128; - +// assets impl xassets::Trait for Test { - const CHAINX_Token: TokenString = b"pcx"; - const CHAINX_TOKEN_DESC: DescString = b"this is pcx for mock"; - type Balance = Balance; type Event = (); - type OnMoveToken = (); + type OnAssetChanged = (); + type OnAssetRegistration = (); } impl Trait for Test { type Event = (); - type OnDepositToken = (); - type OnWithdrawToken = (); } -// This function basically just builds a genesis storage key/value store according to -// our desired mockup. pub fn new_test_ext() -> runtime_io::TestExternalities { let mut r = system::GenesisConfig::::default() .build_storage() - .unwrap(); + .unwrap() + .0; // balance r.extend( balances::GenesisConfig:: { - balances: vec![(1, 1000), (2, 510)], - transaction_base_fee: 0, - transaction_byte_fee: 0, - existential_deposit: 500, - transfer_fee: 0, - creation_fee: 0, - reclaim_rebate: 0, - } - .build_storage() - .unwrap(), - ); - // token balance - let t: Token = Token::new(b"x-btc".to_vec(), b"btc token".to_vec(), 8); - let t2: Token = Token::new(b"x-eth".to_vec(), b"eth token".to_vec(), 4); - - r.extend( - xassets::GenesisConfig:: { - chainx_precision: 8, - token_list: vec![(t, vec![]), (t2, vec![])], - transfer_token_fee: 10, - } - .build_storage() - .unwrap(), - ); - // financialrecords - r.extend( - GenesisConfig:: { withdrawal_fee: 10 } - .build_storage() - .unwrap(), - ); - r.into() -} - -pub fn new_test_ext2() -> runtime_io::TestExternalities { - let mut r = system::GenesisConfig::::default() - .build_storage() - .unwrap(); - // balance - r.extend( - balances::GenesisConfig:: { - balances: vec![(1, 1000), (2, 510)], + balances: vec![(1, 1000), (2, 510), (3, 1000)], transaction_base_fee: 0, transaction_byte_fee: 0, existential_deposit: 0, @@ -123,53 +66,74 @@ pub fn new_test_ext2() -> runtime_io::TestExternalities { reclaim_rebate: 0, } .build_storage() - .unwrap(), + .unwrap() + .0, ); - // token balance - let t: Token = Token::new(b"x-btc".to_vec(), b"btc token".to_vec(), 8); - let t2: Token = Token::new(b"x-eth".to_vec(), b"eth token".to_vec(), 4); + + let btc_asset = xassets::Asset::new( + b"BTC".to_vec(), // token + Chain::Bitcoin, + 8, // bitcoin precision + b"BTC chainx".to_vec(), + ) + .unwrap(); + + let eth_asset = xassets::Asset::new( + b"ETH".to_vec(), // token + Chain::Ethereum, + 8, // bitcoin precision + b"ETH chainx".to_vec(), + ) + .unwrap(); r.extend( xassets::GenesisConfig:: { - chainx_precision: 8, - token_list: vec![(t, vec![]), (t2, vec![])], - transfer_token_fee: 10, + pcx: (3, b"PCX onchain token".to_vec()), + memo_len: 128, + // asset, is_psedu_intention, init for account + // Vec<(Asset, bool, Vec<(T::AccountId, u64)>)>; + asset_list: vec![ + (btc_asset, true, vec![(3, 100)]), + (eth_asset, true, vec![(3, 100)]), + ], } .build_storage() - .unwrap(), - ); - // financialrecords - r.extend( - GenesisConfig:: { withdrawal_fee: 10 } - .build_storage() - .unwrap(), + .unwrap() + .0, ); + r.into() } -type FinancialRecords = Module; -type Balances = xassets::Module; -//type Balances = balances::Module; +type Records = Module; +type XAssets = xassets::Module; #[test] fn test_normal() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_Token = b"x-btc".to_vec(); + let btc_token = b"BTC".to_vec(); // deposit - let index = FinancialRecords::deposit_with_index(&a, &btc_Token, 100).unwrap(); - assert_eq!(index, 0); - FinancialRecords::deposit_finish_with_index(&a, index, Some(vec![])).unwrap(); + assert_ok!(Records::deposit(&a, &btc_token, 100)); + assert_eq!(XAssets::free_balance(&a, &btc_token), 100); + // withdraw - let index = - FinancialRecords::withdrawal_with_index(&a, &btc_Token, 50, vec![], vec![]).unwrap(); - assert_eq!(index, 1); - FinancialRecords::withdrawal_locking_with_index(&a, index).unwrap(); - assert_eq!( - FinancialRecords::withdrawal_finish_with_index(&a, index, Some(vec![])), - Ok(1) - ); + assert_ok!(Records::withdrawal( + &a, + &btc_token, + 50, + b"addr".to_vec(), + b"ext".to_vec() + )); + + let numbers = Records::withdrawal_application_numbers(Chain::Bitcoin, 10).unwrap(); + assert_eq!(numbers.len(), 1); + + for i in numbers { + assert_ok!(Records::withdrawal_finish(i)); + } + assert_eq!(XAssets::free_balance(&a, &btc_token), 50); }) } @@ -177,388 +141,94 @@ fn test_normal() { fn test_normal2() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_Token = b"x-btc".to_vec(); - let eth_Token = b"x-eth".to_vec(); + let btc_token = b"BTC".to_vec(); + let eth_token = b"ETH".to_vec(); // deposit - FinancialRecords::deposit_init(&a, &btc_Token, 100).unwrap(); - assert_ok!(FinancialRecords::deposit_finish( + assert_ok!(Records::deposit(&a, &btc_token, 100)); + assert_eq!(XAssets::free_balance(&a, &btc_token), 100); + assert_ok!(Records::deposit(&a, ð_token, 500)); + assert_eq!(XAssets::free_balance(&a, ð_token), 500); + + // withdraw + assert_ok!(Records::withdrawal( &a, - &btc_Token, - Some(vec![]) + &btc_token, + 50, + b"addr".to_vec(), + b"ext".to_vec() )); - assert_ok!(FinancialRecords::deposit( + // withdrawal twice at once + assert_ok!(Records::withdrawal( &a, - ð_Token, + ð_token, 100, - Some(vec![]) + b"addr".to_vec(), + b"ext".to_vec() )); - - assert_eq!(Balances::total_token_of(&a, &btc_Token), 100); - assert_eq!(Balances::total_token_of(&a, ð_Token), 100); - - // withdraw - FinancialRecords::withdrawal(&a, &btc_Token, 50, vec![], vec![]).unwrap(); - FinancialRecords::withdrawal(&a, ð_Token, 50, vec![], vec![]).unwrap(); - - assert_ok!(FinancialRecords::withdrawal_finish( + assert_ok!(Records::withdrawal( &a, - &btc_Token, - Some(vec![]) + ð_token, + 50, + b"addr".to_vec(), + b"ext".to_vec() )); - assert_ok!(FinancialRecords::withdrawal_finish(&a, ð_Token, None)); - assert_eq!(Balances::total_token_of(&a, &btc_Token), 50); - assert_eq!(Balances::total_token_of(&a, ð_Token), 100); + let mut numbers1 = Records::withdrawal_application_numbers(Chain::Bitcoin, 10).unwrap(); + assert_eq!(numbers1.len(), 1); - assert_eq!(FinancialRecords::record_list_len_of(&a), 4); + let numbers2 = Records::withdrawal_application_numbers(Chain::Ethereum, 10).unwrap(); + assert_eq!(numbers2.len(), 2); - let key1 = (a, btc_Token.clone()); - let key2 = (a, eth_Token.clone()); + numbers1.extend(numbers2); - assert_eq!(FinancialRecords::last_deposit_index_of(&key1).unwrap(), 0); - assert_eq!( - FinancialRecords::last_withdrawal_index_of(&key1).unwrap(), - 2 - ); - assert_eq!(FinancialRecords::last_deposit_index_of(&key2).unwrap(), 1); - assert_eq!( - FinancialRecords::last_withdrawal_index_of(&key2).unwrap(), - 3 - ); + for i in numbers1 { + assert_ok!(Records::withdrawal_finish(i)); + } + assert_eq!(XAssets::free_balance(&a, &btc_token), 50); + assert_eq!(XAssets::free_balance(&a, ð_token), 500 - 50 - 100); }) } #[test] -fn test_last_not_finish() { +fn test_withdrawal_larger() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_Token = b"x-btc".to_vec(); - FinancialRecords::deposit_init(&a, &btc_Token, 100).unwrap(); - assert_err!( - FinancialRecords::withdrawal(&a, &btc_Token, 50, vec![], vec![]), - "the account has no deposit record for this token yet" - ); - // let deposit fail - assert_ok!(FinancialRecords::deposit_finish(&a, &btc_Token, None)); // 1. deposit failed - assert_eq!(Balances::total_token_of(&a, &btc_Token), 0); + let btc_token = b"BTC".to_vec(); + assert_ok!(Records::deposit(&a, &btc_token, 10)); assert_err!( - FinancialRecords::withdrawal(&a, &btc_Token, 50, vec![], vec![]), - "not a existed token in this account token list" - ); - assert_eq!(FinancialRecords::record_list_len_of(&a), 1); - - FinancialRecords::deposit_init(&a, &btc_Token, 100).unwrap(); - assert_ok!(FinancialRecords::deposit_finish( - &a, - &btc_Token, - Some(vec![]) - )); // 2. deposit success - - assert_ok!(FinancialRecords::withdrawal( - &a, - &btc_Token, - 50, - vec![], - vec![] - )); - - assert_ok!(FinancialRecords::deposit(&a, &btc_Token, 50, Some(vec![]))); // 3. deposit success - - assert_eq!(Balances::total_token_of(&a, &btc_Token), 150); - assert_eq!( - Balances::free_token(&(a.clone(), btc_Token.clone())), - 100 - ); - - assert_ok!(FinancialRecords::withdrawal_finish(&a, &btc_Token, None)); // 4. withdrawal failed - assert_eq!( - Balances::free_token(&(a.clone(), btc_Token.clone())), - 150 - ); - - assert_ok!(FinancialRecords::withdrawal( - &a, - &btc_Token, - 25, - vec![], - vec![] - )); - assert_ok!(FinancialRecords::withdrawal_finish( - &a, - &btc_Token, - Some(vec![]) - )); // destroy token here 5. withdrawal success - assert_eq!(FinancialRecords::record_list_len_of(&a), 5); - assert_eq!( - Balances::free_token(&(a.clone(), btc_Token.clone())), - 125 + Records::withdrawal(&a, &btc_token, 50, b"addr".to_vec(), b"ext".to_vec()), + "free balance too low to reserve" ); - - assert_eq!(Balances::total_token(&btc_Token), 125); }) } #[test] -fn test_withdrawal_larger() { +fn test_withdrawal_chainx() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_Token = b"x-btc".to_vec(); - assert_ok!(FinancialRecords::deposit(&a, &btc_Token, 10, Some(vec![]))); - + let chainx_token = XAssets::TOKEN.to_vec(); assert_err!( - FinancialRecords::withdrawal(&a, &btc_Token, 50, vec![], vec![]), - "not enough free token to withdraw" + Records::deposit(&a, &chainx_token, 10), + "can\'t deposit/withdrawal chainx token" ); - assert_eq!(FinancialRecords::record_list_len_of(&a), 1); - }) -} -#[test] -fn test_withdrawal_first() { - with_externalities(&mut new_test_ext(), || { - let a: u64 = 1; // accountid - let btc_Token = b"x-btc".to_vec(); assert_err!( - FinancialRecords::withdrawal(&a, &btc_Token, 50, vec![], vec![]), - "the account has no deposit record for this token yet" + Records::withdrawal(&a, &chainx_token, 50, b"addr".to_vec(), b"ext".to_vec()), + "can\'t deposit/withdrawal chainx token" ); }) } #[test] -fn test_multi_token() { +fn test_withdrawal_first() { with_externalities(&mut new_test_ext(), || { let a: u64 = 1; // accountid - let btc_Token = b"x-btc".to_vec(); - let eth_Token = b"x-eth".to_vec(); - let key1 = (a, btc_Token.clone()); - let key2 = (a, eth_Token.clone()); - - assert_err!( - FinancialRecords::withdrawal_finish(&a, &btc_Token, Some(vec![])), - "have not executed withdrawal() or withdrawal_init() yet for this record" - ); + let btc_token = b"BTC".to_vec(); assert_err!( - FinancialRecords::withdrawal(&a, &btc_Token, 50, vec![], vec![]), - "the account has no deposit record for this token yet" - ); - - assert_ok!(FinancialRecords::deposit( - &a, - &btc_Token, - 100, - Some(vec![]) - )); // index = 0 - assert_ok!(FinancialRecords::deposit( - &a, - ð_Token, - 100, - Some(vec![]) - )); // eth 100 index = 1 - assert_ok!(FinancialRecords::deposit( - &a, - &btc_Token, - 100, - Some(vec![]) - )); // btc 200 index = 2 - - assert_eq!(FinancialRecords::last_deposit_index_of(&key1), Some(2)); - assert_eq!(FinancialRecords::last_deposit_index_of(&key2), Some(1)); - assert_eq!(FinancialRecords::last_withdrawal_index_of(&key2), None); - - assert_eq!(Balances::total_token_of(&a, &btc_Token), 200); - assert_eq!(Balances::total_token_of(&a, ð_Token), 100); - - // withdraw - assert_ok!(FinancialRecords::withdrawal( - &a, - &btc_Token, - 50, - vec![], - vec![] - )); // index = 3 - assert_err!(FinancialRecords::withdrawal(&a, &btc_Token, 25, vec![], vec![]), "the last action have not finished yet! only if the last deposit/withdrawal have finished you can do a new action."); - - assert_ok!(FinancialRecords::withdrawal( - &a, - ð_Token, - 50, - vec![], - vec![] - )); // parallel withdraw index = 4 - - assert_eq!( - Balances::free_token(&(a.clone(), btc_Token.clone())), - 150 - ); - assert_eq!( - Balances::free_token(&(a.clone(), eth_Token.clone())), - 50 - ); - - assert_ok!(FinancialRecords::deposit(&a, ð_Token, 50, Some(vec![]))); // deposit while withdraw index = 5 - - assert_eq!( - Balances::free_token(&(a.clone(), eth_Token.clone())), - 100 + Records::withdrawal(&a, &btc_token, 50, vec![], vec![]), + "not a existed token in this account token list" ); - - assert_ok!(FinancialRecords::withdrawal_finish( - &a, - &btc_Token, - Some(vec![]) - )); - assert_ok!(FinancialRecords::withdrawal_finish(&a, ð_Token, None)); - - assert_eq!(Balances::total_token_of(&a, &btc_Token), 150); - assert_eq!(Balances::total_token_of(&a, ð_Token), 150); - - assert_eq!(FinancialRecords::last_deposit_index_of(&key1), Some(2)); - assert_eq!(FinancialRecords::last_deposit_index_of(&key2), Some(5)); - - assert_eq!(FinancialRecords::last_withdrawal_index_of(&key1), Some(3)); - assert_eq!(FinancialRecords::last_withdrawal_index_of(&key2), Some(4)); - - assert_eq!(FinancialRecords::record_list_len_of(&a), 6); - }) -} - -#[test] -fn test_withdraw_log_cache() { - with_externalities(&mut new_test_ext2(), || { - // issue - let a: u64 = 1; // accountid - let b: u64 = 2; // accountid - let btc_Token = b"x-btc".to_vec(); - let eth_Token = b"x-eth".to_vec(); - // let key_a_btc = (a, btc_Token.clone()); - // let key_a_eth = (a, eth_Token.clone()); - // let key_b_btc = (b, btc_Token.clone()); - // let key_b_eth = (b, eth_Token.clone()); - - FinancialRecords::deposit(&a, &btc_Token, 1000, Some(vec![])).unwrap(); - FinancialRecords::deposit(&b, &btc_Token, 1000, Some(vec![])).unwrap(); - FinancialRecords::deposit(&a, ð_Token, 1000, Some(vec![])).unwrap(); - FinancialRecords::deposit(&b, ð_Token, 1000, Some(vec![])).unwrap(); - - assert_eq!(FinancialRecords::record_list_len_of(&a), 2); - assert_eq!(FinancialRecords::record_list_len_of(&b), 2); - - // withdraw - FinancialRecords::withdrawal(&a, &btc_Token, 100, vec![], vec![]).unwrap(); - assert_eq!(FinancialRecords::record_list_len_of(&a), 3); - FinancialRecords::withdrawal(&b, &btc_Token, 100, vec![], vec![]).unwrap(); - assert_eq!(FinancialRecords::record_list_len_of(&a), 3); - - let log_a = FinancialRecords::withdraw_log_cache((a, 2)).unwrap(); - assert_eq!(log_a.prev(), None); - assert_eq!(log_a.next(), Some((2, 2))); - let log_b = FinancialRecords::withdraw_log_cache((b, 2)).unwrap(); - assert_eq!(log_b.prev(), Some((1, 2))); - assert_eq!(log_b.next(), None); - - FinancialRecords::withdrawal(&a, ð_Token, 100, vec![], vec![]).unwrap(); - FinancialRecords::withdrawal(&b, ð_Token, 100, vec![], vec![]).unwrap(); - - // btc cache - if let Some(btc_header) = FinancialRecords::log_cache_mheader(&btc_Token) { - let mut index = btc_header.index(); - let mut v = vec![]; - while let Some(node) = FinancialRecords::withdraw_log_cache(&index) { - v.push((node.data.accountid(), node.data.index())); - if let Some(next) = node.next() { - index = next; - } else { - break; - } - } - assert_eq!(v.as_slice(), [(a, 2), (b, 2)]); - } else { - panic!("unreachable!") - } - // eth cache - if let Some(eth_header) = FinancialRecords::log_cache_mheader(ð_Token) { - let mut index = eth_header.index(); - let mut v = vec![]; - while let Some(node) = FinancialRecords::withdraw_log_cache(&index) { - v.push((node.data.accountid(), node.data.index())); - if let Some(next) = node.next() { - index = next; - } else { - break; - } - } - assert_eq!(v.as_slice(), [(a, 3), (b, 3)]); - } else { - panic!("unreachable!") - } - - // withdraw finish - // loop linked node collection and find out - - // for example - // loop cache and withdraw for b finish - // btc relay withdraw finish - assert_ok!(FinancialRecords::withdrawal_finish( - &b, - &btc_Token, - Some(vec![]) - )); - - if let Some(btc_header) = FinancialRecords::log_cache_mheader(&btc_Token) { - let mut index = btc_header.index(); - let mut v = vec![]; - while let Some(node) = FinancialRecords::withdraw_log_cache(&index) { - v.push((node.data.accountid(), node.data.index())); - if let Some(next) = node.next() { - index = next; - } else { - break; - } - } - assert_eq!(v.as_slice(), [(a, 2)]); - } else { - panic!("unreachable!") - } - - let log = FinancialRecords::withdraw_log_cache((a, 2)).unwrap(); - assert_eq!(log.prev(), None); - assert_eq!(log.next(), None); - - // btc relay withdraw err - assert_ok!(FinancialRecords::withdrawal_finish(&a, &btc_Token, None)); - // all cache removed - assert_eq!(FinancialRecords::log_cache_mheader(&btc_Token) == None, true); - - // eth relay withdraw - assert_ok!(FinancialRecords::withdrawal_finish( - &a, - ð_Token, - Some(vec![]) - )); - if let Some(eth_header) = FinancialRecords::log_cache_mheader(ð_Token) { - let mut index = eth_header.index(); - let mut v = vec![]; - while let Some(node) = FinancialRecords::withdraw_log_cache(&index) { - v.push((node.data.accountid(), node.data.index())); - if let Some(next) = node.next() { - index = next; - } else { - break; - } - } - assert_eq!(v.as_slice(), [(b, 3)]); - } else { - panic!("unreachable!") - } - - assert_ok!(FinancialRecords::withdrawal_finish( - &b, - ð_Token, - Some(vec![]) - )); - assert_eq!(FinancialRecords::log_cache_mheader(&btc_Token) == None, true); }) }