Building a Web3 CLI Tool for the Ballerina Language: From Idea to Reality
Thilan Dissanayaka WSO2 January 21, 2020

Building a Web3 CLI Tool for the Ballerina Language: From Idea to Reality

๐Ÿš€ Excited to finally share my journey of building a web3 CLI tool for Ballerina! This tool bridges the gap between Ethereum smart contracts and the Ballerina programming language by automatically generating a Ballerina connector from any given smart contract.

In this post, Iโ€™ll walk you through:

Why I built this tool

The technologies involved

The challenges I faced (and how I solved them)

How it all works under the hood

๐ŸŒ Why a Web3 Tool for Ballerina? Ballerina is a modern programming language designed for integration. With its powerful built-in concurrency and network primitives, I saw a huge potential for it to be used in blockchain-related applications.

However, Ballerina lacked official tools to interact with Ethereum smart contracts โ€” unlike other ecosystems like JavaScript or Python.

So I decided to build a solution: a CLI tool that can automatically generate Ballerina code for any smart contract using its ABI file โ€” just like the Ballerina OpenAPI tool does for REST APIs.

๐Ÿ”ง Technologies Used Hereโ€™s what I used to bring this project to life:

Technology Purpose Java CLI tool development, ABI parsing, codegen Ballerina Target language for generated code Ballerina Syntax Tree API To generate Ballerina syntax programmatically Solidity Smart contract language Ganache Local Ethereum blockchain for testing PicoCLI CLI parsing framework in Java ๐Ÿ›  How It Works Parse the Smart Contract ABI The user provides an ABI file (JSON) exported from Solidity. The tool parses this ABI to extract all the functions, inputs, and outputs.

Generate Remote Functions For each method in the smart contract, the tool creates a corresponding remote function in Ballerina with proper types and structure.

Sanitize Solidity Identifiers Some Solidity identifiers (like match, type, etc.) are reserved keywords in Ballerina. I added a sanitizer step to rename or safely transform them during generation.

Use Ballerina Syntax Tree API This part was the most complex. I had to dynamically build Ballerina code from Java using the official Syntax Tree API. This allows the tool to generate clean, valid, and idiomatic Ballerina code.

Output the Connector The final result is a fully functional Ballerina connector module that can:

Call contract functions (view or state-changing)

Fetch Ethereum accounts

Query blocks and transactions

Interact with events (coming soon)

๐Ÿงช Crypto Hurdle: No Keccak-256? One major roadblock I hit early on was that Ballerina didnโ€™t support Keccak-256 hashing, which is essential for generating Ethereum function selectors.

โœ… So I decided to contribute to the Ballerina Crypto Library myself and added Keccak-256 support! This contribution made the rest of the work possible.

๐Ÿ’ก Challenges Faced

Challenge How I Solved It ABI parsing and type mapping Wrote a custom parser to map Solidity types to Ballerina types Reserved keywords in Ballerina Built a sanitizer for identifiers Code generation via Java Learned and used the Ballerina Syntax Tree API Function selector hashing Added Keccak-256 to the Ballerina crypto module CLI integration Used PicoCLI for a clean and user-friendly CLI interface ๐Ÿงฐ Sample Usage

java -jar ballerina-web3-cli.jar \
  --abi /path/to/MyContract.abi.json \
  --output ./generated \
  --package mycontract

This command generates a full Ballerina module named mycontract with all the functions from the smart contract!

๐Ÿ”— GitHub Repository You can check out the tool on GitHub: ๐Ÿ‘‰ https://github.com/SkFatharAli/web3-ballerina

Feel free to explore, give feedback, or even contribute.

๐Ÿ™Œ Final Thoughts This project pushed me to learn new thingsโ€”from ABI parsing to language internals to writing Ballerina from Java. But it was worth every bit of effort.

Ballerina has great potential in the blockchain ecosystem. With this tool, I hope to make smart contract integration easier and faster for everyone using Ballerina.

If youโ€™re building something similar or curious about Ballerina + Web3 โ€” Iโ€™d love to connect!

Would you like help turning this blog post into a full HTML/Markdown page or uploading it to your blog platform (like Dev.to, Medium, Hashnode, etc.)?

ALSO READ
Writing a Shell Code for Linux
Apr 21, 2020 Exploit Development

Shellcode is a small piece of machine code used as the payload in exploit development. In this post, we write Linux shellcode from scratch โ€” starting with a simple exit, building up to spawning a shell, and explaining every decision along the way.

Exploiting a Stack Buffer Overflow on Windows
Apr 12, 2020 Exploit Development

In a previous tutorial we discusses how we can exploit a buffer overflow vulnerability on a Linux machine. I wen through all theories in depth and explained each step. Now today we are going to jump...

Exploiting a  Stack Buffer Overflow  on Linux
Apr 01, 2020 Exploit Development

Have you ever wondered how attackers gain control over remote servers? How do they just run some exploit and compromise a computer? If we dive into the actual context, there is no magic happening....

Basic concepts of Cryptography
Mar 01, 2020 Cryptography

Ever notice that little padlock icon in your browser's address bar? That's cryptography working silently in the background, protecting everything you do online. Whether you're sending an email,...

Common Web Application Attacks
Feb 05, 2020 Application Security

Web applications are one of the most targeted surfaces by attackers. This is primarily because they are accessible over the internet, making them exposed and potentially vulnerable. Since these...

Remote Code Execution (RCE)
Jan 02, 2020 Application Security

Remote Code Execution (RCE) is the holy grail of application security vulnerabilities. It allows an attacker to execute arbitrary code on a remote server โ€” and the consequences are as bad as it sounds. In this post, we'll go deep into RCE across multiple languages, including PHP, Java, Python, and Node.js.

> > >