From 52209dbbebd76745932448c9213464d53df41e26 Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Thu, 4 Jan 2024 06:07:38 +0000 Subject: [PATCH] #573 - add vote_recharge_gpos function --- libraries/app/database_api.cpp | 8 ++--- .../wallet/include/graphene/wallet/wallet.hpp | 14 +++++++++ libraries/wallet/wallet.cpp | 31 +++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index f3ec33f6..5ca0869b 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -2197,7 +2197,7 @@ vector database_api_impl::lookup_vote_ids(const vector &v case vote_id_type::committee: { auto itr = committee_idx.find(id); if (itr != committee_idx.end()) - result.emplace_back( variant( *itr, 2 ) ); // Depth of committee_member_object is 1, add 1 to be safe + result.emplace_back(variant(*itr, 2)); // Depth of committee_member_object is 1, add 1 to be safe else result.emplace_back(variant()); break; @@ -2205,7 +2205,7 @@ vector database_api_impl::lookup_vote_ids(const vector &v case vote_id_type::witness: { auto itr = witness_idx.find(id); if (itr != witness_idx.end()) - result.emplace_back( variant( *itr, 2 ) ); // Depth of witness_object is 1, add 1 here to be safe + result.emplace_back(variant(*itr, 2)); // Depth of witness_object is 1, add 1 here to be safe else result.emplace_back(variant()); break; @@ -2213,13 +2213,13 @@ vector database_api_impl::lookup_vote_ids(const vector &v case vote_id_type::worker: { auto itr = for_worker_idx.find(id); if (itr != for_worker_idx.end()) { - result.emplace_back( variant( *itr, 4 ) ); // Depth of worker_object is 3, add 1 here to be safe. + result.emplace_back(variant(*itr, 4)); // Depth of worker_object is 3, add 1 here to be safe. // If we want to extract the balance object inside, // need to increase this value } else { auto itr = against_worker_idx.find(id); if (itr != against_worker_idx.end()) { - result.emplace_back( variant( *itr, 4 ) ); // Depth of worker_object is 3, add 1 here to be safe. + result.emplace_back(variant(*itr, 4)); // Depth of worker_object is 3, add 1 here to be safe. // If we want to extract the balance object inside, // need to increase this value } else { diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index 7a53e5e0..b3c10dde 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -1990,6 +1990,7 @@ class wallet_api std::vector witnesses_to_reject, uint16_t desired_number_of_witnesses, bool broadcast = false); + /** Set the voting proxy for an account. * * If a user does not wish to take an active part in voting, they can choose @@ -2037,6 +2038,18 @@ class wallet_api uint16_t desired_number_of_committee_members, bool broadcast = false); + /** Vote recharge gpos. + * + * Explicit voting recharge function that is used solely when the user wishes to recharge their GPOS voting power + * without changing their voting slate + * + * @param voting_account the name or id of the account who is voting with their shares + * @param broadcast true if you wish to broadcast the transaction + * @return the signed transaction changing your vote for the given SON + */ + signed_transaction vote_recharge_gpos(string voting_account, + bool broadcast = false); + /** Signs a transaction. * * Given a fully-formed transaction that is only lacking signatures, this signs @@ -2896,6 +2909,7 @@ FC_API( graphene::wallet::wallet_api, (update_witness_votes) (set_voting_proxy) (set_desired_witness_and_committee_member_count) + (vote_recharge_gpos) (get_account) (get_account_id) (get_block) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 82dfa404..ca043925 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -3101,6 +3101,31 @@ public: return sign_transaction( tx, broadcast ); } FC_CAPTURE_AND_RETHROW( (account_to_modify)(desired_number_of_witnesses)(desired_number_of_committee_members)(broadcast) ) } + signed_transaction vote_recharge_gpos(string voting_account, + bool broadcast /* = false */) + { try { + + const std::vector vbo_info = get_vesting_balances(voting_account); + std::vector::const_iterator vbo_iter; + vbo_iter = std::find_if(vbo_info.begin(), vbo_info.end(), [](vesting_balance_object_with_info const& obj){return obj.balance_type == vesting_balance_type::gpos;}); + if( vbo_info.size() == 0 || vbo_iter == vbo_info.end()) + FC_THROW("Account ${account} has no core Token ${TOKEN} vested and will not be allowed to recharge the votes", ("account", voting_account)("TOKEN", GRAPHENE_SYMBOL)); + + const account_object voting_account_object = get_account(voting_account); + + account_update_operation account_update_op; + account_update_op.account = voting_account_object.id; + account_update_op.new_options = voting_account_object.options; + account_update_op.extensions.value.update_last_voting_time = true; //Allow user to vote in each sub-period (Update voting time, which is reference in calculating VF) + + signed_transaction tx; + tx.operations.push_back( account_update_op ); + set_operation_fees( tx, _remote_db->get_global_properties().parameters.current_fees); + tx.validate(); + + return sign_transaction( tx, broadcast ); + } FC_CAPTURE_AND_RETHROW( (voting_account)(broadcast) ) } + signed_transaction sign_transaction(signed_transaction tx, bool broadcast = false) { set pks = _remote_db->get_potential_signatures(tx); @@ -5610,6 +5635,12 @@ signed_transaction wallet_api::set_desired_witness_and_committee_member_count(st desired_number_of_committee_members, broadcast); } +signed_transaction wallet_api::vote_recharge_gpos(string voting_account, + bool broadcast /* = false */) +{ + return my->vote_recharge_gpos(voting_account, broadcast); +} + void wallet_api::set_wallet_filename(string wallet_filename) { my->_wallet_filename = wallet_filename;