Last active
September 16, 2023 07:13
-
-
Save tserg/5b51699146be0ffdc561e0d999aacfe0 to your computer and use it in GitHub Desktop.
ETH Singapore 2023 Intro to Vyper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!