0

I am trying to deploy a smart contract using BrowserSolc and Metamask. and deployed successfully when the contract doesn't have constructor parameters but now I am trying to deploy with the constructor parameter but unable to do it.

How to add a parameter to the send_ethTransaction method

const source = "pragma solidity 0.4.24;contract SujiToken { string public name = 'Suji';    string public symbol = 'SUJI';  string public standard = 'Suji Token v1.0'; uint256 public totalSupply; event Transfer(     address indexed _from,      address indexed _to,        uint256 _value      );  event Approval(     address indexed _owner,     address indexed _spender,       uint256 _value      );  mapping(address => mapping(address => uint256)) public allowance;   mapping(address => uint256) public balanceOf;   constructor(uint256 _amount) public{ require(_amount >0,'amount should be greater than zero');      totalSupply = _amount;      balanceOf[msg.sender] = totalSupply;    }   function transfer(address _to, uint256 _value) public payable returns (bool success){       require(balanceOf[msg.sender] >= _value);       balanceOf[msg.sender] -= _value;        balanceOf[_to] += _value;       emit Transfer(msg.sender,_to,_value);       return true;    }   function approve(address _spender, uint256 _value) public returns (bool success){       allowance[msg.sender][_spender] = _value;       emit Approval(msg.sender,_spender,_value);      return true;    }   function transferFrom(address _from, address _to, uint _value) public returns (bool success) {      require(_value <= balanceOf[_from]);        require(_value <= allowance[_from][msg.sender]);        balanceOf[_from] -= _value;     balanceOf[_to] += _value;       allowance[_from][msg.sender] -= _value;     emit Transfer(_from, _to, _value);      return true;    }}"
BrowserSolc.loadVersion("soljson-v0.4.24+commit.e67f0147.js", async(compiler) => {
    // console.log(compiler);
    optimize = 1;
    var result = compiler.compile(source,optimize);
    console.log(result);
    var byteCode = result.contracts[':SujiToken'].bytecode;
    // console.log(byteCode);
    if(window.ethereum){
        var web3 = new Web3(ethereum);
        await ethereum.enable();
        const params = {
            "type" : "ERC20",
            "from": ethereum.selectedAddress,
            "gasPrice": "750000", // 10000000000000
            "data": "0x"+byteCode
        };
        ethereum.sendAsync({
            method: 'eth_sendTransaction',
            params: [params],
            from: ethereum.selectedAddress, // Provide the user's account to use.
        }, function (err, result) {
            console.log(result);
            if(result){
                $('input[contract_hash]').val(result.result);
                //$('form').submit();
            }
        })
        // ethereum.send
        // console.log(window.web3);
    }else{
        console.log("Non ethereum browser has been detected");
    }
});

this is the code i am trying to implement.

0

You can pass parameters of the constructor during deployment like this:

// Instantiate from an existing address:
var myContractInstance = MyContract.at(myContractAddress);
// Or deploy a new contract:
// Deploy the contract asynchronous from Solidity file:
...
const fs = require("fs");
const solc = require('solc')
let source = fs.readFileSync('nameContract.sol', 'utf8');
let compiledContract = solc.compile(source, 1);
let abi = compiledContract.contracts['nameContract'].interface;
let bytecode = compiledContract.contracts['nameContract'].bytecode;
let gasEstimate = web3.eth.estimateGas({data: bytecode});
let MyContract = web3.eth.contract(JSON.parse(abi));
var myContractReturned = MyContract.new(param1, param2, {
  from:mySenderAddress,
  data:bytecode,
  gas:gasEstimate}, function(err, myContract){
   if(!err) {
      // NOTE: The callback will fire twice!
      // Once the contract has the transactionHash property set and once its deployed on an address.
       // e.g. check tx hash on the first call (transaction send)
      if(!myContract.address) {
          console.log(myContract.transactionHash) // The hash of the transaction, which deploys the contract

      // check address on the second call (contract deployed)
      } else {
          console.log(myContract.address) // the contract address
      }
       // Note that the returned "myContractReturned" === "myContract",
      // so the returned "myContractReturned" object will also get the address set.
   }
 });
// Deploy contract syncronous: The address will be added as soon as the contract is mined.
// Additionally you can watch the transaction by using the "transactionHash" property
var myContractInstance = MyContract.new(param1, param2, {data: bytecode, gas: 300000, from: mySenderAddress});
myContractInstance.transactionHash // The hash of the transaction, which created the contract
myContractInstance.address // undefined at start, but will be auto-filled later

For more information about this, you can read this document.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.