Application Binary Interface (ABI) In Smart Contract

Roy Nguyen
Mar 21 · 3 min read

Smart contract ABI in ethereum network

The Application Binary Interface is the standard way to interact with contracts in the Ethereum ecosystem, both from outside the blockchain and for contract-to-contract interaction. We assume the interface functions of a contract are strongly typed, known at compilation time and static. No introspection mechanism will be provided. We assume that all contracts will have the interface definitions of any contracts they call available at compile-time.

By example with solidity:

pragma solidity ^0.4.0;

contract Test {
function Test() public { b = 0x12345678901234567890123456789012; }
event Event(uint indexed a, bytes32 b);
event Event2(uint indexed a, bytes32 b);
function foo(uint a) public { Event(a, b); }
bytes32 b;
}

we can compile solidity code to ABI and byte code. The byte code are run by EVM and stored in ethereum database. The ABI are generated to JSON format :

[
{
"type":"event",
"inputs":[
{
"name":"a",
"type":"uint256",
"indexed":true
},
{
"name":"b",
"type":"bytes32",
"indexed":false
}
],
"name":"Event"
},
{
"type":"event",
"inputs":[
{
"name":"a",
"type":"uint256",
"indexed":true
},
{
"name":"b",
"type":"bytes32",
"indexed":false
}
],
"name":"Event2"
},
{
"type":"function",
"inputs":[
{
"name":"a",
"type":"uint256"
}
],
"name":"foo",
"outputs":[]
}
]

The result with two types are function and event, we use exactly functions name and parameters type to interact with smart contract and Events leverage the existing function ABI in order to interpret this (together with an interface spec) as a properly typed structure.

Blockchain Platform with smart contract

The developers who are develop smart contract in each blockchain platform will hope that they don’t study any new language program, they can build a program with the scalability and high performance by experiences of language. Build a Virtual Machine (VM) in Platform to support many popular languages is a good solution.

In the order to solve this challenge, We can choose to build a VM with webassembly (Wasm), a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust. You can find an implementation webassembly VM easily . However, there are many differences from build normal application and smart contract in blockchain such as read/write a file, connect to internet,… We will discuss in the other stories. In this post, we study about how to compile C/C++/Rust,…. to webassembly and ABI which interact with contracts in blockchain network. with webassembly, we can use llvm project (https://github.com/llvm/llvm-project), The LLVM project has multiple components, This contains all of the tools, libraries, and header files needed to process intermediate representations and converts it into object files. Tools include an assembler, disassembler, bitcode analyzer, and bitcode optimizer. It also contains basic regression tests. and Clang — compiler front-end for the C family of languages (https://github.com/llvm/llvm-project/tree/master/clang) can help convert to wasm.

In the wasm, we can not know exactly parameters type of C language in each function after clang compile to wasm, Therefor we need to build a tool for generate ABI from source code C language.

This is a good ideal for this problem:

The tool help us get parameters type and name, it use clang to compile to wasm and get that informations of functions at the same time after modify some implement of clang. This is a solution which was chosen by EOS platform.

To get more information about that tool, we can build and make example with C / C++ program and study how to implement that tool.

References:

  1. https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html
  2. https://github.com/rpav/c2ffi
  3. https://github.com/llvm

Roy Nguyen

Written by

Blockchain Developer at The Super Foundation Team, Vietnam

More From Medium

Also tagged Webassembly

Also tagged Webassembly

Debugging Comes to Blazor

Also tagged Webassembly

Top on Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade