From d62526a7184719772628e83feab94f506f33dce3 Mon Sep 17 00:00:00 2001 From: "Vladimir N. Korotenko" Date: Tue, 11 Nov 2025 11:21:30 +0300 Subject: [PATCH] Deals create --- dm-cli/Deals.cpp | 130 +++++++++++++++++++++++++++++++++++++ dm-cli/Deals.hpp | 2 +- dm-cli/Replace.cpp | 5 +- dm-cli/common.hpp | 5 ++ dm-cli/createdistr.sh | 2 +- dm-cli/tpl/deal_create.txt | 3 + vc/dm-cli.vcxproj | 1 + vc/dm-cli.vcxproj.filters | 3 + 8 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 dm-cli/tpl/deal_create.txt diff --git a/dm-cli/Deals.cpp b/dm-cli/Deals.cpp index 25e8eac..ca82f97 100644 --- a/dm-cli/Deals.cpp +++ b/dm-cli/Deals.cpp @@ -93,7 +93,122 @@ int Deals::Update() { return Negative(); return 1; } + int Deals::Create() { + /* + Использование: dm-cli deal create [параметры] + + Создать новую сделку + + [smhd] означает секунды, минуты, часы, дни. + Например: --leave-before 2020-01-01, or --leave-before 10d. + + -a|--at Сайт проведения сделки + -s|--seller Продавец в сделке + -c|--customer Покупатель в сделке + -t|--type [prepayment|postpayment] + Тип сделки + -s|--sum Сумма сделки в BTC + -l|--leave-before { <гггг-мм-дд> [чч:мм:сс UTC] | <время>[smhd] } + Время окончания сделки (длительность сделки), по умолчанию: 14d + -p|--pay { <гггг-мм-дд> [чч:мм:сс UTC] | <время>[smhd] } + Время для оплаты сделки; по умолчанию: 1d + + */ + + + string at; + string seller; + string customer; + string type; + string sum; + string leave_before; + string pay; + + + + at = GetVal("-a", "--at"); + seller = GetVal("-s", "--seller"); + customer = GetVal("-c", "--customer"); + type = GetVal("-t", "--type"); + sum = GetVal("-g", "--sum"); + leave_before = GetVal("-l", "--leave_before"); + pay = GetVal("-p", "--pay"); + + // start curl + string data(""); + data += ReadFile(DEAL_CREATE_TPL); + + + data = Replace(data, string("{{ADDRESS}}"), address); + data = Replace(data, string("{{AT}}"), at); + data = Replace(data, string("{{TYPE}}"), type); + data = Replace(data, string("{{SELLER}}"), seller); + data = Replace(data, string("{{CUSTOMER}}"), customer); + data = Replace(data, string("{{SUM}}"), sum); + + + if (pay.length() == 0) { + //{{UNTIL}} + data = Replace(data, string("{{UNTIL}}"), ""); + } + else { + string untl(",\"until\": \""); + untl.append(pay); + untl.append("\""); + data = Replace(data, string("{{UNTIL}}"), untl); + + } + + if (leave_before.length() == 0) { + //{{FEETBACK}} + data = Replace(data, string("{{FEETBACK}}"), ""); + } + else { + + string untl(",\"feedback\": {\"leave-before\": \""); + untl.append(leave_before); + untl.append("\"}"); + + data = Replace(data, string("{{FEETBACK}}"), untl); + } + + /* + "payment": { + "address": "", + "until": "", + "sum": "" + }, + "feedback": { + "leave-before": "", + "status": "", + "comments": "" + } + + */ + /* + curl -X POST "{{ADDRESS}}/api/v1/deal/create" \ + -H "Content-Type: application/json" \ + -d '{"at":"{{AT}}","type":"{{TYPE}}","seller":{"address": "{{SELLER}}"},"customer":{"address":"{{CUSTOMER}}"},"payment":{"sum":"{{SUM}}" {{UNTIL}} } {{FEETBACK}} }' + */ + + + + string result = ExecCommand(data.c_str()); + nlohmann::json jsonData = nlohmann::json::parse(result); + + std::ostringstream stream; + stream << "success: " << jsonData["result"]["success"] << endl; + stream << "message: " << jsonData["result"]["message"] << endl; + string decoded = stream.str(); + cout << decoded << endl; + string payload = cleanup_html(base64_decode(jsonData["payload"])); + if (isDebug) + ShowDebug(data, decoded, 1, payload); + + return 0; + + return 0; } @@ -102,3 +217,18 @@ int Deals::Status() { return 0; } int Deals::Complite() { return 0; } int Deals::Cancel() { return 0; } int Deals::Negative() { return 0; } + +string Deals::GetVal(const string first, const string second) +{ + string aval; + string res(""); + if (parser.cmdOptionExists(first)) { + aval = parser.getCmdOption(first); + if (aval.length() > 0) res = aval; + } + if (parser.cmdOptionExists(second)) { + aval = parser.getCmdOption(second); + if (aval.length() > 0) res = aval; + } + return res; +} diff --git a/dm-cli/Deals.hpp b/dm-cli/Deals.hpp index a51cadb..87fbcea 100644 --- a/dm-cli/Deals.hpp +++ b/dm-cli/Deals.hpp @@ -29,7 +29,7 @@ private: int Cancel(); int Negative(); - + std::string GetVal(const std::string first,const std::string second); }; diff --git a/dm-cli/Replace.cpp b/dm-cli/Replace.cpp index 3a8fb84..3b92fe4 100644 --- a/dm-cli/Replace.cpp +++ b/dm-cli/Replace.cpp @@ -1,9 +1,10 @@ #include #include "common.hpp" 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 > -1 && stop) return source.replace(start, stop, out); - return std::string(""); + if (start >= 0 ) res = source.replace(start, stop, out); + return res; } \ No newline at end of file diff --git a/dm-cli/common.hpp b/dm-cli/common.hpp index 51ec881..70ff1e5 100644 --- a/dm-cli/common.hpp +++ b/dm-cli/common.hpp @@ -30,6 +30,8 @@ + + // LANG=ru_RU.UTF-8 #define LANG_RU "ru_RU.UTF-8" #define DEFAULTADDRESS "https://127.0.0.1:4999" @@ -38,6 +40,9 @@ #define ACCOUNT_UPDATE_BTM "tpl/accountupdatebtm.txt" #define ACCOUNT_UPDATE_PGP "tpl/accountupdatepgp.txt" +#define DEAL_CREATE_TPL "tpl/deal_create.txt" + + /* Show help screen. */ diff --git a/dm-cli/createdistr.sh b/dm-cli/createdistr.sh index 3736571..9dd8fed 100644 --- a/dm-cli/createdistr.sh +++ b/dm-cli/createdistr.sh @@ -1 +1 @@ -tar -czvf dm-cli_distr.tar.gz tpl dm-cli *.txt \ No newline at end of file +tar -czvf dm-cli_distr.tar.gz tpl dm-cli *.txt diff --git a/dm-cli/tpl/deal_create.txt b/dm-cli/tpl/deal_create.txt new file mode 100644 index 0000000..8ad4ae2 --- /dev/null +++ b/dm-cli/tpl/deal_create.txt @@ -0,0 +1,3 @@ +curl -X POST "{{ADDRESS}}/api/v1/deal/create" \ + -H "Content-Type: application/json" \ + -d '{"at":"{{AT}}","type":"{{TYPE}}","seller":{"address": "{{SELLER}}"},"customer":{"address":"{{CUSTOMER}}"},"payment":{"sum":"{{SUM}}" {{UNTIL}} } {{FEETBACK}} }' \ No newline at end of file diff --git a/vc/dm-cli.vcxproj b/vc/dm-cli.vcxproj index 61959b2..f19da7d 100644 --- a/vc/dm-cli.vcxproj +++ b/vc/dm-cli.vcxproj @@ -80,6 +80,7 @@ + 17.0 diff --git a/vc/dm-cli.vcxproj.filters b/vc/dm-cli.vcxproj.filters index 5931157..3ef00fe 100644 --- a/vc/dm-cli.vcxproj.filters +++ b/vc/dm-cli.vcxproj.filters @@ -140,5 +140,8 @@ Resource Files + + Resource Files + \ No newline at end of file