ABCDEFGHIJKLMNOPQRSTUVWX
1
FeatureSolidityVyper
2
Version$ solc --version
Version: 0.6.12
$ vyper --version
0.2.3
3
General notes on syntaxSolidity loosely borrows its syntax from Javascript and CVyper syntax is valid Python 3 syntax (but the opposite is not true)
4
Block delimiters{ }: # Vyper uses Python's off-side rule
5
Statement separator;\n' and :
6
End of line comment// comment# comment
7
Multiple line comment/* multiple line
comment */
# Multiple line
# comment
8
Constantuint constant TOTAL_SUPPLY = 10000000;TOTAL_SUPPLY: constant(uint256) = 10000000
9
Assignmentv = 1;v = 1
10
Parallel assignment(x, y) = (0, 1);Tuple to tuple assignment not supported
11
Swap(x, y) = (y, x);
12
Compound assignment-=, *=, /=, %=, |=, &=, ^=-=, *=, /=, %=, |=, &=, ^=
13
Increment and decrementi++, ++i, i--, --ii += 1, i -= 1
14
Nullnull doesn't exist in Solidity but any unitialized variables take a default value represented by 0 in memorynull doesn't exist in Vyper but any unitialized variables take a default value represented by 0 in memory
15
Set variable to default valuedelete v // doesn't work with mappingsv = empty(uint256)
16
Null testv == 0v == 0
17
Conditional expressionx > 0 ? x : -xConditional expression not supported
18
Contract lifecycle
19
FeatureSolidityVyper
20
Contract creationContract c = new Contract(args);
21
Contract creation with fundingContract c = new Contract{value: amount}(args);
22
Salted contract creation (CREATE2)Contract c = new Contract{salt: salt}(args);
23
Create forwarder contractcontract: address = create_forwarder_to(other_contract, value)
24
Selfdestruct (Avoid)selfdestruct(refundAddr)selfdestruct(refund_addr)
25
Interfaces
26
FeatureSolidityVyper
27
Interfacesinterface HelloWorld {
function hello() external pure;
function world(int) external pure;
}
interface HelloWorld:
def hello(): nonpayable
def world(uint256): nonpayable
28
Interface type
interface HelloWorldWithEvent {
event Event();
function hello() external pure;
function world(int) external pure;
}

contract Test {
bytes4 public hello_world_with_event = type(HelloWorldWithEvent).interfaceId;
}
29
Operators
30
FeatureSolidityVyper
31
True and falsetrue falseTrue False
32
FalsehoodsFALSEFALSE
33
Logical operators&& || !and or not
34
Relational operators` = != < > <= => `` == != < > <= => `
35
Min and maxmax(x, y)
36
Arithmetic operators` =+ - * / % ** unary- `` =+ - * / % ** unary- `
37
Integer division//
38
Bit operators<< >> & | ^ ~<< >> & | ^ ~
39
Binary & hex literalsuint x = 0x52
string memory s = hex"52"
a: address= 0x14d465376c051Cbcd80Aa2d35Fd5df9910f80543
b: Bytes[32]= b'\x01\x02\x03\x04\x05\x06... (32 bytes)
d: Bytes[1] = 0b00010001
40
Data structures
41
FeatureSolidityVyper
42
String typestringString[N] # N is a fixed number
43
Bytes typebytes // dynamic
bytes1, bytes2, ..., bytes32 // packed
bytes[N] // N is a fixed number, unpacked
Bytes[N] # N is a fixed number
44
String literal"don't \"no\""
'don"t \'no\''
"don't \"no\""
'don"t \'no\''
45
String lengthbytes(s).lengthlen(s)
46
String literal escapes
\<newline> (escapes an actual newline)
\\ (backslash)
\' (single quote)
\" (double quote)
\b (backspace)
\f (form feed)
\n (newline)
\r (carriage return)
\t (tab)
\v (vertical tab)
\xNN (hex escape)
\uNNNN (unicode escape)
\<newline> (escapes an actual newline)
\\ (backslash)
\' (single quote)
\" (double quote)
\a (bell)
\b (backspace)
\f (form feed)
\n (newline)
\r (carriage return)
\t (tab)
\v (vertical tab)
\ooo (octal escape)
\xNN (hex escape)
\uNNNN (unicode escape)
\uNNNNNNNN (unicode escape)
47
Are strings mutable?YesYes
48
Sliceabi.decode(_payload[:4], (bytes4))
// array slices only implemented for calldata arrays
slice(x, _start, _len)
49
String comparisonkeccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2))keccak256(s1) == keccak256(s2)
50
String concatenationabi.encodePacked(s1, s2)concat(s1, s2)
51
Array literal[1, 2, 3][1, 2, 3]
52
Lengtha.lengthlen(a)
53
Empty testa.length == 0
54
Lookupa[0]a[0]
55
Updatea[0] = 1;a[0] = 1
56
Out of bounds accessFailing assertionFailing assertion
57
Add new elementa.push(3); # Dynamic arrays
58
Remove elementa.pop(); # Dynamic arrays
59
Struct
struct Pair {
uint x;
uint y;
} // Creating a struct

