A 3-minute Smart Contract and Delphi — Part 2

Stefan
5 min readApr 23, 2019

--

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 Sepolia test net. If you don’t know what Sepolia is or how to create an account on the Sepolia 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 Sepolia 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 Ethereum Mainnet
  5. A popup window appears. Click on Sepolia (if Sepolia isn’t visible, you might need to click on show/hide test networks first)
  6. Navigate to https://remix.ethereum.org
  7. Create a new, untitled Solidity file
  8. Rename it to ArithValue.sol and press Enter
  9. Write the following lines of code in the ArithValue.sol file, as shown below:

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

The 1st line starts with pragma. As you probably realized already, it states which version of Solidity we are about to use. pragma 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 0.8.0 or lower.

We then start defining the contract. We name it ArithValue, 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 100 to the number variable. Whenever the contract gets initiated for the first time, it will set a default value of 100 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.

Sepolia

We will now deploy this Smart Contract to the Sepolia 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 Sepolia 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 FormCreate 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 getNumber. Then double-click on the Button, and add the following code to the OnClick 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 getNumber. You should be greeted with this dialog:

Now drop another Button component on your main form. Please label this button incrementNumber. Then double-click on the Button, and add the following code to the OnClick 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 incrementNumber. After a few seconds, your transaction will appear in a new block. Now click on the Button labelled getNumber again. The value is now 101

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

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

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

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)

--

--

Stefan
Stefan

Written by Stefan

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

Responses (1)