Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
101 lines (73 sloc) 2.69 KB
# Author: Florian Tramer
macro True: 1
macro False: 0
# mapping from account address to current balance
data _accountBalances[2^256]
# mapping from account owner to accounts allowed to withdraw specified amounts
data _approvals[2^256][2^256]
# total amount of tokens deposited
data total_supply
event Transfer(_from:address:indexed, _to:address:indexed, _value:uint256)
event Approval(_owner:address:indexed, _spender:address:indexed, _value:uint256)
def deposit():
# use unsigned LT(x, y)
if ( lt(self.total_supply + msg.value, self.total_supply) ):
~invalid()
self._accountBalances[msg.sender] += msg.value
self.total_supply += msg.value
log(type=Transfer, 0x0, msg.sender, msg.value)
def withdraw(_value: uint256):
if ( msg.value != 0 ):
~invalid()
# sender has enough resources AND send worked
if ( lt(self._accountBalances[msg.sender], _value) ):
~invalid()
# reduce funds BEFORE we send
self._accountBalances[msg.sender] -= _value
self.total_supply -= _value
if ( send(msg.sender, _value) ):
log(type=Transfer, msg.sender, 0x0, _value)
return(True:bool)
else:
~invalid()
def const totalSupply():
if ( msg.value != 0 ):
~invalid()
return(self.total_supply:uint256)
def const balanceOf(_owner: address):
if ( msg.value != 0 ):
~invalid()
return(self._accountBalances[_owner]:uint256)
def transfer(_to: address, _value: uint256):
if ( msg.value != 0 ):
~invalid()
# sender has enough resources
if ( self._accountBalances[msg.sender] >= _value ):
self._accountBalances[msg.sender] -= _value
self._accountBalances[_to] += _value
log(type=Transfer, msg.sender, _to, _value)
return(True:bool)
else:
~invalid()
def transferFrom(_from: address, _to: address, _value: uint256):
if ( msg.value != 0 ):
~invalid()
# sender is approved to withdraw AND origin account has enough resources
if ( self._approvals[_from][msg.sender] >= _value && self._accountBalances[_from] >= _value ):
self._approvals[_from][msg.sender] -= _value
self._accountBalances[_from] -= _value
self._accountBalances[_to] += _value
log(type=Transfer, _from, _to, _value)
return(True:bool)
else:
~invalid()
def approve(_spender: address, _value: uint256):
if ( msg.value != 0 ):
~invalid()
self._approvals[msg.sender][_spender] = _value
log(type=Approval, msg.sender, _spender, _value)
return(True:bool)
def allowance(_owner: address, _spender: address):
if ( msg.value != 0 ):
~invalid()
return(self._approvals[_owner][_spender]:uint256)