Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

11 Tutorials For Working With Ethereum From Delphi

11 Tutorials For Working With Ethereum From Delphi

Ethereum is used to create decentralized applications that utilize the benefits of cryptocurrency and blockchain technology. These decentralized applications (dapps) can be trustworthy, which means that once they are deployed to the Ethereum network, they will always run as programmed. They can control and regulate digital assets in order to create new kinds of financial applications. They can be decentralized in a way that no single entity or person controls them and are nearly impossible to censor.

In this blog post, we’ll look at some of the most popular and effective tutorials that can help you adapt to a quick hands-on for working with Ethereum from IDE Software: Delphi.

How can I connect Delphi to a local (in-memory) blockchain?

This simple tutorial primarily focuses on connecting Delphi to a local (in-memory) blockchain using Delphereum, which is a Delphi interface to the Ethereum blockchain. 

There are a few prerequisites and GitHub repositories/libraries that are required before you actually move on to using Delphereum. After that, you will be required to download and start a Ganache instance to run your personal Ethereum blockchain.

Once Ganache is up and running, you can move ahead and start a new Delphi project. For simplicity in understanding, the tutorial uses an easy approach of just connecting to an Ethereum blockchain and retrieve the account balance. 

const
  URL = 'http://127.0.0.1:7545';
begin
  web3.eth.getBalance(TWeb3.Create(URL), Edit1.Text, procedure(qty: BigInteger; err: IError)
  begin
    TThread.Synchronize(nil, procedure
    begin
      if Assigned(err) then
        MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
      else
        ShowMessage(web3.eth.utils.fromWei(qty, web3.eth.utils.ether) + ' ether');
    end);
  end);
end;

For a detailed and descriptive understanding, check out the full-length tutorial here

How can I connect Delphi to the Ethereum main net?

The previous tutorial focused on connecting Delphi with local (in-memory) blockchain. In this tutorial, we take a step forward and connect Delphi to Ethereum main net. 

To connect to the Ethereum main net, you will need to sign up for a free account on Infura to get a project ID.

const
  URL = '<YOUR_INFURA_PROJECT_ENDPOINT>';
begin
  web3.eth.getBalance(TWeb3.Create(URL), Edit1.Text, procedure(qty: BigInteger; err: IError)
  begin
    TThread.Synchronize(nil, procedure
    begin
      if Assigned(err) then
        MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
      else
        ShowMessage(web3.eth.utils.fromWei(qty, web3.eth.utils.ether) + ' ether');
    end);
  end);
end;

Notice that in the code above, we have replaced the local Ganache blockchain URL with the Ethereum main net link that we retrieved from Infura. 

For a detailed and descriptive understanding, check out the full-length tutorial here.

Can I connect Delphi to smart contracts?

In this tutorial, the idea is to explore how can you connect your Delphi applications with the deployed Ethereum smart contracts. The tutorial uses BNB’s smart contract and synchronizes the execution state with the main thread for a seamless connection. 

var
  Client: TWeb3;
const
  URL = 'https://mainnet.infura.io/v3/your-project-id';
begin
  Client := TWeb3.Create(URL);
  web3.eth.call(Client, '0xB8c77482e45F1F44dE1745F52C74426C631bDD52', 'symbol()', [], procedure(tup: TTuple; err: IError)
  var
    Symbol: string;
  begin
    Symbol := tup.ToString;
    web3.eth.call(Client, '0xB8c77482e45F1F44dE1745F52C74426C631bDD52', 'totalSupply()', [], procedure(const str: string; err: IError)
    begin
      TThread.Synchronize(nil, procedure
      begin
        if Assigned(err) then
          MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
        else
          ShowMessage(web3.eth.utils.fromWei(str, web3.eth.utils.ether) + ' ' + Symbol);
      end);
    end);
  end);
end;

For a detailed and descriptive understanding, check out the full-length tutorial here.

What is an easy way to generate an Ethereum-signed message signature in Delphi?

