Collats NFT Vault

Details

The vault is the contract that manages the amount of Collats backing an ERC721 asset. It keeps track of the amount of Collats that an NFT has assigned.

Ethereum Mainnet

0x157CD665f569E916A38d9607D7fB28D3083c203b

Polygon Mainnet

0x157CD665f569E916A38d9607D7fB28D3083c203b

Binance Smart Chain Mainnet

0x157CD665f569E916A38d9607D7fB28D3083c203b

Interface

// SPDX-License-Identifier: MIT
pragma solidity 0.8.14;

interface ICollatsNFTVault {
    function buyAndAddCollats(address nftAddress, uint256 tokenId)
        external
        payable
        returns (uint256 collatsBought);

    function buyAndAddCollatsWithERC20(
        address nftAddress,
        uint256 tokenId,
        address token,
        uint256 amount
    ) external returns (uint256 collatsBought);

    function addCollats(
        address nftAddress,
        uint256 tokenId,
        uint256 amount
    ) external;

    function addCollatsInBulk(
        address[] memory nftAddresses,
        uint256[] memory tokenIds,
        uint256[] memory amounts
    ) external;

    function withdrawCollats(
        address to,
        address nftAddress,
        uint256 tokenId,
        uint256 amount
    ) external returns (bool);

    function balanceOf(address nftAddress, uint256 tokenId)
        external
        view
        returns (uint256);
}

The ICollatsNFTVault interface defines a contract that allows users to buy and add "collats" to a specific non-fungible token (NFT) identified by its address and token ID. It also allows users to add collats to an NFT in bulk, withdraw collats from an NFT, and check the balance of collats for a specific NFT.

The buyAndAddCollats function allows users to buy and add collats to an NFT by paying with Ether. The buyAndAddCollatsWithERC20 function allows users to buy and add collats to an NFT by paying with a specific ERC20 token. The addCollats function allows users to add a specific amount of collats to an NFT. The addCollatsInBulk function allows users to add collats to multiple NFTs in a single transaction. The withdrawCollats function allows users to withdraw a specific amount of collats from an NFT and send them to a specified address. The balanceOf function allows users to check the balance of collats for a specific NFT.

Contract to contract example

Here is sample code in Solidity 8 that demonstrates how to call the functions of the ICollatsNFTVault interface from another contract:

pragma solidity 0.8.14;

//The import will depend on the location of your file system 
import "./ICollatsNFTVault.sol";

// Replace "CollatsNFTVault" with the actual contract address of the deployed ICollatsNFTVault contract
contract MyContract {
    ICollatsNFTVault private collatsNFTVault = ICollatsNFTVault("CollatsNFTVault");

    function buyAndAddCollats(address nftAddress, uint256 tokenId) external payable {
        uint256 collatsBought = collatsNFTVault.buyAndAddCollats(nftAddress, tokenId).value;
        // Do something with the returned value "collatsBought"
    }

    function buyAndAddCollatsWithERC20(
        address nftAddress,
        uint256 tokenId,
        address token,
        uint256 amount
    ) external {
        uint256 collatsBought = collatsNFTVault.buyAndAddCollatsWithERC20(nftAddress, tokenId, token, amount);
        // Do something with the returned value "collatsBought"
    }

    function addCollats(
        address nftAddress,
        uint256 tokenId,
        uint256 amount
    ) external {
        collatsNFTVault.addCollats(nftAddress, tokenId, amount);
        // No return value to use
    }

    function addCollatsInBulk(
        address[] memory nftAddresses,
        uint256[] memory tokenIds,
        uint256[] memory amounts
    ) external {
        collatsNFTVault.addCollatsInBulk(nftAddresses, tokenIds, amounts);
        // No return value to use
    }

    function withdrawCollats(
        address to,
        address nftAddress,
        uint256 tokenId,
        uint256 amount
    ) external returns (bool) {
        bool success = collatsNFTVault.withdrawCollats(to, nftAddress, tokenId, amount);
        if (success) {
            // Withdrawal successful
        } else {
            // Withdrawal failed
        }
    }

    function balanceOf(address nftAddress, uint256 tokenId) external view returns (uint256) {
        uint256 balance = collatsNFTVault.balanceOf(nftAddress, tokenId);
        // Do something with the returned value "balance"
    }
}

In this example, the contract MyContract calls the functions of the ICollatsNFTVault contract by calling the corresponding functions on an instance of the ICollatsNFTVault contract, which is stored in the collatsNFTVault variable. The contract address of the deployed ICollatsNFTVault contract needs to be provided when the collatsNFTVault variable is initialized.

The return values of the buyAndAddCollats and buyAndAddCollatsWithERC20 functions are stored in the variables collatsBought, which can then be used for further processing. The withdrawCollats function returns a boolean value indicating whether the withdrawal was successful or not, which can be checked in an if statement. The balanceOf function returns the balance of the specified NFT for the given token ID, which can be stored in the balance variable and used for further processing.

