Skip to content

Latest commit

 

History

History
77 lines (52 loc) · 1.74 KB

创建新合约.md

File metadata and controls

77 lines (52 loc) · 1.74 KB

创建新合约

Vyper 合约可以使用函数create_forwarder_to部署新合约。 create_forwarder_to也被称为“最小代理合约”。 它是如何工作的,我们不会在这里解释。 在这里,我们将重点介绍如何使用它来部署新合约。

如何使用create_forwarder_to

部署 ContractToDeploy。 这就是“主副本”。 所有已部署的协定都将执行此主副本中的代码。 调用 deploy() 传递主副本的地址和设置新合约所需的任何其他参数

这是合约部署:

ContractToDeploy.vy

# @version ^0.3.7


owner: public(address)


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


# call once after create_forwarder_to
@external
def setup(owner: address):
  assert self.owner == ZERO_ADDRESS, "owner != zero address"
  self.owner = owner


# DANGER: never have selfdestruct in original contract used by create_forwarder_to
@external
def kill():
  selfdestruct(msg.sender)

Create.vy

# @version ^0.3.7


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


event Log:
    addr: address


@external
def deploy(_masterCopy: address, owner: address):
    addr: address = create_forwarder_to(_masterCopy)
    ContractToDeploy(addr).setup(owner)
    log Log(addr)


@external
def deployTest(_masterCopy: address):
    addr: address = create_forwarder_to(_masterCopy)
    ContractToDeploy(addr).setup(self)
    log Log(addr)

关注我们

Yanbo的TwitterWeb3Club的Twitter

加入Web3Club 官方讨论群:YanboTravelAllWorld(微信号)

加入我们