1

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

4
  • You can take the JS code and convert it to C++. Sep 22, 2020 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, 2020 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, 2020 at 7:09
  • You may use the JSON RPC API of Ethereum (eth.wiki/json-rpc/API) Sep 27, 2020 at 7:17

2 Answers 2

2

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);
    close(q);
    std::cout << b << std::endl;
}
 c++ --std=c++17 '/home/alex/Desktop/etr/port.cpp' '/home/alex/Desktop/etr/keccak.cpp'   -o c
0

There is. You can use libethc. An example for encoding ERC20 balanceOf call would be:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <ethc/abi.h>

#define ok(ethcop) assert(ethcop >= 0)

int main(void) {
  struct eth_abi abi;
  char *fn = "balanceOf(address)", *addr="0x876D477Bd5cD050E6162cf757E1Bc02D93cdC0fE", *hex;
  size_t hexlen;

  ok(eth_abi_init(&abi, ETH_ABI_ENCODE));

  ok(eth_abi_call(&abi, &fn, NULL));  // balanceOf(
    ok(eth_abi_address(&abi, &addr)); //   0x876D477Bd5cD050E6162cf757E1Bc02D93cdC0fE
  ok(eth_abi_call_end(&abi));         // )

  ok(eth_abi_to_hex(&abi, &hex, &hexlen));
  ok(eth_abi_free(&abi));
  printf("encoded abi: %s\n", hex); // encoded abi: 70a08231000000000000000000000000876d477bd5cd050e6162cf757e1bc02d93cdc0fe
  free(hex);
  return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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