Bridging Smart Contract

In order to integrate your DeFi protocol, you’ll need to implement an adapter in the form of a bridging contract. For example, MockDexSingularityBridge.sol is used as the bridging contract for handling interactions between our Mock DeFi exchange and The Singularity.

Mock smart contract integration diagram

There is a hard constraint imposed that limits each DeFi interaction with the Singularity to spending up to 4 notes at once. Similarly, up to 4 notes may be created as a result of a single transaction.

Take the following MockDex.sol contract as an example:

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.20;

interface IMockDex {
    
    function getAssets() external returns (address[4] memory assets);
    function swap(uint256 amountIn, uint256 minAmount, address caller) 
        external payable returns (uint256);
}

The swap method in our example takes an amountIn qty worth of ETH in exchange for minAmount qty worth of WETH. For this interaction, a single note (containing ETH) gets spent in exchange for a single newly created note (containing WETH) which the user receives.

Mock smart contract integration diagram

When a user of your protocol wants to interact via the Singularity, they’ll invoke a method The defiCall method will be invoked by _defiCall via GeneralDefiIntegrationAssetManager.sol that subsequently calls the defiCall method in the bridging contract you write. Let’s convey this in code through the implementation of the defiCall method definition within the MockDexSingularityBridge.sol contract.

    function defiCall(
        uint256[] calldata amountsOrNftIds,
        string calldata defiParameters
    )
        external
        payable
        returns (address[] memory assets, uint256[] memory outAmounts)
    {
        (uint256 amountIn, uint256 minAmountOut) = _decodeDefiParameters(
            defiParameters
        );
        uint256 swapOut = _mockDex.swap{value: amountsOrNftIds[0]}(amountsOrNftIds[0], minAmountOut, address(this));
        IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2).forceApprove(msg.sender, swapOut);
        IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2).safeTransfer(msg.sender, swapOut);
        assets = new address[](4);
        assets[0] = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
        assets[1] = address(0);
        assets[2] = address(0);
        assets[3] = address(0);
        outAmounts = new uint256[](4);
        outAmounts[0] = swapOut;
        outAmounts[1] = 0;
        outAmounts[2] = 0;
        outAmounts[3] = 0;
    }

In the case of MockDex, the assets and outAmounts arrays each consist of one item indicative of the assets being spent and received by the user.

NOTE: It’s important to reference values from defiParameters that’s passed.

Once you've implemented the smart contract bridging contract, it's onto integrating your Dapp with the frontend interface for the contract.

Last updated