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.
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:
pragmasolidity 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 contractcontract MyContract { ICollatsNFTVault private collatsNFTVault =ICollatsNFTVault("CollatsNFTVault");functionbuyAndAddCollats(address nftAddress,uint256 tokenId) externalpayable {uint256 collatsBought = collatsNFTVault.buyAndAddCollats(nftAddress, tokenId).value;// Do something with the returned value "collatsBought" }functionbuyAndAddCollatsWithERC20(address nftAddress,uint256 tokenId,address token,uint256 amount ) external {uint256 collatsBought = collatsNFTVault.buyAndAddCollatsWithERC20(nftAddress, tokenId, token, amount);// Do something with the returned value "collatsBought" }functionaddCollats(address nftAddress,uint256 tokenId,uint256 amount ) external { collatsNFTVault.addCollats(nftAddress, tokenId, amount);// No return value to use }functionaddCollatsInBulk(address[] memory nftAddresses,uint256[] memory tokenIds,uint256[] memory amounts ) external { collatsNFTVault.addCollatsInBulk(nftAddresses, tokenIds, amounts);// No return value to use }functionwithdrawCollats(address to,address nftAddress,uint256 tokenId,uint256 amount ) externalreturns (bool) {bool success = collatsNFTVault.withdrawCollats(to, nftAddress, tokenId, amount);if (success) {// Withdrawal successful } else {// Withdrawal failed } }functionbalanceOf(address nftAddress,uint256 tokenId) externalviewreturns (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.
Here is an example of how you can call the functions of the CollatsNFTVault contract from JavaScript using Web3:
constWeb3=require('web3');const collatsNFTVaultAbi = require('collatsNFTVaultAbi.json'); // Replace with the actual ABI of the ICollatsNFTVault contract
consttokenAbi=require('./ERC20Abi.json');// Replace "http://localhost:7545" with the actual RPC endpoint of your Ethereum networkconstweb3=newWeb3('http://localhost:7545');// Replace "CollatsNFTVault" with the actual contract address of the deployed CollatsNFTVault contractconstcollatsNFTVaultAddress="CollatsNFTVault";constcollatsNFTVault=newweb3.eth.Contract(collatsNFTVaultAbi, collatsNFTVaultAddress);// Replace "MyToken" with the actual contract address of the ERC20 token contractconsttokenAddress="MyToken";consttoken=newweb3.eth.Contract(tokenAbi, tokenAddress);asyncfunctionbuyAndAddCollats(nftAddress, tokenId, value) {constcollatsBought=awaitcollatsNFTVault.methods.buyAndAddCollats(nftAddress, tokenId).send({ value: value });console.log(collatsBought);}asyncfunctionrequestApprovalAndBuyCollatsWithERC20(nftAddress, tokenId, token, amount) {// Check the current allowance for the ERC20 tokensconstallowance=awaittoken.methods.allowance(web3.eth.defaultAccount, collatsNFTVaultAddress).call();if (allowance < amount) {// Request approval for the desired amount of tokensawaittoken.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);}asyncfunctionaddCollats(nftAddress, tokenId, amount) {awaitcollatsNFTVault.methods.addCollats(nftAddress, tokenId, amount).send({ from:web3.eth.defaultAccount });}asyncfunctionaddCollatsInBulk(nftAddresses, tokenIds, amounts) { await collatsNFTVault.methods.addCollatsInBulk(nftAddresses, tokenIds, amounts).send({ from: web3.eth.defaultAccount });
}asyncfunctionwithdrawCollats(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'); }}asyncfunctionbalanceOf(nftAddress, tokenId) {constbalance=awaitcollatsNFTVault.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:
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
consttokenAbi=require('./ERC20Abi.json');// Replace "http://localhost:7545" with the actual RPC endpoint of your Ethereum networkconstprovider=newethers.providers.JsonRpcProvider('http://localhost:7545');// Replace "CollatsNFTVault" with the actual contract address of the deployed ICollatsNFTVault contractconstcollatsNFTVaultAddress="CollatsNFTVault";constcollatsNFTVault=newContract(collatsNFTVaultAddress, collatsNFTVaultAbi, provider);// Replace "MyToken" with the actual contract address of the ERC20 token contractconsttokenAddress="MyToken";consttoken=newContract(tokenAddress, tokenAbi, provider);asyncfunctionbuyAndAddCollats(nftAddress, tokenId, value) {constcollatsBought=awaitcollatsNFTVault.buyAndAddCollats(nftAddress, tokenId, { value: value });console.log(`Bought ${collatsBought} collats for NFT ${nftAddress} with token ID ${tokenId}`);}asyncfunctionrequestApprovalAndBuyCollatsWithERC20(nftAddress, tokenId, token, amount) {// Check the current allowance for the ERC20 tokensconstallowance=awaittoken.allowance(provider.getSigner().getAddress(), collatsNFTVaultAddress);if (allowance.lt(amount)) {// Request approval for the desired amount of tokensawaittoken.approve(collatsNFTVaultAddress, amount).send(); }// Call the buyAndAddCollatsWithERC20 functionconstcollatsBought=awaitcollatsNFTVault.buyAndAddCollatsWithERC20(nftAddress, tokenId, token, amount);console.log(`Bought ${collatsBought} collats for NFT ${nftAddress} with token ID ${tokenId} using an ERC20`);}asyncfunctionaddCollats(nftAddress, tokenId, amount) {awaitcollatsNFTVault.addCollats(nftAddress, tokenId, amount);console.log(`Added ${amount} collats to NFT ${nftAddress} with token ID ${tokenId}`);}asyncfunctionaddCollatsInBulk(nftAddresses, tokenIds, amounts) {awaitcollatsNFTVault.addCollatsInBulk(nftAddresses, tokenIds, amounts);console.log(`Added collats to NFTs ${nftAddresses} with token IDs ${tokenIds}`);}asyncfunctionwithdrawCollats(to, nftAddress, tokenId, amount) {constsuccess=awaitcollatsNFTVault.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}`); }}asyncfunctionbalanceOf(nftAddress, tokenId) {constbalance=awaitcollatsNFTVault.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:
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.