I am curious to know if there is a way to do metaprogramming in Solidity. My use case is about generating a smart contract from user inputted data.


I'm not sure about the concrete theoretical underpinnings, but if by metaprogramming you mean 'code that writes code', then yes you can do metaprogramming in Ethereum.

Solidity running on Ethereum Virtual Machine (EVM) allows following mechanisms to the programmers:

  • Creating new objects on the fly. This also means creating objects (loosely speaking contracts) which also get deployed on the EVM.
  • Accessing attributes of objects.
  • Passing attributes during object creation.

Now with these ingredients, you can create your own 'metaprogramming' recipes in Solidity. Imagination is the limit! Also, remember that EVM is Turing Complete, and above-mentioned features are available to the programmers in Solidity. Additionally, Solidity was designed for Ethereum EVM, and it was influenced by C++, Python and JavaScript, all of which have mechanisms allowing a rich (sort of) metaprogramming experience.

Based on what you asked, here's a sample case:

// A very simple Money contract. It knows its name and territory.

contract Money {
    string name;
    string territory;
    constructor(string _name, string _territory) public {
        name = _name;
        territory = _territory;
    function moneyName() public constant returns (string){
        return name;

The Banker contract is creating different kinds of money on the fly.

contract Banker {
    string name;
    address[] public contracts;

    constructor(string _name) public {
        name = _name;
    function createMoney (string _name, string _territory) public{
        Money m = new Money(_name, _territory);
    function howMany() public constant returns (uint){
        return contracts.length;
    function showMoney(uint index) public constant returns (address) {
        assert (index < contracts.length);

        // You can use the address returned to fetch the money contract.
        return contracts[index];



In my personal opinion, unless there's a strong reason to do so, Metaprogramming in the Ethereum would a stretch which will generally come at some cost and there is a reason the community is not too much into it. In one of the his essays, Paul Graham beautifully puts the idea of metaprogramming along the lines that the benefit of writing, say a template for metaprogramming, should outweigh the simplicity of not using it. In my opinion, there are two things you should consider when implementing metaprogramming in writing contracts (see, it's so unnatural even to call it programming)- obfuscation vs simplicity, and the cost of deploying a contract for something which could be easily treated as data. But again, there could be some very strong use cases too. Keep us guided :)

  • that makes sense. So how would I create a contract on the fly from user input? I feel like that would require a bunch of ugly switch statements.... – Cyzanfar Oct 22 '18 at 15:28
  • @Cyzanfar maybe you can edit your question with extra details? – DaveIdito Oct 22 '18 at 15:47

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.