Skip to content

Instantly share code, notes, and snippets.

@tserg
Last active September 16, 2023 07:13
Show Gist options
  • Save tserg/5b51699146be0ffdc561e0d999aacfe0 to your computer and use it in GitHub Desktop.
Save tserg/5b51699146be0ffdc561e0d999aacfe0 to your computer and use it in GitHub Desktop.
ETH Singapore 2023 Intro to Vyper
# @version ^0.3.7
# Declare an enum with three members
enum Permission:
EXECUTE # 1
WRITE # 2
READ # 4
# Declare a public storage mapping of address to Permission enum
permissions: public(HashMap[address, Permission])
# Constructor
# For illustration only
@external
def __init__():
pass
# External functions that can modify state
@external
def grant_permission(a: address, p: Permission):
self.permissions[a] |= p
@external
def revoke_permission(a: address, p: Permission):
self.permissions[a] &= ~p
# External functions that can read from state but not modify it
@external
@view
def can_read(a: address) -> bool:
return Permission.READ in self.permissions[a]
@external
@view
def can_write(a: address) -> bool:
return Permission.WRITE in self.permissions[a]
@external
@view
def can_execute(a: address) -> bool:
return Permission.EXECUTE in self.permissions[a]
@external
@view
def get_uint256(a: address) -> uint256:
return convert(self.permissions[a], uint256)
@bguiz
Copy link

bguiz commented Sep 10, 2023

thanks!

@tserg
Copy link
Author

tserg commented Sep 10, 2023

welcome! you can also join the Vyper Discord any time if you have any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment