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=gt7duzPFKccN+egSvFFp7iOboRV5MQzdKPhGJL11M2Y=; h=From:To:Cc:Subject:Message-Id:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:From; b=hh9gnD921BE7tjY2SAaYV30ZZuyAKzUgYko8s485ePxM01SpnhoPJstgbw+Bs5Imp PbiandlKfmCdi2pXaE2sLIIrqBGB4aratTscoEtvpG/phD9YBfgH1MLA6opiaulIPY Wg699rQSoZbTWu74zzVWNc5exq/WG5oEesBrm0Owb8P95KytTCxupdauFlHS3DSggV S74Yvjv+c0W3q5VxwwEezF6Qu+mMzlkSVS4R4JchcSROGCfYI+WWBvrDnSgEzutzsr kN78re7cdppsjpTSU241wTIPxVBauy2U13XPJ3CBYD0X5bPYTzfdesmf7ykr2P4Zm8 fMt7orly1aFiQ== Received: from server-vie001.gnuweeb.org (unknown [192.168.57.1]) by server-vie001.gnuweeb.org (Postfix) with ESMTPSA id E2DED3106502; 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 4/9] gwarnt: p2p: Add P2P OKX Date: Tue, 10 Sep 2024 23:44:09 +0200 Message-Id: <20240910214414.3401712-5-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 OKX. Signed-off-by: Alviro Iskandar Setiawan --- 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 +#include +#include +#include + +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 okx::get_data(void) +{ + return get_data(fiat_, crypto_, trade_type_); +} + +std::vector okx::get_data(const std::string &fiat, + const std::string &crypto, + const std::string &trade_type) +{ + std::vector 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().c_str(), nullptr); + p.min_amount_ = strtod(q["quoteMinAmountPerOrder"].get().c_str(), nullptr); + p.max_amount_ = strtod(q["quoteMaxAmountPerOrder"].get().c_str(), nullptr); + p.tradable_amount_ = strtod(q["availableAmount"].get().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 +#include + +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 get_data(void); + std::vector 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