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

Gelato Team

Jun 2, 2023

Opium Protocol is a decentralized platform that allows users to create and trade customized financial products without intermediaries. It simplifies decentralized derivatives trading through Opium pools, where traders stake funds. By integrating Gelato's Web3 Functions, Opium automates monitoring and execution of deposits and withdrawals, ensuring seamless transactions.

Automating Opium's Functions

Opium Protocol leverages Gelato's Web3 Functions to automate smart contract functions tied to scheduled deposits and withdrawals:

DepositScheduler

Direct deposits into pools are only available for short periods during staking phases. To address this, users can schedule deposits for the next staking phase by locking collateral in the DepositScheduler contract. Gelato Web3 Functions automatically execute these deposits once the staking window opens.

WithdrawalScheduler

Likewise, users can schedule withdrawals by locking LP tokens in the WithdrawalScheduler. Gelato Web3 Functions ensure these withdrawals execute automatically during the appropriate staking phase window.

Integrations

The integration process begins by pulling data from the Opium subgraph to track all pending deposits and withdrawals:

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

The system then compares the current block timestamp against pool staking parameters to determine if deposits or withdrawals can be processed:

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

If the staking phase is active and scheduled amounts exceed reserve requirements, Gelato Web3 Functions generate calldata to execute deposits or withdrawals:

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

To optimize gas and improve efficiency, this calldata is sent to the Opium Multicall contract, aggregating multiple actions in one transaction:

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

This integration allows users to manage deposits and withdrawals seamlessly, with automation handled by Gelato's Web3 Functions.

Join the Revolution!

Gelato's Web3 Functions empower developers to create serverless, decentralized applications that connect smart contracts with off-chain data, bridging on-chain and off-chain worlds effortlessly.

Join the community on Discord.

Web3 Functions are available in private beta. Learn more in the documentation or start with this Hardhat template.

Apply here to test 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.