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 4/9] gwarnt: p2p: Add P2P OKX
Date: Tue, 10 Sep 2024 23:44:09 +0200 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
Add an interface to collect P2P ads from OKX.
Signed-off-by: Alviro Iskandar Setiawan <[email protected]>
---
Makefile | 3 +-
src/gwarnt/p2p/okx.cpp | 101 +++++++++++++++++++++++++++++++++++++++++
src/gwarnt/p2p/okx.hpp | 35 ++++++++++++++
3 files changed, 138 insertions(+), 1 deletion(-)
create mode 100644 src/gwarnt/p2p/okx.cpp
create mode 100644 src/gwarnt/p2p/okx.hpp
diff --git a/Makefile b/Makefile
index 732d1b7..6aa0b32 100644
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,8 @@ CXX_SOURCES := \
src/gwarnt/helpers.cpp \
src/gwarnt/net.cpp \
src/gwarnt/p2p_ad.cpp \
- src/gwarnt/p2p/binance.cpp
+ src/gwarnt/p2p/binance.cpp \
+ src/gwarnt/p2p/okx.cpp
C_OBJECTS := $(C_SOURCES:.c=.o)
CXX_OBJECTS := $(CXX_SOURCES:.cpp=.o)
diff --git a/src/gwarnt/p2p/okx.cpp b/src/gwarnt/p2p/okx.cpp
new file mode 100644
index 0000000..f37ace4
--- /dev/null
+++ b/src/gwarnt/p2p/okx.cpp
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <gwarnt/p2p/okx.hpp>
+#include <gwarnt/helpers.hpp>
+#include <gwarnt/json.hpp>
+#include <exception>
+
+using json = nlohmann::json;
+
+namespace gwarnt {
+namespace p2p {
+
+okx::okx(void)
+{
+ net_.add_header("Content-Type", "application/json");
+}
+
+okx::~okx(void) = default;
+
+void okx::set_fiat(const std::string &fiat)
+{
+ fiat_ = fiat;
+}
+
+void okx::set_crypto(const std::string &crypto)
+{
+ crypto_ = crypto;
+}
+
+void okx::set_trade_type(const std::string &trade_type)
+{
+ trade_type_ = trade_type;
+}
+
+std::vector<gwarnt::p2p_ad> okx::get_data(void)
+{
+ return get_data(fiat_, crypto_, trade_type_);
+}
+
+std::vector<gwarnt::p2p_ad> okx::get_data(const std::string &fiat,
+ const std::string &crypto,
+ const std::string &trade_type)
+{
+ std::vector<gwarnt::p2p_ad> ads;
+ std::string type;
+ std::string url;
+ std::string p;
+ json *jent;
+ json j;
+
+ type = trade_type;
+ strtoupper(type);
+ if (type != "BUY" && type != "SELL")
+ throw std::runtime_error("Invalid trade type: " + trade_type);
+
+ p = "side=" + trade_type + "&fiatCurrency=" + fiat + "&cryptoCurrency=" + crypto;
+ url = "https://www.okx.com/v3/c2c/tradingOrders/getMarketplaceAdsPrelogin?&paymentMethod=all&userType=all&hideOverseasVerificationAds=false&sortType=price_asc&limit=1000¤tPage=1&numberPerPage=1000&"
+ + p;
+
+ net_.set_method("GET");
+ net_.set_url(url);
+ net_.set_curl_opt(CURLOPT_ACCEPT_ENCODING, "gzip");
+ net_.exec();
+
+ const std::string &res = net_.get_resp();
+ j = json::parse(res);
+
+ if (type == "SELL")
+ jent = &j["data"]["sell"];
+ else
+ jent = &j["data"]["buy"];
+
+ for (const auto &q : *jent) {
+ try {
+ gwarnt::p2p_ad p;
+
+ p.ad_id_ = q["id"];
+ p.merchant_name_ = q["nickName"];
+ p.trade_type_ = type;
+ p.fiat_ = fiat;
+ p.crypto_ = crypto;
+ p.price_ = strtod(q["price"].get<std::string>().c_str(), nullptr);
+ p.min_amount_ = strtod(q["quoteMinAmountPerOrder"].get<std::string>().c_str(), nullptr);
+ p.max_amount_ = strtod(q["quoteMaxAmountPerOrder"].get<std::string>().c_str(), nullptr);
+ p.tradable_amount_ = strtod(q["availableAmount"].get<std::string>().c_str(), nullptr);
+ p.exchange_ = "okx";
+
+ for (auto &r : q["paymentMethods"])
+ p.methods_.push_back(r);
+
+ ads.push_back(p);
+ } catch (const std::exception &e) {
+ printf("gwarnt::p2p::okx::get_data: %s\n", e.what());
+ }
+ }
+
+ return ads;
+}
+
+} /* namespace gwarnt */
+} /* namespace p2p */
diff --git a/src/gwarnt/p2p/okx.hpp b/src/gwarnt/p2p/okx.hpp
new file mode 100644
index 0000000..3422ac5
--- /dev/null
+++ b/src/gwarnt/p2p/okx.hpp
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#ifndef GWARNT__P2P__OKX_HPP
+#define GWARNT__P2P__OKX_HPP
+
+#include <gwarnt/net.hpp>
+#include <gwarnt/p2p_ad.hpp>
+
+namespace gwarnt {
+namespace p2p {
+
+class okx {
+public:
+ okx(void);
+ ~okx(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);
+
+private:
+ gwarnt::net net_;
+ std::string fiat_;
+ std::string crypto_;
+ std::string trade_type_;
+};
+
+} /* namespace p2p */
+} /* namespace gwarnt */
+
+#endif /* GWARNT__P2P__OKX_HPP */
--
Alviro Iskandar Setiawan
next prev 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 ` [RFC PATCH 3/9] gwarnt: p2p: Add P2P Binance Alviro Iskandar Setiawan
2024-09-10 21:44 ` Alviro Iskandar Setiawan [this message]
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-5-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