Router

Overview

The Router.sol allows for complex transaction sequences to be executed in a single transaction. It inherits from Dispatcher.sol and IERC3156FlashBorrower. It primarily handles the execution of a sequence of commands, including flash loans within Spectra Principal Tokens.

Router Methods

execute

Handles the execution of command sequence.

function execute(
        bytes calldata commands,
        bytes[] calldata inputs
) external payable
Input ParameterTypeDescription

commands

bytes calldata

A set of concatenated commands, each 1 byte in length

inputs

bytes[] calldata

An array of byte strings containing ABI encoded inputs for each command

execute

Processes a batch of commands before a specified deadline. This method checks for deadline and then forwards the call to execute().

function execute(
        bytes calldata commands,
        bytes[] calldata inputs,
        uint256 deadline
) external payable
Input ParameterTypeDescription

commands

bytes calldata

A set of concatenated commands, each 1 byte in length

inputs

bytes[] calldata

An array of byte strings containing ABI encoded inputs for each command

deadline

uint256

The deadline by which the transaction must be executed

View Methods

previewRate

Simulates encoded commands along with provided inputs, and return the resulting rate.

function previewRate(
        bytes calldata commands,
        bytes[] calldata inputs
) external view returns (uint256 output)
Input ParameterTypeDescription

commands

bytes calldata

A set of concatenated commands, each 1 byte in length

inputs

bytes[] calldata

An array of byte strings containing ABI encoded inputs for each command

outputRate

uint256

The preview value for rate, where the rate is defined as the amount of tokens returned by the router after a sequence of operations per unit of token deposited

The following commands are not supported by previewRate():

  • FLASH_LOAN

  • CURVE_SPLIT_IBT_LIQUIDITY

  • CURVE_ADD_LIQUIDITY

  • CURVE_REMOVE_LIQUIDITY

  • CURVE_REMOVE_LIQUIDITY_ONE_COIN

previewSpotRate

Simulates encoded commands along with provided inputs, and return the resulting spot rate.

function previewSpotRate(
        bytes calldata commands,
        bytes[] calldata inputs
    ) external view returns (uint256 output)
Input ParameterTypeDescription

commands

bytes calldata

A set of concatenated commands, each 1 byte in length

inputs

bytes[] calldata

An array of byte strings containing ABI encoded inputs for each command

outputSpotRate

uint256

The preview value for spot rate, where the rate is defined as the amount of tokens returned by the router after a sequence of operations per unit of token deposited

The following commands are not supported by previewSpotRate():

  • FLASH_LOAN

  • CURVE_SPLIT_IBT_LIQUIDITY

  • CURVE_ADD_LIQUIDITY

  • CURVE_REMOVE_LIQUIDITY

  • CURVE_REMOVE_LIQUIDITY_ONE_COIN

onFlashLoan

Implements the flash loan callback, handling the loan repayment and potential shortfall from the original sender.

function onFlashLoan(
        address initiator,
        address token,
        uint256 amount,
        uint256 fee,
        bytes calldata data
    ) external returns (bytes32)
Input ParameterTypeDescription

initiator

address

The initiator of the loan.

token

address

The loan currency.

amount

uint256

The amount of tokens to borrow.

fee

uint256

The additional amount of tokens to repay.

data

bytes calldata

Arbitrary data structure, intended to contain user-defined parameters.

Return ParameterTypeDescription

bool

If successful, onFlashLoan return the keccak256 hash of “ERC3156FlashBorrower.onFlashLoan”.

Conforms to EIP-3156 standards.

Dispatcher

The Dispatcher.sol is an abstract contract that facilitates a variety of financial operations, including token transfers, swaps, and flash loans. Each command has specific input requirements and functionalities.

TRANSFER_FROM Command

Transfers the specified amount of the ERC20 token from the message sender (msgSender) to the contract itself. It's crucial for security that the transfer originates only from the message sender.

Input ParameterTypeDescription

address

address

The address of the token to transfer

value

uint256

The amount of token to transfer

TRANSFER_FROM_WITH_PERMIT Command

Transfers the specified amount of the ERC20 token from the message sender (msgSender) to the contract itself using the EIP-2612 signed approvals.

Input ParameterTypeDescription

address

address

The address of the token to transfer

value

uint256

The amount of token to transfer

deadline

uint256

The deadline for the transaction

v

uint8

Signature

r

bytes32

Signature

s

bytes32

Signature

TRANSFER Command

Transfers a specified amount of the ERC20 token from the contract to the provided recipient address. If a special value indicating the contract's entire balance is provided, it transfers the entire token balance of the contract to the recipient.

Input ParameterTypeDescription

token

address

The address of the token to transfer

value

uint256

The amount of token to transfer

recipient

address

The address of the transfer receiver

CURVE_SWAP Command

Executes a token swap using a Curve finance pool. The command involves specifying the tokens within the pool to swap, the amount to swap, and the minimum acceptable amount for the output token, ensuring slippage protection.

Input ParameterTypeDescription

pool

address

The address of the pool

i

uint256

The token index to input from the swap

j

uint256

The token index to output from the swap

amountIn

uint256

The amount of token in

minAmountOut

uint256

The minimum amount of token out (slippage protection)

