Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent of Solidity constructor #99

Open
afosan opened this issue Jan 27, 2024 · 2 comments
Open

Equivalent of Solidity constructor #99

afosan opened this issue Jan 27, 2024 · 2 comments

Comments

@afosan
Copy link
Contributor

afosan commented Jan 27, 2024

Is there an equivalent of Solidity constructor in Stylus? If not, is there any Stylus specific pattern you would suggest to achieve the same behavior?

I want to set a state variable to a value from msg context or passed as an argument to the constructor. Sharing a simple example Solidity contract to illustrate.

contract Example {
    address public owner;
    uint256 public num;

    constructor(uint256 _num) {
        owner = msg.sender;
        num = _num;
    }
}

edit: I have found an example using init function and initialized variable. It still seems possible that someone frontruns you to call init function (e.g. to become "owner" of the contract). Another example hardcodes the "owner" to prevent that.

edit2: I have just found the part about constructors here. I feel it might be helpful to have a link to it under "Developer reference" section.

@rauljordan
Copy link
Contributor

Hi @afosan , one pattern that could work for you today would be to use a multicall program to both deploy and activate in a single tx. Here's how:

You can deploy a simple multicall program our team wrote in Rust for Stylus here.

Then, you can prepare a deployment + activation in a single tx and interact with this multicall program:

For the calldata needed to do a deployment, see here. For the calldata to do an activation, see here.

To send a deployment tx, send the calldata for deployment to a nil address. To do an activation, send the activation calldata to the ARB_WASM_ADDRESS. You can put these together according to how the multicall program requires them and send them. This will guarantee atomic deploy + activation in one tx

@afosan
Copy link
Contributor Author

afosan commented Feb 2, 2024

Thanks for the explanation @rauljordan!

I also thought about employing multicall to achieve atomicity (not just for deployment and activation, also for "initialization" of the contract, e.g. where the owner is set). In fact, created an issue on multicall repo and it is now deployed on Stylus testnet here.

I think both Multicall3 and Stylus implementation actually need modification though. Multicall3 always uses call and Stylus implementation can employ other call types as well (e.g. staticcall, delegatecall) but neither actually uses create or create2 opcode. I am working on a Stylus contract right now which can perform deploy, activate and (optionally) initialize in a single tx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants