Spotlight: Flash Arbitrage

0xdec4f
CORE Vault
Published in
7 min readNov 15, 2020

To make sure you have a good understanding on how to use the new feature, we need to break down the mechanics on how CORE’s Flash Arbitrage works and then take a deep dive into the smart contracts.

CORE Flash Arbitrage consists of two principals:

  • Arbitrage
  • Uniswap Flash Swaps

What is Arbitrage?

Arbitrage represents an opportunity for low risk profit from discrepancies in prices of financial instruments. While Supply and Demand is the primary driving factor behind financial markets, a change in one of these factors can affect the price of an asset. To successfully profit from such an opportunity, one must spot the differences in price. In other words, if one and the same product is traded on two markets at different prices, one may profit from arbitrage. In today’s financial markets, traders use automated trading systems that rely on algorithms to spot these discrepancies and profit before the market reacts.

What is a Flash Swap?

The second principal that CORE’s Flash Arbitrage system takes advantage of are Uniswap Flash Swaps. This feature allows the user to take out a loan from any liquidity pool on Uniswap without any costs, as long as the loan is returned at the end of the transaction. Uniswap allows two ways for this return to happen:

  • The loan is paid back with the corresponding pool / pair token
  • The loan is paid back with the original ERC20 token

Example:

You may create a Flash Swap by withdrawing temporarily the liquidity of the wBTC/ETH pool.

In the current example the price of 1 wBTC is 34 ETH.

If you withdraw 1 wBTC as a flash swap from the wBTC/ETH pool, you must end your transaction by either returning 34 Ethereum or 1 wBTC + Uniswap 0.3% fee. If you are unable to meet these ending conditions, the flash swap will fail, and the transaction will get rolled back. If one takes out a Flash Swap and utilizes an arbitrage opportunity before returning the loan, it will generate zero risk profits.

CORE Flash Arbitrage

Flash Arbitrage is build to bring a powerful and precise tool to the community. The contract uses a specifically crafted algorithm to process a wealth of data and identify opportunities across different pools. CORE’s Flash Arbitrage can be summarized in three steps:

The contract takes a Flash Swap from a pool and performs the arbitrage strategy which was previously selected. The revenue generated through the arbitrage is split between the Caller and the CORE Buyer Contract. The ratio depends on the Fee on Transfer (FoT) function being on or off for the performed strategy. When an arbitrage strategy is performed with FoT on, the max revenue split for the Caller is 10% and 90% for the CORE Buyer Contract. If FoT is turned off, the maximum revenue for the Caller is 65% while the CORE Buyer Contract receives 35%.

The CORE Buyer Contract will add rewards to specific CORE pools to keep them at a designated APY. If all pools reach this threshold, the contract instead adds liquidity to CORE pools and subsequently burns the LP token. The mint and burn of liquidity pool tokens has a compounding effect on the APY of CORE liquidity pools. It increases the liquidity and value of LP tokens while not diluting the APY.

A Closer Look inside an Arbitrage Strategy

The second step of a successful CORE Flash Arbitrage involves selecting a strategy. The image shows a strategy performed on the CORE/cBTC pool while taking a Flash Swap from the wBTC/ETH pool.

Due to the special rules in CORE’s ecosystem a Flash Arbitrage performed on a CORE pool has several positive effects. First of all, every 0.3% Uniswap Fee charged for transactions are directly contributed to the Total Value Permanently Locked (TVPL). In this example it happens between cBTC and CORE and between CORE and Ethereum. Another beneficial effect on the system is the 1% Fee on Transfer which stayed active in this strategy, generating 0.01895 CORE for the CORE/ETH LP token holders.

This Arbitrage strategy performed 3 swaps and one wrap.

  1. 0.45891154 wBTC wrapped to 0.45891154 cBTC
  2. 0.45891154 cBTC swapped to 1.895527328 CORE
  3. 1.895527328 CORE swapped to 15.526442558 ETH
  4. 15.41569512 ETH swapped to 0.45891154 wBTC

Due to the price advantage of the three swaps, a surplus of Ethereum was generated, resulting in 0.1107474 ETH being split between the Caller & CORE Buyer Contract.

Deep Dive into the Contracts 📀

