A 3-minute Smart Contract and Delphi — Part 1

Stefan
5 min readApr 18, 2019

In this article, we will develop our first Smart Contract. Before we dive into the world of Smart Contracts, let us step back for a moment and look at what differentiates Ethereum from Bitcoin.

While Bitcoin is the mother of all blockchains, the Bitcoin business rules are pretty simple;

  1. Alice has zero Bitcoin
  2. Bob has 1 Bitcoin
  3. Bob transfers 1 Bitcoin to Alice
  4. Alice has 1 Bitcoin
  5. Bob has zero Bitcoin

These business rules are hard-coded into the protocol and as such there isn’t much Bitcoin can do other than store and transfer value.

Ethereum, on the other hand, can do all of the above and execute code. You can think of Ethereum as a global computer, which has its own Turing complete programming language, and that has zero downtime.

Code is law

Smart Contracts are written in a Turing complete programming language called Solidity. By contrast, Bitcoin is not Turing complete. If you want to do something a hundred times on the Bitcoin blockchain, you would have to copy and paste a transaction a hundred times, while in Ethereum you could just write it once and tell the computer to execute it in a loop.

As software developers, we are familiar with states of code while in execution (that is, during run-time). In simple terms, it is the configuration of a piece of code at any given instant of time. Smart Contracts store each state of code on the Ethereum blockchain and will remain there for eternity — even after you and me have died. That is why this technology is often referred to as immutable and unstoppable.

Remix

In this article, we will develop our first Smart Contract using Remix.

https://remix.ethereum.org

Remix is a browser-based compiler and IDE that enables users to build Ethereum contracts with the Solidity programming language.

Because we don’t want to spend any real-world ether at this time, we will deploy our Smart Contract to the Sepolia test net.

Sepolia

The Sepolia testnet (launched late 2021) is essentially a testing environment that you can use before you bring your code onto the mainnet. When developers are building dapps, or experimenting on the network, they use the Sepolia testnet to avoid losing money paying real-world ether for transaction fees and smart contract deployments.

Before we can deploy a smart contract on the Sepolia testnet, we need some test ether in a wallet. Test ether carries no real-world value, but we need some to pay for transaction fees. The easiest way to get some test ether is to request it from a so-called faucet.

Assuming you have an account with MetaMask, please navigate to https://sepoliafaucet.com, paste your (MetaMask test account) address and click on Send Me ETH. Congratulations! You have successfully acquired 0.5 test ether in your MetaMask wallet on the Sepolia testnet.

Your first Smart Contract

Assuming you have an account with some faucet ether in MetaMask on the Sepolia test net, we will now write our first 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. Click on Deploy & run transactions in the icon panel

8. Open the Environment drop-down, and click on: Injected Provider — MetaMask

9. You should be greeted with this (MetaMask) dialog:

10. Click on File explorer in the icon panel

11. Create a new, untitled Solidity file

12. Rename it to HelloWorld.sol and press Enter.

13. Write the following lines of code in the HelloWorld.sol file, as shown below:

14. Click on Solidity compiler in the icon panel

15. Click on Compile HelloWorld.sol

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

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

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

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

20. Included with the transaction is a contract address (not the transaction hash). Copy the contract address to your clipboard.

Congratulations! You have successfully created your first 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. 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 must…

  1. 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.
  2. replace the smart contract address in the example above with your own smart contract address (copied to your clipboard in step #20 above).

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

Congratulations! You have successfully created your first Smart Contract, and then connected to that Smart Contract from Delphi.

In the next article, we will take things further and create a simple Smart Contract that will both read and write data to the blockchain.

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

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