BLOG — Use cases

556 days ago

Automating Aavegotchi's Token Buybacks with Gelato's Web3 Functions

Aavegotchi is a unique DeFi-enabled crypto collectibles game, allowing players to stake Non-Fungible Token (NFT) avatars known as Aavegotchis with interest-generating tokens.

Aavegotchi's native token is $GHST, this serves as the base ecosystem currency for purchasing on-chain Aavegotchi assets and enables players to stake and participate in AavegotchiDAO governance. To maintain the GHST token value, Aavegotchi leverages Gelato's Web3 Functions for automated token buybacks.

Token Buybacks

The Web3 Function interacts with the GHSTAutoBuy smart contract's buyGHST function to periodically swap USDC for GHST.

Additionally, it employs the ParaSwap API to fetch optimal swap rates and execute transactions, purchasing GHST tokens with USDC.

Gelato x Aavegotchi Integration

The Web3 Function acts as a bridge between the GHSTAutoBuy smart contract and the ParaSwap API, ensuring a seamless buyback mechanism.

It communicates with the GHSTAutoBuy smart contract to fetch the necessary parameters and verifies whether the buyback can be executed.

try {
  buybackContract = new Contract(BUYBACK_ADDRESS, AUTOBUY_ABI, provider);
  mySlippage = await buybackContract.slippage();
  mySlippage = mySlippage / 100;
  buybackAmount = await buybackContract.buyAmount();
  canBuy = await buybackContract.canBuy();
  if (!canBuy) {
  }
} catch (err) {
  return { canExec: false, message: `RPC call failed` };
}

Once the conditions for a buyback are met, such as sufficient time passing since the last transaction, the Web3 Function proceeds with the token swap.

The Web3 Function identifies the tokens involved in the swap and the amount, then creates a swapper object to communicate with the ParaSwap API.

const srcToken = getToken(srcTokenSymbol, networkID);
const destToken = getToken(destTokenSymbol, networkID);

It fetches the best rate for the token pair and calculates the minimum destination amount considering user-defined slippage tolerance.

const ps = createSwapper(networkID, API_URL);
const priceRoute = await ps.getRate({
  srcToken,
  destToken,
  srcAmount
});
const minAmount = new BigNumber(priceRoute.destAmount).times(1 - slippage / 100).toFixed(0);

Afterwards, the buildSwap method is used to obtain a transaction object with the swap details.

const transactionRequest = await ps.buildSwap({
  srcToken,
  destToken,
  srcAmount,
  minAmount,
  priceRoute,
  ...rest
});

This object is utilized to execute the swap, completing the buyback process.

return {
  canExec: true,
  callData: buybackContract.interface.encodeFunctionData("buyGHST", [tx.data])
};

The integration of Aavegotchi contract, Gelato Web3 Function, and ParaSwap API enables the GHSTAutoBuy smart contract to efficiently conduct GHST token buybacks, benefiting the Aavegotchi ecosystem and users.

About Gelato's Web3 Functions

Web3 Functions provide an innovative solution for developers to create serverless, decentralized applications with ease. They enable seamless integration of smart contracts with off-chain data, bridging the gap between on-chain and off-chain worlds.

By leveraging Web3 Functions, developers can build robust, scalable, and decentralized web3 applications, supported by a reliable and resilient infrastructure.

About Gelato

Gelato is a Web3 Cloud Platform empowering developers to create automated, gasless, and off-chain-aware Layer 2 chains and smart contracts. Over 400 web3 projects rely on Gelato for years to facilitate millions of transactions in DeFi, NFTs, and gaming.

  • Gelato RaaS: Deploy your own tailor-made ZK or OP L2 chains in a single click with native Account Abstraction and all Gelato middleware baked in.

  • Web3 Functions: Connect your smart contracts to off-chain data & computation by running decentralized cloud functions.

  • Automate: Automate your smart contracts by executing transactions automatically in a reliable, developer-friendly & decentralized manner.

  • Relay: Give your users access to reliable, robust, and scalable gasless transactions via a simple-to-use API.

  • Account Abstraction SDK: Gelato has partnered with Safe, to build a fully-fledged Account Abstraction SDK, combining Gelato's industry's best gasless transaction capabilities, with the industry's most secure smart contract wallet.

Subscribe to our newsletter and turn on your Twitter notifications to get the most recent updates about the Gelato ecosystem! If you are interested in being part of the Gelato team and building the future of the Internet browse the open positions and apply here.