How to make a cloud function that deploys a smart contract?

I want to deploy a new copy of a smart contract and transfer the ownership to each user that authenticates to my site for the first time.

How would I go about doing this? I am thinking some combination of cloud functions and ethers or web3 but I am not sure.

You could do that in theory. You will have to use a custom RPC url, web3. The problem could be that you will need to hardcode the private key in cloud code and we donโ€™t recommend this.

Any specific reason? What if it is an account that only exists to deploy?

for possible security reasons, there is a higher chance for that private key to get leaked that way

you could you a separate server for that, if you have one

1 Like

Thereโ€™s a way to deploy a contract with another contract and maybe do some fixes with that instead of using the cloud function way.

Hereโ€™s an example

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

contract D {
    uint public x;

    constructor(uint a) {
        x = a;
    }

    function setX(uint _x) public {
        x = _x;
    }
}

contract C {
    uint public y;
    // D public createdD;

    constructor(uint _y) {
        y = _y;
    }
    
    function createD(uint arg) public returns (D) {
        D newD = new D(arg);
        return newD;
    }

    function setY(uint _y) public{
        y = _y;
    }
}
1 Like