0

I've been reading and trying but to no avail. I'm trying to compare the current wallet address in MetaMask with the wallet address which owns a certain token via a smart contract method "ownerOf" which returns the address of the token owner.

I tried using "valueOf()", "JSON.stringify()", toString() and also "localcompare" and none worked. Localcompare always return a value of more than 0 even the two addresses match.

Below is my code.

async function doesTokenIdExist(tokenId, contract, walletAddress) {


  await contract.methods.ownerOf(tokenId).call(function(err,res){
        if(!err){

            console.log("Output of localeCompare: " + res.localeCompare(walletAddress));

            if (tokenAddress.localeCompare(walletAddress) == 0){
                console.log("Wallet owner owns tokenId");
                setTokenExist();
            } 

        } else {
            console.log(err);
        }
    });

}
1

Maybe you should use tolowercase() on both addresses, and better to console.log them first and see if one of them has 0x or if they already equal

| improve this answer | |
  • Gosh! You're a genius. I actually been outputting every single output I could and didn't realize the case. Yeah, some addresses have all small caps while some mix of big and small caps. Never thought blockchain coding is soo tricky. – warnerque May 16 '19 at 13:56
  • you are welcome, it happen to me alot ;-) – Majd TL May 16 '19 at 14:42

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.