Setting up your contract

Setting up the Contract

First, declare the solidity version used to compile the contract. If needed (if you are using older versions of solidity) add abicoder v2 to allow arbitrary nested arrays and structs to be encoded and decoded in calldata, a feature that we use when transacting with a pool.

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity =0.8.20;

After that, import the contracts needed from the npm package installation.

import '@cryptoalgebra/integral-core/contracts/interfaces/IAlgebraPool.sol';
import '@cryptoalgebra/integral-core/contracts/libraries/TickMath.sol';

import '@cryptoalgebra/integral-periphery/contracts/interfaces/INonfungiblePositionManager.sol';
import '@cryptoalgebra/integral-periphery/contracts/libraries/TransferHelper.sol';
import '@cryptoalgebra/integral-periphery/contracts/base/LiquidityManagement.sol';

import '@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol';

Then, create a contract called LiquidityExamples and inherit both IERC721Receiver and LiquidityManagement.

For this case, we've chosen to hardcode the token contract addresses. Most likely, you would use an input parameter for this in production, allowing you to change the pools and tokens you are interacting with on a per-transaction basis.

contract LiquidityExamples is IERC721Receiver, LiquidityManagement {

    address public constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
    address public constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;

Declare an immutable public variable nonfungiblePositionManager of type INonfungiblePositionManager.

Enabling ERC721 Interactions

EEvery NFT is identified by a unique uint256 ID inside the ERC-721 smart contract, declared as the tokenId

What is NFT?

To allow deposits of ERC721 expressions of liquidity, create a struct called Deposit, a mapping of uint256 to the Deposit struct, then declare that mapping as a public variable deposits.

The Constructor

Declare the constructor here, which is executed once when the contract is deployed. Our constructor hard codes the address of the non-fungible position manager interface, router, and the periphery immutable state constructor, which requires the factory, pool deployer and the address of WMATIC.

Allowing custody of ERC721 tokens

To allow the contract to custody ERC721 tokens, implement the onERC721Received function within the inherited IERC721Receiver.sol contract.

IERC721Receiver Contract

The from identifier may be omitted because it is not used.

Creating a Deposit

To add a Deposit instance to the deposits mapping, create an internal function called _createDeposit that destructures the positions struct returned by positions in nonfungiblePositionManager.sol. Pass the relevant variables token0, token1 and liquidity to the deposits mapping.

The Full Contract Setup

Last updated