4

When calling a function on a smart contract that takes a tokenID, I pass in the tokenID into web3 - but web3 then parses that string into another value it seems, pads it to the left with a ton of zeros and then the contract call fails.

tokenID: "5347359381416754638292106073567748474581852345025787828513953871505554409252111"

export async function getTokenURI(address, tokenID) {
  try {
    console.log(address, typeof tokenID);
    return await getterContract(ABI, address, "tokenURI", [tokenID]);
  } catch (err) {
    console.log(err);
    console.log(address, typeof tokenID);
    return "";
  }
}
export const getterContract = async (ABI, address, methodName, params) => {
  try {
    const contractInstance = await getDeployedContract(ABI, address);

    return await contractInstance.methods[methodName](...params)
      .call()
      .then((res) => {
        return res;
      });
  } catch (e) {
    throw e;
  }
};

Calling getTokenURI and passing in the token ID above - the actual value web3 is sending to the contract is: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005347359381416754638292106073567748474581852345025787828513953871505554409252111

Instead of the correct value of 5347359381416754638292106073567748474581852345025787828513953871505554409252111

When passing in a smaller value such as 123456 it works fine.

Both values are passed into the function as strings - so not sure why the length of the string matters?

It seems to be converted or something since its padded to the left with a ton of zeros.

How to fix this?

2
  • What's the function signature in solidity? What's tokenID javascript type?
    – Ismael
    Jan 19, 2021 at 21:04
  • 2
    Did you manage to solve this? if so how did you do it? I have the same error.
    – brandbei37
    Jul 19, 2021 at 15:40

1 Answer 1

1

The reason it is 'padded to the left with a ton of zeros' is because your return value represents a storage slot, so is represented in the total amount of bytes. With a quick check, I can affirm your getTokenURI return has 256 characters - so, this is a standard storage slot of 256 bytes; uint256.

To answer your initial question of why your contract call is failing, I'd have to know a bit more. What exactly do you mean when you say 'the contract fails'? Do you get errors? What errors do you get, if so?

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.