1

Is it possible to pass data into the constructor to be used for the smart contract? Essentially I want to let a user on the front end enter data that will be used to generate a token.

contract TicketToken is ERC20 {

    string public symbol;
    string public name;
    uint256 public price;

    function TicketToken(string _name, string _symbol, uint256 _price) {
        owner = msg.sender;
        name = _name;
        symbol = _symbol;
        price = _price;
}

How would I be able to do this on web3/React?

2

Passing data to a smart contract via constructor is not different than deploying a contract without data passed in the constructor. from doc.

You just need to passe your data as your firsts parameters before setting configurations (from, gas, data ...)

// 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.
    }
  });

Then with React you'll just have to create a form or something to get your client's values and pass then to a function which will deploy the contract like above, but this is kind of unrelated to web3

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.