How it Works

About Off-chain vs On-chain

The Nexera-FI Development Kit is structured around a combination of off-chain and on-chain components. This architecture ensures that off-chain and on-chain components work together to deliver a powerful yet easy-to-understand integration.

The SDK can be used in off-chain for handling complex decision-making and strategy formulation, while the on-chain components execute these strategies reliably and securely in the blockchain environment.

Off-Chain Components

Off-chain components refer to the elements that operate outside the blockchain. Generally speaking:

  1. The conceptual strategy itself (including triggers and the actions to be executed).
  2. The actual code implementing said strategy through Nexera-Fi SDK.
  3. The data input for the strategy (most likely a live data feed).
  4. The Executor or Transaction Relayer, in charge of sending the transactions to the Orchestrator contract, according to the strategy.
backend

For example: If at a given point in time t0, your DeFi strategy needs to swap a native token on Ethereum, and then a withdraw, provided that you have properly configured your instance, you can make this call through the SDK:

// Execute Swap action
await OrchestratorInstance.execute(
    [{
        adapter: NativeAdapter.address,
        op: Operation.SWAP,
        send: out,
        minReceive: expectedIn,
        extraData: "0x"
    }],
    {nonce: nonce_}
);
 
// Withdraw specified assets from the Orchestrator and transfer them to a designated receiver
await OrchestratorInstance.withdraw(WETH,
        totalReceived[0][0].amount,
        wallet_signer.address,
        {nonce: nonce_})

On-Chain Components

The on-chain components are Smart Contracts deployed to the blockchain and are responsible for the execution of transactions, handling of assets, and interaction with various DeFi protocols. Smart contracts are the backbone of the on-chain architecture. They execute the actions decided by the off-chain components, manage assets, and ensure that transactions are completed according to the predefined rules and conditions.

Smart Contracts Diagram

On-chain the strategies are managed by primitive functions, interpreted by the Orchestrator with the use of Adapters. Some examples are:

function _swap(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal virtual;
 
function _deposit(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal virtual;
 
function _withdraw(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal virtual ;
 
function _collect(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal virtual;
 
function _operate(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal virtual;
 
function _getBalances(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn
) private view returns(uint256[] memory balances)

Adapters are specialized on-chain components that translate the generic actions from the strategy into protocol-specific transactions. They enable the SDK to communicate and interact with different DeFi protocols.

For example, within ICompoundCErc20CEther (an Adapter for Compound), you'll find the native Protocol interactions:

/**
* @notice Sender redeems cTokens in exchange for a specified amount of underlying asset
* @dev Accrues interest whether or not the operation succeeds, unless reverted
* @param redeemAmount The amount of underlying to redeem
* @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
*/
function redeemUnderlying(uint redeemAmount) external returns (uint);

When the on-chain strategy Orchestrator receives the set of actions from the off-chain strategy and orchestrates their execution through the Adapters, it acts as a relayer from actions to protocols. You can check more examples here.

On this page