Implement Borrower Features

This page describes how you can implement Borrower features into your Platform.

Typically, web3 platforms implement two types of utilities (features) for NFTs: on-chain and off-chain.

On-chain utility refers to smart contracts, meaning only holders of a specific NFT can call a function.

Off-chain utility refers to features external to the blockchain. For example, a game only allows holders of a specific NFT to access their website.

The following section will discuss how you can enable Borrowers to use on-chain and off-chain utilities.


Enabling Off-chain Utility

When integrating NFTs with only off-chain utility, developers don't need to deploy additional solidity to enable NFT Rentals.

Let's say you are a developer building a game that entirely runs on a backend (server or cloud). The only blockchain aspect of your game is that items are represented as NFTs, which can be bought, sold, and transferred directly in the blockchain.

To enable a Borrower to access the utility of a borrowed NFT, all your system needs to do is recognize which NFTs your users are borrowing at any given time. There are multiple ways for your backend to find out this information:

  1. Monitoring Events: The Roles Registry contract issues a blockchain event every time an NFT is rented. You can monitor these events directly on the blockchain and store all rentals in a database for your backend.

  2. Calling View Functions: The Roles Registry contract also has view functions that can query the state of a rental. All functions and events are documented in the ERC-7432 specification.

  3. Subgraph API: TheGraph is a blockchain indexing technology that allows developers to query indexed data easily. You can use the subgraph GraphQL API to find if your user has any rentals.

  4. Orium API: Orium's API is also a great way to determine if your users have active rentals. This API has additional filters and sorting features to facilitate the integration process.

You can Contact our team to get access to the Orium and Subgraph's APIs.


Enabling On-chain Utility

Assume you are developing a blockchain game where only the owner can farm tokens using an NFT. To expand your community, you decide to allow NFT holders to rent their NFTs when they are not using them.

To farm tokens, users have to call the following function:

contract MyNFT {

    function farm(uint256 _tokenId) external {
        // verifies if function caller is the owner of the NFT
        require(ownerOf(_tokenId) == msg.sender, "Only the owner can farm");
        // if the caller is the owner, proceed to farm tokens
        farmTokens();
    }

}

Now, you want to allow an NFT Borrower to do that on behalf of the user. To implement this feature, we can modify the farm function to check if the NFT is rented:

contract MyNFT {

    IERC7432 immutable RolesRegistry = IERC7432(0x4c7E4a30a749d78935ED7674590026f7b74AFea0);
    bytes32 immutable FARMER_ROLE = keccak256("FARMER_ROLE");

    function farm(uint256 _tokenId) external {
        address owner = ownerOf(_tokenId);
        
        // verifies if function caller is the owner of the NFT
        if (owner == msg.sender) {
            // if the caller is the owner, proceed to farm tokens
            farmTokens();
        } else if (RolesRegistry.hasRole(FARMER_ROLE, address(this), _tokenId, owner, msg.sender)) {
            // if the caller has borrower the NFT, they can proceed to farm as well
            farmTokens();
        }
        
        revert("User is not authorized to farm with this NFT");
    }

}

Last updated