# 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.

<figure><img src="/files/HXYSMIhK9Bh6Rp5NKzIW" alt=""><figcaption><p>Mock smart contract integration diagram</p></figcaption></figure>

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:

```solidity
// 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.

<figure><img src="/files/h8ivTwyPVI5vI9GdFl3q" alt=""><figcaption><p>Mock smart contract integration diagram</p></figcaption></figure>

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.

```solidity
    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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://singularityzk.gitbook.io/singularity/for-developers/custom-sdk-integration/bridging-smart-contract.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
