From 46ac4d79a3eeac0a2253080cd64db054061ca16d Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Fri, 5 Aug 2022 11:29:21 +0300 Subject: [PATCH] withdraw transaction --- .../peerplays_sidechain/ethereum/encoders.cpp | 14 +- .../peerplays_sidechain/ethereum/encoders.hpp | 10 +- .../sidechain_net_handler_ethereum.hpp | 1 - .../sidechain_net_handler_ethereum.cpp | 137 +++++++----------- 4 files changed, 72 insertions(+), 90 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp b/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp index 2b8984f6..e17bbdfb 100644 --- a/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp +++ b/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp @@ -6,7 +6,7 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum { //! base_encoder -std::string base_encoder::encode_uint256(uint64_t value) +std::string base_encoder::encode_uint256(boost::multiprecision::uint256_t value) { return (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), value)).str(); } @@ -41,5 +41,17 @@ std::string update_owners_encoder::encode(const std::vector #include +#include namespace graphene { namespace peerplays_sidechain { namespace ethereum { class base_encoder { public: - static std::string encode_uint256(uint64_t value); + static std::string encode_uint256(boost::multiprecision::uint256_t value); static std::string encode_address(const std::string& value); static std::string encode_string(const std::string& value); }; @@ -19,6 +20,13 @@ public: std::string encode(const std::vector>& owners_weights, const std::string& object_id) const; }; +class withdrawal_encoder { +public: + const std::string function_signature = "e088747b"; //! withdraw(address,uint256,string) + + std::string encode(const std::string& to, boost::multiprecision::uint256_t amount, const std::string& object_id) const; +}; + /*class ethereum_function_call_encoder { public: enum operation_t { diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp index 8606c095..af9812d0 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp @@ -25,7 +25,6 @@ public: std::string send_transaction(const std::string& params); std::string get_transaction_receipt(const std::string& params); - //std::string withdraw(); }; class sidechain_net_handler_ethereum : public sidechain_net_handler { diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp index 1553ef50..c01ab57d 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp @@ -62,11 +62,6 @@ std::string ethereum_rpc_client::get_transaction_receipt(const std::string& para return send_post_request("eth_getTransactionReceipt", "[\"" + params + "\"]", debug_rpc_calls); } -/*std::string ethereum_rpc_client::withdraw() { - const std::string params = "[ { \"from\": \"0x5FbBb31BE52608D2F52247E8400B7fCaA9E0bC12\", \"to\": \"0x3E84f248Cd00A2FDaaDfa0dC5c3ff64D8767Fb01\", \"data\": \"0xe088747b00000000000000000000000009ee460834498a4ee361beb819470061b7381b490000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000006312e33362e330000000000000000000000000000000000000000000000000000\" } ]"; - return send_post_request("eth_sendTransaction", params, debug_rpc_calls); -}*/ - sidechain_net_handler_ethereum::sidechain_net_handler_ethereum(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map &options) : sidechain_net_handler(_plugin, options) { sidechain = sidechain_type::ethereum; @@ -275,81 +270,48 @@ bool sidechain_net_handler_ethereum::process_proposal(const proposal_object &po) const auto &idx = database.get_index_type().indices().get(); const auto swwo = idx.find(swwo_id); if (swwo != idx.end()) { + uint32_t swwo_block_num = swwo->block_num; + std::string swwo_peerplays_transaction_id = swwo->peerplays_transaction_id; + uint32_t swwo_op_idx = std::stoll(swwo->peerplays_uid.substr(swwo->peerplays_uid.find_last_of("-") + 1)); - //uint32_t swwo_block_num = swwo->block_num; - //std::string swwo_peerplays_transaction_id = swwo->peerplays_transaction_id; - //uint32_t swwo_op_idx = std::stoll(swwo->peerplays_uid.substr(swwo->peerplays_uid.find_last_of("-") + 1)); - // - //const auto &block = database.fetch_block_by_number(swwo_block_num); - // - //for (const auto &tx : block->transactions) { - // if (tx.id().str() == swwo_peerplays_transaction_id) { - // operation op = tx.operations[swwo_op_idx]; - // transfer_operation t_op = op.get(); - // - // price asset_price = database.get(t_op.amount.asset_id).options.core_exchange_rate; - // asset peerplays_asset = asset(t_op.amount.amount * asset_price.base.amount / asset_price.quote.amount); - // - // process_ok = (t_op.to == gpo.parameters.son_account()) && - // (swwo->peerplays_from == t_op.from) && - // (swwo->peerplays_asset == peerplays_asset); - // break; - // } - //} - // - //object_id_type object_id = op_obj_idx_1.get().object_id; - //std::string op_tx_str = op_obj_idx_1.get().transaction; - // - //const auto &st_idx = database.get_index_type().indices().get(); - //const auto st = st_idx.find(object_id); - //if (st == st_idx.end()) { - // - // std::string tx_str = ""; - // - // if (object_id.is()) { - // const auto &idx = database.get_index_type().indices().get(); - // const auto swwo = idx.find(object_id); - // if (swwo != idx.end()) { - // - // std::stringstream ss_trx(boost::algorithm::unhex(op_tx_str)); - // hive::signed_transaction op_trx; - // fc::raw::unpack(ss_trx, op_trx, 1000); - // - // uint64_t symbol = 0; - // if (swwo->withdraw_currency == "HBD") { - // symbol = hive::asset::hbd_symbol_ser; - // } - // if (swwo->withdraw_currency == "HIVE") { - // symbol = hive::asset::hive_symbol_ser; - // } - // - // hive::transfer_operation t_op; - // t_op.from = wallet_account_name; - // t_op.to = swwo->withdraw_address; - // t_op.amount.amount = swwo->withdraw_amount; - // t_op.amount.symbol = symbol; - // t_op.memo = ""; - // - // hive::signed_transaction htrx; - // htrx.ref_block_num = op_trx.ref_block_num; - // htrx.ref_block_prefix = op_trx.ref_block_prefix; - // htrx.set_expiration(op_trx.expiration); - // - // htrx.operations.push_back(t_op); - // - // std::stringstream ss; - // fc::raw::pack(ss, htrx, 1000); - // tx_str = boost::algorithm::hex(ss.str()); - // } - // } - // - // transaction_ok = (op_tx_str == tx_str); - //} + const auto &block = database.fetch_block_by_number(swwo_block_num); + + for (const auto &tx : block->transactions) { + if (tx.id().str() == swwo_peerplays_transaction_id) { + operation op = tx.operations[swwo_op_idx]; + transfer_operation t_op = op.get(); + + price asset_price = database.get(t_op.amount.asset_id).options.core_exchange_rate; + asset peerplays_asset = asset(t_op.amount.amount * asset_price.base.amount / asset_price.quote.amount); + + process_ok = (t_op.to == gpo.parameters.son_account()) && + (swwo->peerplays_from == t_op.from) && + (swwo->peerplays_asset == peerplays_asset); + break; + } + } + + object_id_type object_id = op_obj_idx_1.get().object_id; + std::string op_tx_str = op_obj_idx_1.get().transaction; + + const auto &st_idx = database.get_index_type().indices().get(); + const auto st = st_idx.find(object_id); + if (st == st_idx.end()) { + + std::string tx_str = ""; + + if (object_id.is()) { + const auto &idx = database.get_index_type().indices().get(); + const auto swwo = idx.find(object_id); + if (swwo != idx.end()) { + tx_str = create_withdrawal_transaction(*swwo); + } + } + + transaction_ok = (op_tx_str == tx_str); + } } - process_ok = true; - transaction_ok = true; - should_approve = process_ok && transaction_ok; break; @@ -588,18 +550,17 @@ bool sidechain_net_handler_ethereum::settle_sidechain_transaction(const sidechai return false; } - for(const auto &entry_receipt : json_receipt.get_child("result.logs")) { - if( boost::algorithm::to_lower_copy(wallet_contract_address) == entry_receipt.second.get("address") ) { - count += 1; - //! Fixme - compare data somehow? - //if( sto.transaction == entry_receipt.second.get("data") ) { - //} - } + if( "0x1" == json_receipt.get("result.status") ) + { + count += 1; + //! Fixme - compare data somehow? + //if( sto.transaction == entry_receipt.second.get("data") ) { + //} } } //! Check that we have all transactions - if(count != json.count("result_array")) { + if(count != json.get_child("result_array").size()) { wlog("Not all receipts received for transaction ${id}", ("id", sto.id)); return false; } @@ -617,7 +578,6 @@ std::string sidechain_net_handler_ethereum::create_primary_wallet_transaction(co owners_weights.emplace_back(std::make_pair(pub_key_str, son.weight)); } - //! Create data of transaction ethereum::transaction transaction; ethereum::update_owners_encoder encoder; transaction.data = encoder.encode(owners_weights, object_id); @@ -629,7 +589,10 @@ std::string sidechain_net_handler_ethereum::create_deposit_transaction(const son } std::string sidechain_net_handler_ethereum::create_withdrawal_transaction(const son_wallet_withdraw_object &swwo) { - return "Withdrawal-Transaction"; + ethereum::transaction transaction; + ethereum::withdrawal_encoder encoder; + transaction.data = encoder.encode(swwo.withdraw_address.substr(2), swwo.withdraw_amount.value*10000000000, swwo.id.operator std::string()); + return transaction.serialize(); } std::string sidechain_net_handler_ethereum::sign_transaction(const sidechain_transaction_object &sto) {