Skip to content

Instantly share code, notes, and snippets.

@maurelian
Last active March 18, 2018 18:52
Show Gist options
  • Save maurelian/0ab71465b467e991680698de75c3fe9e to your computer and use it in GitHub Desktop.
Save maurelian/0ab71465b467e991680698de75c3fe9e to your computer and use it in GitHub Desktop.
## [It would be nice to import a library, another contract's interface, or a
## parent to inherit from]
## but that's not supported. Yes, inheritance is dangerous, but code reuse can
## be very beneficial as
## well. I believe adoption will be held back by this.
## [Declare a positive number] Why change the type from `uint256`? `num` is
## less descriptive, ie. it could include irrational numbers for all I know.
## Also, will 4-byte identifiers be compatible with solidity?
## ie. `bytes4(keccak256('uint256'))` or `bytes4(keccak256('num'))` ? Or is
## this breaking the ABI?
some_uint: public(num256) # wrapping the type in `public()` adds visual clutter
some_int: public(num)
price: public(num (wei / currency)) # I like these 'dimensional' types.
## [Declare an array of 32 bytes] I couldn't figure out how to do this.
# someBytes: private(bytes32) # error: "Base type with units can only be num, decimal"
## [Declare a string]: I tried many ways, and looked at the docs,
## but couldn't figure out how to do this. It always got the error "Invalid
## top-level statement".
# example_string: public(string)
# exampleString = ""
# exampleString = public("")
## [Defining a struct]: This worked fine, and I like the comma dangle but I
## couldn't find anything useful to actually do with a struct. (see below)
User: {
joinedTime: timestamp,
isActive: bool,
}
## [Declare a mapping to track users.
## ie. in solidity: `mapping(address => User) userMap;`].
## There is support for mappings, but apparently not to a struct.
## For example, this works:
num_map: num[address]
## This does not work:
# userMap: User[address] # will that eventually work?
## Another note about mappings:
## Contrary to the stated goals, this syntax feels like it's _very much_
## optimized for writing over reading. Compared to solidity, I feel it is hard
## to determine that we're looking at a mapping. ie.
## `mapping(address=>uint256) numMap;` (solidity)
## `num_map: num[address]` (vyper's mapping looks a lot like an array)
## [Declare a list of addresses, up to length 10]: lists are always bounded at
## a fixed size, and cannot be extended. This is pretty restrictive, but it
## seems like a reasonable decision given the design goals.
addr_array: address[10]
## I couldn't find a list push method, so needed to track the last index written to.
addr_count: num
## Ultimately, I just ended up using 2 more lists to and kept all the data about a user in the same index across 3 lists
user_times_array: timestamp[10] ## timestamp is a built-in type, which is nice.
user_bool_array: bool[10]
## [Initalize the contract]: I like this pattern much more than Solidity's
## approach. It's more readable, and `init` is closer to the language used in ## the yellow paper.
@public
@payable
def __init__(_some_uint: num256, _some_int: num):
# I really like the use of `self.some_var`, it requires you to be explicit
# about using storage variables.
self.some_uint = _some_uint
self.some_int = _some_int
## [Make some functions for getting and setting stuff]
## Generally, I like the way functions work. They are quite readable.
## I like that variables are members of `self`
@public
# declaring visibility as a decorator is nice IMO, and good for auditing
def newUser():
self.addr_count = self.addr_count + 1
i = self.addr_count
self.addr_array[i] = msg.sender
self.user_times_array[i] = block.timestamp
self.user_bool_array[i] = true
@public
@constant
# I think I like this `->` syntax over solidity's `returns`
def thingReturner(i: num) -> (address, timestamp, bool):
addr = self.addr_array[i]
time = self.user_times_array[i]
status = self.user_bool_array[i]
return (addr, time, status)
@public
@constant
# notice that timedelta is a built in type. That's cool.
def howLongSinceJoined(i: num) -> timedelta:
delta = block.timestamp - self.user_times_array[i]
return delta
## [a simple getter for the eth balance]: notice that wei is a built in type.
## That's cool.
@public
@constant
# I would prefer if the return type had to be wrapped in brackets for
# consistency
def getBalance() -> num(wei):
# I like how the current balance is represented as a member of `self`
# similar to storage vars
return self.balance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment