Cross-Chain Yield Aggregation
Combining ML inference and Chain Abstraction to optimize DeFi yield across chains.
Everyone wants to earn interest on their crypto. The problem is, there are so many different places across many blockchains offering different rates, with various levels of risk. What if you could have an on-chain, automated investment manager that could optimize your yield for you? Let’s learn how to create this kind of system using the native AI and chain abstraction capabilities of Ritual.
In this tutorial, we will:
- Read yield data from multiple EVM chains via Wormhole queries
- Execute a Classical ML model to optimize yield
- Schedule periodic position rebalancing via scheduled transactions
Initial assumptions
For sake of example, assume that:
- For each of our yield sources, we have an on-chain read function that returns data including APY, liquidity, and 24H volume.
- We can black-box a function for updating our cross-chain yield positions, also executed via Wormhole queries
- We have already created a Classical ML model that consumes on-chain yield metrics and returns an allocation as output
Execute initial setup
First, we can setup our smart contract by inheriting the IScheduler
interface:
We can also upload our Classical ML model to Hugging Face or Arweave via our infernet-ml
toolkit.
Read and update yield allocations
Next, we can:
- Tap into our multi-chain yield sources to collect yield data
- Process the yield data by executing our Classical ML model
- Update our position allocations
We can assume a sample interface for yield data as follows:
Using this interface, we can proceed to build a function that chains together the Wormhole and Classical ML inference precompiles to run our desired pipeline:
As you can see above, we:
- Iterate through our yield sources, collecting their yield data
- Parse and package this data for execution by an ONNX model
- Use the returned array from the ONNX model to update our allocations
Setup rebalancing automatically
Finally, with the updateYieldAllocation()
function setup, we can easily schedule our position allocations to rebalance automatically through the use of scheduled transactions: