Swap one token for another in Delphi

Let’s get started with a simple token swap.

swap is an easy access function that will make a trade between two tokens. It expects the following parameters:

  1. owner is the owner of the tokens we are sending to the pool.
  2. kind is the type of swap we want to perform — either (a) GivenIn or (b) GivenOut
  3. assetIn is the address of the token which we are sending to the pool.
  4. assetOut is the address of the token which we will receive in return.
  5. amount is the amount of tokens we (a) are sending to the pool, or (b) want to receive from the pool.
  6. limit is the “other amount” aka (a) minimum amount of tokens to receive, or (b) maximum amount of tokens to send.
  7. deadline is a Unix timestamp. Your transaction will revert if it is still pending after this epoch.

Watching trade history

Delphereum includes an easy access function for Balancer events named listen. You will need to pass a web3 client (indicating what chain you want to connect to) and a closure that Delphereum will call back into every time a trade happened on the Balancer protocol. Here is an example:

Demo

Should you be interested in a complete demo project, then here is a repo for you, including binaries for Windows and macOS. Everyone is encouraged to read the code and fork the demo project if you want.

Acknowledgement

The Balancer implementation for the Delphi programming language would not have been made possible without an awesome grant from Balancer DAO.

Disclaimer

Delphereum and the Balancer demo app are provided free of charge. There is no warranty and no independent audit has been or will be commissioned. You are encouraged to read the code and decide for yourself whether it is secure. The authors do not assume any responsibility for bugs, vulnerabilities, or any other technical defects. Use at your own risk.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Stefan

Stefan

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