Skip to content

Instantly share code, notes, and snippets.

@saadSarwar28
Created August 18, 2021 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saadSarwar28/abf7f805b97ac713b6697b9bb9b0feeb to your computer and use it in GitHub Desktop.
Save saadSarwar28/abf7f805b97ac713b6697b9bb9b0feeb to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.4+commit.c7e474f2.js&optimize=false&runs=200&gist=
pragma solidity >=0.7.0 <0.9.0;
// SPDX-License-Identifier: MIT
/**
* @title Ownership
* check Ownership of nfts
*/
interface deployedContract {
function totalSupply() external view returns (uint);
function ownerOf(uint tokenId)external view returns (address);
function balanceOf(address owner) external view returns(uint);
}
contract Ownership {
function checkOwnership(address walletAddress, address contractAddress) public view returns (uint[] memory){
// getting total supply of the contract
uint256 totalSupply = _getTotalSupply(contractAddress);
// getting total number of nfts owned by an address
uint balance = _getBalanceOf(contractAddress, walletAddress);
// initializing an array with the size of total number of nfts owned by an address.
uint[] memory ids = new uint[](balance);
// index to insert the nft id in ids array, no need to iterate over and over again on ids array
uint indexToInsert = 0;
// iterating through the total number of nfts present in contract
for (uint id = 0; id < totalSupply; id++) {
// contracts whose owner is the given address are added to the array to be returned.
// incrementing id by one because nft ids start from 1
if (walletAddress == deployedContract(contractAddress).ownerOf(id + 1)) {
ids[indexToInsert] = id + 1;
indexToInsert++;
}
}
return ids;
}
function _getTotalSupply(address contractAddress) internal view returns(uint256) {
return deployedContract(contractAddress).totalSupply();
}
function _getBalanceOf(address contractAddress, address owner) internal view returns(uint256) {
return deployedContract(contractAddress).balanceOf(owner);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment