DEV Community

Voren Tai
Voren Tai

Posted on

Learn to play buy sell trade in c++ pump.fun

Here is the script:

#include <iostream>
#include <string>
#include <map>
#include <curl/curl.h>
#include <json/json.h>
#include <unistd.h>

using namespace std;

size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    ((string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(int argc, char *argv[]) {
    // Parse command-line arguments
    string tradeType, mint, privateKey;
    float amount, priorityFee;
    int slippage, interval, frequency;

    for (int i = 1; i < argc; i += 2) {
        string arg = argv[i];
        if (arg == "--trade_type") {
            tradeType = argv[i + 1];
            if (tradeType != "buy" && tradeType != "sell") {
                cerr << "Error: Invalid trade type. Must be 'buy' or 'sell'." << endl;
                return 1;
            }
        } else if (arg == "--mint") {
            mint = argv[i + 1];
        } else if (arg == "--amount") {
            amount = stof(argv[i + 1]);
        } else if (arg == "--slippage") {
            slippage = stoi(argv[i + 1]);
        } else if (arg == "--priority_fee") {
            priorityFee = stof(argv[i + 1]);
        } else if (arg == "--private_key") {
            privateKey = argv[i + 1];
        } else if (arg == "--interval") {
            interval = stoi(argv[i + 1]);
        } else if (arg == "--frequency") {
            frequency = stoi(argv[i + 1]);
        } else {
            cerr << "Error: Unknown argument '" << arg << "'" << endl;
            return 1;
        }
    }

    if (tradeType.empty() || mint.empty() || privateKey.empty() || amount == 0 || slippage == 0 || priorityFee == 0 || interval == 0 || frequency == 0) {
        cerr << "Error: Missing required argument." << endl;
        return 1;
    }

    // Set up API request
    string url = "https://pumpapi.fun/api/trade";
    Json::Value payload;
    payload["trade_type"] = tradeType;
    payload["mint"] = mint;
    payload["amount"] = amount;
    payload["slippage"] = slippage;
    payload["priorityFee"] = priorityFee;
    payload["userPrivateKey"] = privateKey;

    CURL *curl;
    CURLcode res;
    string readBuffer;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if (curl) {
        for (int i = 0; i < frequency; i++) {
            string payloadStr = payload.toStyledString();
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payloadStr.c_str());

            res = curl_easy_perform(curl);
            if (res != CURLE_OK) {
                cerr << "cURL error: " << curl_easy_strerror(res) << endl;
                return 1;
            }

            Json::Value response;
            Json::Reader reader;
            bool parsingSuccessful = reader.parse(readBuffer, response);
            if (!parsingSuccessful) {
                cerr << "Error parsing JSON response." << endl;
                return 1;
            }

            if (response.isMember("tx_hash")) {
                string transactionId = response["tx_hash"].asString();
                cout << "Transaction ID: " << transactionId << endl;
            } else {
                cerr << "Error: " << response.toStyledString() << endl;
            }

            readBuffer.clear();
            usleep(interval * 1000000); // sleep for interval seconds
        }
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Docs

https://docs.pumpapi.fun/

Top comments (0)