I am actually looking towards cost reasonable design idea where I can know that which token number belongs to which wallet address. let's say addressA bought first five tokens, so token no 1,2,3,4,5 belongs to wallet addressA and so on.

If some one come and ask to which token holder does token no 3 belongs then in gas cost effective way, it can tell addressA.

Is this possible? I am open to discussions, ideas, or if someone have already done this?

Feel free to share your thoughts.

I have tried this code snippet:

pragma solidity 0.5.12;
contract A{
    struct Info {
        uint index;
        address owner;

    Info[] public list;

    mapping(address => uint) public balanceOf;
    uint totalSupply;

    function buy(uint amount) public {
        totalSupply += amount;
        balanceOf[msg.sender] += amount;
        list.push(Info(totalSupply, msg.sender));


    function call(uint index) public view returns (address){
        return getOwnerOfToken(index);


    function getOwnerOfToken(uint index) public view returns(address) {
        uint256 lo = 0;
        uint256 hi = list.length;

        while (lo + 1 < hi) {
            uint256 mid = (lo + hi) / 2;
            if (list[mid].index >= index)
                lo = mid;
                hi = mid;

        if (list[hi].index >= index)
            return list[hi].owner;
        if (list[lo].index >= index)
            return list[lo].owner;

        revert("not found");

It is currently working ok when getOwnerOfToken() is directly called but fails when call() is called, my need is to call a function which shall inside call this getOwnerOfToken().

  • Are you talking about fungible or non-fungible tokens? What if Alice has two tokens #5 and #13, and then transfers one tokens to Bob? Which token, #5 or #13 will be transferred? – Mikhail Vladimirov Dec 11 '19 at 8:52
  • I am talking of erc20 tokens – Fariha Abbasi Dec 12 '19 at 7:14
  • Then, if one have many tokens and transfers only some of them, what indexes will be actually transferred? – Mikhail Vladimirov Dec 12 '19 at 11:27

Such tokens are not identical, a.k.a. fungible, or divisible because they have unique numbers.

That is a non-fungible token.

Consider ERC721 for that.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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