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;
else
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()
.