Last active
November 27, 2019 15:29
-
-
Save jacqueswww/3198e25aeee1ec0da4b84a85319f56be to your computer and use it in GitHub Desktop.
Stake Based Priority Queuing
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
from vyper.interfaces import ERC20 | |
implements: ERC20 | |
MINIMUM_STAKE: constant(uint256(wei)) = 1 * 10 ** 17 | |
NONREFUNDABLE_DEPOSIT: constant(uint256) = 5 * 10 ** 16 | |
Transfer: event({_from: indexed(address), _to: indexed(address), _value: uint256}) | |
Approval: event({_owner: indexed(address), _spender: indexed(address), _value: uint256}) | |
name: public(string[64]) | |
symbol: public(string[32]) | |
decimals: public(uint256) | |
balanceOf: public(map(address, uint256)) | |
allowances: map(address, map(address, uint256)) | |
total_supply: uint256 | |
@public | |
def __init__(): | |
init_supply: uint256 = 0 | |
self.name = 'Network Credits' | |
self.symbol = '' | |
self.decimals = 18 | |
self.total_supply = 1 | |
@public | |
@constant | |
def totalSupply() -> uint256: | |
return self.total_supply | |
@public | |
@constant | |
def allowance(_owner : address, _spender : address) -> uint256: | |
return self.allowances[_owner][_spender] | |
@public | |
def transfer(_to : address, _value : uint256) -> bool: | |
self.balanceOf[msg.sender] -= _value | |
self.balanceOf[_to] += _value | |
log.Transfer(msg.sender, _to, _value) | |
return True | |
@public | |
def transferFrom(_from : address, _to : address, _value : uint256) -> bool: | |
# NOTE: vyper does not allow underflows | |
# so the following subtraction would revert on insufficient balance | |
self.balanceOf[_from] -= _value | |
self.balanceOf[_to] += _value | |
# NOTE: vyper does not allow underflows | |
# so the following subtraction would revert on insufficient allowance | |
self.allowances[_from][msg.sender] -= _value | |
log.Transfer(_from, _to, _value) | |
return True | |
@public | |
def approve(_spender : address, _value : uint256) -> bool: | |
self.allowances[msg.sender][_spender] = _value | |
log.Approval(msg.sender, _spender, _value) | |
return True | |
@constant | |
@private | |
def get_amount_to_add(current_balance: uint256, amount: uint256(wei)) -> uint256(1/wei): | |
return amount * (1 / (1 + (amount + current_balance) ** 2)) | |
@public | |
@payable | |
def purchase_credits(node: address): | |
assert msg.value > MINIMUM_STAKE | |
assert msg.sender == node | |
current_balance: uint256 = self.balanceOf[node] | |
amount_to_add: uint256 = as_unitless_number(self.get_amount_to_add(current_balance, msg.value)) | |
self.balanceOf[node] += amount_to_add | |
self.total_supply += amount_to_add | |
@public | |
def witdraw_credits(node: address, amount: uint256): | |
assert msg.sender == node | |
current_balance: uint256 = self.balanceOf[node] | |
assert current_balance > NONREFUNDABLE_DEPOSIT | |
amount_to_send: uint256 = current_balance - NONREFUNDABLE_DEPOSIT | |
send(node, amount_to_send) | |
self.total_supply -= amount_to_send |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment