Built in Functions

Vyper contains a set amount of built in functions that would be timely and/or unachievable to write in Vyper.



def floor(a) -> b:
  :param a: value to round down
  :type a: decimal

  :output b: int128

Rounds a decimal down to the nearest integer.


def ceil(a) -> b:
  :param a: value to round up
  :type a: decimal

  :output b: int128

Rounds a decimal up to the nearest integer.


def convert(a, b) -> c:
  :param a: value to convert
  :type a: either bool, decimal, int128, uint256 or bytes32
  :param b: the destination type to convert to
  :type b: type of either decimal, int128, uint256 or bytes32

  :output c: either decimal, int128, uint256 or bytes32

Converts a variable/ literal from one type to another.


def as_wei_value(a, b) -> c:
  :param a: value for the ether unit
  :type a: uint256 or int128 or decimal
  :param b: ether unit name (e.g. ``"wei"``)
  :type b: str_literal

  :output c: wei_value

The value of the input number as wei, converted based on the specified unit.


def as_unitless_number(a) -> b:
  :param a: value to remove units from
  :type a: either decimal or int128

  :output b: either decimal or int128

Turns a int128, uint256, decimal with units into one without units (used for assignment and math).


def slice(a, start=b, len=c) -> d:
  :param a: bytes to be sliced
  :type a: either bytes or bytes32
  :param b: start position of the slice
  :type b: int128
  :param c: length of the slice
  :type c: int128

  :output d: bytes

Takes a list of bytes and copies, then returns a specified chunk.


def len(a) -> b:
  :param a: value to get the length of
  :type a: bytes

  :output b: int128

Returns the length of a given list of bytes.


def concat(a, b, ...) -> c:
  :param a: value to combine
  :type a: bytes
  :param b: value to combine
  :type b: bytes

  :output b: bytes

Takes 2 or more bytes arrays of type bytes32 or bytes and combines them into one.

sha3/ keccak256

def sha3(a) -> b:
  :param a: value to hash
  :type a: either str_literal, bytes, bytes32

  :output b: bytes32

Returns keccak256``(Ethereum's sha3) hash of input. Note that it can be called either by using ``sha3 or keccak256.


def method_id(a, b) -> c:
  :param a: method declaration
  :type a: str_literal
  :param b: type of output
  :type b: either bytes32 or bytes[4]

  :output c: either bytes32 or bytes[4]

Takes a function declaration and returns its method_id (used in data field to call it).


def ecrecover(hash, v, r, s) -> b:
  :param hash: a signed hash
  :type hash: bytes32
  :param v:
  :type v: uint256
  :param r: elliptic curve point
  :type r: uint256
  :param s: elliptic curve point
  :type s: uint256

  :output b: address

Takes a signed hash and vrs and returns the public key of the signer.


def ecadd(a, b) -> sum:
  :param a: pair to be added
  :type a: uint256[2]
  :param b: pair to be added
  :type b: uint256[2]

  :output sum: uint256[2]

Takes two elliptical curves and adds them together.


def ecmul(a, b) -> product:
  :param a: pair to be multiplied
  :type a: uint256[2]
  :param b: number to be multiplied
  :type b: uint256

  :output product: uint256[2]

Takes two elliptical curves and multiplies them together.


def extract32(a, b, type=c) -> d:
  :param a: where 32 bytes are extracted from
  :type a: bytes
  :param b: start point of bytes to be extracted
  :type b: int128
  :param c: type of output
  :type c: either bytes32, int128, or address

  :output d: either bytes32, int128, or address


def _RLPList(a, b) -> c:
  :param a: encoded data
  :type a: bytes
  :param b: RLP list
  :type b: list

  :output c: LLLnode

Takes encoded RLP data and an unencoded list of types. Usage:

vote_msg: bytes <= 1024 = ...

values = RLPList(vote_msg, [int128, int128, bytes32, bytes, bytes])

var1: int128 = values[0]
var2: int128 = values[1]
var3: bytes32 = values[2]
var4: bytes <= 1024 = values[3]
var5: bytes <= 1024 = values[4]

Note: RLP decoder needs to be deployed if one wishes to use it outside of the Vyper test suite. Eventually, the decoder will be available on mainnet at a fixed address. But for now, here’s how to create RLP decoder on other chains:

1. send 6270960000000000 wei to 0xd2c560282c9C02465C2dAcdEF3E859E730848761

2. Publish this tx to create the contract:


3. This is the contract address: 0xCb969cAAad21A78a24083164ffa81604317Ab603

Low Level Built in Functions

Vyper contains a set of built in functions which executes unique OPCODES such as send or selfdestruct.

Low Level Functions


def send(a, b):
  :param a: the destination address to send ether to
  :type a: address
  :param b: the wei value to send to the address
  :type b: uint256

Sends ether from the contract to the specified Ethereum address. Note that the amount to send should be specified in wei.


def raw_call(a, b, outsize=c, gas=d, value=e) -> f:
  :param a: the destination address to call to
  :type a: address
  :param b: the data to send the called address
  :type b: bytes
  :param c: the max-length for the bytes array returned from the call.
  :type c: fixed literal value
  :param d: the gas amount to attach to the call.
  :type d: uint256
  :param e: the wei value to send to the address (Optional)
  :type e: uint256

  :output f: bytes[outsize]

Calls to the specified Ethereum address. The call should pass data and may optionally send eth value (specified in wei) as well. The call must specify a gas amount to attach the call and and the outsize. Returns the data returned by the call as a bytes array with the outsize as the max length.


def selfdestruct(a):
  :param a: the address to send the contracts left ether to
  :type a: address

Causes a self destruction of the contract, triggers the SELFDESTRUCT opcode (0xff). CAUTION! This method will delete the contract from the Ethereum blockchain. All none ether assets associated with this contract will be “burned” and the contract will be inaccessible.


def assert(a):
  :param a: the boolean condition to assert
  :type a: bool

Asserts the specified condition, if the condition is equals to true the code will continue to run. Otherwise, the OPCODE REVERT (0xfd) will be triggered, the code will stop it’s operation, the contract’s state will be reverted to the state before the transaction took place and the remaining gas will be returned to the transaction’s sender.

Note: To give it a more Python like syntax, the assert function can be called without parenthesis, the syntax would be assert your_bool_condition. Even though both options will compile, it’s recommended to use the Pythonic version without parenthesis.


def raw_log(a, b):
  :param a: the address of the contract to duplicate.
  :type a: * (any input)
  :param b: the name of the logged event.
  :type b: bytes

Emits a log without specifying the abi type, with the arguments entered as the first input.


def create_with_code_of(a, value=b):
  :param a: the address of the contract to duplicate.
  :type a: address
  :param b: the wei value to send to the new contract instance
  :type b: uint256 (Optional)

Duplicates a contract’s code and deploys it as a new instance, by means of a DELEGATECALL. You can also specify wei value to send to the new contract as value=the_value.


def blockhash(a) -> hash:
  :param a: the number of the block to get
  :type a: uint256

  :output hash: bytes32

Returns the hash of the block at the specified height.

Note: The EVM only provides access to the most 256 blocks. This function will return 0 if the block number is greater than or equal to the current block number or more than 256 blocks behind the current block.