You can think of a smart contract as a microservice that runs on the backend of your application. Ethereum is the global computer that executes your smart contracts and maintains their state on the blockchain. Smart contracts cannot be shutdown by anyone, and have zero downtime.
A token on the Ethereum blockchain (that is not ether) is essentially a smart contract that adheres to a certain programming interface. In this article, we will find the smart contract of a top 10 token, and then connect to that smart contract from Delphi.
Before we begin, we need to clone a few GitHub repositories.
Because floating points do not really exist in smart contracts, we first need to clone Rudy Velthuis’ excellent BigIntegers library:
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
Now open etherscan.io in your web browser. Etherscan is a so-called blockchain explorer, where you can easily lookup, confirm and validate transactions that have taken place on the blockchain. Navigate to the following web page:
This page will give you a list of (ERC-20) tokens on the Ethereum blockchain. You can pick any token, but for the purpose of this example we will click on BNB. We are then taken to the detail page for the BNB token:
This page gives us a lot of interesting information about the token, including the total supply and the price of the token. But the piece of information that we’re really interested in, is the source code to the smart contract. Click on the token contract address. We are then taken to the detail page for the smart contract:
What we will do is create a simple Hello World project that calls a smart contract function and get the totalSupply() of a token. Nothing more, nothing less.
Launch Delphi, and then start a new project. Please make sure both the BigNumbers and Delphereum libraries are in your search path. Drop 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:
Please note that you will need to replace the URL in the example above with your Infura endpoint. If you are unsure what an Infura endpoint is, then please refer to this article.
The example above breaks down to this. After we have created a new instance of the Delphereum client, we will then call a function named
symbol() on BNB’s smart contract. Because calls to the Delphereum library are asynchronous, the
web3.eth.callfunction expects a so-called promise. Inside this anonymous method, we store the symbol name in a variable and then complete the promise chain with another call to a function named
totalSupply(). We then concatenate everything together and prompt the user. Please note that we need to synchronise the execution state with the main thread, and that is what
TThread.Synchronizedoes for us.
Run your newly created Delphi project (F9) and click on the Button. You should be greeted with this dialog:
Congratulations! You have successfully connected a native application to a smart contract. In the next article, we will generate an Ethereum-signed message signature.