2

I'm trying to deploy the following smart contract:

pragma solidity ^0.5.00;

contract TestContract {

   address public owner;
   string public word;

   constructor(string memory _word) public{
      owner = msg.sender;
      word = _word;
   }

   modifier onlyOwner() {
      require(msg.sender == owner);
      _;
   }

   function getWord() public view returns (string memory) {
      return(word);
   }

   function updateWord(string memory _word) public onlyOwner {
      word = _word;
   }
}

But I'm having trouble deploying it with the constructors parameters. I'm using MetaMask and web3.js (that I got from here: https://github.com/ethereum/web3.js/blob/develop/dist/web3.js) with the following test code:

var Web3 = require('web3');
var web3 = new Web3(web3.currentProvider);

var code = '0x608060405234801561001057600080fd5b506040516106b...';

var abi = JSON.parse('[{"constant":true,"inputs":[],"name":"word","outputs":...');

var myContract;
function createExampleContract() {

    document.getElementById('create').style.visibility = 'hidden';
    document.getElementById('code').innerText = code;

    web3.eth.defaultAccount = web3.eth.coinbase;
    document.getElementById('status').innerText = "transaction sent, waiting for confirmation";

    web3.eth.contract(abi).new({data: code}, function (err, contract) {
        if(err) {
            console.error(err);
            return;
        } else if(contract.address){
            myContract = contract;
            console.log('address: ' + myContract.address);
            document.getElementById('status').innerText = 'Mined!';
            document.getElementById('call').style.visibility = 'visible';
        }
    });
}

function callExampleContract() {
    var param = parseInt(document.getElementById('value').value);

    myContract.getWord(param, function(err, result) {
      if (err) {
        console.log('e: ' + err);
      } else {
        console.log('r: ' + result);
      }
    });
}

I got the code and abi with this two terminal commands solc --bin contract.sol and solc --abi contract.sol

I was trying arguments:['param'] after data:code and the contract deployment goes through but when I call the getWord method, this error shows up BigNumber Error: new BigNumber() not a base 16 number and searching on-line I found out that this may accour due to null values returning from the method call. So how do I deploy the contract with constructor arguments properly?

Thank you all!! =)

2 Answers 2

2

Looking at the web3 v0.20.xx documentation, the constructor parameters go just before the {data:code} like this:

web3.eth.contract(abi).new(param1,param2,{data:code}, callback);

Hope this helps for future references!

2

Referncing the documentation: web3.js v1.3.1
solc version: 0.8.0
web3.js version: 1.3.1

Suppose following is your smart contract:

contract myContract{
    constructor(int a, string memory str){
        // Some code using the arguments.
    }
}

Following is how you deploy the contract:

var intObj = 10;
var strObj = 'Hello';
contract = await new web3.eth.Contract(compiledContract.abi)
    .deploy({ 
        data: compiledContract.bytecode, 
        arguments: [intObj, strObj] // Writing you arguments in the array
    })
    .send({ from: accounts[0], gas: minGas });
    

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.