A 3-minute Smart Contract and Delphi — Part 1
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;
- Alice has zero Bitcoin
- Bob has 1 Bitcoin
- Bob transfers 1 Bitcoin to Alice
- Alice has 1 Bitcoin
- 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 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.
Assuming you have an account with some faucet ether in MetaMask on the Ropsten 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 Main Ethereum Network
5. A popup window appears. Click on Ropsten Test Network
6. Navigate to https://remix.ethereum.org
7. Click on Plugin manager in the icon panel
8. You might need to install a few plugins. Locate the Solidity compiler plugin and click on Activate. Now repeat this step for the Deploy & run transaction plugin.
9. Click on Deploy & run transactions in the icon panel
10. Open the Environment drop-down, and click on: Injected Web3
11. You should be greeted with this (MetaMask) dialog:
12. Click on File explorer in the icon panel
13. Click on the plus sign to create a new, untitled Solidity file
14. Rename it to HelloWorld.sol
and click on OK
15. Write the following lines of code in the HelloWorld.sol
file, as shown below:
16. Click on Solidity compiler in the icon panel
17. Click on Compile HelloWorld.sol
18. Click on Deploy & run transactions in the icon panel
19. Click on the button labelled Deploy. You should be greeted with this (MetaMask) dialog:
20. Click on Confirm. Please wait. The creation of your Smart Contract is pending. After awhile, MetaMask should respond with this:
21. Click on the MetaMask toast. You are now taken to the detail page of the transaction that created your Smart Contract.
22. 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 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. 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…
- 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.
- replace the smart contract address in the example above with your own smart contract address (copied to your clipboard in step #22 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.