I am writing a c++ wallet and I need to be able to encode smart contract function calls in ABI format. I found that the web3 library has the ability, but it is written in JS. Is there a c++ alternative?

Thank you

  • You can take the JS code and convert it to C++. Sep 22 '20 at 6:56
  • @goodvibration, I could, and could also call the JS but I prefer a native library over converting the code myself or raising a JS process and communicating with it.
    – Epic
    Sep 22 '20 at 7:05
  • I am aware of web3.js, web3.py and web3j (Java), with the first one being the most widely used. There might also be web3.php and web3.go. I have never encountered anything to imply that there's web3.cpp. Sep 22 '20 at 7:09
  • You may use the JSON RPC API of Ethereum (eth.wiki/json-rpc/API) Sep 27 '20 at 7:17

i found two of them and it's really bad. One is 392 mb size haha. Really you don't need it if you deal with known contract. here is sample of token mint from cpp.

#include <sys/socket.h>
#include <netdb.h>
#include <iostream>
#include <sstream>
#include <openssl/bn.h>
#include <string>
#include <iomanip>
#include <algorithm>
#include "keccak.h"
#include <unistd.h>

int main(int argc , char *argv[])
    Keccak keccak256(Keccak::Keccak256);
    std::stringstream f;
    const char et[] = "mint(address,uint256)";
    std::string ettag(keccak256(et), 0, 8);
    f << "0x" << ettag;
    std::string addr("0xF9cBf7b08f09ED3d4516E8b7A3FCbe4Dc7B3Cd40");
    std::string addrone(addr, 2, 40);
    f << std::setfill('0') << std::setw(64) << addrone; 
    unsigned long long c = 17000000000000000000ULL;
//  std::reverse((unsigned char*)&c, (unsigned char*)&c + 8);
    f << std::hex << std::setw(64) << c;
    std::cout << "and then " << f.str().c_str() << std::endl;
    int q;
    hostent *host = gethostbyname("localhost");
    sockaddr_in server = {AF_INET, htons( 8545 ), *((unsigned long*)host->h_addr)};
    q = socket(AF_INET , SOCK_STREAM , 0);
    connect(q, (sockaddr *)&server, sizeof(server));
    std::stringstream fvea;

    fvea << R"({"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from": "0x031200BCE52f44EDd8e3988c09faBF106b508F86","to": "0xEf49513C0261848b0e49c61f750Ec06a8d204AEe","gas": "0x31f90","gasPrice": "0xa111a000","value": "0x0", "data": ")" << f.str() << R"("}], "id":49})";
    std::stringstream cp;
    cp << "POST / HTTP/1.1\r\nContent-Length: "<< fvea.str().length() << "\r\n\r\n"<< fvea.str();
    char b[2501]={};
    send(q, cp.str().c_str(), cp.str().length(), 0);
    recv(q, b, 2500, 0);
    std::cout << b << std::endl;
 c++ --std=c++17 '/home/alex/Desktop/etr/port.cpp' '/home/alex/Desktop/etr/keccak.cpp'   -o c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.