Transferring Ether with Delphi
In a previous article, we generated an Ethereum-signed message signature in Delphi. In this article, we will build on that concept and sign a transaction so that we can transfer cryptocurrency out of our wallet to another wallet.
If you want to send cryptocurrency from A to B — or write anything to the blockchain for that matter — you then need to send a so-called transaction to the network.
When transactions are sent to the network, they first end up in a so-called mempool. Miners will pick transactions out of this mempool for them to verify. After a certain number of confirmations, your transaction will be included with a block. Because miners pay money for hardware and energy, they will want to be compensated for the work that they do. This is where gas comes into play.
When you create a transaction, you need to include a gas price and a gas limit.
Gas is different from Ether. In essence, gas is the metering unit for use of the Ethereum network. To help clarify this, consider gas to be synonymous with kilowatts or fuel. Using more computation and storage in Ethereum means that more gas is used.
The gas price generally reflects how fast you want your transaction mined. Because it will help their profitability, miners will prefer transactions with a higher gas price. Sometimes it makes sense to pay more if you want your transaction mined quickly. Or you may want to save some money and offer a cheap gas price, especially if you don’t care how quickly it is mined. Most people will use a default gas price when they make a transaction, and this is generally OK.
Gas limit is the maximum gas you will want to pay for the transaction. You will never pay more than the gas limit. A transaction that runs out of gas is reverted. When mined successfully, all remaining gas is refunded to the transaction’s originator: the user who initiated the transaction.
An awesome web site about gas prices and transaction confirmation times is the ETH Gas Station.
Using Delphereum, you can now sign transactions and send them to the Ethereum network from Delphi. Here is how to get started.
Step #1. Clone GitHub repositories
Step #2. Download Ganache
Ganache is a personal Ethereum blockchain which you can use to run tests, execute commands, and inspect state.
Download and start Ganache. You should see the following screen. The important thing to take away here is RPC server address. You will need that address later.
Ganache will give you 10 accounts. Each account is given 100 ether. When you close and then restart Ganache, you then reset the blockchain and return to this default 10-accounts-with-100-ether setup.
Step #3. Your new Delphi project
What we will do is create a simple Hello World program that will transfer 0.01 ether from
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:
For your convenience, Delphereum includes an overloaded version of
sendTransaction() without gas price and gas limit. When calling this overloaded version of
sendTransaction(), what Delphereum does is calculate the median gas price from the last few blocks.
Run your newly created Delphi project (F9) and click the Button component. Your program should respond with a transaction hash:
If you are on the main net, then you can look up this transaction hash on etherscan. Because we are currently connected to Ganache (not the main net), you can click on the transactions tab and see your transaction there:
Congratulations! You have successfully transferred cryptocurrency with Delphi.