Übergeben von Daten an Smart Contract über Konstruktor

Ist es möglich, Daten an den Konstruktor zu übergeben, die für den Smart Contract verwendet werden sollen? Im Wesentlichen möchte ich einen Benutzer am Frontend Daten eingeben lassen, die zum Generieren eines Tokens verwendet werden.

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;
}

Wie könnte ich das auf web3/React machen?

Antworten (1)

Das Übergeben von Daten an einen Smart Contract über den Konstruktor unterscheidet sich nicht von der Bereitstellung eines Vertrags ohne Daten, die im Konstruktor übergeben werden. von Dok .

Sie müssen nur Ihre Daten als Ihre ersten Parameter übergeben, bevor Sie Konfigurationen vornehmen (von, Gas, Daten ...)

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

Dann müssen Sie mit React nur ein Formular oder so etwas erstellen, um die Werte Ihres Clients zu erhalten, und dann an eine Funktion übergeben, die den Vertrag wie oben bereitstellt, aber das hat nichts mit web3 zu tun