From 3585739fe6a5b14def0364525da5252e44066e62 Mon Sep 17 00:00:00 2001 From: "Vladimir N. Korotenko" Date: Sat, 15 Nov 2025 17:07:48 +0300 Subject: [PATCH] Complite action added --- dm-cli/Deals.cpp | 87 +++++++++++++++++++++++++++++++++++++--------- dm-cli/Deals.hpp | 1 + dm-cli/Replace.cpp | 12 ++++++- dm-cli/common.hpp | 4 +++ 4 files changed, 86 insertions(+), 18 deletions(-) diff --git a/dm-cli/Deals.cpp b/dm-cli/Deals.cpp index 53e9487..db00159 100644 --- a/dm-cli/Deals.cpp +++ b/dm-cli/Deals.cpp @@ -13,7 +13,7 @@ #include using namespace std; - +using json = nlohmann::json; int Deals::HelpCreate() const { if (isRussian) @@ -201,11 +201,11 @@ string Deals::GetDate(const string date) { } std::time_t utcTimeInSeconds = std::time(nullptr); utcTimeInSeconds += append; - - - tm * tmm = gmtime(&utcTimeInSeconds); + + + tm* tmm = gmtime(&utcTimeInSeconds); char buff[70]; - strftime(buff, sizeof buff, "%Y-%m-%d %H:%M:%S UTC", tmm); + strftime(buff, sizeof buff, "%Y-%m-%d %H:%M:%S UTC", tmm); string result = buff; return result; } @@ -214,22 +214,19 @@ string Deals::GetDate(const string date) { } } -int Deals::Status() -{ - +string Deals::CommonStatus() { string payaddress = parser.getLast(); - /* - curl -X POST "https://testnet-dm2.bitdeals.org/api/v1/deal/status" \ - -H "Content-Type: application/json" \ - -d '{"address":"3e251d8fdcfa80adf5a3050064fc4ecb01d304d1"}' - */ string data("curl -X POST \""); data.append(address); data.append("/api/v1/deal/status\" -H \"Content-Type: application/json\" "); - data.append("-d '{\"address\":\""); + data.append("-d \"{\\\"address\\\":\\\""); data.append(payaddress); - data.append("\"}'"); - + data.append("\\\"}\""); + return data; +} +int Deals::Status() +{ + string data = CommonStatus(); string result = ExecCommand(data.c_str()); if (result.length() == 0) return NoResponse(""); @@ -258,7 +255,63 @@ int Deals::Status() } return 0; } -int Deals::Complite() { return 0; } +int Deals::Complite() { + + string data = CommonStatus(); + string result = ExecCommand(data.c_str()); + if (result.length() == 0) + return NoResponse(""); + nlohmann::json jsonData = nlohmann::json::parse(result); + nlohmann::json deal = jsonData["deal"]; + + string positive = "Positive"; + int refund = 0; + if (parser.cmdOptionExists("-p") || parser.cmdOptionExists("--positive")) + { + positive = GetVal("-p", "--positive"); + } + if (parser.cmdOptionExists("--refund")) + { + refund = stoi(parser.getCmdOption("--refund")); + } + if (parser.cmdOptionExists("-r")) + { + refund = stoi(parser.getCmdOption("-r")); + } + + deal["feedback"]["status"] = positive; + if (refund > 0) + deal["feedback"]["refund"] = refund; + + string jsf = deal.dump(); + jsf = ReplaceAll(jsf, "\"", "\\\""); + data = "curl -X POST \""; + data.append(address); + data.append("/api/v1/deal/complete\" -H \"Content-Type: application/json\" "); + data.append("-d \""); + data.append(jsf); + data.append("\""); + + result = ExecCommand(data.c_str()); + if (result.length() == 0) + return NoResponse(""); + + + + + std::ostringstream stream; + stream << "success: " << jsonData["result"]["success"] << endl; + stream << "message: " << jsonData["result"]["message"] << endl; + + + + string decoded = stream.str(); + std::cout << decoded << endl; + + if (isDebug) + ShowDebug(data, decoded, 0, result); + return 0; +} int Deals::Cancel() { return 0; } int Deals::Negative() { return 0; } diff --git a/dm-cli/Deals.hpp b/dm-cli/Deals.hpp index ffe5c99..f505a82 100644 --- a/dm-cli/Deals.hpp +++ b/dm-cli/Deals.hpp @@ -25,6 +25,7 @@ private: int Update(); int Create(); int Status(); + std::string CommonStatus(); int Complite(); int Cancel(); int Negative(); diff --git a/dm-cli/Replace.cpp b/dm-cli/Replace.cpp index 8128ff6..35c7276 100644 --- a/dm-cli/Replace.cpp +++ b/dm-cli/Replace.cpp @@ -1,14 +1,24 @@ #include #include "common.hpp" #include +#include + std::string Replace(std::string source, std::string from, std::string out) { std::string res(""); size_t start = 0, stop = 0; start = source.find(from); stop = from.length(); - if (start >= 0 ) res = source.replace(start, stop, out); + if (start >= 0) res = source.replace(start, stop, out); return res; } +std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) { + size_t start_pos = 0; + while ((start_pos = str.find(from, start_pos)) != std::string::npos) { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); // Handles case where 'to' is a substring of 'from' + } + return str; +} int NoResponse(std::string mgs) { diff --git a/dm-cli/common.hpp b/dm-cli/common.hpp index 5e45a6b..c4787bb 100644 --- a/dm-cli/common.hpp +++ b/dm-cli/common.hpp @@ -54,6 +54,10 @@ void ShowDebug(std::string url, std::string result, bool success, std::string de std::string ReadFile(const char *filename); /// @breef Replace in string std::string Replace(std::string source, std::string from, std::string out); + +/// @breef Replace all entryes in string +std::string ReplaceAll(std::string str, const std::string& from, const std::string& to); + /// @breef Вывод сообщения о таймауте или пустой строке ответа int NoResponse(std::string mgs);