Pair memory pair = Pair(2, 3); // Instantiating a struct variable
require(pair.y > pair.x); // Accessing elements
struct Pair:
x: uint256
y: uint256 # Creating a struct

pair: Pair = Pair({x: 2, y: 3}) # Instantiating a struct variable
assert pair.y > pair.x # Accessing elements
60
Mapping sizeImpossible to knowImpossible to know
61
Lookupm[2]m[2]
62
Updatem[2] = 1;m[2] = 1
63
Missing key behaviourA mapping has no concept of set keys, a mapping always refers to a hashed value that is the same for a given mapping and key
A mapping has no concept of set keys, a mapping always refers to a hashed value that is the same for a given mapping and key
64
Delete keym[2] = 0;m[2] = empty(uint256)
65
Immutable variablesuint immutable x; // have to be assigned in the constructor
66
Functions
67
FeatureSolidityVyper
68
Define functionfunction add2(uint x, uint y) public pure returns (uint) {
return x + y;
}
@external
def add2(x: uint256, y: uint256) -> uint256:
return x + y
69
Function argument storage location
function first(uint[] calldata x) public pure returns (uint) {
// this function doesn't copy x to memory
return x[0];
}

function first(uint[] memory x) public pure returns (uint) {
// this function first copies x to memory
return x[0];
}
70
Invoke functionadd2(x, y)add2(x, y)
71
External function callsc.f{gas: 1000, value: 4 ether}()c.f()
raw_call(address, data, outsize, gas, value, is_delegate_call)
72
Control flow
73
FeatureSolidityVyper
74
If statementif (a > 2) {
...
else if (a == 0) {
...
} else {
...
}
if a > 2:
...
elif a == 0:
...
else:
...
75
For loopfor (uint i = 0; i < 3; i++) {
...
}
for i in range(3):
...
76
While loopwhile (a > 0) {
...
}
77
Do-While loopdo {
...
} while (a > 0);
78
Return valuereturn x + y;return x + y
79
Breakbreak;break
80
Continuecontinue;continue
81
Assertassert(x > y);assert x > y
82
Requirerequire(x > y);
83
Revertrequire(false, "revert reason")raise "revert reason"
84
Exception handling
interface DataFeed { function getData(address token) external returns (uint value); }

contract FeedConsumer {
DataFeed feed;
uint errorCount;
function rate(address token) public returns (uint value, bool success) {
// Permanently disable the mechanism if there are
// more than 10 errors.
require(errorCount < 10);
try feed.getData(token) returns (uint v) {
return (v, true);
} catch Error(string memory /*reason*/) {
// This is executed in case
// revert was called inside getData
// and a reason string was provided.
errorCount++;
return (0, false);
} catch (bytes memory /*lowLevelData*/) {
// This is executed in case revert() was used
// or there was a failing assertion, division
// by zero, etc. inside getData.
errorCount++;
return (0, false);
}
}
}
85
Misc
86
FeatureSolidityVyper
87
Comments
NatSpec conventions for functions:

/// @author
/// @notice
/// @dev
/// @param
/// @return

Events:

/// The address `participant` just registered for the gathering.
event Registered(address participant);

Special inheritance syntax for contracts:

/// @inheritdoc OtherContract
def foo():
"""
@author
@notice
@dev
@param
@return
"""
...
88
Payment with error on failure (Avoid for Solidity)address.transfer()send(address, value)
89
Payment with false on failure (Avoid for Solidity)address.send()
90
Payment with gas forwarding (WARNING)address.call.value().gas()()raw_call(address, data, outsize, gas, value, is_delegate_call)
91
Event loggingevent Deposit(
address indexed _from,
bytes32 indexed _id,
uint _value
);

emit Deposit(msg.sender, _id, msg.value);
event Deposit:
_from: indexed(address)
_id: indexed(bytes32)
_value: uint256

log Deposit(msg.sender, _id, msg.value)
92
Units, global constants and type ranges
1 ether
1 finney
1 szabo
1 wei
1 gwei
1 seconds
1 minutes
1 hours
1 days
1 weeks
1 years // deprecated
type(uint).min
type(uint).max
type(int8).min
type(int8).max
...
ZERO_ADDRESS
as_wei_value(1, "finney")
as_wei_value(1, "szabo")
as_wei_value(1, "wei")
as_wei_value(1, "babbage")
as_wei_value(1, "shannon")
EMPTY_BYTES32
MAX_INT128
MIN_INT128
MAX_DECIMAL
MIN_DECIMAL
MAX_UINT256
ZERO_WEI
93
Block and transaction properties
blockhash(blockNumber)
block.coinbase
block.difficulty
block.gaslimit
block.number

block.timestamp
now // alias for block.timestamp
gasleft()
msg.data
msg.gas
msg.sender
msg.sig
msg.value
tx.gasprice
tx.origin
blockhash(blockNumber)
block.coinbase
block.difficulty

block.number
block.prevhash # Same as blockhash(block.number - 1)
block.timestamp



msg.gas
msg.sender

msg.value

tx.origin