Skip to content

Commit 4c3973a

Browse files
Mariusz-Trelavogel76
authored andcommitted
Move estimate_hive_collateral function into protocol library.
1 parent 852e66d commit 4c3973a

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <hive/protocol/asset.hpp>
4+
#include <hive/protocol/config.hpp>
5+
6+
namespace hive { namespace protocol {
7+
struct hive_collateral
8+
{
9+
static asset estimate_hive_collateral( const price& current_median_history, const price& current_min_history, const asset& hbd_amount_to_get )
10+
{
11+
//must reflect calculations from collateralized_convert_evaluator::do_apply
12+
13+
FC_ASSERT( !static_cast<price>( current_median_history ).is_null(), "Cannot estimate conversion collateral because there is no price feed." );
14+
15+
auto needed_hive = multiply_with_fee( hbd_amount_to_get, current_min_history,
16+
HIVE_COLLATERALIZED_CONVERSION_FEE, HIVE_SYMBOL );
17+
uint128_t _amount = ( uint128_t( needed_hive.amount.value ) * HIVE_CONVERSION_COLLATERAL_RATIO ) / HIVE_100_PERCENT;
18+
asset required_collateral = asset( fc::uint128_to_uint64(_amount), HIVE_SYMBOL );
19+
20+
return required_collateral;
21+
}
22+
};
23+
24+
} } // hive::protocol
25+

libraries/wallet/wallet.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <hive/protocol/crypto_memo.hpp>
1010
#include <hive/protocol/transaction_util.hpp>
1111
#include <hive/protocol/version.hpp>
12+
#include <hive/protocol/hive_collateral.hpp>
1213
#include <hive/wallet/wallet.hpp>
1314
#include <hive/wallet/api_documentation.hpp>
1415
#include <hive/wallet/reflect_util.hpp>
@@ -2163,14 +2164,11 @@ wallet_serializer_wrapper<hive::protocol::asset> wallet_api::estimate_hive_colla
21632164
//must reflect calculations from collateralized_convert_evaluator::do_apply
21642165

21652166
auto fhistory = get_feed_history().value;
2166-
FC_ASSERT( !static_cast<price>( fhistory.current_median_history ).is_null(), "Cannot estimate conversion collateral because there is no price feed." );
21672167

2168-
auto needed_hive = multiply_with_fee( hbd_amount_to_get.value, fhistory.current_min_history,
2169-
HIVE_COLLATERALIZED_CONVERSION_FEE, HIVE_SYMBOL );
2170-
uint128_t _amount = ( uint128_t( needed_hive.amount.value ) * HIVE_CONVERSION_COLLATERAL_RATIO ) / HIVE_100_PERCENT;
2171-
asset required_collateral = asset( fc::uint128_to_uint64(_amount), HIVE_SYMBOL );
2172-
2173-
return { required_collateral };
2168+
return { hive::protocol::hive_collateral::estimate_hive_collateral(
2169+
fhistory.current_median_history,
2170+
fhistory.current_min_history,
2171+
hbd_amount_to_get.value ) };
21742172
}
21752173

21762174
wallet_signed_transaction wallet_api::publish_feed(

0 commit comments

Comments
 (0)