# Integrating with external Defi protocols

### Introduction

The integration of Veilnyx with any DeFi protocol aims to supercharge the users of that protocol with Veilnyx's privacy and compliance features. In this integration example, we integrate **Veilnyx with Uniswap**, one of the leading decentralized exchange protocols offering liquidity and trading capabilities. This integration will enable users to execute trades on Uniswap while maintaining complete privacy over their transactions.

**Benefits of Integrating with Uniswap**

* **Enhanced Privacy:** Users can trade on Uniswap without exposing their wallet address.
* **Selective Deanonymisation:** Transactions can be decoded when necessary using a revoker and a guardian network, ensuring compliance and accountability.
* **Seamless Experience:** The integration ensures that users can leverage the best of both protocols without compromising on usability.

### Getting Started

#### Prerequisites

Before integrating Veilnyx with an external DeFi protocol, ensure you have the following prerequisites in place:

* **Veilnyx Protocol as Submodule:** Include the Veilnyx protocol as a submodule in your project. This will provide the necessary functionalities required for the integration.
* **Interface/Periphery Contracts of the Target DeFi Protocol:** Obtain the interface of the periphery contracts of the DeFi protocol you are integrating with. These contracts will be essential for interacting with the protocol’s core functionalities.
* **Address of the Periphery Contracts:** Ensure you have the correct addresses of the periphery contracts of the DeFi protocol you're integrating with, on the deployment network (e.g., Ethereum mainnet, testnet, etc.). These addresses are crucial for interfacing with the target protocol.
* **Involved Token Addresses:** Identify and obtain the addresses of the tokens involved in the transactions on the deployment network. This includes both the tokens you plan to trade and any intermediary tokens required by the protocol.

With these prerequisites in place, you are ready to proceed with the installation and setup of the Veilnyx protocol. Let's go!

#### Installation

To integrate Veilnyx with your chosen DeFi protocol, follow these steps to install and set up the necessary tools and libraries. This guide assumes you are using **Foundry and Solidity** for smart contract development.

**Install Foundry:**\
Foundry is a fast, portable, and modular toolkit for Ethereum application development. Installing Foundry will also handle the installation of the Solidity compiler (solc).

```bash
curl -L https://foundry.paradigm.xyz | bash
foundryup
```

**Install Veilnyx Protocol**

Install the Veilnyx protocol using Foundry:

```bash
forge install veilnyx/v1-protocol
```

**Create the interface of the periphery contract**

Include just the interface of the periphery contract of the DeFi protocol you are integrating with in your project directory, under the `interfaces` folder.

{% hint style="warning" %}
We advise against including the complete DeFi protocol as a submodule, as you can run into version conflicts with the Solidity compiler and other common dependencies like OpenZeppelin.
{% endhint %}

**Install OpenZeppelin Contracts**

OpenZeppelin provides a library of modular, reusable, and secure smart contracts for Ethereum. Install OpenZeppelin contracts for secure ERC20-related operations.

```bash
forge install OpenZeppelin/openzeppelin-contracts
```

With these steps completed, your development environment is set up, and you are ready to proceed with integrating Veilnyx with your target DeFi protocol.

### Transaction Flow