This tutorial talks about signing messages completely off-chain in Delphi using Delphereum. Starting right from generating a signature and gradually moving towards validating it, the tutorial provides an end-to-end guide on generating cryptographic signatures in Delphi. 

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(
    web3.eth.sign(
      'b5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7', 'Hello, World!'
    )
  );
end;

For a detailed and descriptive understanding, check out the full-length tutorial here.

Is it possible to transfer ether with Delphi?

This tutorial is a perfect guide on knowing how to send ethers over the Ethereum blockchain network. For demonstration and simplicity purposes, the tutorial uses Ganache as an example to set a local blockchain Ethereum network. The sendTransaction() function of Delphereum allows you to send ethers across the network. 

procedure TForm1.Button1Click(Sender: TObject);
begin
  web3.eth.tx.sendTransaction(
    TWeb3.Create('http://127.0.0.1:7545'),                              // Ganache
    '24622d680bb9d6d80c4d3fb4b4818e738de64b521948b1b1c85616eeeda54ee1', // from private key
    '0xaDDcedc01C1070bCE0aF7eb853845e7811EB649C',                       // to public key
    web3.eth.utils.toWei('0.01', ether),                                // 0.01 ether
    procedure(tx: TTxHash; err: IError)
    begin
      TThread.Synchronize(nil, procedure
      begin
        if Assigned(err) then
          MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
        else
          MessageDlg(string(tx), TMsgDlgType.mtInformation, [TMsgDlgBtn.mbOK], 0);
      end);
    end
  );
end;

For a detailed and descriptive understanding, check out the full-length tutorial here.

How can I make a program to transfer ERC-20 tokens with Delphi?

This tutorial focuses on providing a simple step-by-step guide for creating a program to transfer ERC-20 tokens with Delphi using MetaMask, TST, Infura, and Delphereum. Using the Transfer() function, you can easily send the ERC-20 tokens. 

var
  ERC20: TERC20;
begin
  ERC20 := TERC20.Create(TWeb3.Create(
    Ropsten,                                         // Ropsten test net
    'https://ropsten.infura.io/v3/your-project-id'), // RPC access to Ethereum
    '0x722dd3F80BAC40c951b51BdD28Dd19d435762180');   // TST smart contract address
  try
    ERC20.Transfer(
      '24622d680bb9d6d80c4d3fb4b4818e738de64b521948b1b1c85616eeeda54ee1', // from private key
      '0xaDDcedc01C1070bCE0aF7eb853845e7811EB649C',                       // to public key
      1000000000000000,                                                   // 0.001 TST
      procedure(rcpt: ITxReceipt; err: IError)
      begin
        TThread.Synchronize(nil, procedure
        begin
          if Assigned(err) then
            MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
          else
            MessageDlg(string(rcpt.txHash), TMsgDlgType.mtInformation, [TMsgDlgBtn.mbOK], 0);
        end);
      end);
  finally
    ERC20.Free;
  end;
end;

For a detailed and descriptive understanding, check out the full-length tutorial here.

How do Delphi and Ethereum Name Service (ENS) operate?

This tutorial focuses on providing assistance with meaningful address names that are easy to remember rather than long cryptic values that are time-consuming and prone to errors. Using the Ethereum Name Service, you can easily translate human-readable names to Ethereum addresses. ENS is secure and decentralized and eliminates the need to remember or type long addresses. 

TAddress.New(TWeb3.Create(
  Mainnet,                                         // Ethereum main net
  'https://mainnet.infura.io/v3/your-project-id'), // RPC access to Ethereum
  'thecoinoffering.eth',                           // Ethereum domain name
  procedure(addr: TAddress; err: IError)
  begin
    TThread.Synchronize(nil, procedure
    begin
      if Assigned(err) then
        MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
      else
        ShowMessage(addr);
    end);
  end);

For a detailed and descriptive understanding, check out the full-length tutorial here.

