DefiTxBase.sol

The DefiTxBase contract is an abstract Solidity smart contract that implements a common interface and provides common helper functions for executing decentralized finance (DeFi) transactions within a decentralized application (DApp) or smart contract ecosystem. It offers flexibility for passing inputs, mapping return values, and handling various types of DeFi transactions. This documentation will provide a detailed overview of the contract's components, functions, and usage.

Contract Information

  • SPDX-License-Identifier: MIT

  • Solidity Version: 0.8.10

State Variables

  • BaseRegistry public constant registry: A constant reference to the BaseRegistry contract, which is used to manage base addresses within the system.

  • BaseLogger public constant logger: A constant reference to the BaseLogger contract, which is used for logging events.

Constants and Enums

DefiTxType

An enumeration representing different types of DeFi transactions:

  • FL_DEFI_TX: Flash loan DeFi transaction.

  • STANDARD_DEFI_TX: Standard DeFi transaction.

  • FEE_DEFI_TX: Fee-based DeFi transaction.

  • CHECK_DEFI_TX: DeFi transaction for checking purposes.

  • CUSTOM_DEFI_TX: Custom DeFi transaction type.

Other Constants

  • SUB_MIN_INDEX_VALUE and SUB_MAX_INDEX_VALUE: The minimum and maximum values for subscription parameter indices (128 and 255, respectively).

  • RETURN_MIN_INDEX_VALUE and RETURN_MAX_INDEX_VALUE: The minimum and maximum values for return parameter indices (1 and 127, respectively).

  • NO_PARAM_MAPPING: A constant indicating that no parameter mapping is needed.

Events

  • DefiTxEvent(string indexed logName, bytes data): An event emitted when a DeFi transaction is executed, providing a log name and transaction data.

Abstract Functions

  1. executeDefiTx

    • Visibility: Public

    • Parameters:

      • _callData: An array of input values, each encoded as bytes.

      • _subData: An array of subscribed values, which can replace input values if specified.

      • _paramMapping: An array that specifies how return and subscribed values are mapped in inputs.

      • _returnValues: An array of return values from DeFi transactions before, which can be injected into inputs.

    • Returns: A bytes32 value returned through the DSProxy contract, with each DeFi transaction implementing its specific return value.

    This abstract function defines the interface for executing a DeFi transaction with the provided input values.

  2. executeDefiTxDirect

    • Visibility: Public

    • Parameters:

      • _callData: An array of input values, each encoded as bytes.

    This abstract function is used to execute a single DeFi transaction directly, saving gas when only one DeFi transaction needs to be executed.

  3. defiTxType

    • Visibility: Public

    • Returns: An unsigned integer representing the type of DeFi transaction being implemented (e.g., DefiTxType.FL_DEFI_TX).

    This abstract function defines the type of DeFi transaction implemented by the contract.

  4. _parseParamUint

    • Visibility: Internal

    • Parameters:

      • _param: The original uint256 input value.

      • _mapType: Indicates the type of the input in paramMapping.

      • _subData: An array of subscription data that can replace the input value.

      • _returnValues: An array of subscription data that can replace the input value.

    • Returns: The modified uint256 value after injecting return/sub values if specified.

    This internal helper method takes a uint256 input parameter, _param, and injects return/sub values into it based on the specified mapping type, _mapType. If the mapping type indicates a return injection, the method injects the corresponding value from _returnValues. If the mapping type indicates a subscription replacement, the method injects the value from _subData. The modified value is then returned.

  5. _parseParamAddr

    • Visibility: Internal

    • Parameters:

      • _param: The original address input value.

      • _mapType: Indicates the type of the input in paramMapping.

      • _subData: An array of subscription data that can replace the input value.

      • _returnValues: An array of subscription data that can replace the input value.

    • Returns: The modified address value after injecting return/sub values if specified.

    This internal helper method takes an address input parameter, _param, and injects return/sub values into it based on the specified mapping type, _mapType. If the mapping type indicates a return injection, the method injects the corresponding value from _returnValues. If the mapping type indicates a subscription replacement, the method injects the value from _subData. The modified address value is then returned.

  6. _parseParamABytes32

    • Visibility: Internal

    • Parameters:

      • _param: The original bytes32 input value.

      • _mapType: Indicates the type of the input in paramMapping.

      • _subData: An array of subscription data that can replace the input value.

      • _returnValues: An array of subscription data that can replace the input value.

    • Returns: The modified bytes32 value after injecting return/sub values if specified.

    This internal helper method takes a bytes32 input parameter, _param, and injects return/sub values into it based on the specified mapping type, _mapType. If the mapping type indicates a return injection, the method injects the corresponding value from _returnValues. If the mapping type indicates a subscription replacement, the method injects the value from _subData. The modified bytes32 value is then returned.

  7. isReplaceable

    • Visibility: Internal

    • Parameters:

      • _type: Indicates the type of the input in paramMapping.

    • Returns: A boolean value indicating whether the paramMapping value _type indicates the need for value injection (true) or not (false).

    This internal helper method checks whether the paramMapping value _type indicates the need for value injection. If _type is not equal to NO_PARAM_MAPPING, it returns true, indicating that value injection is required. Otherwise, it returns false.

  8. isReturnInjection

    • Visibility: Internal

    • Parameters:

      • _type: Indicates the type of the input in paramMapping.

    • Returns: A boolean value indicating whether the paramMapping value _type is in the return value range (true) or not (false).

    This internal helper method checks whether the paramMapping value _type is within the return value range. If _type is within the range, it returns true, indicating that it is a return value injection. Otherwise, it returns false.

  9. getReturnIndex

    • Visibility: Internal

    • Parameters:

      • _type: Indicates the type of the input in paramMapping.

    • Returns: An unsigned integer representing the index in the return array value corresponding to the paramMapping value _type.

    This internal helper method transforms the paramMapping value _type into an index in the return array value. It ensures that _type is within the valid return index range before performing the transformation and returning the index.

  10. getSubIndex

    • Visibility: Internal

    • Parameters:

      • _type: Indicates the type of the input in paramMapping.

    • Returns: An unsigned integer representing the index in the sub-array value corresponding to the paramMapping value _type.

    This internal helper method transforms the paramMapping value _type into an index in the sub-array value. It ensures that _type is within the valid subscription index range before performing the transformation and returning the index.

    These helper methods are crucial for handling parameter mapping, value injection, and ensuring that the correct values are used in DeFi transactions based on the specified logic. They contribute to the flexibility and functionality of the DefiTxBase contract, making it suitable for various types of DeFi transactions within a smart contract ecosystem.

Last updated