Note that the buyAndAddCollats function is marked payable, which means that it can accept Ether as a payment. Make sure to provide enough Ether when calling the function, or the transaction will fail.

ICollatsNFTVault ABI

[
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "nftAddress",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "tokenId",
          "type": "uint256"
        },
        {
          "internalType": "uint256",
          "name": "amount",
          "type": "uint256"
        }
      ],
      "name": "addCollats",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address[]",
          "name": "nftAddresses",
          "type": "address[]"
        },
        {
          "internalType": "uint256[]",
          "name": "tokenIds",
          "type": "uint256[]"
        },
        {
          "internalType": "uint256[]",
          "name": "amounts",
          "type": "uint256[]"
        }
      ],
      "name": "addCollatsInBulk",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "nftAddress",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "tokenId",
          "type": "uint256"
        }
      ],
      "name": "balanceOf",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "nftAddress",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "tokenId",
          "type": "uint256"
        }
      ],
      "name": "buyAndAddCollats",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "collatsBought",
          "type": "uint256"
        }
      ],
      "stateMutability": "payable",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "nftAddress",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "tokenId",
          "type": "uint256"
        },
        {
          "internalType": "address",
          "name": "token",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "amount",
          "type": "uint256"
        }
      ],
      "name": "buyAndAddCollatsWithERC20",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "collatsBought",
          "type": "uint256"
        }
      ],
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "to",
          "type": "address"
        },
        {
          "internalType": "address",
          "name": "nftAddress",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "tokenId",
          "type": "uint256"
        },
        {
          "internalType": "uint256",
          "name": "amount",
          "type": "uint256"
        }
      ],
      "name": "withdrawCollats",
      "outputs": [
        {
          "internalType": "bool",
          "name": "",
          "type": "bool"
        }
      ],
      "stateMutability": "nonpayable",
      "type": "function"
    }
  ]

JavaScript

Web3.js

Here is an example of how you can call the functions of the CollatsNFTVault contract from JavaScript using Web3:

const Web3 = require('web3');
const collatsNFTVaultAbi = require('collatsNFTVaultAbi.json'); // Replace with the actual ABI of the ICollatsNFTVault contract
const tokenAbi = require('./ERC20Abi.json');

// Replace "http://localhost:7545" with the actual RPC endpoint of your Ethereum network
const web3 = new Web3('http://localhost:7545');

// Replace "CollatsNFTVault" with the actual contract address of the deployed CollatsNFTVault contract
const collatsNFTVaultAddress = "CollatsNFTVault";
const collatsNFTVault = new web3.eth.Contract(collatsNFTVaultAbi, collatsNFTVaultAddress);

// Replace "MyToken" with the actual contract address of the ERC20 token contract
const tokenAddress = "MyToken";
const token = new web3.eth.Contract(tokenAbi, tokenAddress);

async function buyAndAddCollats(nftAddress, tokenId, value) {
  const collatsBought = await collatsNFTVault.methods.buyAndAddCollats(nftAddress, tokenId).send({ value: value });
  console.log(collatsBought);
}

async function requestApprovalAndBuyCollatsWithERC20(nftAddress, tokenId, token, amount) {
  // Check the current allowance for the ERC20 tokens
  const allowance = await token.methods.allowance(web3.eth.defaultAccount, collatsNFTVaultAddress).call();
  if (allowance < amount) {
    // Request approval for the desired amount of tokens
    await token.methods.approve(collatsNFTVaultAddress, amount).send({ from: web3.eth.defaultAccount });
  }

  // Call the buyAndAddCollatsWithERC20 function
  const collatsBought = await collatsNFTVault.methods.buyAndAddCollatsWithERC20(nftAddress, tokenId, token, amount).send({ from: web3.eth.defaultAccount });
  console.log(collatsBought);
}

async function addCollats(nftAddress, tokenId, amount) {
  await collatsNFTVault.methods.addCollats(nftAddress, tokenId, amount).send({ from: web3.eth.defaultAccount });
}

async function addCollatsInBulk(nftAddresses, tokenIds, amounts) {
  await collatsNFTVault.methods.addCollatsInBulk(nftAddresses, tokenIds, amounts).send({ from: web3.eth.defaultAccount });
}

async function withdrawCollats(to, nftAddress, tokenId, amount) {
  const success = await collatsNFTVault.methods.withdrawCollats(to, nftAddress, tokenId, amount).send({ from: web3.eth.defaultAccount });
  if (success) {
    console.log('Withdrawal successful');
  } else {
    console.log('Withdrawal failed');
  }
}

async function balanceOf(nftAddress, tokenId) {
  const balance = await collatsNFTVault.methods.balanceOf(nftAddress, tokenId).call();
  console.log(balance);
}

In this example, the functions of the CollatsNFTVault contract are called using the send method of the Contract object, which sends a transaction to the Ethereum network. The buyAndAddCollats function is marked payable, which means that it can accept Ether as a payment.