What is the quickest way to create a smart contract with Delphi?

The tutorial about creating a smart contract using Delphi is divided into two parts. The first part talks about creating a smart contract and then connecting to it using the Delphereum interface. It also provides a guide to set up MetaMask and Remix

web3.eth.call(TWeb3.Create(
    Ropsten,                                         // Ropsten test net
    'https://ropsten.infura.io/v3/your-project-id'), // RPC access to Ethereum
    '0x0a25cad45f87973177d524f3152860a17dfbe30c',    // smart contract address
    'myFirstHelloWorld()', [], procedure(tup: TTuple; err: IError)
  begin
    TThread.Synchronize(nil, procedure
    begin
      if Assigned(err) then
        MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
      else
        ShowMessage(tup.ToString);
    end);
  end);

For a detailed and descriptive understanding of the first part of this tutorial, check out the full-length tutorial here.

The second part takes a level-up and talks about both reading and writing data to the blockchain network. 

// Read
web3.eth.call(Client,
  '0x94863b36b2d245cdeb3686837fb81ebd78a18086',
  'getNumber()', [], procedure(qty: BigInteger; err: IError)
begin
  TThread.Synchronize(nil, procedure
  begin
    if Assigned(err) then
      MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
    else
      ShowMessage(qty.ToString);
  end);
end);
// Write
web3.eth.write(Client, 'b5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7', // from private key
  '0x94863b36b2d245cdeb3686837fb81ebd78a18086',                       // to smart contract
  'setNumber(uint256)', [143],
  200000,                                                             // gas limit
  procedure(rcpt: ITxReceipt; err: IError) begin end);

For a detailed and descriptive understanding of the first part of this tutorial, check out the full-length tutorial here.

How to connect Delphi to Ethereum via QuikNode?

This tutorial provides an overview of connecting Delphi to the Ethereum blockchain network via QuikNode rather than creating your own Infura dependency. QuikNode is a powerful remote node service that gives you access to full-node without you running one yourself. Delphereum interface is again a prominent choice of adoption here as well. 

var
  client: TWeb3;
begin
  client := TWeb3.Create('https://<your-node-name>.quiknode.pro/<your-token>/');
  web3.eth.blockNumber(client, procedure(num: BigInteger; err: IError)
  begin
    TThread.Synchronize(nil, procedure
    begin
      if Assigned(err) then
        MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
      else
        MessageDlg(num.ToString, TMsgDlgType.mtInformation, [TMsgDlgBtn.mbOK], 0);
    end);
  end);
end;

For a detailed and descriptive understanding, check out the full-length tutorial here.

What is an easy way to monitor Ethereum mempool using Delphi?

In this tutorial, the primary idea is to study Ethereum mempool and how you can monitor it from a Delphi application. It talks about Blocknative and subscription to its mempool events that will allow you to seamlessly monitor the network. 

var
  Mempool: IMempool;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Mempool := TSgcMempool.Subscribe(
    Mainnet,                                      // ethereum main net
    'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',       // your blocknative API key
    '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', // address to watch
    procedure(event: TJsonObject; err: IError)    // continuous events (or a blocknative error)
    var
      tx: TJsonObject;
    begin
      tx := web3.eth.blocknative.mempool.getTransaction(event);
      if Assigned(tx) then
        TThread.Synchronize(nil, procedure
        begin
          Memo1.Lines.Add(tx.ToString);
        end);
    end,
    procedure(err: IError) // non-blocknative-error handler (probably a socket error)
    begin
      TThread.Synchronize(nil, procedure
      begin
        MessageDlg(err.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0);
      end);
    end,
    procedure
    begin
      // connection closed
    end);
end;

For a detailed, descriptive, and step-by-step understanding, check out the full-length tutorial here.

For more information and prerequisite knowledge for working with Ethereum from Delphi, check out this developer guide by Ethereum


Try these examples for yourself – download a free trial of RAD Studio today!

Exit mobile version