move icon indicating copy to clipboard operation
move copied to clipboard

[Feature Request] Add Create2 or similar features on creating a new contract from a contract

Open hskang9 opened this issue 3 years ago • 3 comments

🚀 Feature Request

Create2 is a evm opcode that simplifies creation of a contract from another contract. detailed info

Motivation

Is your feature request related to a problem? Please describe.

Create2 is often required to make dexes and it saves a memory where a contract saves.

Pitch

Solution: have a create2 like logic in sui

For example, in solidity

pragma solidity >0.4.99 <0.6.0;

contract Factory {
  event Deployed(address addr, uint256 salt);

  function deploy(bytes memory code, uint256 salt) public {
    address addr;
    assembly {
      addr := create2(0, add(code, 0x20), mload(code), salt)
      if iszero(extcodesize(addr)) {
        revert(0, 0)
      }
    }

    emit Deployed(addr, salt);
  }
}

or in cosmwasm

        return Ok(Response::new()
            .add_attribute("method", "try_create_vault")
            .add_submessage(SubMsg {
                id: 1,
                gas_limit: None,
                msg: CosmosMsg::Wasm(WasmMsg::Instantiate {
                    code_id: config.vault_code_id,
                    funds: vec![input.clone()],
                    admin: Some(env.contract.address.to_string()),
                    label: "vault".to_string(),
                    msg: to_binary(&primitives::vault::msg::InstantiateMsg {
                        vault_id: config.count,
                        manager: env.contract.address.to_string(),
                        collateral: input.clone().denom,
                        debt: config.stablecoin,
                        v1: config.v1,
                        borrow: d_amount,
                        created_at: env.block.time.seconds(),
                    })?,
                }),
                reply_on: ReplyOn::Success,
            }));

Describe alternatives you've considered

  1. Find a way to get contract compiled byte code from a move module
  2. Add a way to instantiate with encoded arguments with contract bytecode

Are you willing to open a pull request? (See CONTRIBUTING)

With grant, I think I can.

Additional context

https://decrypt.co/4427/create2-ethereum-upgrade-developer-launches-constantinople#:~:text=The%20premise%20of%20CREATE2%2C%20on,enjoy%20yourself%20now%2C%20pay%20later.

hskang9 avatar Aug 01 '22 10:08 hskang9

Could you please explain the motivation in more detail?

Create2 is often required to make dexes

There are several Move DEX implementations that work without a create2-like EVM opcodes (see https://github.com/MystenLabs/awesome-move#defi). Is there a problem with these implementations that a new opcode would fix?

it saves a memory where a contract saves.

I did not understand this part--would you mind elaborating on what this means and what problems with Move this will solve?

sblackshear avatar Aug 01 '22 16:08 sblackshear

  1. In Ethereum, there is a memory limit of smart contract code, and gas fee increases on accumulation of data related to it. To fix gas cost and memory limit on complex logics, projects EVM based chains prefer using create2 opcode. However, it seems there is no need for that in move. I am not sure how move language charges gas cost, but it is interesting to see Move does not seem to be needed to care about contract size nor gas cost state storage management.

  2. I meant a memory where a contract stores its state.

hskang9 avatar Aug 02 '22 20:08 hskang9

We try to implement this on Move but have some complex problems.

One motivation is to deploy Move modules in the Move module.

We want to update the Move modules by on-chain governance on Starcoin, try to save the Move module bytes as resources, and then deploy the modules when the upgrade proposal is passed.

But we find deploy Move bytecode in the Move module will cause a code cache problem, so we use the two-phase upgrade strategy.

https://github.com/starcoinorg/starcoin-framework/blob/98e96e6e33e7b93301bb0a341a85e3b6133ed2de/sources/PackageTxnManager.move#L85-L90

Another problem is even if we have the create2 things, we can not call the new Module after deployment(No dynamic call in Move); this makes create2 not so helpful.

But I think create2 is an interesting feature.

We use DApp to auto-generate Move code and deploy it to replace the create2.

jolestar avatar Aug 04 '22 14:08 jolestar