Powering a DeFi Bot with Programmable Transaction Blocks

Programmable Transaction Blocks in Sui Move are a powerful means of consolidating instructions to run efficiently while incurring minimal gas fees.

Powering a DeFi Bot with Programmable Transaction Blocks

A basic guideline for people choosing investment funds is to look for those with the lowest management fees. Capital should be working, not going towards fees. Similarly, Mysten Lab's Capy Trading Bot leverages Programmable Transaction Blocks (PTBs) to focus capital on trading rather than gas fees and enable strategies with complex inputs.

Mysten Labs created Capy Trader as a reference project with an open source repo. The bot monitors token prices then automatically executes trading strategies designed to accumulate more tokens. Strategies include triangular arbitrage, trend following, and market difference exploitation.  

Capy Trading Bot's success requires that it frequently update its monitored token prices and potentially initiate many trades in a short period of time. On most blockchains, this amount of activity would quickly spend any token gains in gas fees. PTBs, however, consolidate many actions into a single transaction, keeping gas fees constrained and maximizing gains.

Executing instructions

Mysten Labs released Capy Trading Bot code under the open source MIT license, and does not provide a compiled example. Developers may customize and improve the code for their own purposes and even include it in commercial apps.

The existing code supports trading on three Sui DEXes in multiple token types. Capy Trading Bot monitors feeds from the CryptoCurrency eXchange Trading Library (CCTX) to get the latest token prices from Binance, although developers may customize the code for other feeds.

Graph showing various token rates on Cetus
Capy Trading Bot monitors token trading rates to find the best opportunities to execute trades.

Capy Trading Bot operates autonomously based on whichever trading strategy its developer sets. The repo offers three strategies:

  • Triangular Arbitrage: The bot compares token exchange rates in swap pools across different DEXes, looking for differentials where it can essentially buy low and sell high.
  • Ride the Trend: The bot looks at short and long term moving averages for a single token, determining when the rate is most likely to increase.
  • Market Difference: The bot compares the relative price of a token pair in a swap pool with the price of the same pair on an exchange, looking for opportunities to go long or short a token.

These strategies represent very common techniques traders use in all manner of exchanges to make gains. The triangular arbitrage strategy in particular benefits from PTBs, as it needs to monitor exchange rates in three swap pools simultaneously. If these swap pools were compared in different transactions, rather than in one PTB, the exchange rate might change, erasing any gains that might be made in a more timely trade.

In addition to these three strategies, Capy Trading Bot lets developers create and implement their own custom strategies. The basis for any strategy involves designing a formula based on data from token trading rates to trigger an event that will trade tokens.

The initial lines of the capybot.ts, the main Capy Bot Trader source file, imports the TransactionBlock from "@mysten/sui.js".

import {
	...
	TransactionBlock,
} from "@mysten/sui.js";

A subsequent call to TransactionBlock defines the conditions and specific trades it will make. Ultimately the code executes TransactionBlock, making the trade based on the strategy the bot was using.

In this use of a PTB, the bot monitors its data sources and waits for the condition that will trigger it to execute a trade. The PTB lets the bot run through multiple instructions without actually executing its transaction, and incurring a gas fee, until its conditions are met.

Efficient transactions

This DeFi use case for PTBs demonstrates their distinct efficiency in apps that need to monitor data streams and execute actions based on specific conditions. Other blockchains may require many transactions to achieve the same results, incurring extensive gas fees. PTBs consolidate instructions in such a manner as to greatly limit gas fee expenditures.

Developers coming from traditional environments will find the expenses of executing an app absurd on most blockchains in the Web3 world. Sui, on the other hand, offers developers a more efficient environment that supports more extensive apps.