How to get a percentage fee when user sold their NFTs

Is there any ways to get a percentage fee when user sold their NFTs though my NFT platform?

I create a smart contract with ERC1155.
It allows users to create and mint their own NFTs.

After user mint, the user can post their NFTs on Rarible. They can sell their NFTs.

I’m planning to get a percentage fee when the NFT is sold.

Maybe the fee goes to the contract. And sometime I need to execute withdraw() to get the balance of the contract (= fees) .

My question is How can I write the percentage fee into my smart contract?
Should I overwrite transfer or something?

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/access/Ownable.sol";


contract NFTCryplo is ERC1155, Ownable {
  mapping(uint256 => address) private minters;

  constructor() ERC1155(""){}

  function mint(address account,uint256 id,uint256 amount) public {
    require(minters[id] == address(0) || minters[id] == msg.sender);

    if(minters[id] == address(0)) {
      minters[id] = msg.sender;
    }
    _mint(account,id,amount,"");
  }

  function uri(uint256 _id) override public pure returns (string memory) {
    return string(
        abi.encodePacked(
          "https://example.com/nft/metadatas/",
          Strings.toString(_id)
          )
        );
  }

  function withdraw() onlyOwner public payable {
    payable(msg.sender).transfer(address(this).balance);
  }

}

Since you are selling on rarible, you need to set the royalties as per the rarible standards.

Check this rarible docs.
https://docs.rarible.org/overview/tokens-fees-royalties/?h=standard#royalties

1 Like

Maybe that’s a good point. I’ve read the document more than 10 times.
But I still don’t get how should I write it into my solidity code.

Could you please explain a bit more? :pray:

I am trying to implement royalties as per this article.

You basically need to import these contracts from Rarible Github

import "./RoyaltiesV2Impl.sol";
import "./LibPart.sol";
import "./LibRoyaltiesV2.sol";

after that add the below code to your NFT contract

function setRoyalties(uint _tokenId, address payable _royaltiesReceipientAddress, uint96 _percentageBasisPoints) public onlyOwner {
        LibPart.Part[] memory _royalties = new LibPart.Part[](1);
        _royalties[0].value = _percentageBasisPoints;
        _royalties[0].account = _royaltiesReceipientAddress;
        _saveRoyalties(_tokenId, _royalties);
    }
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721) returns (bool) {
        if(interfaceId == LibRoyaltiesV2._INTERFACE_ID_ROYALTIES) {
            return true;
        }
        return super.supportsInterface(interfaceId);
    }

Apparently, I am stuck at some error while implementing this. But you can also try to execute from your side.

1 Like

Thank you, Johnversus!!

It’s the exact article that I wanted.
I’m still working on my code. I’ll update here once mine works.

Now I’m trying to understand how and when setRoyalties is called. Maybe it’s called by NFT owner when they mint their NFTs?

Anyway, Thanks!!

Yes based on the tutorial setRoyalties is called after mint for each token. And it’s only the contract owner that calls it.

1 Like

I contacted Rarible and they gave me a Github link for ERC721 and ERC1155. I asked but they didn’t answer if for the ERC1155 contract I have to follow the same structure as the ERC721 contract. See the image below and tell me please.

What do you mean by this? What are you wanting to do?