519 days ago
Automating Abracadabra Yield Strategies with Gelato Web3 Functions
Introduction
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), a stablecoin tied to the US dollar. The real magic of Abracadabra comes from the way it allows users to unlock the value in their assets and use it in different ways, expanding their opportunities in the DeFi universe.
This magic is powered by a combination of Abracadabra's smart strategies and the help of Gelato's Web3 Functions, which automate these strategies for maximum efficiency.
Gelato Web3 Functions enables smart contract developers to securely fetch data from any API and let you run custom logic to reliably push data on-chain.
Strategies automated by Gelato Web3 Functions
Let's discuss the GMX strategy: Abracadabra uses Gelato Web3 Functions to create a fully automated process that manages reward tokens efficiently using Gelato Web3 Functions.Let’s dive in!
Time Verification:
The function begins by checking the time that has passed since the strategy was last executed. This is performed by fetching the timestamp of the last execution from the contract, and comparing it to the current time. If the set interval has not passed, the function will stop execution. Gelato's Web3 Functions provide a provider that allows your 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 that have been accumulated in the contract. This is done by invoking a function on the contract, which returns the total balance of reward tokens after claiming.
let rewardTokenBalanceResponse = await contract.totalRewardsBalanceAfterClaiming();
let rewardTokenBalance = rewardTokenBalanceResponse;
Reward Token Swap Quote:
If there are reward tokens to be collected (i.e., if the balance is greater than zero), the function fetches a quote from the 0x API to convert the reward tokens into a desired output token. The API requires the address of the reward token, the desired output token, and the amount of reward token to swap. In case API requires api keys, this can be done with secrets. For more info click 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 then calculates the minimum acceptable amount of output token that it is willing to receive from the swap. This is to ensure that the swap will not proceed if the slippage is too high, thereby protecting the user from potential price impacts.
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 the data for the run function, which will perform the token swap operation when it is executed.
const iface = new Interface(["function run(uint256,bytes) external"])
let callData = iface.encodeFunctionData("run", [minAmountOut.toString(), quoteResObj.data]);
Following the GMX Strategy, Abracadabra also employs three additional strategies, automated by Gelato's Web3 Functions. These include the LUSD Strategy, the Interest Strategy, and the Solidly Strategy. Each of these contributes to optimizing yield farming, managing liquidity and maximizing user returns in the Abracadabra ecosystem.
Dive Deeper with Gelato
Web3 Functions provide an innovative solution for developers to create serverless, decentralized applications with ease.
Join our community and developer discussion on Discord.
Web3 Functions are available today in private beta. For more information, please check out the Web3 Functions documentation. To learn how to write, test, and deploy your own Web3 Functions, check out our in-depth tutorial.
Apply here to be one of the first to test Gelato's Web3 Functions!