From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1726004659; bh=O5peRW0zWYpY745e/v2PzdClt6OsGw1HTMO5o6dUjKw=; h=From:To:Cc:Subject:Message-Id:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:From; b=adv2v0qYjraaPgUZUzdRqDJ3yOJfwurPGRP94H2X8Lxmo6GxN1h2Yd4oc6OBeJvfX JsSqvZrQuoY5BoYXZC4Ta7SkO/I1KBoTi385BG0didETkaZaTVTyWNpPhtV75kjGuT WiSrdNBwHsPZSRYLcworYFZS107X39NiSUNUh/qtAGxcz2qcG+L1o2lcinlPXXz4x9 sGnF6hGNqfUPRlvqlVJJG7nwUbMfQOamtw+Aqp8DJHbPJM9dN+dOyg4O7f0OLau+c6 WogoNYdfr/zwaMcFOEOVHS2BmXLGJrrditcbEuwla9rJT8lQReaFnDURf9SCWX0Dgn VaW3O94y0mfIg== Received: from server-vie001.gnuweeb.org (unknown [192.168.57.1]) by server-vie001.gnuweeb.org (Postfix) with ESMTPSA id C7E333106500; Tue, 10 Sep 2024 21:44:19 +0000 (UTC) From: Alviro Iskandar Setiawan To: Ammar Faizi , Michael William Jonathan Cc: Alviro Iskandar Setiawan , Ravel Kevin Ethan , GNU/Weeb Mailing List Subject: [RFC PATCH 3/9] gwarnt: p2p: Add P2P Binance Date: Tue, 10 Sep 2024 23:44:08 +0200 Message-Id: <20240910214414.3401712-4-alviro.iskandar@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240910214414.3401712-1-alviro.iskandar@gnuweeb.org> References: <20240910214414.3401712-1-alviro.iskandar@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Add an interface to collect P2P ads from Binance. Signed-off-by: Alviro Iskandar Setiawan --- Makefile | 3 +- src/gwarnt/p2p/binance.cpp | 142 +++++++++++++++++++++++++++++++++++++ src/gwarnt/p2p/binance.hpp | 42 +++++++++++ 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 src/gwarnt/p2p/binance.cpp create mode 100644 src/gwarnt/p2p/binance.hpp diff --git a/Makefile b/Makefile index 7dbfb71..732d1b7 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,8 @@ CXX_SOURCES := \ src/gwarnt/entry.cpp \ src/gwarnt/helpers.cpp \ src/gwarnt/net.cpp \ - src/gwarnt/p2p_ad.cpp + src/gwarnt/p2p_ad.cpp \ + src/gwarnt/p2p/binance.cpp C_OBJECTS := $(C_SOURCES:.c=.o) CXX_OBJECTS := $(CXX_SOURCES:.cpp=.o) diff --git a/src/gwarnt/p2p/binance.cpp b/src/gwarnt/p2p/binance.cpp new file mode 100644 index 0000000..541a5de --- /dev/null +++ b/src/gwarnt/p2p/binance.cpp @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include + +using json = nlohmann::json; + +namespace gwarnt { +namespace p2p { + +binance::binance(void) +{ + net_.add_header("Content-Type", "application/json"); + net_.add_header("x-passthrough-token", ""); +} + +binance::~binance(void) = default; + +void binance::set_fiat(const std::string &fiat) +{ + fiat_ = fiat; +} + +void binance::set_crypto(const std::string &crypto) +{ + crypto_ = crypto; +} + +void binance::set_trade_type(const std::string &trade_type) +{ + trade_type_ = trade_type; +} + +std::vector binance::get_data(void) +{ + return get_data(fiat_, crypto_, trade_type_); +} + +std::vector binance::get_data(const std::string &fiat, + const std::string &crypto, + const std::string &trade_type, + uint64_t page) +{ + std::vector ads; + + if (page == ~0ull) { + std::vector tmp; + size_t i; + + for (i = 1; i <= 5; i++) { + tmp = __get_data(fiat, crypto, trade_type, page); + ads.insert(ads.end(), tmp.begin(), tmp.end()); + } + } else { + ads = __get_data(fiat, crypto, trade_type, page); + } + + return ads; +} + +std::vector binance::__get_data(const std::string &fiat, + const std::string &crypto, + const std::string &trade_type, + uint64_t page) +{ + std::vector ads; + std::string type; + json j; + + type = trade_type; + strtoupper(type); + if (type == "BUY") + type = "BUY"; + else if (type == "SELL") + type = "SELL"; + else + throw std::runtime_error("Invalid trade type: " + trade_type); + + j["fiat"] = fiat; + j["page"] = page; + j["rows"] = 20; + j["tradeType"] = type; + j["asset"] = crypto; + j["countries"] = json::array(); + j["proMerchantAds"] = false; + j["shieldMerchantAds"] = false; + j["filterType"] = "all"; + j["periods"] = json::array(); + j["additionalKycVerifyFilter"] = 0; + j["publisherType"] = "merchant"; + j["payTypes"] = json::array(); + j["classifies"] = json::array(); + j["classifies"].push_back("mass"); + j["classifies"].push_back("profession"); + j["classifies"].push_back("fiat_trade"); + + net_.set_url("https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search"); + net_.set_method("POST"); + net_.set_data(j.dump()); + net_.set_curl_opt(CURLOPT_ACCEPT_ENCODING, "gzip"); + net_.exec(); + + const std::string &res = net_.get_resp(); + j = json::parse(res); + + for (const auto &i : j["data"]) { + try { + const auto &adv = i["adv"]; + gwarnt::p2p_ad p; + + p.ad_id_ = adv["advNo"]; + p.merchant_name_ = i["advertiser"]["nickName"]; + p.fiat_ = adv["fiatUnit"]; + p.crypto_ = adv["asset"]; + p.trade_type_ = adv["tradeType"]; + p.price_ = strtod(adv["price"].get().c_str(), nullptr); + p.min_amount_ = strtod(adv["minSingleTransAmount"].get().c_str(), nullptr); + p.max_amount_ = strtod(adv["maxSingleTransAmount"].get().c_str(), nullptr); + p.tradable_amount_ = strtod(adv["tradableQuantity"].get().c_str(), nullptr); + p.exchange_ = "binance"; + + for (auto &j : adv["tradeMethods"]) { + try { + p.methods_.push_back(j["tradeMethodName"]); + } catch (...) { + p.methods_.push_back("p:"+j["identifier"].get()); + } + } + + ads.push_back(p); + } catch (std::exception &e) { + printf("gwarnt::p2p::binance::__get_data: %s\n", e.what()); + } + } + + return ads; +} + +} /* namespace gwarnt */ +} /* namespace p2p */ diff --git a/src/gwarnt/p2p/binance.hpp b/src/gwarnt/p2p/binance.hpp new file mode 100644 index 0000000..1b73dbe --- /dev/null +++ b/src/gwarnt/p2p/binance.hpp @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#ifndef GWARNT__P2P__BINANCE_HPP +#define GWARNT__P2P__BINANCE_HPP + +#include +#include + +namespace gwarnt { +namespace p2p { + +class binance { +public: + binance(void); + ~binance(void); + + void set_fiat(const std::string &fiat); + void set_crypto(const std::string &crypto); + void set_trade_type(const std::string &trade_type); + std::vector get_data(void); + std::vector get_data(const std::string &fiat, + const std::string &crypto, + const std::string &trade_type, + uint64_t page = ~0ull); + +private: + std::vector __get_data(const std::string &fiat, + const std::string &crypto, + const std::string &trade_type, + uint64_t page); + + + gwarnt::net net_; + std::string fiat_; + std::string crypto_; + std::string trade_type_; +}; + +} /* namespace p2p */ +} /* namespace gwarnt */ + +#endif /* GWARNT__P2P__BINANCE_HPP */ -- Alviro Iskandar Setiawan