Skip to content

OwnerOf 721 and 1155 type error with integers #701

@sneakzttv

Description

@sneakzttv

Owner of 721 and 1155 methods work fine with a string as the token id "0x23123123" etc but the call fails when using an integer. We should be inputting this as a string and then trying to parse it as an int if it doesn't start with "0x".

You can see a working example below on adil mainnet with a modified owner of method that takes an integer as a parameter, if the contract data builds the token id as a string it throws an error saying the token doesn't exist. We need to account for both cases here.

using System.Threading.Tasks;
using ChainSafe.Gaming.UnityPackage;
using UnityEngine;
using Web3Unity.Scripts.Prefabs;
using ABI = ChainSafe.Gaming.UnityPackage.ABI;

/* This prefab script should be copied & placed on the root of an object.
Change the class name, variables and add any additional changes at the end of the execute function.
The initialize function should be called by a method of your choosing */

/// <summary>
/// Fetches the owner of an ERC721 token id
/// </summary>
public class OwnerOfTest : MonoBehaviour
{
    // Variables
    private string contractAddress = "0xcb775d2E24eFE447379Fd6249870922A327dec41";
    private int tokenId = 431464;
    private string _abi = ABI.ERC_721;

    /// <summary>
    /// Starts the task, you can put this in the start function or call it from a button/event
    /// </summary>
    public async void InitializeTask()
    {
        await ExecuteTask();
    }

    /// <summary>
    /// Executes the prefab task and sends the result to the console, you can also save this into a variable for later use
    /// </summary>
    private async Task ExecuteTask()
    {
        var balance = await OwnerOf(contractAddress, tokenId);
        SampleOutputUtil.PrintResult(balance.ToString(), nameof(Erc721Sample), nameof(Erc721Sample.OwnerOf));
    }
    
    /// <summary>
    /// Modified owner of call to use an int instead of a string
    /// </summary>
    /// <param name="contractAddress"></param>
    /// <param name="tokenId"></param>
    /// <returns></returns>
    private async Task<string> OwnerOf(string contractAddress, int tokenId)
    {
        var method = "ownerOf";
        var contract = Web3Accessor.Web3.ContractBuilder.Build(_abi, contractAddress);
        var contractData = await contract.Call(method, new object[]
        {
            tokenId
        });
        return contractData[0].ToString();
    }
}

Proposed fix:
Add a check to see if the string begins with "0x", if not parse it into an integer and place the result into the contract call object.

Metadata

Metadata

Labels

Type: BugAdded to issues and PRs if they are addressing a bug

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions