A 3-minute Smart Contract and Delphi — Part 2

In a previous article, we created a simple Smart Contract that did not do much other than output the message “Hello World!”

In this article, we will both read and write data to the blockchain. It is important to realize that while reading from the blockchain is free, writing to the blockchain will cost gas. If you don’t know what gas is or does, then please refer to this article.

Because we don’t want to spend any real-world ether on gas at this time, we will deploy our Smart Contract to the Ropsten test net. If you don’t know what Ropsten is or how to create an account on the Ropsten test net, then please refer to this article.

From the perspective of a Delphi developer, you can think of deploying a Smart Contract as creating an instance of a class. When you then set a property value, your instance of the Smart Contract will keep that value for eternity (assuming no one else will change the property value).

Remix

In this article, we will develop our Smart Contract using Remix. Remix is a browser-based IDE for writing Smart Contracts in the Solidity programming language.

Assuming you have an account with some faucet ether in MetaMask on the Ropsten test net, we will now write a Smart Contract.

  1. Start Google Chrome
  2. Click on the MetaMask icon
  3. Unlock MetaMask with your MetaMask password
  4. You should see the MetaMask main menu. In the top of this window, click on Main Ethereum Network
  5. A popup window appears. Click on Ropsten Test Network
  6. Navigate to https://remix.ethereum.org
  7. Click on the plus sign in the upper-left corner to create a new, untitled Solidity file
  8. Rename it to and click on OK
  9. Write the following lines of code in the file, as shown below:

Before we deploy this Smart Contract, let me walk you through this code.

The 1st line starts with . As you probably realized already, it states which version of Solidity we are about to use. is a language construct that gives a directive to the compiler about how to process its inputs. In our example, our directive is to make the compiler behave as if it is running a Smart Contract on Solidity version or lower.

We then start defining the contract. We name it , as we are going to store and manipulate a value using arithmetic operations. There are no naming rules here. Try to stick to some logical names.

Then, we must declare an unsigned integer value that will store our number, which must be a positive integer.

Now we need to give our contract a constructor. Basically, this is a function that will be called whenever our contract is deployed and initiated for the first time. Inside the constructor, we assign to the number variable. Whenever the contract gets initiated for the first time, it will set a default value of to our unsigned integer variable number.

Last but not least, we create four other functions inside the contract that basically set, fetch, increment, and decrement the value we assigned to the variable number.

Ropsten

We will now deploy this Smart Contract to the Ropsten test net. Please follow these steps:

1. Click on Solidity compiler in the icon panel

2. Click on Compile ArithValue.sol

3. Click on the Deploy & run transactions in the icon panel

4. Click on the button labelled Deploy. You should be greeted with this (MetaMask) dialog:

5. Click on Confirm. Please wait. The creation of your Smart Contract is pending. After awhile, MetaMask should respond with this:

6. Click on the MetaMask toast. You are now taken to the detail page of the transaction that created your Smart Contract.

7. Included with the transaction is a contract address. Copy this contract address to your clipboard.

Congratulations! You have successfully created your Smart Contract on the Ropsten test net.

Delphi

We will now turn our attention to 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:

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

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

Double-click on your newly created main form, and add the following code to the event handler:

Please note that you must 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.

Now drop a Button component on your main form. Please label this button . Then double-click on the Button, and add the following code to the event handler:

Please note that you must replace the smart contract address in the example above with your own smart contract address.

Run your newly created Delphi project (F9) and click on the Button labelled . You should be greeted with this dialog:

Now drop another Button component on your main form. Please label this button . Then double-click on the Button, and add the following code to the event handler:

Please note that you must replace the private key in the example above with your own private key. Also, please note that your account must contain some faucet ether to pay for gas.

Click on the Button labelled . After a few seconds, your transaction will appear in a new block. Now click on the Button labelled again. The value is now

Now drop another Button component on your main form. Please label this button . Then double-click on the Button, and add the following code to the event handler:

Please note that while we are using in Solidity, we are using in Delphi. This is because is a synonym for . When encoding the function selector, has to be used.

After a few seconds, your transaction will appear in a new block. Now click on the Button labelled again. The value is now

Congratulations! You have successfully created a Smart Contract, and then written data to that Smart Contract from Delphi.

Read more

  1. Connecting Delphi to a local (in-memory) blockchain
  2. Connecting Delphi to the Ethereum main net
  3. Connecting Delphi to Smart Contracts
  4. Generating an Ethereum-signed message signature in Delphi
  5. Transferring Ether with Delphi
  6. Transferring ERC-20 tokens with Delphi
  7. Delphi and Ethereum Name Service (ENS)

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store