Skip to content

Commit

Permalink
Added vesting related methods to asset_symbol_type and a simple test s…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiwonik committed Feb 13, 2018
1 parent e6b9c24 commit 30e4822
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
56 changes: 56 additions & 0 deletions libraries/protocol/asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions libraries/protocol/include/steem/protocol/asset_symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ); }
Expand Down
37 changes: 37 additions & 0 deletions tests/tests/smt_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 30e4822

Please sign in to comment.