跳转至

01.HelloWorld

Vyper example code : https://www.vyperexamples.com/

What's Vyper

Smart contract language,

aims

  • simple
  • secure

unlike solidity

  • no overflow
  • no unbounded arrays
  • no infinite loops
  • no modifiers
  • no inheritance
  • no assembly

使用Vyper的项目:uniswap V1、curve

install

本地

本地只能编译,无法deploy

pip install vyper

vyper --version
0.3.7+commit.6020b8b

remix

Plugin manager搜索vyper安装

HelloWorld

# @version >=0.2.16
# @notice Simple greeting contract

# @notice Returns the string "Hello World!"
# @notice The @external decorator means this function can only be called by external parties ie. by other contracts or by a wallet making a transaction
# @notice The @view decorator means that this function can read the contract state but not alter it. Cannot consume gas.
@external
@view
def helloWorld() -> String[24]:
    return "Hello World!"

@external:只可以被外部调用

@view:这个function只能读取contract state,不能修改它,cannot consume gas/

本地编译

vyper helloworld.vy

0x6100b961000f6000396100b96000f36003361161000c576100a1565b60003560e01c346100a757632f2f4859811861009f57600436106100a757602080608052600c6040527f48656c6c6c6f20576f726c64000000000000000000000000000000000000000060605260408160800181518082526020830160208301815181525050508051806020830101601f82600003163682375050601f19601f8251602001011690509050810190506080f35b505b60006000fd5b600080fda165767970657283000307000b

remix编译部署

image-20231124141025052

编译

image-20231124141045483

First App

# @version >=0.2.2
# @notice Simple contract for getting, incrementing and decrementing a counter

# @var The counter
# @dev Vyper automatically creates a getter method when you set a variable as public
# @dev We don't even really need the get() function we made
# @dev uint256 is set to the default value of 0 when the contract is deployed
counter: public(uint256)

# @notice Returns the counter
# @return The current count
@external
@view
def get() -> uint256:
    return self.counter

# @notice Increment count by 1
@external
def inc():
    self.counter += 1

# @notice Decrement count by 1
# @dev Will fail if count is 0
@external
def dec():
    self.counter -= 1