BLOG — Use cases

324 days ago

Unleash DeFi Automation: Opium & Gelato's Web3 Functions Integration

Opium Protocol is a decentralized platform that allows users to create and trade customized financial products without relying on third parties.

The platform simplifies decentralized derivatives trading through Opium pools, where traders stake their funds. Opium Protocol has integrated Gelato's Web3 Functions to monitor and execute direct deposit and withdrawal availability in the pools, ensuring seamless transactions.

Automating Opium's Functions

Opium Protocol uses Gelato's Web3 Functions to automate smart contract functions related to scheduled deposits and withdrawals. These functions include:

DepositScheduler

Direct deposits into the pool are only available for a brief period during the Staking phase. To address this, users can schedule deposits for the next staking phase by locking collateral in the DepositScheduler contract. Gelato's Web3 Functions automatically execute these scheduled deposits when direct deposits become available.

WithdrawalScheduler

Similarly, users can schedule withdrawals for the next staking phase by locking their LP tokens in the WithdrawalScheduler. Gelato's Web3 Functions ensure the execution of these scheduled withdrawals when direct withdrawals are available.

Integrations

The integration begins by harnessing the power of the Opium subgraph to gather all scheduled deposits and withdrawals from their respective Scheduler contracts. This ensures every pending transaction is tracked and included in the automation process.

let deposits = await opiumSubgraph.getPendingDeposits();
let withdrawals = await opiumSubgraph.getPendingWithdrawals();

Then, the current block time is compared to the pool's staking phase parameters to ascertain whether the staking phase is active, which is an essential condition for scheduled deposits and withdrawals.

let currentBlockTime = await web3.eth.getBlock('latest').timestamp;
let isActive = currentBlockTime >= pool.stakingPhase.start && currentBlockTime <= pool.stakingPhase.end;

Once the staking phase is confirmed as active and the scheduled amount exceeds the reserve coefficient, the Gelato Web3 Function springs into action. It generates the necessary calldata to execute the scheduled deposits or withdrawals.

if (isActive && (scheduledDepositAmount > reserveCoefficient)) {
    let depositCallData = gelatoWeb3.generateCallData('deposit', scheduledDepositAmount);
} else if (isActive && (scheduledWithdrawalAmount > reserveCoefficient)) {
    let withdrawalCallData = gelatoWeb3.generateCallData('withdraw', scheduledWithdrawalAmount);
}

In a strategic move to optimize gas fees and boost efficiency, this generated calldata is dispatched to the Opium Multicall contract's aggregate function. This function collates multiple transactions, facilitating a smooth transaction process.

let multicallData = [depositCallData, withdrawalCallData];
let result = await opiumMulticall.aggregate(multicallData);

This deep integration enables users to effortlessly manage their deposits and withdrawals within the Opium Protocol, all while taking advantage of the automation benefits offered by Gelato's Web3 Functions.

Join the revolution!

Gelato's 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.

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 test Web3 Functions!