Connecting Delphi to a local (in-memory) blockchain

In this article, we will connect Delphi to a local (in-memory) blockchain using Delphereum, a Delphi interface to the Ethereum blockchain.

What is Delphi?

Delphi is a computer language and IDE that compiles into native code for Windows, macOS, iOS, and Android.

What is Ethereum?

Ethereum is a blockchain-based distributed computing platform and operating system featuring smart contracts. Data on the blockchain isn’t owned by anyone, cannot be shut down, and has zero downtime.

Step #1. Clone GitHub repositories

Before we begin, we need to clone a few GitHub repositories.

Because Ethereum denotes balances in wei (aka the smallest subdivision of ether), we first need to clone Rudy Velthuis’ excellent BigNumbers library:

git clone https://github.com/svanas/DelphiBigNumbers

Next up are Ugochukwu Mmaduekwe’s excellent crypto libraries:

git clone https://github.com/Xor-el/SimpleBaseLib4Pascal
git clone https://github.com/Xor-el/HashLib4Pascal
git clone https://github.com/Xor-el/CryptoLib4Pascal

Last but not least is Delphereum, a web3 implementation for the Delphi programming language:

git clone https://github.com/svanas/delphereum

Step #2. Download Ganache

Ganache is a personal Ethereum blockchain which you can use to run tests, execute commands, and inspect state.

https://truffleframework.com/ganache

Download and start Ganache. You should see the following screen. The important thing to take away here is RPC server address, circled in the below screen shot. You will need that address later.

Ganache will give you 10 accounts. Each account is given 100 ether. When you close and then restart Ganache, you then reset the blockchain and return to this default 10-accounts-with-100-ether setup.

Step #3. Start a new Delphi project

What we will do is create a simple Hello World project that connects to an Ethereum blockchain and then get the balance of an account. Nothing more, nothing less.

Launch Delphi, and then start a new project. Please make sure the dependencies (cloned above) are in your search path.

Drop an Edit and a Button component on your newly created main form. Then double-click on the Button component, and add the following code to the OnClick event handler:

Because calls to the Delphereum library are asynchronous, the web3.eth.getBalancefunction expects a so-called promise. Inside this anonymous method, we need to synchronise the execution state with the main thread, and that is what TThread.Synchronizedoes for us.

Step #4. Get the balance of an Ethereum account

Run your newly created Delphi project (F9). Switch over to Ganache, then copy the address of an account to the clipboard (it does not matter which one, as long as the account contains some ether).

Switch back to your Delphi application and paste the address of the account into the Edit component. Now click the Button component. You should be greeted with this dialog:

Congratulations! You have successfully connected a native application to a local (in-memory) Ethereum blockchain. In the next article, we will connect Delphi to the Ethereum main net.

Delphi/Rust/Go developer. Ethereum consultant. Embarcadero MVP. Ex-Adobe, Macromedia. Helped build 1Password.

Delphi/Rust/Go developer. Ethereum consultant. Embarcadero MVP. Ex-Adobe, Macromedia. Helped build 1Password.