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.

Author

Sam Blackshear

Co-Founder, CTO, Mysten Labs, Creator of Move Language

CTO of Mysten Labs and inventor of Move language. Former Principal Engineer at Meta.

Read more like this

Enhancing Blockchain Security with Multi-Signature Wallets

Desig Labs multi-signature technology makes it possible to create blockchain wallets requiring any number of keys to open.

8min read
Sui Foundation

SuiNS Token Utility: Empowering Community Governance and Decentralization

NS token utility will initially be used for purchasing names and governance but will evolve as SuiNS does.

2min read
SuiNS

Adapting AMMs for DeepBook

An automated market maker leveraging DeepBook would benefit from an existing market and asset value support.

2min read
Sui Foundation

The Future of Payments Is Programmable

Each generation of payments infrastructure improved speed. But the friction between systems was never solved. Until now.

5min read
Tal Tchwella

All About Closed-Loop Tokens

Enforcing specific rules and policies on token usage ensures greater security, compliance, and tailored functionality for specific app needs.

3min read
Sam Blackshear

eSui Dollar Now Live on Sui as DeepBook Becomes DeFi’s Premier Destination for Passive & Active Rewards

eSui Dollar (suiUSDe) is now live on Sui, bringing synthetic dollars to DeepBook Margin and unlocking new programmable DeFi strategies

3min read
Sui Foundation