You will use the Veilnyx[ SDK to create and submit a `ZTransaction`](https://docs.veilnyx.com/veilnyx-sdk/sending-transaction) Veilnyx Pool proxy contract for execution of your transaction. Here’s how your `ZTransaction` will flow:

<figure><img src="https://1310074946-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfuRJzJ8cEqekSVBqpKIH%2Fuploads%2FkboI65LKPOo1wWwR42Sl%2Fimage.png?alt=media&#x26;token=49eaf81a-d511-4899-9d44-00d58cbe635d" alt=""><figcaption></figcaption></figure>

### Veilnyx \`IAdaptor\` implementation

Veilnyx offers the `IAdaptor` An interface for protocol devs to implement in their DeFi adaptors. This interface offers a `adaptorConnect(...)` function:

```solidity
function adaptorConnect(
        uint24[] calldata inAssetIds,
        uint256[] calldata inValues,
        bytes calldata payload
    )
        external
        payable
        virtual
        returns (uint24[] memory outAssetIds, uint256[] memory outValues);
```

`inAssetIds` : The array of incoming asset IDs involved in the transaction.

{% hint style="info" %}
Veilnyx offers a struct `Asset` to whitelist the assets you’re going to use during Veilnyx protocol initialization itself. The asset IDs used as `inAssetIds` and `outAssetIds` are returned once an asset is whitelisted during Veilnyx initialization. We will cover this in the deployment section.
{% endhint %}

`inValues` : The quantity of each incoming asset.

`payload` : Miscellaneous parameters (encoded as bytes) that your DeFi adaptor might need for the transaction to the target DeFi protocol. These are generally parameters expected by the target protocol to execute. For eg, Uniswap expects information of `outToken`, `beneficiary`, `slippage protection`, etc, to execute a swap, which can all be encoded in the `payload` params and made available to your adaptor contract by decoding it.

Here’s an implementation of the `adaptorConnect(...)` function of the UniswapV3 adaptor which executes a swap between WETH<>USDC tokens:

{% hint style="info" %}
The `adaptorConnect(...)` is called via `delegatecall` by the `Veilnyx::AdaptorHandler.sol`, with all the required input tokens/assets already in balance. The converted assets or outputs are then returned in the same function.
{% endhint %}

```solidity
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.24;

import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { AdaptorBase } from "src/base/AdaptorBase.sol";
import {Asset, AssetType} from "src/libraries/Asset.sol";
import {ISwapRouter02} from "./ISwapRouter02.sol";

contract UniswapV3Adapter is AdaptorBase {
    // Errors //
    error UnsupportedAsset(uint24 assetId);
    error MultiAssetSwap();
    error ZeroValues();
    error ZeroAddress();

    ISwapRouter02 public immutable swapRouter02; // Uniswap's Periphery contract interface
    uint24 public constant feeTier = 3000; // Uniswap variable

    constructor(address swapRouter02_, address pool_) AdaptorBase(pool_) {
        swapRouter02 = ISwapRouter02(swapRouter02_); // Uniswap V3 Swap router
    }

/// @dev Will be called by the Veilnyx AdaptorHandler.sol to execute the swap.
    function adaptorConnect(
        uint24[] calldata inAssetIds,
        uint256[] calldata inValues,
        bytes calldata payload
    )
        external
        payable
        override
        returns (uint24[] memory outAssetIds, uint256[] memory outValues)
    {
        uint256 inValue;
        // Checks
        if (inAssetIds.length != 1 || inValues.length != 1) {
            revert MultiAssetSwap();
        }

        if (inValues[0] == 0) {
            revert ZeroValues();
        } else {
            inValue = inValues[0];
            console.log("UniswapV3Adp:: inValue to swap:", inValue);
        }

        Asset memory inAsset = getAsset(inAssetIds[0]); // offered by Veilnyx::adaptorBase.sol
        if (!inAsset.isSupported) {
            revert UnsupportedAsset(inAssetIds[0]);
        }

        if (inAsset.assetAddress == address(0)) {
            revert ZeroAddress();
        }

        // decoding payload
        (uint24 outAssetId, address beneficiary, uint256 minOut) = abi.decode(
            payload,
            (uint24, address, uint256)
        );

        Asset memory outAsset = getAsset(outAssetId); // offered by Veilnyx::adaptorBase.sol
        if (!outAsset.isSupported) {
            revert UnsupportedAsset(outAssetId);
        }

        if (beneficiary == address(0)) {
            // means the out tokens will go to the Veilnyx AdaptorHandler and have to be processed to the pool
            beneficiary = address(this); // AdaptorHandler.sol
        }

        // Executing swap using UniswapV3
        uint256 tokenOutAmount = swapExactInputSingle({
            tokenIn: inAsset.assetAddress,
            tokenInAmt: inValue,
            tokenOut: outAsset.assetAddress,
            minOut: minOut,
            beneficiary: beneficiary
        });

        // initialising out arrays
        if (beneficiary == address(this)) { // address(this) > Veilnyx::AdaptorHandler.sol
            outAssetIds = new uint24[](1);
            outValues = new uint256[](1);

            outAssetIds[0] = outAssetId;
            outValues[0] = tokenOutAmount;
        } else {
            outAssetIds = new uint24[](0);
            outValues = new uint256[](0);
        }

        return (outAssetIds, outValues);
    }

    /// @param tokenIn The address of the token to be swapped
    /// @param tokenInAmt The amount of `tokenIn` tokens to swap
    /// @param tokenOut The address of the output token
    /// @param minOut The minimum amount of output token the user expects to get back. This is used for slippage protection.
    /// @param beneficiary The address which will receive the output tokens. This will be the Veilnyx Convertor.sol contract.
    function swapExactInputSingle(
        address tokenIn,
        uint256 tokenInAmt,
        address tokenOut,
        uint256 minOut,
        address beneficiary
    ) public returns (uint256 amountOut) {
        // Veilnyx convertor to approve the Uniswap adaptor the inTokens received.
        SafeERC20.forceApprove(
            IERC20(tokenIn),
            address(swapRouter02), 
            tokenInAmt
        );

        // Create the params that will be used to execute the swap
        /// @param sqrtPriceLimitX96 This is the sqrt of potential value decrease of outAsset relative to inAsset (uint160), that the trader is willing to ignore for the swap. We will be deactivating this protective measure for the MVP. We will only be deploying the slippage protection using `amountOutMinimum`
        ISwapRouter02.ExactInputSingleParams memory params = ISwapRouter02
            .ExactInputSingleParams({
                tokenIn: tokenIn,
                tokenOut: tokenOut,
                fee: feeTier,
                recipient: beneficiary,
                amountIn: tokenInAmt,
                amountOutMinimum: minOut,
                sqrtPriceLimitX96: uint160(0)
            });

        // The call to `exactInputSingle` executes the swap.
        amountOut = swapRouter02.exactInputSingle(params);
    }
```

### Deploy

Once you’re done testing your DeFi adaptor, it’s finally time to ship!

If you’re deploying on a network which has Veilnyx and the target DeFi protocols, then you just need to deploy your newly written adaptor. Just grab the `Veilnyx PoolProxy` contract address, the `periphery contract` addresses of your target protocol and deploy your adaptor with the required addresses:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {UniswapV3Adapter} from "src/adaptors/uniswap-v3/UniswapV3Adapter.sol";

contract UniswapV3AdaptorDeploy {
    function run() external broadcast returns(UniswapV3Adapter) {
        address poolProxy = `${veilnyx_pool_proxy_address_on_target_network}`;
        address uniswapRouter02 = `${uniswapSwapRouter02_address_on_target_network};

        UniswapV3Adapter uniswapV3Adapter = new UniswapV3Adapter(uniswapRouter02, poolProxy);
        return uniswapV3Adapter;
    }
}
```

If either protocols are not deployed on your target network, please refer to their respective documentation to deploy the protocols on your target network before proceeding with your adaptor deployment.
