0

I would like to create a smart contract based on user input. User would provide some information/data from the UI, and based on the information provided my smart contract logic would get defined, and then the smart contract would get deployed. Is it possible to achieve this?

1

Thanks for the answer. One more query. Is it possible to dynamically define the functions of smart contract based on user input. Meaning, my smart contract would be empty initially, and based on user input(from the front end), my solidity functions would get defined.

you cannot add or remove the functions into an already deployed contract. But a possible solution to your problem can be providing multiple contracts according to the users input.

Inputs from users can be passed in a contract as constructor parameters.

pragma solidity >0.4.99 <0.6.0;
contract D {
    uint public x;
    constructor(uint a) public payable {
        x = a;
    }
}
contract C {
    D d = new D(4); // will be executed as part of C's constructor
    function createD(uint arg) public {
        D newD = new D(arg);
        newD.x();
    }
    function createAndEndowD(uint arg, uint amount) public payable {
        // Send ether along with the creation
        D newD = (new D).value(amount)(arg);
        newD.x();
    }
}
1

Of course yes, even if with some limitations.

You can define the contract template on the basis of some parameters, which can be addresses, numbers, strings. Then you can instance the contract using the keyword “new” passed to a second contract which deploy the final one using “new” and passing to its constructor the parameters (param1, param2...)

On that basis you can, for instance,

  • define the flow of the program by means of “if(param1==whatever... then... else... etc
  • cancel functions contained in the contract by means of a first line saying “require (param1 != whatever on line 1 of the function (any function which ever revert if called is functionally equivalent to a not existent function)
  • give dimension to arrays
  • choose some equation to be used in a set
  • and so on

What is not possible nor useful in Solidity is to compose the contract to be compiled and deployed on a textual basis, like a text editor I mean: String manipulation is poor and costly in EVM.

That textual approach you are asking for in the comment below is that I defined ‘textual’, I.e. you “compose” your contract and, then, you can compile and deploy it without “not-useful parts or modules” in it. This cannot be done in Solidity, but it can be done in JavaScript if you absolutely need it. There you can have your web based UI, compose the contract as you like and require, compile/deploy it. It will be on blockchain forever working the way you defined and your users can have a different contract from your JavaScript based... juke box 😊

I hope this short answer can be understood and useful.

  • Thanks for the answer. One more query. Is it possible to dynamically define the functions of smart contract based on user input. Meaning, my smart contract would be empty initially, and based on user input(from the front end), my solidity functions would get defined. – Sumit Maiti Jan 4 at 6:18
  • Added extended comment to the answer – Rick Park Jan 4 at 9:44
0

If you mean you want to deploy contracts from a template with some values passed in as parameters, this is a common pattern.

Have a look over here: Deploy contract from contract in Solidity

You can use MyContract c = new MyContract(args); to pass in the parameters the template constructor requires.

Hope it helps.

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.