Skip to content

Commit

Permalink
#313 sidechain_deposit_transaction - add all params to function manually
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Dobromyslov committed Mar 8, 2022
1 parent d19520c commit f7bee47
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 38 deletions.
22 changes: 13 additions & 9 deletions libraries/wallet/include/graphene/wallet/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1789,23 +1789,27 @@ class wallet_api

/** Broadcast signed transaction for manually sidechain deposit
* @param son_name_or_id ID or name of the son account
* @param account_from_name_or_id ID or name of the account transaction from
* @param account_to_name_or_id ID or name of the account transaction to
* @param sidechain Sidechain type (bitcoin, HIVE, etc)
* @param transaction_id ID of transaction
* @param operation_index Index of operation
* @param asset_name_or_id the symbol or id of the asset in question
* @param amount The amount to deposit.
* @param sidechain_from Sidechain address transaction from
* @param sidechain_to Sidechain address transaction to
* @param sidechain_currency Sidechain currency
* @param sidechain_amount Sidechain amount to deposit
* @param peerplays_from_name_or_id ID or name of the account transaction from
* @param peerplays_to_name_or_id ID or name of the account transaction to
* @returns the signed transaction.
*/
signed_transaction sidechain_deposit_transaction( const string &son_name_or_id,
const string &account_from_name_or_id,
const string &account_to_name_or_id,
signed_transaction sidechain_deposit_transaction( const string &son_name_or_id,
const sidechain_type& sidechain,
const string &transaction_id,
uint32_t operation_index,
const string &asset_name_or_id,
const string &amount);
const string &sidechain_from,
const string &sidechain_to,
const string &sidechain_currency,
int64_t sidechain_amount,
const string &peerplays_from_name_or_id,
const string &peerplays_to_name_or_id);

