[Ethereum] BigNumber Error: new BigNumber() not a base 16 number:

bignumberjavascriptweb3js

I'm new to Ethereum and javascript so hope someone can help me with this as none of the other answers on BigNumber help me understand why this won't work for me. I've tried converting the number to a bignumber using BigNumber.js but then it tells me its no longer a number?!

The CreateNewProject function works fine (except it calls metamask twice but that's a seperate issue (feel free to answer that one as well). The main issue at the moment is with bigNumber and the getProject Function

My Smart Contract is

struct Project {
    address projectOwner;
    uint projectID;
    string projectName;
    uint goal;
    uint pledged;
    uint totalPledges;
    mapping(uint => Pledge) pledges;
}

struct Pledge {
    address pledge;
    uint pledged;
}


uint projectCounter = 1;
mapping(uint => Project) projectsList;


function CreateProject(string calldata _projectName, uint _goal) external returns (uint projectID){
    projectID = projectCounter;
    projectsList[projectID] = Project(msg.sender, projectID, _projectName, _goal, 0, 0);
    projectCounter++;
}


//This will return the details of a specified project by projectID
function getProject(uint _projectID) public view returns (address projectOwner, string memory projectName, uint goal, uint pledged) {
    require(projectCounter > 0, "No Projects Exist");
    Project memory p = projectsList[_projectID];

    return (p.projectOwner, p.projectName, p.goal, p.pledged);
}

My web3 js functions to interact with these:

function CreateNewProject() {

    var projectName = document.getElementById("ProjectName").value;
    var projectGoal = document.getElementById("ProjectGoal").value;
    web3.eth.sendTransaction(
    ProjectContractInstance.CreateProject(projectName, projectGoal, function(error, result) {
        if (!error) {
            console.log(JSON.stringify(result));
            document.getElementById("test1").innerHTML = "Project created!  Your project number is ";
            console.log(JSON.stringify("a" + a));
        } else {
            console.log(error);
            console.log("error happened");
        }
    }));

}

function GetProject() {

    const BigNumber = require('bignumber.js');

    // Set DECIMAL_PLACES for the original BigNumber constructor
    BigNumber.set({ DECIMAL_PLACES: 16 })
    let x = new BigNumber(1);

    var b = ProjectContractInstance.getProject(x ,function(error, result) {
        if(!error) {
            console.log(JSON.stringify(result));
            console.log("no error");
        } else {
            console.log(error);

            console.log("error occurred");
        }
    })
}

I cannot understand why this keeps returning:

BigNumber Error: new BigNumber() not a base 16 number: 
at T (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:289033)
at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:294599
at new r (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:286552)
at i.formatOutputString [as _outputFormatter] (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:171979)
at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:176324
at i.decode (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:176346)
at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:169300
at Array.map (<anonymous>)
at d.decodeParams (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:169273)
at u.unpackOutput (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:199674)

when i change it to base 16 using:

var hexString = (11).toString(16);

and then pass hexString into the function:

 function getProject() {

    const BigNumber = require('bignumber.js');

    // Set DECIMAL_PLACES for the original BigNumber constructor
    BigNumber.set({ DECIMAL_PLACES: 16 })
    let x = new BigNumber(1);

    var b = ProjectContractInstance.getProject(hexString ,function(error, result) {
        if(!error) {
            console.log(JSON.stringify(result));
            console.log("no error");
        } else {
            console.log(error);

            console.log("error occurred");
        }
    })
}

inpage.js:1 Uncaught BigNumber Error: new BigNumber() not a number: b
at T (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:289033)
at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:294599
at new r (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:286959)
at new r (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:286391)
at h (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:179632)
at Object.toTwosComplement (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:181223)
at i.a [as _inputFormatter] (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:170396)
at i.encode (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:175604)
at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/inpage.js:1:168053
at Array.map (<anonymous>)

So basically the first error tells me the number i am passing is not base 16 and when i change it to base 16 it tells me its no longer a number!! (no shit its now hex as requested)!!! Someone please help me understand this before i lose my mind?

As i've said if i create a new project on my browser this is created fine and i can run getProject from Remix to display the project i created through the web3 function but i cannot return the project. Please Help!!!

Best Answer

I'm not sure and I can not test but if I were you I would try to do a result.toNumber()

Edit: And for the two Metamask I would advise to remove the web3.eth.sendTransaction When you call the contract function, the use of web3 is encapsulated. I would say you are trying to send a transaction of a contract function call. Just call the contract function.

Edit2: You call the contract function with a callback so it's asynchronous. The result is in the callback, not in the return. You can either go into callback hell or use async/await syntax like : var b = await ProjectContractInstance.getProject(1);