Arbitraging thUSD’s Stability Pool for Fun and Profit

Yaron Velner
B.Protocol
Published in
3 min readApr 2, 2024

Threshold USD integrated B.Protocol’s Backstop Automated Market Maker (B.AMM) to manage its thUSD stability pool deposits. The motivation for the integration is to increases the stability pool capital efficiency and to support higher liquidation volume over time (e.g., see here).

At its core, the process relies on arbitrage bots who supply thUSD in return for a discounted ETH or tBTC.

In this post, we explain how bots could arbitrage the B.AMM, and present an open source smart contract to facilitate such an arbitrage.

Buying discounted ETH

Every liquidation of ETH collateral takes some thUSD from the stability pool, and gives some ETH in return. The B.AMM offers the ETH for sale, in return to thUSD. The ETH is offered with a varying discount over chainlink market price, where the discount depends on the quantity of ETH that is available for sale. The bigger the quantity is, the higher the discount. The reader can find more technical details on the discount formula here.

How to arbitrage?

B.AMM address (Ethereum mainnet):

Price query function: getSwapCollateralAmount(uint256 thusdQty)

Swap function: swap(uint256 thusdAmount, uint256 minCollateralReturn, address payable dest)

Arbitrage bot smart contract reference:

  • Deployed address: 0xfbea9aaa9a822aedd429fdab3099a2dba942f196
  • Arbitrage function: swap(uint ethQty, address bamm, address payable profitReceiver)
  • ethQty: the amount of ETH to buy in the arbitrage
  • bamm: the B.AMM address that is used in the arbitrage
  • profitReceiver: the destination address for the profit.

A potential backend implementation would be to call the swap function offchain with various sizes, and to execute a tx when it is not expected to revert. The backend can also be implemented with Gelato.

Buying discounted tBTC

Every liquidation of tBTC collateral takes some thUSD from the stability pool, and gives some tBTC in return. The B.AMM offers the tBTC for sale, in return to thUSD. The tBTC is offered with a varying discount over chainlink market price, where the discount depends on the quantity of tBTC that is available for sale. The bigger the quantity is, the higher the discount. The reader can find more technical details on the discount formula here.

How to arbitrage?

B.AMM address (Ethereum mainnet):

Price query function: getSwapCollateralAmount(uint256 thusdQty)

Swap function: swap(uint256 thusdAmount, uint256 minCollateralReturn, address payable dest)

Arbitrage bot smart contract reference:

  • Deployed address: 0x067a0209c6e03f9a077d967e48b410b7bf811ec9
  • Arbitrage function: swap(uint btcQty, address bamm, address payable profitReceiver)
  • btcQty: the amount of WBTC to buy in the arbitrage. 8 decimals should be used to encode the qty. The WBTC is later exchanged to tBTC.
  • bamm: the B.AMM address that is used in the arbitrage
  • profitReceiver: the destination address for the profit.

A potential backend implementation would be to call the swap function offchain with various sizes, and to execute a tx when it is not expected to revert. The backend can also be implemented with Gelato.

Disclaimer

The reference bot code comes with no warranty. Using the bots does not require any funds, however gas fees could be lost in the event of unsuccessful arbitrage. Users should set reasonable gas prices when using the bots.

About B.Protocol

B.Protocol has been building open-source protocols and infrastructure for risk mitigation and assessment for the DeFi ecosystem since 2020. Through our research arm, RiskDAO, and its novel risk framework, we have supported over a dozen DeFi protocols with risk analysis, research, audits, and monitoring. Our Risk Oracle, together with the SmartLTV formula, and the Risk Level Index automate the process of setting risk parameters for lending platforms in a transparent way, building the next generation of DeFi risk management infrastructure.

Website | Twitter | Discord | Medium | Github

--

--