1

How would I go about passing a user input into a smart contract? Say I want to pass a token name via user input and set it as the token name inside the smart contract. How would I do this?

2 Answers 2

2

If you are new to this, then I'd suggest starting with the Solidity documentation. You'll get an understanding of how smart contracts work from there.

After that, I'd move onto creating your own contracts using the Online Solidity Compiler.

In your specific example, play about with the contract below to get an understanding of how you'd pass a token name to a smart contract.

pragma solidity ^0.4.0;

contract TokenSample {
    address creator;
    string tokenName;

    function TokenSample(string _tokenName) public { 
        creator = msg.sender;
        tokenName = _tokenName;
    }

    function setTokenName(string x) {
        tokenName = x;
    }

    function get() constant returns (string) {
        return tokenName;
    }
}

In remix:

Passing token name by constructor

Remix example of passing string to smart contract

Once you understand the above, I'd start looking at the Web3 JS API as mentioned in another answer. Here you'll be able to provide an interface for users to interact with your contract, as well as deploy it.

tokenContract = eth.contract(contractAbi);
token = tokenContract.new(constructorParam1, {from:eth.accounts[0], data:contractCode, gas:3000000})

From there forth, you'll just be mostly using standard web development practices to continue development. i.e. JavaScript, HTML, CSS

0

Assuming you already have developed such smart contract with a function that receives a parameter and assign that value to a state variable, you would have to build a front-end, which could be a web developed with Javascript.

You would have to use the Web3 JS API to instantiate your deployed contract and execute its functions: https://github.com/ethereum/wiki/wiki/JavaScript-API

You can also use a framework such as Truffle to make the compilation, deployment and usage of the smart contracts easier: https://github.com/trufflesuite/truffle

3
  • I'm wondering how to do this with Web3 Nov 9, 2017 at 1:38
  • @JorahFriendzone what have you tried so far? Nov 9, 2017 at 1:59
  • I'm able to link the contract to Remix via web3, I'm just unsure how to pass data into the constructor Nov 9, 2017 at 5:22

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.