/** Vote for a given witness.
*
Expand Down
86 changes: 57 additions & 29 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2831,20 +2831,20 @@ class wallet_api_impl
} FC_CAPTURE_AND_RETHROW( (voting_account)(sons_to_approve)(sons_to_reject)(desired_number_of_sons)(broadcast) ) }

signed_transaction sidechain_deposit_transaction( const string &son_name_or_id,
const string &account_from_name_or_id,
const string &account_to_name_or_id,
const sidechain_type& sidechain,
const string &transaction_id,
uint32_t operation_index,
const string &asset_name_or_id,
const string &amount)
const string &sidechain_from,
const string &sidechain_to,
const string &sidechain_currency,
int64_t sidechain_amount,
const string &peerplays_from_name_or_id,
const string &peerplays_to_name_or_id )
{
//! Get data we need to procced transaction
const auto dynamic_props = get_dynamic_global_properties();
const auto dynamic_global_props = get_dynamic_global_properties();
const auto global_props = get_global_properties();
const auto son_obj = get_son(son_name_or_id);
FC_ASSERT(son_obj.status == son_status::active, "Son account is not active, current status: ${status}", ("status", son_obj.status));
const auto account_obj_from = get_account(account_from_name_or_id);
const auto account_obj_to = get_account(account_to_name_or_id);
const std::string sidechain_str = [&sidechain](){
switch (sidechain) {
case sidechain_type::peerplays : return "peerplays";
Expand All @@ -2854,28 +2854,52 @@ class wallet_api_impl
FC_THROW("Wrong sidechain type: ${sidechain}", ("sidechain", sidechain));
}
}();
fc::optional<asset_object> asset_obj = get_asset(asset_name_or_id);
FC_ASSERT(asset_obj, "Could not find asset matching ${asset}", ("asset", asset_name_or_id));
const auto asset_val = asset_obj->amount_from_string(amount);
const auto asset_price = asset_obj->options.core_exchange_rate;
const auto peerplays_from_obj = get_account(peerplays_from_name_or_id);
const auto peerplays_to_obj = get_account(peerplays_to_name_or_id);
const price sidechain_currency_price = [this, &sidechain_currency, &global_props](){
if(sidechain_currency == "BTC")
{
fc::optional<asset_object> asset_obj = get_asset(object_id_to_string(global_props.parameters.btc_asset()));
FC_ASSERT(asset_obj, "Could not find asset matching ${asset}", ("asset", "BTC"));
return asset_obj->options.core_exchange_rate;
}
else if(sidechain_currency == "HBD")
{
fc::optional<asset_object> asset_obj = get_asset(object_id_to_string(global_props.parameters.hbd_asset()));
FC_ASSERT(asset_obj, "Could not find asset matching ${asset}", ("asset", "HBD"));
return asset_obj->options.core_exchange_rate;
}
else if(sidechain_currency == "HIVE")
{
fc::optional<asset_object> asset_obj = get_asset(object_id_to_string(global_props.parameters.hive_asset()));
FC_ASSERT(asset_obj, "Could not find asset matching ${asset}", ("asset", "HIVE"));
return asset_obj->options.core_exchange_rate;
}
else
{
fc::optional<asset_object> asset_obj = get_asset(sidechain_currency);
FC_ASSERT(asset_obj, "Could not find asset matching ${asset}", ("asset", sidechain_currency));
return asset_obj->options.core_exchange_rate;
}
}();

//! Create transaction
signed_transaction son_wallet_deposit_create_transaction;
son_wallet_deposit_create_operation op;
op.payer = son_obj.son_account;
op.son_id = son_obj.id;
op.timestamp = dynamic_props.time;
op.block_num = dynamic_props.head_block_number;
op.timestamp = dynamic_global_props.time;
op.block_num = dynamic_global_props.head_block_number;
op.sidechain = sidechain;
op.sidechain_uid = sidechain_str + "-" + transaction_id + "-" + std::to_string(operation_index);
op.sidechain_transaction_id = transaction_id;
op.sidechain_from = object_id_to_string(account_obj_from.id);
op.sidechain_to = object_id_to_string(account_obj_to.id);
op.sidechain_currency = object_id_to_string(asset_obj->id);
op.sidechain_amount = asset_val.amount;
op.peerplays_from = account_obj_from.id;
op.peerplays_to = account_obj_to.id;
op.peerplays_asset = asset(asset_val.amount * asset_price.base.amount / asset_price.quote.amount);
op.sidechain_from = sidechain_from;
op.sidechain_to = sidechain_to;
op.sidechain_currency = sidechain_currency;
op.sidechain_amount = sidechain_amount;
op.peerplays_from = peerplays_from_obj.id;
op.peerplays_to = peerplays_to_obj.id;
op.peerplays_asset = asset(op.sidechain_amount * sidechain_currency_price.base.amount / sidechain_currency_price.quote.amount);
son_wallet_deposit_create_transaction.operations.push_back(op);

return sign_transaction(son_wallet_deposit_create_transaction, true);
Expand Down Expand Up @@ -5393,22 +5417,26 @@ signed_transaction wallet_api::update_son_votes(string voting_account,
}

signed_transaction wallet_api::sidechain_deposit_transaction( const string &son_name_or_id,
const string &account_from_name_or_id,
const string &account_to_name_or_id,
const sidechain_type& sidechain,
const string &transaction_id,
uint32_t operation_index,
const string &asset_name_or_id,
const string &amount)
const string &sidechain_from,
const string &sidechain_to,
const string &sidechain_currency,
int64_t sidechain_amount,
const string &peerplays_from_name_or_id,
const string &peerplays_to_name_or_id)
{
return my->sidechain_deposit_transaction(son_name_or_id,
account_from_name_or_id,
account_to_name_or_id,
sidechain,
transaction_id,
operation_index,
asset_name_or_id,
amount);
sidechain_from,
sidechain_to,
sidechain_currency,
sidechain_amount,
peerplays_from_name_or_id,
peerplays_to_name_or_id);
}

signed_transaction wallet_api::vote_for_witness(string voting_account,
Expand Down

0 comments on commit f7bee47

Please sign in to comment.