CORE’s high level arbitrage suite consists of three contracts:

  • Arbitrage Executor

The Arbitrage Executor is the brain of our Flash Arbitrage, it calculates scenarios and executes the trades. This contract is closed source.

  • Arbitrage Controller

The Arbitrage Controller is responsible for adding and storing all strategies. It calls the Arbitrage Executor and then splits the profits between the Caller and the CORE Buyer Contract. It vastly simplifies the interface to the Executor.

  • Core Buyer Contract

This contract turns the arbitrage profits into CORE and transfers them directly to farmers.

No input is required to run these contracts, the loan is created by using Uniswaps Flash Swaps. This means only your gas cost is at stake. While the contract isn’t guaranteeing gas returns, you can always query the amount returned.

Overview of the Arbitrage Controller functions:

The creation of a new strategy emits an event which can be listened to and queried by.

The Arbitrage Controller is storing each strategy in a public array inside the smart contract. Each strategy has a name and an array of token0out — this is to support Uniswap so we know which way to conduct the trade in the Executor.

address[] pairs; // Array of pair addresses

This shows the array of pairs that the trade will be in.

uint256[] feeOnTransfers; //Array of fee on transfers 1% = 10

Strategies support Fee on Transfer tokens with fee on transfer array here. It allows people to input that this strategy has a fee on transfer, which signals the contract to calculate and execute the strategy correctly.

bool cBTCSupport;  // Should the algorithm check for cBTC and 
wrap/unwrap it
// Note not checking saves gas

cBTC support is determined by itself. You can not input it.

bool feeOff; // Allows for adding CORE strategies - where there is 
no fee on the executor

This allows for a FoT Off strategy. Adding strategies involving CORE and feeOff is not permitted by anyone. To avoid atypical scenarios with feeOff.

Revenue split: The revenue split between the Caller and CORE Buyer Contract is dependant on the strategy utilizing feeOff or feeOn.

Strategies that are outside of CORE reward the Caller with the majority of the profits. The ease of use and profit split encourages usage of the service. It is easier to add strategies to this contract and share the profits than having to create your own complex suite of contracts. All strategies, including new creations, are open source and can be used by everyone.

Adding New Strategies

Users can add new strategies as long as they are valid. To create a valid strategy one must input an array of pairs (comma separated list) which has a logically flow from each other.

This closes the loop by itself, no need to input CORE/wETH again since the last pair will be treated as a return pair. The controller detects if cBTC is used and sets the support automatically. It makes sure the Executor picks the correct branch to execute and save gas in case support for cBTC is not on.

addNewStrategy function is used for adding new strategies with no fee on transfers anywhere.

addNewStrategyWithFeeOnTransferTokens supports fee on transfer input, to support fee on transfer tokens all over.

numberOfStrategies function returns the number of strategies (routes of arbitrage) inside the contract which everyone can easily scan in a loop.

Querying Profits from Strategies. The executor has a view function which returns the queried profit if the strategy was executed at that exact moment.
This function should be used to scan all outstanding strategies from the previous step and calling executions immediately.

Strategy Execution

There are two paths that strategy execution can take:

  1. Skip calculating Optimal Input
  2. Calculate Optimal Input at the time of execution

Number 1 is meant for miners, which know the optimal input without calculating it on chain.

Self calculating execution just requires strategy PID. Strategies that do not automatically calculate amount input, meant for miners.

Adding Optimal Input:

A view that returns the optimal input amount, which should be inputted in the execution function below:

Both execution strategy functions ensure you get promised profit and non-opaque split numbers.

The Flash Arbitrage executor does not hold any tokens, it only swaps them, picking the best path that will lower the gas used as much as possible as well as not writing any balances in its memory to save additional gas costs.

CORE Buyer Contract

This contract receives tokens, either from the Arbitrage Controller, or any other source in general and sells them for CORE.

If a CORE pair exists for the received token, it will sell it directly to CORE. In case of not having a token pair with CORE, the contract will sell the token for Ethereum and then buy CORE in the CORE/WETH pool immediately. All CORE will be distributed to farmers.

Anyone can call this function by inserting an address for the token. This will automatically conduct a swap and distribute the token to LP stakers.

💬 Join Us!

--

--