recipient

address

The address of the swap recipient

DEPOSIT_ASSET_IN_IBT Command

Wraps the specified amount of an underlying ERC20 token into an ERC4626 compliant tokenized vault (ERC4626.deposit()). The operation deposits the underlying token into the 4626 wrapper and transfers the resulting wrapped tokens to the specified recipient.

Input ParameterTypeDescription

ibt

address

The address of the IBT token

assets

uint256

The amount of token to deposit

recipient

address

The address of the transfer receiver

DEPOSIT_ASSET_IN_PT Command

Deposit the specified amount of an underlying ERC20 token in the PT (deposit()).

Input ParameterTypeDescription

pt

address

The address of the PT token

assets

uint256

The amount of token to deposit

ptRecipient

address

The address of the receiver of PTs

ytRecipient

address

The address of the receiver of YTs

minShares

uint256

The minimum amount of minted shares from this deposit

DEPOSIT_IBT_IN_PT Command

Deposit the specified amount of an IBT token in the PT (depositIBT()).

Input ParameterTypeDescription

pt

address

The address of the PT token

ibts

uint256

The amount of token to deposit

ptRecipient

address

The address of the receiver of PTs

ytRecipient

address

The address of the receiver of YTs

minShares

uint256

The minimum amount of minted shares from this deposit

REDEEM_IBT_FOR_ASSET Command

Unwraps the specified amount of the ERC4626 token, converting it back into the underlying token (ERC4626.withdraw()). The unwrapped tokens are then transferred to the specified recipient.

Input ParameterTypeDescription

token

address

The address of the token

shares

uint256

The amount of shares to burn

recipient

address

The address of the transfer receiver

REDEEM_PT_FOR_ASSET Command

Redeem the specified amount of share, converting it back into the underlying token (redeem()).

Before expiry, the PT shares amount is both the PT and YT amount. After expiry, only the PT shares are burnt.

Input ParameterTypeDescription

pt

address

The address of the PT

shares

uint256

The amount of shares to burn

recipient

address

The address of the transfer receiver

minAssets

uint256

The minimum amount of asset to be returned to the user

REDEEM_PT_FOR_IBT Command

Redeem the specified amount of share, converting it back into the IBT token (redeemForIBT()).

Before expiry, the PT shares amount is both the PT and YT amount. After expiry, only the PT shares are burnt.

Input ParameterTypeDescription

pt

address

The address of the PT

shares

uint256

The amount of shares to burn

recipient

address

The address of the transfer receiver

minIbts

uint256

The minimum amount of IBT to be returned to the user

FLASH_LOAN Command

Facilitates a flash loan transaction. It enables borrowing of a specified amount of an ERC20 token from a lender, with the requirement that it is returned within the same transaction, along with any agreed-upon fees.

Input ParameterTypeDescription

lender

address

The address of the flash loan lender

token

address

The address of the token to borrow

amount

uint256

The amount of token to borrow

data

bytes

Additional data for the flash loan

ASSERT_MIN_BALANCE Command

Checks if the specified token balance of an owner's address is at least the provided minimum value. If the balance is below the minimum, the command reverts the entire transaction.

Input ParameterTypeDescription

token

address

The address of the token

owner

address

The address of the token owner to check the balance from

minValue

uin256

The minimum amount of token required

CURVE_SPLIT_IBT_LIQUIDITY Command

Split the input IBT amount into IBT for the pool and to deposit in the Principal Token. Add liquidity to the pool with the resulting PT and the split IBT amounts.

Input ParameterTypeDescription

pool

address

The address of the Curve pool

ibts

uint256

The amount of IBT to deposit

recipient

address

The address of the LP token recipient

ytRecipient

address

The address of the YT recipient

minPTShares

uint256

The minimum amount of minted PT/YT shares from the portion of IBT deposited in the PT contract.

CURVE_ADD_LIQUIDITY Command

Add liquidity to the Curve pool specifying each token amount in input and the minimum amount of LP token to mint (slippage protection).

Input ParameterTypeDescription

pool

address

The address of the Curve pool

amounts

uint256[2]

The amount of IBT/PT to deposit

min_mint_amount

uint256

The minimum amount of LP token to mint

recipient

address

The address of the LP token recipient

CURVE_REMOVE_LIQUIDITY Command

Remove liquidity from Curve pool specifying the amount lps of LP tokens to burn and the minum amount of liquidity to withdraw for each token (slippage protection).

Input ParameterTypeDescription

pool

address

The address of the Curve pool

lps

uint256

The amount of LP tokens to burn

min_amounts

uint256[2]

The minimum amount of assets to receive

recipient

address

The address of the tokens recipient

CURVE_REMOVE_LIQUIDITY_ONE_COIN Command

Remove liquidity from Curve pool by withdrawing a single coin. The user specify the amount lps of LP tokens to burn and the minimum amount of liquidity to withdraw for the chosen token (slippage protection).

Input ParameterTypeDescription

pool

address

The address of the Curve pool

lps

uint256

The amount of LP tokens to burn

i

uint256

The index of the token to withdraw from the pool

min_amount

uint256

The minimum amount of assets to receive

recipient

address

The address of the token recipient

Last updated