diff --git a/src/SimpleWallet/SimpleWallet.cpp b/src/SimpleWallet/SimpleWallet.cpp index 35fd91a9c4..9310c1224a 100644 --- a/src/SimpleWallet/SimpleWallet.cpp +++ b/src/SimpleWallet/SimpleWallet.cpp @@ -1,4 +1,5 @@ // Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers, The Karbovanets developers +// Copyright (c) 2014-2016 XDN developers // // This file is part of Bytecoin. // @@ -223,6 +224,13 @@ struct TransferCommand { destination.amount = de.amount; dsts.push_back(destination); + + if (!remote_fee_address.empty()) { + destination.address = remote_fee_address; + destination.amount = de.amount * 0.25 / 100; + dsts.push_back(destination); + } + } } @@ -441,6 +449,26 @@ bool writeAddressFile(const std::string& addressFilename, const std::string& add return true; } +bool processServerFeeAddressResponse(const std::string& response, std::string& fee_address) { + try { + std::stringstream stream(response); + JsonValue json; + stream >> json; + + auto rootIt = json.getObject().find("fee_address"); + if (rootIt == json.getObject().end()) { + return false; + } + + fee_address = rootIt->second.getString(); + } + catch (std::exception&) { + return false; + } + + return true; +} + } std::string simple_wallet::get_commands_str() { @@ -580,7 +608,11 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) { fail_msg_writer() << "failed to parse daemon address: " << m_daemon_address; return false; } + remote_fee_address = getFeeAddress(); } else { + if (!m_daemon_host.empty()) { + remote_fee_address = getFeeAddress(); + } m_daemon_address = std::string("http://") + m_daemon_host + ":" + std::to_string(m_daemon_port); } @@ -990,6 +1022,28 @@ bool simple_wallet::show_blockchain_height(const std::vector& args) return true; } //---------------------------------------------------------------------------------------------------- +std::string simple_wallet::getFeeAddress() { + + HttpClient httpClient(m_dispatcher, m_daemon_host, m_daemon_port); + + HttpRequest req; + HttpResponse res; + + req.setUrl("/feeaddress"); + httpClient.request(req, res); + + if (res.getStatus() != HttpResponse::STATUS_200) { + throw std::runtime_error("Remote server returned code " + std::to_string(res.getStatus())); + } + + std::string address; + if (!processServerFeeAddressResponse(res.getBody(), address)) { + throw std::runtime_error("Failed to parse server response"); + } + + return address; +} +//---------------------------------------------------------------------------------------------------- bool simple_wallet::transfer(const std::vector &args) { try { TransferCommand cmd(m_currency); diff --git a/src/SimpleWallet/SimpleWallet.h b/src/SimpleWallet/SimpleWallet.h index 6ae8f0edb8..5ea85105c7 100755 --- a/src/SimpleWallet/SimpleWallet.h +++ b/src/SimpleWallet/SimpleWallet.h @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers +// Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers, The Karbovanets developers // // This file is part of Bytecoin. // @@ -39,6 +39,8 @@ #include #include +std::string remote_fee_address; + namespace CryptoNote { /************************************************************************/ @@ -55,6 +57,7 @@ namespace CryptoNote bool process_command(const std::vector &args); std::string get_commands_str(); + std::string getFeeAddress(); const CryptoNote::Currency& currency() const { return m_currency; }