Automating Abracadabra Yield Strategies with Gelato Web3 Functions

Gelato Team

Jul 3, 2023

Introduction

Abracadabra illustration

Abracadabra is a decentralized finance (DeFi) tool that lets users deposit tokens, earn interest, and borrow a US dollar-pegged stablecoin called Magic Internet Money (MIM). The real magic of Abracadabra lies in how it allows users to unlock the value in their assets and use it in multiple ways, expanding opportunities in the DeFi ecosystem.

This functionality is powered by a combination of Abracadabra's smart strategies and Gelato's Web3 Functions, which automate these strategies for maximum efficiency. Gelato Web3 Functions enable smart contract developers to securely fetch data from any API and run custom logic to reliably push data on-chain.

Strategies Automated by Gelato Web3 Functions

One example is the GMX strategy: Abracadabra uses Gelato Web3 Functions to create a fully automated process that manages reward tokens efficiently. Let’s dive in!

Time Verification

The function begins by checking the elapsed time since the strategy was last executed. This is done by fetching the timestamp of the last execution from the contract and comparing it to the current time. If the required interval has not passed, the function halts. Gelato's Web3 Functions provide a provider that allows code to perform RPC calls in any EVM-compatible network.

let lastExecuted = await contract.lastExecution();
let timeNowSecBig = BigNumber.from(+timeNowSec.toFixed(0));

if (timeNowSecBig.lt(lastExecuted.add(intervalInSeconds))) {
  return { canExec: false, message: "" };
}

Checking the Reward Token Balance

The function then checks the balance of reward tokens accumulated in the contract by calling a contract function that returns the total balance of reward tokens after claiming.

let rewardTokenBalanceResponse = await contract.totalRewardsBalanceAfterClaiming();
let rewardTokenBalance = rewardTokenBalanceResponse;

Reward Token Swap Quote

If the balance is greater than zero, the function fetches a swap quote from the 0x API to convert reward tokens into a desired output token. Secrets can be used to securely handle API keys. For more info, see here.

let quoteApi = `${zeroExApiBaseUrl}/swap/v1/quote?buyToken=${outputToken}&sellToken=${rewardToken}&sellAmount=${rewardTokenBalance.toString()}`;
let quoteApiRes: any = await ky.get(quoteApi).json();
let quoteResObj = quoteApiRes;

Minimum Acceptable Output Calculation

The function calculates the minimum acceptable amount of output tokens to ensure swaps don’t proceed with excessive slippage.

let toTokenAmount = BigNumber.from(quoteResObj.buyAmount);
const minAmountOut = toTokenAmount.sub(
  toTokenAmount.mul(rewardSwappingSlippageInBips).div(BIPS)
);

Transaction Preparation

Finally, the function prepares a transaction to execute the token swap. This involves encoding data for the run function, which performs the token swap.

const iface = new Interface(["function run(uint256,bytes) external"]);
let callData = iface.encodeFunctionData("run", [minAmountOut.toString(), quoteResObj.data]);

In addition to the GMX Strategy, Abracadabra employs three other strategies automated by Gelato Web3 Functions: the LUSD Strategy, the Interest Strategy, and the Solidly Strategy. Each contributes to optimizing yield farming, managing liquidity, and maximizing user returns.

Dive Deeper with Gelato

Web3 Functions provide developers with a powerful way to create serverless, decentralized applications with ease.

Join our community and developer discussions on Discord.

Web3 Functions are available today in private beta. For more details, check out the documentation. To learn how to write, test, and deploy your own Web3 Functions, read our in-depth tutorial.

Apply here to be one of the first to test Gelato's Web3 Functions!

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.