Conflux-Chain / ConfluxWeb

Conflux JavaScript API, under active development
GNU Lesser General Public License v3.0
7 stars 5 forks source link

Error: insufficient data for uint256 type #6

Closed saarshah closed 4 years ago

saarshah commented 4 years ago

I am trying to deploy contract and then calling its getter and setter functions. I can deployed my contract through program and got its address and then i feed this newly got address to my getter and setter functions, it become error (given below). However, if i just saved that address as hard coded, then my all program works well as expected. Error:

{ Error: insufficient data for uint256 type (arg="", coderType="uint256", value="0x", version=4.0.39)
    at Object.throwError (C:\Users\amirali\Desktop\temp\node_modules\ethers\errors.js:76:17)
    at CoderNumber.decode (C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:403:20)
    at C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:688:32
    at Array.forEach (<anonymous>)
    at unpack (C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:680:12)
    at CoderTuple.decode (C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:807:22)
    at AbiCoder.decode (C:\Users\amirali\Desktop\temp\node_modules\ethers\utils\abi-coder.js:956:61)
    at Method.decode (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\src\contract.js:113:27)
    at Called.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\src\contract.js:69:24)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  reason: 'insufficient data for uint256 type',
  code: 'INVALID_ARGUMENT',
  arg: '',
  coderType: 'uint256',
  value: '0x' }

and here is my program

async function main() {

for(r=0; r<sources.length; r++) {
  var addresDeployed = await deployCon(r);
  console.log("============Contract of "+r+" received with address ", addresDeployed ); // here i can print my deployed contract's address
//   fixedAddress = '0xabb58549df616bf70658a7b28ee430ca74a60c67'
  console.log("============================================Getter Functions of Contract No. ", r);
  var myget = await genericGetterFunction(addresDeployred, r);

  console.log("===============================================Setter Functions of Contract No. ", r);
  var myset =await genericSetterFunction(addresDeployred, r)

  console.log("================================================Again Getter Functions of Contract No. ", r);
  var myget1 = await genericGetterFunction(addresDeployred, r);

  console.log("Contract no. : "+r+ "  completed !")
}
}

main().catch(e => console.error(e));

  ////////////////// deploy contract  ///////////////

async function deployCon(key) {

abi = sources[key].abi;
code = "0x"+sources[key].bytecode;

const contract = cfx.Contract({ abi, code });
const estimateDeployGas = await contract.constructor(constructorParameters[key].input[0]).estimateGas();

 const contractAddress = await contract.constructor(constructorParameters[key].input[0])
 .sendTransaction({
   from:account, 
   gas: estimateDeployGas, 
 })
 .deployed();
console.log("Address of Contract: ", contractAddress);
return contractAddress;
}

//////////////////  getters //////////////////////
async function genericGetterFunction(myContractAddress, key){
  abi = sources[key].abi;
  code = "0x"+sources[key].bytecode;

  const myContract = cfx.Contract({ abi, code });
  myContract.address = myContractAddress;

  for(j=0; j < getterFunctionsNameObject[key].length; j++){
      myFunc = getterFunctionsNameObject[key][j].func;
          res = await myContract[myFunc]();
  console.log(" Getter output", res);
  }

  }

//////////////////  setters //////////////////////
async function genericSetterFunction(myContractAddress, key){
  abi = sources[key].abi;
  code = "0x"+sources[key].bytecode;
  const myContract = cfx.Contract({ abi, code });
  myContract.address = myContractAddress;

  for(j=0; j < setterFunctionsObject[key].length; j++){
    myFunc = setterFunctionsObject[key][j].func;
    res = await myContract[myFunc](...setterFunctionsObject[key][j].input)
    console.log("Setter Output", res);
  }

  }

UPDATED: I checked my contract (see below). I haved also called directly getTotalSupply function , still same error..

code to deploy and immediately called its function

async function deployCon(key) {

abi = sources[key].abi;
code = "0x"+sources[key].bytecode;

const contract = cfx.Contract({ abi, code }); 
const estimateDeployGas = await contract.constructor(...constructorParameters[key].input).estimateGas();

 const contractAddress = await contract.constructor(...constructorParameters[key].input)
 .sendTransaction({
   from:account, 
   gas: estimateDeployGas, 
 })
 .deployed();
console.log("Address of Contract: ", contractAddress);
contract.address = contractAddress;

console.log(await contract.getTotalSupply());
return contract;
}

MyContract.sol

pragma solidity ^0.5.12;

contract MyContract {

    uint256 public  totalSupply ; 
    mapping( address => uint256) public  balances ;
    address payable public owner;

    constructor(address payable _wallet) public payable {
        totalSupply = 6;
        owner = _wallet;
    }

    function () external payable{
        buyToken();
    }

    function buyToken() public payable {
        require(totalSupply >= (msg.value/1000000000000000000)*2);
        balances[msg.sender] += (msg.value/1000000000000000000)*2;
        totalSupply -=(msg.value/1000000000000000000)*2;
    }

    function getTotalSupply()public view returns  (uint256 ){
        return totalSupply;
    }
       function setTotalSupply(uint256 newSupply) public returns  (uint256 )  {
        totalSupply = newSupply;
        return totalSupply;        
    }
}
GeekBerry commented 4 years ago

Error: insufficient data for uint256 type (arg="", coderType="uint256", value="0x", version=4.0.39) It look like you called a method which should return a "uint256" but got "0x", and abi-docoder can not handle it and throw insufficient data for uint256 type.

saarshah commented 4 years ago

I checked my contract (see below). I have also called directly getTotalSupply function , still same error..

code to deploy and immediately called its function

async function deployCon(key) {

abi = sources[key].abi;
code = "0x"+sources[key].bytecode;

const contract = cfx.Contract({ abi, code }); 
const estimateDeployGas = await contract.constructor(...constructorParameters[key].input).estimateGas();

 const contractAddress = await contract.constructor(...constructorParameters[key].input)
 .sendTransaction({
   from:account, 
   gas: estimateDeployGas, 
 })
 .deployed();
console.log("Address of Contract: ", contractAddress);
contract.address = contractAddress;

console.log(await contract.getTotalSupply());
return contract;
}

MyContract.sol

pragma solidity ^0.5.12;

contract MyContract {

    uint256 public  totalSupply ; 
    mapping( address => uint256) public  balances ;
    address payable public owner;

    constructor(address payable _wallet) public payable {
        totalSupply = 6;
        owner = _wallet;
    }

    function () external payable{
        buyToken();
    }

    function buyToken() public payable {
        require(totalSupply >= (msg.value/1000000000000000000)*2);
        balances[msg.sender] += (msg.value/1000000000000000000)*2;
        totalSupply -=(msg.value/1000000000000000000)*2;
    }

    function getTotalSupply()public view returns  (uint256 ){
        return totalSupply;
    }
       function setTotalSupply(uint256 newSupply) public returns  (uint256 )  {
        totalSupply = newSupply;
        return totalSupply;        
    }
}

I have also tested the code of example, (which you provided earlier).. it also giving same error...