The addCollats, addCollatsInBulk, and withdrawCollats functions do not have any return values, so the returned value of the send method can be used to check the status of the transaction (e.g. whether it was mined successfully or not). The balanceOf function is a view function, which means that it does not modify the state of the contract and does not need to be called with the send method. Instead, it can be called with the call method, which returns the value of the function without sending a transaction.

You can call these functions in your code by passing the required parameters. For example:

buyAndAddCollats('0x123456...', 1, '100000000000000000');
requestApprovalAndBuyCollatsWithERC20('0x123456...', 1, '0x987654...', '1000000000000000000');
addCollats('0x123456...', 1, '1000000000000000000');
addCollatsInBulk(['0x123456...', '0x654321...'], [1, 2], ['1000000000000000000', '50000000000000000000']);
withdrawCollats('0x123456...', '0x654321...', 1, '50000000000000000000');
balanceOf('0x123456...', 1);

Ethers.js

Here is an example of how you can call the functions of the CollatsNFTVault contract from JavaScript using Ethers.js:

const { Contract, ethers } = require('ethers');
const collatsNFTVaultAbi = require('collatsNFTVaultAbi.json'); // Replace with the actual ABI of the ICollatsNFTVault contract
const tokenAbi = require('./ERC20Abi.json');

// Replace "http://localhost:7545" with the actual RPC endpoint of your Ethereum network
const provider = new ethers.providers.JsonRpcProvider('http://localhost:7545');

// Replace "CollatsNFTVault" with the actual contract address of the deployed ICollatsNFTVault contract
const collatsNFTVaultAddress = "CollatsNFTVault";
const collatsNFTVault = new Contract(collatsNFTVaultAddress, collatsNFTVaultAbi, provider);

// Replace "MyToken" with the actual contract address of the ERC20 token contract
const tokenAddress = "MyToken";
const token = new Contract(tokenAddress, tokenAbi, provider);

async function buyAndAddCollats(nftAddress, tokenId, value) {
  const collatsBought = await collatsNFTVault.buyAndAddCollats(nftAddress, tokenId, { value: value });
  console.log(`Bought ${collatsBought} collats for NFT ${nftAddress} with token ID ${tokenId}`);
}

async function requestApprovalAndBuyCollatsWithERC20(nftAddress, tokenId, token, amount) {
  // Check the current allowance for the ERC20 tokens
  const allowance = await token.allowance(provider.getSigner().getAddress(), collatsNFTVaultAddress);
  if (allowance.lt(amount)) {
    // Request approval for the desired amount of tokens
    await token.approve(collatsNFTVaultAddress, amount).send();
  }

  // Call the buyAndAddCollatsWithERC20 function
  const collatsBought = await collatsNFTVault.buyAndAddCollatsWithERC20(nftAddress, tokenId, token, amount);
  console.log(`Bought ${collatsBought} collats for NFT ${nftAddress} with token ID ${tokenId} using an ERC20`);

}

async function addCollats(nftAddress, tokenId, amount) {
  await collatsNFTVault.addCollats(nftAddress, tokenId, amount);
  console.log(`Added ${amount} collats to NFT ${nftAddress} with token ID ${tokenId}`);
}

async function addCollatsInBulk(nftAddresses, tokenIds, amounts) {
  await collatsNFTVault.addCollatsInBulk(nftAddresses, tokenIds, amounts);
  console.log(`Added collats to NFTs ${nftAddresses} with token IDs ${tokenIds}`);
}

async function withdrawCollats(to, nftAddress, tokenId, amount) {
  const success = await collatsNFTVault.withdrawCollats(to, nftAddress, tokenId, amount);
  if (success) {
    console.log(`Withdrew ${amount} collats from NFT ${nftAddress} with token ID ${tokenId} and sent them to ${to}`);
  } else {
    console.log(`Failed to withdraw collats from NFT ${nftAddress} with token ID ${tokenId}`);
  }
}

async function balanceOf(nftAddress, tokenId) {
  const balance = await collatsNFTVault.balanceOf(nftAddress, tokenId);
  console.log(`Balance of collats for NFT ${nftAddress} with token ID ${tokenId}: ${balance}`);
}

You can call these functions in your code by passing the required parameters. For example:

buyAndAddCollats('0x123456...', 1, '100000000000000000');
requestApprovalAndBuyCollatsWithERC20('0x123456...', 1, '0x987654...', '100000000000000000');
addCollats('0x123456...', 1, '100000000000000000');
addCollatsInBulk(['0x123456...', '0x654321...'], [1, 2], ['100000000000000000', '5000000000000000000']);
withdrawCollats('0x123456...', '0x654321...', 1, '5000000000000000000');
balanceOf('0x123456...', 1);

This script calls all the functions of the CollatsNFTVault contract and logs the results to the console. It calls the buyAndAddCollats function, the buyAndAddCollatsWithERC20 function, the addCollats function, the addCollatsInBulk function, the withdrawCollats function, and the balanceOf function.

Keep in mind that this script is just an example, and you may need to modify it to fit your specific needs. You will also need to handle errors and handle the case where the JsonRpcProvider is not available.

Last updated