BLOG — Use cases

26 days ago

Aavegotchi X Gelato 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.

Get Started in Minutes!

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, use this Hardhat template

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