TransWikia.com

Building a recursive smart contract -- is it possible?

Ethereum Asked by user491880 on November 18, 2021

I would like to create a sort of tree type hierarchy of contracts. Think of it like a multi-level-marketing scheme — the person at the top will receive X. For each of the k people he recruits he agrees to give them a payout alpha*X / k …. given the condition that they recruit at least k more people to the same type of contract.

I would like the nodes to be transferable — so someone could sell any of the payouts associated with node contracts to someone else if they like (I’m assuming this is true of all smart contracts? I’m new ethereum)

Could you point me towards how one would implement such a structure?

One Answer

What you're looking for is the factory contract storage pattern, and not a "recursive" smart contract. Recursion doesn't quite make sense here.

The idea is that you have a contract that can deploy contracts. Below is an example of a CampaignFactory contract that deploys Campaign contracts. What you'd want to do then is add a linked list feature to each new campaign.

You could add the Multi-level marketing aspect by allowing each additional Campaign to also have the functionality of the CampaignFactory, as well as have a variable like address creator that points back to the original creator.

It would then be pretty easy to send the MLM money 'up' the chain.

contract CampaignFactory {

    Campaign[] campaigns;
    address creator;
    mapping(address => Campaign[]) campaignsByOwner;

    function createCampaign(uint min_contrib) public {
        Campaign newCampaign = new Campaign(min_contrib, msg.sender);
        campaignsByOwner[msg.sender].push(newCampaign);
        campaigns.push(newCampaign);
    }

    function getAllCampaigns() public view returns (Campaign[] memory) {
        return campaigns;
    }

    function getCampaignByAddress(address creator) public view returns (Campaign[] memory) {
        return campaignsByOwner[creator];
    }
}

Answered by Patrick Collins on November 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP