Last active
January 13, 2024 15:11
-
-
Save charles-cooper/fb5caff4eee8bbf92ed86cefaa39a855 to your computer and use it in GitHub Desktop.
more module examples
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
### | |
# access_control.vy | |
owner: address | |
def __init__(): | |
self.owner = msg.sender | |
def check_owner(): | |
assert msg.sender == self.owner | |
### | |
### | |
# base_token.vy | |
totalSupply: uint256 | |
balances: HashMap[address, uint256] | |
def __init__(initial_supply: uint256): | |
self.totalSupply += initial_supply | |
self.balances[msg.sender] += initial_supply | |
@external | |
def transfer(recipient: address, amount: uint256): | |
self.balances[msg.sender] -= amount # safesub | |
self.balances[recipient] += amount | |
### | |
### | |
# mintable_token.vy | |
import base_token | |
import access_control | |
uses: base_token, access_control | |
@external | |
def mint(recipient: address, amount: uint256): | |
access_control.check_owner() | |
self._mint_to(recipient, amount) | |
@internal | |
def _mint_to(recipient: address, amount: uint256): | |
base_token.totalSupply += amount | |
base_token.balances[recipient] += amount | |
### | |
### | |
# contract.vy | |
import access_control | |
import mintable_token | |
import base_token | |
initializes: access_control | |
initializes: base_token | |
initializes: mintable_token[ | |
base_token := base_token, | |
access_control := access_control, | |
] | |
def __init__(): | |
base_token.__init__(100) # required by `initializes: base_token` | |
access_control.__init__() # required by `initializes: access_control` | |
mintable_token.__init__() # required by `initializes: mintable_token` | |
export: mintable_token.mint | |
export: base_token.transfer |
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
# similar, but the modules are designed a little differently by the library author. | |
### | |
# access_control.vy | |
owner: address | |
def __init__(): | |
self.owner = msg.sender | |
def check_owner(): | |
assert msg.sender == self.owner | |
### | |
### | |
# base_token.vy | |
totalSupply: uint256 | |
balances: HashMap[address, uint256] | |
def __init__(initial_supply: uint256): | |
self.totalSupply += initial_supply | |
self.balances[msg.sender] += initial_supply | |
@external | |
def transfer(recipient: address, amount: uint256): | |
self.balances[msg.sender] -= amount # safesub | |
self.balances[recipient] += amount | |
### | |
### | |
# mintable_token.vy | |
import access_control | |
import base_token | |
initializes: access_control | |
initializes: base_token | |
def __init__(initial_supply: uint256): | |
access_control.__init__() | |
base_token.__init__(initial_supply) | |
@external | |
def mint(recipient: address, amount: uint256): | |
access_control.check_owner() | |
self._mint_to(recipient, amount) | |
@internal | |
def _mint_to(recipient: address, amount: uint256): | |
base_token.totalSupply += amount | |
base_token.balances[recipient] += amount | |
### | |
### | |
# contract.vy | |
import mintable_token | |
import base_token | |
initializes: mintable_token # no parameters required, since mintable_token owns all of its writable dependencies | |
initializes: base_token # this line will raise an error! | |
def __init__(): | |
base_token.__init__() # error! mintable_token already owns base_token | |
mintable_token.__init__(100) # that's better |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment