Error: Invalid number type

Hello! I was following along the Ethereum Dapp Programming course and I came across this error while I was coding the (Web3) “create kitty” function.
I can’t seem to fix the issue, I would appreciate it if you could help.

index.js:
Error in question is thrown at the bottom of this file

var web3 = new Web3(Web3.givenProvider);

var instance;
var user;
var contractAddress = "0xd5F8a541E1b3e41B2fc01AA6E4DBB764d51701B0";

$(document).ready(function(){
    window.ethereum.enable().then(function(accounts){
        instance = new web3.eth.Contract(abi, contractAddress, {from: accounts[0]})
        user = accounts[0];

        console.log(instance);
    })
})

function toggleColorMenu() {
    resetMenu();
    $('#eyeShape').addClass("hide");
    $('#decorationShape').addClass("hide");
    $('#animation').addClass("hide");
}
function toggleCattributesMenu() {
    resetMenu();
    $('#headColor').addClass("hide");
    $('#mouthColor').addClass("hide");
    $('#eyeColor').addClass("hide");
    $('#earColor').addClass("hide");
    $('#decorationMidColor').addClass("hide");
    $('#decorationSideColor').addClass("hide");
}
function resetMenu() {
    //Colors
    $('#headColor').removeClass("hide");
    $('#mouthColor').removeClass("hide");
    $('#eyeColor').removeClass("hide");
    $('#earColor').removeClass("hide");
    $('#decorationMidColor').removeClass("hide");
    $('#decorationSideColor').removeClass("hide");

    //Cattributes
    $('#eyeShape').removeClass("hide");
    $('#decorationShape').removeClass("hide");
    $('#animation').removeClass("hide");
}

function rndNum(min, max)
{
    return Math.floor(Math.random() * ((max+1) - min)) + min;
}

function rndKitty() {
    //Generate random colors and cattributes
    defaultDNA.headcolor = rndNum(10, 98);
    defaultDNA.mouthColor = rndNum(10, 98);
    defaultDNA.eyesColor = rndNum(10, 98);
    defaultDNA.earsColor = rndNum(10, 98);
    defaultDNA.decorationMidcolor = rndNum(10, 98);
    defaultDNA.decorationSidescolor = rndNum(10, 98);
    defaultDNA.eyesShape = rndNum(1, 5);
    defaultDNA.decorationPattern = rndNum(1, 2);
    defaultDNA.animation = rndNum(1, 4);

    renderCat(defaultDNA); //Render
}

function defaultKitty(render = true)
{
    defaultDNA = {
        "headcolor" : 96, //default: 10
        "mouthColor" : 95, //default: 13
        "eyesColor" : 72, //default: 96
        "earsColor" : 96, //default: 10
        //Cattributes
        "eyesShape" : 3, //default: 1
        "decorationPattern" : 1, //default: 1
        "decorationMidcolor" : 95, //default: 98
        "decorationSidescolor" : 95, //default: 98
        "animation" :  2, //default: 1
        "lastNum" :  1 //default: 1
        }

    if (render) { renderCat(defaultDNA); }
}

//Create Kitty
function test() {
    var dnaStr = getDna();
    instance.methods.createKittyGen0(dnaStr).send({}, function(error, txHash) //Error thrown here{
        if (error)
            console.log(error);
        
        else 
            console.log(txHash);
        
    })
}

kittyContract.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.5.16;

import "./IERC721.sol";
import "./Ownable.sol";

contract kittyContract is IERC721, Ownable {

    uint16 public constant CREATION_LIMIT_GEN0 = 10;
    string public constant token_name = "Lken";
    string public constant token_symbol = "LKN";

    event Birth (
        address owner, 
        uint256 kittenId, 
        uint256 mumId,
        uint256 dadId,
        uint256 genes
    );

    struct Kitty {
        uint256 genes;
        uint64 birthTime;
        uint32 mumId;
        uint32 dadId;
        uint16 generation;
    }

    Kitty[] kitties;

    mapping(address => uint256) ownerTokenCount;
    mapping(uint256 => address) kittyIndexToOwner;

    uint256 public gen0Counter;


    function createKittyGen0(uint256 _genes) public onlyOwner returns (uint256) {
        require(gen0Counter < CREATION_LIMIT_GEN0);

        gen0Counter++;

        return _createKitty(0, 0, 0, _genes, msg.sender);
    }

    function _createKitty(uint256 _mumId, uint256 _dadId, uint256 _generation, uint256 _genes, address _owner) private returns (uint256) {
        Kitty memory _kitty = Kitty ({
            genes: _genes,
            birthTime: uint64(block.timestamp),
            mumId: uint32(_mumId),
            dadId: uint32(_dadId),
            generation: uint16(_generation)
        });
        uint256 newKittenid = kitties.push(_kitty) - 1;

        emit Birth(_owner, newKittenid, _mumId, _dadId, _genes);
        _transfer(address(0), _owner, newKittenid);
    }

    function getKitty(uint256 _id) external view returns ( uint256 genes, uint256 birthTime, uint256 mumId, uint256 dadId, uint256 generation)
    {
        Kitty storage kitty = kitties[_id];

        genes = kitty.genes;
        birthTime = uint256(kitty.birthTime);
        mumId = uint256(kitty.mumId);
        dadId = uint256(kitty.dadId);
        generation = uint256(kitty.generation);
    }
 
    //Token standard methods

    function balanceOf(address owner) external view returns (uint256 balance) {
        return ownerTokenCount[owner];
    }

    function totalSupply() external view returns (uint256 total) {
        return kitties.length;
    }

    function name() external view returns (string memory tokenName) {
        return token_name;
    }

    function symbol() external view returns (string memory tokenSymbol) {
        return token_symbol;
    }
    
    function ownerOf(uint256 tokenId) external view returns (address owner) {
        return kittyIndexToOwner[tokenId];
    }

    function owns(uint256 tokenId) private view returns (address owner) {
        return kittyIndexToOwner[tokenId];
    }

    function transfer(address _to, uint256 _tokenId) external {
        require(_to != address(0));
        require(_to != address(this));
        require(owns(_tokenId) == msg.sender);

        _transfer(msg.sender, _to, _tokenId);
    }

    function _transfer(address _from, address _to, uint256 _tokenId) internal {
        ownerTokenCount[_to]++;

        kittyIndexToOwner[_tokenId] = _to;

        if (_from != address(0)) {
            ownerTokenCount[_from]--;
        }

        emit Transfer(_from, _to, _tokenId); 
    }

}

Error:

Uncaught Error: invalid number value (arg="_genes", coderType="uint256", value=9695729631959520)
    at Object.n [as throwError] (web3.min.js:1)
    at t.encode (web3.min.js:1)
    at web3.min.js:1
    at Array.forEach (<anonymous>)
    at N (web3.min.js:1)
    at t.encode (web3.min.js:1)
    at e.encode (web3.min.js:1)
    at i.encodeParameters (web3.min.js:1)
    at web3.min.js:1
    at Array.map (<anonymous>)
n	@	web3.min.js:1
t.encode	@	web3.min.js:1
(anonymous)	@	web3.min.js:1
N	@	web3.min.js:1
t.encode	@	web3.min.js:1
e.encode	@	web3.min.js:1
i.encodeParameters	@	web3.min.js:1
(anonymous)	@	web3.min.js:1
o._encodeMethodABI	@	web3.min.js:1
o._processExecuteArguments	@	web3.min.js:1
o._executeMethod	@	web3.min.js:1
test	@	index.js:88
onclick	@	(index):152

Hey @LTL the issue here is that you have to passs the genes values as a string instead as a number.
For that you can convert it before passing it like:

genes = genes.toString();

then pass si after.

Thank you, it worked!

1 Like