Here is a mapping to loans:

mapping (uint256 => Loan) public loans;

I then use: uint256(keccak256(abi.encode(address(msg.sender), tokenId))); to create a unique loanId from a struct.

 function onERC721Received(
        address from,
        uint256 tokenId,
        bytes calldata
        uint256 loanId = uint256(keccak256(abi.encode(address(msg.sender), tokenId)));
        loans[loanId] = Loan({
            nftContract: ERC721(msg.sender),
            tokenId: tokenId,
            borrower: from,
            repayable: false

        // lend(from);
        return 0x150b7a02;

Now, to access my a loan, I retrieve the unique id with the following fuction:

    function getLoanId(address _tokenContract, uint256 _tokenId) public pure returns (uint256) {
      return uint256(keccak256(abi.encode(address(_tokenContract), _tokenId)));

However, the id the keccak256 spits out doesn't return any data from my mapping. Any ideas why?

1 Answer 1


this works just fine. make sure your arguments are correct when you call `getLoanId()'.

you could possibly be confusing the _tokenContract argument with that of the NFT contract and not the msg.sender

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.