public inbox for [email protected]
 help / color / mirror / Atom feed
From: Alviro Iskandar Setiawan <[email protected]>
To: Ammar Faizi <[email protected]>,
	Michael William Jonathan <[email protected]>
Cc: Alviro Iskandar Setiawan <[email protected]>,
	Ravel Kevin Ethan <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [RFC PATCH 3/9] gwarnt: p2p: Add P2P Binance
Date: Tue, 10 Sep 2024 23:44:08 +0200	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

Add an interface to collect P2P ads from Binance.

Signed-off-by: Alviro Iskandar Setiawan <[email protected]>
---
 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 <gwarnt/p2p/binance.hpp>
+#include <gwarnt/helpers.hpp>
+#include <gwarnt/json.hpp>
+#include <exception>
+
+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<gwarnt::p2p_ad> binance::get_data(void)
+{
+	return get_data(fiat_, crypto_, trade_type_);
+}
+
+std::vector<gwarnt::p2p_ad> binance::get_data(const std::string &fiat,
+					      const std::string &crypto,
+					      const std::string &trade_type,
+					      uint64_t page)
+{
+	std::vector<gwarnt::p2p_ad> ads;
+
+	if (page == ~0ull) {
+		std::vector<gwarnt::p2p_ad> 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<gwarnt::p2p_ad> binance::__get_data(const std::string &fiat,
+						const std::string &crypto,
+						const std::string &trade_type,
+						uint64_t page)
+{
+	std::vector<gwarnt::p2p_ad> 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<std::string>().c_str(), nullptr);
+			p.min_amount_ = strtod(adv["minSingleTransAmount"].get<std::string>().c_str(), nullptr);
+			p.max_amount_ = strtod(adv["maxSingleTransAmount"].get<std::string>().c_str(), nullptr);
+			p.tradable_amount_ = strtod(adv["tradableQuantity"].get<std::string>().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<std::string>());
+				}
+			}
+
+			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 <gwarnt/net.hpp>
+#include <gwarnt/p2p_ad.hpp>
+
+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<gwarnt::p2p_ad> get_data(void);
+	std::vector<gwarnt::p2p_ad> get_data(const std::string &fiat,
+					     const std::string &crypto,
+					     const std::string &trade_type,
+					     uint64_t page = ~0ull);
+
+private:
+	std::vector<gwarnt::p2p_ad> __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


  parent reply	other threads:[~2024-09-10 21:44 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-10 21:44 [RFC PATCH 0/9] Introducing GNU/Weeb Arbitrage Opportunity Notification Bot Alviro Iskandar Setiawan
2024-09-10 21:44 ` [RFC PATCH 2/9] gwarnt: Create initial P2P ad data structure Alviro Iskandar Setiawan
2024-09-10 21:44 ` Alviro Iskandar Setiawan [this message]
2024-09-10 21:44 ` [RFC PATCH 4/9] gwarnt: p2p: Add P2P OKX Alviro Iskandar Setiawan
2024-09-10 21:44 ` [RFC PATCH 5/9] gwarnt: Create function to find arbitrage opportunities Alviro Iskandar Setiawan
2024-09-10 21:44 ` [RFC PATCH 6/9] gwarnt: p2p/binance: Fix invalid page Alviro Iskandar Setiawan
2024-09-10 21:44 ` [RFC PATCH 7/9] gwarnt: Create the initial example Alviro Iskandar Setiawan
2024-09-10 21:44 ` [RFC PATCH 8/9] gwarnt: Add README file Alviro Iskandar Setiawan
2024-09-10 21:44 ` [RFC PATCH 9/9] gwarnt: Add Telegram bot Alviro Iskandar Setiawan
2024-09-10 22:21 ` [RFC PATCH 0/9] Introducing GNU/Weeb Arbitrage Opportunity Notification Bot Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240910214414.3401712-4-alviro.iskandar@gnuweeb.org \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox