Transferring ERC-20 tokens with Delphi

In a previous article, we transferred ether with Delphi. While ether is the number one cryptocurrency on the Ethereum blockchain, the network actually hosts thousands of other cryptocurrencies. Most of these other cryptocurrencies are known as ERC-20 tokens.

ERC-20

ERC-20 is a smart contract interface on the Ethereum blockchain for implementing tokens. ERC stands for Ethereum Request for Comment, and 20 is the number that was assigned to this request. Most of the tokens issued on the Ethereum blockchain are ERC-20 compliant. Because all ERC-20 tokens must adhere to the ERC-20 standard, it is super easy for an exchange to list them because from the technical perspective they all are basically identical. Among the most successful ERC-20 tokens are BNB and BAT and OMG.

Because we do not want to buy any real-world ERC-20 tokens at this time (nor spend any real-world gas on transactions), we will be using the Ropsten testnet (not the Ethereum mainnet).

Ropsten

The Ropsten testnet (launched in late 2016) 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 Ropsten testnet to avoid losing money paying real-world ether for transaction fees and smart contract deployments.

Before we can transfer an ERC-20 token on the Ropsten 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. And the easiest way to load up your account with faucet ether is via MetaMask.

MetaMask

MetaMask is a feature-rich Ethereum wallet for Chrome and Firefox and Opera. MetaMask supports the (Ropsten and Kovan and Rinkeby) test nets and makes it super easy to load up your account with test ether.

Please install MetaMask via a reputable source. Unfortunately, there are many bad actors out there trying to trick you into installing an imposer MetaMask extension into your web browser. In essence, they are then fishing for your private key.

For the purpose of this article, we will create a new account with MetaMask. Please follow these steps, then come back to this article.

Assuming you have successfully created a new account with MetaMask, now please follow these additional steps:

  1. Click on the MetaMask icon
  2. Unlock MetaMask with your MetaMask password
  3. You should see the MetaMask main menu. In the top of this window, click on Main Ethereum Network
  4. A popup window appears. Click on Ropsten Test Network
  5. follow this link https://faucet.metamask.io and click on request 1 ether from faucet
  6. You should be greeted with this (MetaMask) dialog:

Congratulations! You have successfully acquired 1 test ether in your MetaMask wallet on the Ropsten test net.

Before we can transfer an ERC-20 token, we need some of that in our wallet, too. In the real world, we would go to an exchange and then buy an ERC-20 token on the Ethereum mainnet. Because we are currently on Ropsten, what we need is an ERC-20 token on the Ropsten testnet.

TST

TST is a test token on the Ropsten network. Because TST is an ERC-20 token, it adheres to the ERC-20 smart contract interface. Etherscan has a feature called Write Contract and it allows to directly interact with the TST smart contract through the use of MetaMask.

  1. Navigate to the TST smart contract
  2. Click on Write Contract
  3. Click on Connect to Web3
  4. Scroll down to where it reads showMeTheMoney
  5. Paste your (MetaMask test account) address in _to
  6. Enter “1000000000000000000” (no quotes) in _value (uint256)
  7. Click on Write
  8. You should be greeted with this (MetaMask) dialog:

Congratulations! You have successfully acquired one whole TST token in your MetaMask wallet on the Ropsten test net.

Phew. Those were a lot of steps. Thank you for hanging in there. Now that we have a wallet on a test net loaded up with both test ether and a test ERC-20 token, we can turn our attention to Delphi.

Delphereum

Clone Delphereum and the dependencies. Launch Delphi. Start a new project. Please make sure the dependencies are in your search path.

What we will do is create a simple Hello World program that will transfer 0.001 TST from 0x29565d2B25a0D40E1245fa3eC8b7D9d007941aBe to 0xaDDcedc01C1070bCE0aF7eb853845e7811EB649C.

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 will need to change the Infura URL in the above example. If you don’t know what Infura is or does, please refer to this article.

Run your newly created Delphi project (F9) and click the Button component. Your program should respond with a transaction hash:

You can now look up this transaction hash on etherscan. If there weren’t any errors, then you should see a successful transaction:

Congratulations! You have successfully transferred an ERC-20 token with Delphi.

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