Built in Functions¶
Vyper provides a collection of built in functions available in the global namespace of all contracts.
Bitwise Operations¶
-
bitwise_and(x: uint256, y: uint256) → uint256¶ Perform a “bitwise and” operation. Each bit of the output is 1 if the corresponding bit of
xAND ofyis 1, otherwise it’s 0.@external @view def foo(x: uint256, y: uint256) -> uint256: return bitwise_and(x, y)
>>> ExampleContract.foo(31337, 8008135) 12353
-
bitwise_not(x: uint256) → uint256¶ Return the complement of
x- the number you get by switching each 1 for a 0 and each 0 for a 1.@external @view def foo(x: uint256) -> uint256: return bitwise_not(x)
>>> ExampleContract.foo(0) 115792089237316195423570985008687907853269984665640564039457584007913129639935
-
bitwise_or(x: uint256, y: uint256) → uint256¶ Perform a “bitwise or” operation. Each bit of the output is 0 if the corresponding bit of
xAND ofyis 0, otherwise it’s 1.@external @view def foo(x: uint256, y: uint256) -> uint256: return bitwise_or(x, y)
>>> ExampleContract.foo(31337, 8008135) 8027119
-
bitwise_xor(x: uint256, y: uint256) → uint256¶ Perform a “bitwise exclusive or” operation. Each bit of the output is the same as the corresponding bit in
xif that bit inyis 0, and it’s the complement of the bit inxif that bit inyis 1.@external @view def foo(x: uint256, y: uint256) -> uint256: return bitwise_xor(x, y)
>>> ExampleContract.foo(31337, 8008135) 8014766
-
shift(x: uint256, _shift: int128) → uint256¶ Return
xwith the bits shifted_shiftplaces. A positive_shiftvalue equals a left shift, a negative value is a right shift.@external @view def foo(x: uint256, y: int128) -> uint256: return shift(x, y)
>>> ExampleContract.foo(2, 8) 512
Chain Interaction¶
-
create_forwarder_to(target: address, value: uint256 = 0) → address¶ Deploys a small contract that duplicates the logic of the contract at
target, but has it’s own state since every call totargetis made usingDELEGATECALLtotarget. To the end user, this should be indistinguishable from an independantly deployed contract with the same code astarget.
Note
It is very important that the deployed contract at target is code you know and trust, and does not implement the selfdestruct opcode as this will affect the operation of the forwarder contract.
target: Address of the contract to duplicatevalue: The wei value to send to the new contract address (Optional, default 0)Returns the address of the duplicated contract.
@external def foo(_target: address) -> address: return create_forwarder_to(_target)
-
raw_call(to: address, data: Bytes, max_outsize: int = 0, gas: uint256 = gasLeft, value: uint256 = 0, is_delegate_call: bool = False, is_static_call: bool = False) → Bytes[max_outsize]¶ Call to the specified Ethereum address.
to: Destination address to call todata: Data to send to the destination addressmax_outsize: Maximum length of the bytes array returned from the call. If the returned call data exceeds this length, only this number of bytes is returned.gas: The amount of gas to attach to the call. If not set, all remainaing gas is forwarded.value: The wei value to send to the address (Optional, default0)is_delegate_call: IfTrue, the call will be sent asDELEGATECALL(Optional, defaultFalse)is_static_call: IfTrue, the call will be sent asSTATICCALL(Optional, defaultFalse)
Returns the data returned by the call as a
Byteslist, withmax_outsizeas the max length.Returns
Noneifmax_outsizeis omitted or set to0.Note
The actual size of the returned data may be less than
max_outsize. You can uselento obtain the actual size.Returns the address of the duplicated contract.
@external @payable def foo(_target: address) -> Bytes[32]: response: Bytes[32] = raw_call(_target, 0xa9059cbb, max_outsize=32, value=msg.value) return response
-
raw_log(topics: bytes32[4], data: Union[Bytes, bytes32]) → None¶ Provides low level access to the
LOGopcodes, emitting a log without having to specify an ABI type.topics: List ofbytes32log topics. The length of this array determines which opcode is used.data: Unindexed event data to include in the log. May be given asBytesorbytes32.
@external def foo(_topic: bytes32, _data: Bytes[100]): raw_log([_topic], _data)
-
selfdestruct(to: address) → None¶ Trigger the
SELFDESTRUCTopcode (0xFF), causing the contract to be destroyed.to: Address to forward the contract’s ether balance to
Warning
This method delete the contract from the blockchain. All non-ether assets associated with this contract are “burned” and the contract is no longer accessible.
@external def do_the_needful(): selfdestruct(msg.sender)
-
send(to: address, value: uint256) → None¶ Send ether from the contract to the specified Ethereum address.
to: The destination address to send ether tovalue: The wei value to send to the address
Note
The amount to send is always specified in
wei.@external def foo(_receiver: address, _amount: uint256): send(_receiver, _amount)
Cryptography¶
-
ecadd(a: uint256[2], b: uint256[2]) → uint256[2]¶ Take two points on the Alt-BN128 curve and add them together.
@external @view def foo(x: uint256[2], y: uint256[2]) -> uint256[2]: return ecadd(x, y)
>>> ExampleContract.foo([1, 2], [1, 2]) [ 1368015179489954701390400359078579693043519447331113978918064868415326638035, 9918110051302171585080402603319702774565515993150576347155970296011118125764, ]
-
ecmul(point: uint256[2], scalar: uint256) → uint256[2]¶ Take a point on the Alt-BN128 curve (
p) and a scalar value (s), and return the result of adding the point to itselfstimes, i.e.p * s.point: Point to be multipliedscalar: Scalar value
@external @view def foo(point: uint256[2], scalar: uint256) -> uint256[2]: return ecmul(point, scalar)
>>> ExampleContract.foo([1, 2], 3) [ 3353031288059533942658390886683067124040920775575537747144343083137631628272, 19321533766552368860946552437480515441416830039777911637913418824951667761761, ]
-
ecrecover(hash: bytes32, v: uint256, r: uint256, s: uint256) → address¶ Recover the address associated with the public key from the given elliptic curve signature.
r: first 32 bytes of signatures: second 32 bytes of signaturev: final 1 byte of signature
Returns the associated address, or
0on error.
-
keccak256(_value) → bytes32¶ Return a
keccak256hash of the given value._value: Value to hash. Can be a literal string,Bytes, orbytes32.
@external @view def foo(_value: Bytes[100]) -> bytes32 return keccak256(_value)
>>> ExampleContract.foo(b"potato") 0x9e159dfcfe557cc1ca6c716e87af98fdcb94cd8c832386d0429b2b7bec02754f
-
sha256(_value) → bytes32¶ Return a
sha256(SHA2 256bit output) hash of the given value._value: Value to hash. Can be a literal string,Bytes, orbytes32.
@external @view def foo(_value: Bytes[100]) -> bytes32 return sha256(_value)
>>> ExampleContract.foo(b"potato") 0xe91c254ad58860a02c788dfb5c1a65d6a8846ab1dc649631c7db16fef4af2dec
Data Manipulation¶
-
concat(a, b, *args) → Union[Bytes, String]¶ Take 2 or more bytes arrays of type
bytes32,BytesorStringand combine them into a single value.If the input arguments are
Stringthe return type isString. Otherwise the return type isBytes.@external @view def foo(a: String[5], b: String[5], c: String[5]) -> String[100] return concat(a, " ", b, " ", c, "!")
>>> ExampleContract.foo("why","hello","there") "why hello there!"
-
convert(value, type_) → Any¶ Converts a variable or literal from one type to another.
value: Value to converttype_: The destination type to convert to (bool,decimal,int128,uint256orbytes32)
Returns a value of the type specified by
type_.For more details on available type conversions, see Type Conversions.
-
extract32(b: Bytes, start: int128, output_type=bytes32) → Any¶ Extract a value from a
Byteslist.b:Byteslist to extract fromstart: Start point to extract fromoutput_type: Type of output (bytes32,int128, oraddress). Defaults tobytes32.
Returns a value of the type specified by
output_type.@external @view def foo(Bytes[32]) -> address: return extract32(b, 12, output_type=address)
>>> ExampleContract.foo("0x0000000000000000000000009f8F72aA9304c8B593d555F12eF6589cC3A579A2") "0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2"
-
slice(b: Union[Bytes, bytes32, String], start: uint256, length: uint256) → Union[Bytes, String]¶ Copy a list of bytes and return a specified slice.
b: value being slicedstart: start position of the slicelength: length of the slice
If the value being sliced is a
Bytesorbytes32, the return type isBytes. If it is aString, the return type isString.@external @view def foo(s: string[32]) -> string[5]: return slice(s, 4, 5)
>>> ExampleContract.foo("why hello! how are you?") "hello"
Math¶
-
ceil(value: decimal) → int128¶ Round a decimal up to the nearest integer.
value: Decimal value to round up
@external @view def foo(value: decimal) -> uint256: return ceil(value)
>>> ExampleContract.foo(3.1337) 4
-
floor(value: decimal) → int128¶ Round a decimal down to the nearest integer.
value: Decimal value to round down
@external @view def foo(value: decimal) -> uint256: return floor(value)
>>> ExampleContract.foo(3.1337) 3
-
max(a: numeric, b: numeric) → numeric¶ Return the creater value of
aandb. The input values may be any numeric type as long as they are both of the same type. The output value is the same as the input values.@external @view def foo(a: uint256, b: uint256) -> uint256: return max(a, b)
>>> ExampleContract.foo(23, 42) 42
-
min(a: numeric, b: numeric) → numeric¶ Returns the lesser value of
aandb. The input values may be any numeric type as long as they are both of the same type. The output value is the same as the input values.@external @view def foo(a: uint256, b: uint256) -> uint256: return min(a, b)
>>> ExampleContract.foo(23, 42) 23
-
pow_mod256(a: uint256, b: uint256) → uint256¶ Return the result of
a ** b % (2 ** 256).This method is used to perform exponentiation without overflow checks.
@external @view def foo(a: uint256, b: uint256) -> uint256: return pow_mod256(a, b)
>>> ExampleContract.foo(2, 3) 8 >>> ExampleContract.foo(100, 100) 59041770658110225754900818312084884949620587934026984283048776718299468660736
-
sqrt(d: decimal) → decimal¶ Return the square root of the provided decimal number, using the Babylonian square root algorithm.
@external @view def foo(d: decimal) -> decimal: return sqrt(d)
>>> ExampleContract.foo(9.0) 3.0
-
uint256_addmod(a: uint256, b: uint256, c: uint256) → uint256¶ Return the modulo of
(a + b) % c. Reverts ifc == 0.@external @view def foo(a: uint256, b: uint256, c: uint256) -> uint256: return uint256_addmod(a, b, c)
>>> (6 + 13) % 8 3 >>> ExampleContract.foo(6, 13, 8) 3
-
uint256_mulmod(a: uint256, b: uint256, c: uint256) → uint256¶ Return the modulo from
(a * b) % c. Reverts ifc == 0.@external @view def foo(a: uint256, b: uint256, c: uint256) -> uint256: return uint256_mulmod(a, b, c)
>>> (11 * 2) % 5 2 >>> ExampleContract.foo(11, 2, 5) 2
Utilities¶
-
as_wei_value(_value, unit: str) → uint256¶ Take an amount of ether currency specified by a number and a unit and return the integer quantity of wei equivalent to that amount.
_value: Value for the ether unit. Any numeric type may be used, however the value cannot be negative.unit: Ether unit name (e.g."wei","ether","gwei", etc.) indicating the denomination of_value. Must be given as a literal string.
@external @view def foo(s: String[32]) -> uint256: return as_wei_value(1.337, "ether")
>>> ExampleContract.foo(1) 1337000000000000000
-
blockhash(block_num: uint256) → bytes32¶ Return the hash of the block at the specified height.
Note
The EVM only provides access to the most 256 blocks. This function returns
EMPTY_BYTES32if the block number is greater than or equal to the current block number or more than 256 blocks behind the current block.@external @view def foo() -> bytes32: return blockhash(block.number - 16)
>>> ExampleContract.foo() 0xf3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
-
empty(typename) → Any¶ Return a value which is the default (zeroed) value of its type. Useful for initializing new memory variables.
typename: Name of the type
@external @view def foo(): x: uint256[2][5] = empty(uint256[2][5])
-
len(b: Union[Bytes, String]) → uint256¶ Return the length of a given
BytesorString.@external @view def foo(s: String[32]) -> uint256: return len(s)
>>> ExampleContract.foo("hello") 5
-
method_id(method, output_type: type = Bytes[4]) → Union[bytes32, Bytes[4]]¶ Takes a function declaration and returns its method_id (used in data field to call it).
method: Method declaration as given as a literal stringoutput_type: The type of output (Bytes[4]orbytes32). Defaults toBytes[4].
Returns a value of the type specified by
output_type.@external @view def foo() -> Bytes[4]: return method_id('transfer(address,uint256)', output_type=Bytes[4])
>>> ExampleContract.foo() b"\xa9\x05\x9c\xbb"