Skip to content

Latest commit

 

History

History
99 lines (67 loc) · 1.77 KB

接口.md

File metadata and controls

99 lines (67 loc) · 1.77 KB

接口

使用接口调用其他智能合约。

下面是我们将调用的TestInterfaceself.act。

TestInterface.vy

# @version ^0.3.7


owner: public(address)
eth: public(uint256)


@external
def setOwner(owner: address):
    self.owner = owner


@external
@payable
def sendEth():
    self.eth = msg.value


@external
@payable
def setOwnerAndSendEth(owner: address):
    self.owner = owner
    self.eth = msg.value

Interface.vy

# @version ^0.3.7


interface TestInterface:
    # get address of owner
    def owner() -> address: view
    # set new owner
    def setOwner(owner: address): nonpayable
    # send ETH
    def sendEth(): payable
    # set owner and send ETH
    def setOwnerAndSendEth(owner: address): payable


# store contract having the above interface
test: public(TestInterface)


@external
def __init__(test: address):
    # store contract instance
    self.test = TestInterface(test)
    # if you need to get address from self.test
    addr: address = self.test.address


@external
@view
def getOwner() -> address:
    return self.test.owner()


@external
@view
def getOwnerFromAddress(test: address) -> address:
    # you can also call functions by passing in the address of the interface
    return TestInterface(test).owner()


@external
def setOwner(owner: address):
    self.test.setOwner(owner)


@external
@payable
def sendEth():
    self.test.sendEth(value=msg.value)


@external
@payable
def setOwnerAndSendEth(owner: address):
    self.test.setOwnerAndSendEth(owner, value=msg.value)

关注我们

Yanbo的TwitterWeb3Club的Twitter

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

加入我们