Back to glossary

Create New Contract (Vyper Code Example)

Table of Contents

Vyper contracts can deploy new contracts using the function create_minimal_proxy_to.

create_minimal_proxy_to is also known as "minimal proxy contract". How it works, we won't explain it here. Instead, we will focus on how to use it to deploy new contracts.

How to use create_minimal_proxy_to

  1. Deploy ContractToDeploy. This is the "master copy." All deployed contracts will execute code from this master copy.
  2. Call deploy() passing the address of the master copy and any other arguments needed to setup the new contract

Example ContractToDeploy in Vyper

ContractToDeploy.vy

# pragma version ^0.4.0

owner: public(address)

# create_minimal_proxy_to
# 1. Deploy master copy
# 2. Call create_minimal_proxy_to

# Master copy contract M (Test.vy)
#                      |
#                      V
# Factory -- create_minimal_proxy_to --> contract A (Test.vy)

# user -> A -- delegate call --> M

# __init__ is not called when deployed from create_minimal_proxy_to
@deploy
def __init__():
  self.owner = msg.sender

# call once after create_minimal_proxy_to
@external
def set_owner(owner: address):
  assert self.owner == empty(address), "owner != zero address"
  self.owner = owner

# DANGER: never have selfdestruct in original contract used by create_minimal_proxy_to
# This function has been deprecated from version 0.3.8 onwards. The underlying
# opcode will eventually undergo breaking changes, and its use is not recommended.
@external
def kill():
  selfdestruct(msg.sender)

Create.vy

pragma version ^0.4.0

interface ContractToDeploy:
    def set_owner(owner: address): nonpayable

event Log:
    addr: address

@external
def deploy(master_copy: address, owner: address):
    addr: address = create_minimal_proxy_to(master_copy)
    extcall ContractToDeploy(addr).set_owner(owner)
    log Log(addr)

@external
def deploy_test(master_copy: address):
    addr: address = create_minimal_proxy_to(master_copy)
    extcall ContractToDeploy(addr).set_owner(self)
    log Log(addr)

Related Terms

No items found.