diff --git a/libraries/protocol/asset.cpp b/libraries/protocol/asset.cpp index e4db332849..1372117f78 100644 --- a/libraries/protocol/asset.cpp +++ b/libraries/protocol/asset.cpp @@ -217,6 +217,62 @@ uint32_t asset_symbol_type::to_nai()const return nai_data_digits * 10 + nai_check_digit; } +bool asset_symbol_type::is_vesting() const +{ + switch( space() ) + { + case legacy_space: + { + switch( asset_num ) + { + case STEEM_ASSET_NUM_STEEM: + return false; + case STEEM_ASSET_NUM_SBD: + // SBD is certainly liquid. + return false; + case STEEM_ASSET_NUM_VESTS: + return true; + default: + FC_ASSERT( false, "Unknown asset symbol" ); + } + } + case smt_nai_space: + // 5th bit of asset_num is used as vesting/liquid variant indicator. + return asset_num & 0x00000010; + default: + FC_ASSERT( false, "Unknown asset symbol" ); + } +} + +asset_symbol_type asset_symbol_type::get_paired_symbol() const +{ + switch( space() ) + { + case legacy_space: + { + switch( asset_num ) + { + case STEEM_ASSET_NUM_STEEM: + return from_asset_num( STEEM_ASSET_NUM_VESTS ); + case STEEM_ASSET_NUM_SBD: + return *this; + case STEEM_ASSET_NUM_VESTS: + return from_asset_num( STEEM_ASSET_NUM_STEEM ); + default: + FC_ASSERT( false, "Unknown asset symbol" ); + } + } + case smt_nai_space: + { + // Toggle 5th bit of this asset_num. + auto paired_asset_num = asset_num ^ ( 0x1 << 5 ); + return from_asset_num( paired_asset_num ); + } + default: + FC_ASSERT( false, "Unknown asset symbol" ); + } +} + asset_symbol_type::asset_symbol_space asset_symbol_type::space()const { asset_symbol_type::asset_symbol_space s = legacy_space; diff --git a/libraries/protocol/include/steem/protocol/asset_symbol.hpp b/libraries/protocol/include/steem/protocol/asset_symbol.hpp index d48c1e4d65..792b13f9f5 100644 --- a/libraries/protocol/include/steem/protocol/asset_symbol.hpp +++ b/libraries/protocol/include/steem/protocol/asset_symbol.hpp @@ -81,6 +81,16 @@ class asset_symbol_type uint32_t to_nai()const; + /**Returns true when symbol represents vesting variant of the token, + * false for liquid one. + */ + bool is_vesting() const; + /**Returns vesting symbol when called from liquid one + * and liquid symbol when called from vesting one. + * Returns back the SBD symbol if represents SBD. + */ + asset_symbol_type get_paired_symbol() const; + asset_symbol_space space()const; uint8_t decimals()const { return uint8_t( asset_num & 0x0F ); } diff --git a/tests/tests/smt_tests.cpp b/tests/tests/smt_tests.cpp index 76a7790af6..2daff31ae7 100644 --- a/tests/tests/smt_tests.cpp +++ b/tests/tests/smt_tests.cpp @@ -755,5 +755,42 @@ BOOST_AUTO_TEST_CASE( comment_votable_assers_validate ) FC_LOG_AND_RETHROW() } +BOOST_AUTO_TEST_CASE( asset_symbol_vesting_methods ) +{ + try + { + BOOST_TEST_MESSAGE( "Test Comment Votable Assets Validate" ); + + asset_symbol_type Steem = STEEM_SYMBOL; + FC_ASSERT( Steem.is_vesting() == false ); + FC_ASSERT( Steem.get_paired_symbol() == VESTS_SYMBOL ); + + asset_symbol_type Vests = VESTS_SYMBOL; + FC_ASSERT( Vests.is_vesting() ); + FC_ASSERT( Vests.get_paired_symbol() == STEEM_SYMBOL ); + + asset_symbol_type Sbd = SBD_SYMBOL; + FC_ASSERT( Sbd.is_vesting() == false ); + FC_ASSERT( Sbd.get_paired_symbol() == SBD_SYMBOL ); + + ACTORS( (alice) ) + generate_block(); + auto smts = create_smt_3("alice", alice_private_key); + { + for( const asset_symbol_type& liquid_smt : smts ) + { +// Assertion blocked until SMT NAIs are correctly generated. +// FC_ASSERT( liquid_smt.is_vesting() == false ); + auto vesting_smt = liquid_smt.get_paired_symbol(); + FC_ASSERT( vesting_smt != liquid_smt ); +// Assertion blocked until SMT NAIs are correctly generated. +// FC_ASSERT( vesting_smt.is_vesting() ); + FC_ASSERT( vesting_smt.get_paired_symbol() == liquid_smt ); + } + } + } + FC_LOG_AND_RETHROW() +} + BOOST_AUTO_TEST_SUITE_END() #endif