Introduction

The MagnifyCashV1 contract is a decentralized lending platform that allows users to lend and borrow against their NFT assets. This integration guide will walk you through the process of interacting with the MagnifyCashV1 contract using the Ethers.js library. You'll learn how to initialize new lending desks, set loan configurations, deposit and withdraw liquidity, and initiate new loans.

Integration Steps

This guide covers the following key functionalities:

  • Initializing a new lending desk
  • Setting new loan configs for a lending desk
  • Depositing liquidity into a lending desk
  • Withdrawing liquidity from a lending desk
  • Initializing a new loan

You'll need to replace the following placeholders with your actual values:

  • your-ethereum-node-url: The URL of your Ethereum node (e.g., Infura, Alchemy, or your own node)
  • your-private-key: The private key of the Ethereum account you'll be using to interact with the contract
  • contract-address: The address of the deployed MagnifyCashV1 contract
  • 0x...: The addresses of the ERC20 token, NFT collection, and other contract-specific addresses

Code

// Import the necessary Ethers.js modules
const { ethers } = require('ethers');

// Set up the provider and signer
const provider = new ethers.providers.JsonRpcProvider('https://your-ethereum-node-url');
const signer = new ethers.Wallet('your-private-key', provider);

// Load the contract ABI
const contractABI = [
  // Contract ABI goes here
];

// Create the contract instance
const contract = new ethers.Contract('contract-address', contractABI, signer);

// Example usage: Initialize a new lending desk
const erc20 = '0x...'; // Address of the ERC20 token to be used
const depositAmount = ethers.utils.parseEther('1000'); // Initial balance of the lending desk
const loanConfigs = [
  {
    nftCollection: '0x...', // Address of the NFT collection
    minAmount: ethers.utils.parseEther('0.1'), // Minimum loan amount
    maxAmount: ethers.utils.parseEther('1'), // Maximum loan amount
    minInterest: 500, // Minimum interest rate (in basis points)
    maxInterest: 2000, // Maximum interest rate (in basis points)
    minDuration: 24, // Minimum loan duration (in hours)
    maxDuration: 720 // Maximum loan duration (in hours)
  }
];

async function initializeNewLendingDesk() {
  try {
    const tx = await contract.initializeNewLendingDesk(erc20, depositAmount, loanConfigs);
    await tx.wait(); // Wait for the transaction to be mined
    console.log('New lending desk initialized');
  } catch (error) {
    console.error('Error initializing new lending desk:', error);
  }
}

// Example usage: Set new loan configs for a lending desk
const lendingDeskId = 1;
const newLoanConfigs = [
  // New loan config details
];

async function setLendingDeskLoanConfigs() {
  try {
    const tx = await contract.setLendingDeskLoanConfigs(lendingDeskId, newLoanConfigs);
    await tx.wait(); // Wait for the transaction to be mined
    console.log('Lending desk loan configs updated');
  } catch (error) {
    console.error('Error updating lending desk loan configs:', error);
  }
}

// Example usage: Deposit liquidity into a lending desk
const lendingDeskId = 1;
const depositAmount = ethers.utils.parseEther('100');

async function depositLendingDeskLiquidity() {
  try {
    const tx = await contract.depositLendingDeskLiquidity(lendingDeskId, depositAmount);
    await tx.wait(); // Wait for the transaction to be mined
    console.log('Liquidity deposited into lending desk');
  } catch (error) {
    console.error('Error depositing liquidity into lending desk:', error);
  }
}

// Example usage: Withdraw liquidity from a lending desk
const lendingDeskId = 1;
const withdrawalAmount = ethers.utils.parseEther('50');

async function withdrawLendingDeskLiquidity() {
  try {
    const tx = await contract.withdrawLendingDeskLiquidity(lendingDeskId, withdrawalAmount);
    await tx.wait(); // Wait for the transaction to be mined
    console.log('Liquidity withdrawn from lending desk');
  } catch (error) {
    console.error('Error withdrawing liquidity from lending desk:', error);
  }
}

// Example usage: Initialize a new loan
const lendingDeskId = 1;
const nftCollection = '0x...'; // Address of the NFT collection
const nftId = 123; // ID of the NFT to be used as collateral
const loanAmount = ethers.utils.parseEther('0.5');
const loanDuration = 72; // Duration in hours
const maxInterestAllowed = 1500; // Maximum interest rate (in basis points)

async function initializeNewLoan() {
  try {
    const tx = await contract.initializeNewLoan(
      lendingDeskId,
      nftCollection,
      nftId,
      loanDuration,
      loanAmount,
      maxInterestAllowed
    );
    await tx.wait(); // Wait for the transaction to be mined
    console.log('New loan initialized');
  } catch (error) {
    console.error('Error initializing new loan:', error);
  }
}