Skip to content

Instantly share code, notes, and snippets.

@charles-cooper
Created September 13, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charles-cooper/1bb557b64ff0a3f5f960f330938174bb to your computer and use it in GitHub Desktop.
Save charles-cooper/1bb557b64ff0a3f5f960f330938174bb to your computer and use it in GitHub Desktop.
Vyper ERC20 example
[seq,
[mstore, 32, 1461501637330902918203684832716283019655932542976],
[mstore, 64, 170141183460469231731687303715884105727],
[mstore, 96, -170141183460469231731687303715884105728],
[mstore, 128, 1701411834604692317316873037158841057270000000000],
[mstore, 160, -1701411834604692317316873037158841057280000000000],
# Line 26
[codecopy, 320, ~codelen, 128],
[assert, [iszero, callvalue]],
[codecopy, 448, [add, ~codelen, [codeload, ~codelen]], 96],
[assert, [le, [calldataload, [add, 4, [codeload, ~codelen]]], 64]],
[codecopy, 576, [add, ~codelen, [codeload, [add, ~codelen, 32]]], 64],
[assert, [le, [calldataload, [add, 4, [codeload, [add, ~codelen, 32]]]], 32]],
# Line 28
[mstore,
672,
[seq,
# Line 28
[if,
[iszero, [mload, '416' <_supply>]],
0,
[seq,
[assert,
[eq,
[div,
[mul,
[mload, '416' <_supply>],
[seq,
[assert,
[or,
[or, [eq, [mload, '384' <_decimals>], 1], [iszero, [mload, '384' <_decimals>]]],
[lt, 10, [exp, 10, [mload, '384' <_decimals>]]]]],
[exp, 10, [mload, '384' <_decimals>]]]],
[mload, '416' <_supply>]],
[seq,
[assert,
[or,
[or, [eq, [mload, '384' <_decimals>], 1], [iszero, [mload, '384' <_decimals>]]],
[lt, 10, [exp, 10, [mload, '384' <_decimals>]]]]],
[exp, 10, [mload, '384' <_decimals>]]]]],
[mul,
[mload, '416' <_supply>],
[seq,
[assert,
[or,
[or, [eq, [mload, '384' <_decimals>], 1], [iszero, [mload, '384' <_decimals>]]],
[lt, 10, [exp, 10, [mload, '384' <_decimals>]]]]],
[exp, 10, [mload, '384' <_decimals>]]]]]]]],
# Line 29
[with,
_pos,
'448' <_name>,
/* copy byte slice src: _pos dst: [sha3_32, '0' <self.name>] */
[with,
_pos,
_pos,
[with,
_opos,
[sha3_32, '0' <self.name>],
[with,
_actual_len,
[add, [mload, _pos], 32],
[repeat,
288,
0,
3,
[seq,
[if, [gt, [mul, 32, [mload, 288]], _actual_len], break],
[sstore,
[add, _opos, [mload, 288]],
[mload, [add, _pos, [mul, 32, [mload, 288]]]]]]]]]]],
# Line 30
[with,
_pos,
'576' <_symbol>,
/* copy byte slice src: _pos dst: [sha3_32, '1' <self.symbol>] */
[with,
_pos,
_pos,
[with,
_opos,
[sha3_32, '1' <self.symbol>],
[with,
_actual_len,
[add, [mload, _pos], 32],
[repeat,
288,
0,
2,
[seq,
[if, [gt, [mul, 32, [mload, 288]], _actual_len], break],
[sstore,
[add, _opos, [mload, 288]],
[mload, [add, _pos, [mul, 32, [mload, 288]]]]]]]]]]],
# Line 31
[sstore, '2' <self.decimals>, [mload, '384' <_decimals>]],
# Line 32
[sstore, [sha3_64, '3' <self.balanceOf>, caller], [mload, '672' <init_supply>]],
# Line 33
[sstore, '5' <self.total_supply>, [mload, '672' <init_supply>]],
# Line 34
[sstore, '6' <self.minter>, caller],
# Line 35
[mstore, 704, [mload, '672' <init_supply>]],
[log3,
704,
32,
100389287136786176327247604509743168900146139575972864366142685224231313322991,
0,
caller],
[return,
0,
[lll,
[seq,
[mstore, 28, [calldataload, 0]],
[mstore, 32, 1461501637330902918203684832716283019655932542976],
[mstore, 64, 170141183460469231731687303715884105727],
[mstore, 96, -170141183460469231731687303715884105728],
[mstore, 128, 1701411834604692317316873037158841057270000000000],
[mstore, 160, -1701411834604692317316873037158841057280000000000],
# Line 38
[if,
[eq, [mload, 0], '404098525' <totalSupply()>],
[seq,
[assert, [iszero, callvalue]],
# Line 44
[mstore, 0, [sload, '5' <self.total_supply>]],
[seq_unchecked, [return, 0, 32]],
# Line 38
stop]],
# Line 47
[if,
[eq, [mload, 0], '3714247998' <allowance(address,address)>],
[seq,
[assert, [iszero, callvalue]],
/* checking address input */ [uclamplt, [calldataload, 4], [mload, 32]],
/* checking address input */ [uclamplt, [calldataload, 36], [mload, 32]],
# Line 56
[mstore,
0,
[sload,
[sha3_64,
[sha3_64, '4' <self.allowances>, [calldataload, '4' <_owner>]],
[calldataload, '36' <_spender>]]]],
[seq_unchecked, [return, 0, 32]],
# Line 47
stop]],
# Line 59
[if,
[eq, [mload, 0], '2835717307' <transfer(address,uint256)>],
[seq,
[assert, [iszero, callvalue]],
/* checking address input */ [uclamplt, [calldataload, 4], [mload, 32]],
# Line 68
[with,
_stloc,
[sha3_64, '3' <self.balanceOf>, caller],
[sstore,
_stloc,
[seq,
[assert, [ge, [sload, _stloc], [calldataload, '36' <_value>]]],
[sub, [sload, _stloc], [calldataload, '36' <_value>]]]]],
# Line 69
[with,
_stloc,
[sha3_64, '3' <self.balanceOf>, [calldataload, '4' <_to>]],
[sstore,
_stloc,
[seq,
[assert, [ge, [add, [sload, _stloc], [calldataload, '36' <_value>]], [sload, _stloc]]],
[add, [sload, _stloc], [calldataload, '36' <_value>]]]]],
# Line 70
[mstore, 320, [calldataload, '36' <_value>]],
[log3,
320,
32,
100389287136786176327247604509743168900146139575972864366142685224231313322991,
caller,
[calldataload, '4' <_to>]],
# Line 71
[mstore, 0, 1],
[seq_unchecked, [return, 0, 32]],
# Line 59
stop]],
# Line 74
[if,
[eq, [mload, 0], '599290589' <transferFrom(address,address,uint256)>],
[seq,
[assert, [iszero, callvalue]],
/* checking address input */ [uclamplt, [calldataload, 4], [mload, 32]],
/* checking address input */ [uclamplt, [calldataload, 36], [mload, 32]],
# Line 86
[with,
_stloc,
[sha3_64, '3' <self.balanceOf>, [calldataload, '4' <_from>]],
[sstore,
_stloc,
[seq,
[assert, [ge, [sload, _stloc], [calldataload, '68' <_value>]]],
[sub, [sload, _stloc], [calldataload, '68' <_value>]]]]],
# Line 87
[with,
_stloc,
[sha3_64, '3' <self.balanceOf>, [calldataload, '36' <_to>]],
[sstore,
_stloc,
[seq,
[assert, [ge, [add, [sload, _stloc], [calldataload, '68' <_value>]], [sload, _stloc]]],
[add, [sload, _stloc], [calldataload, '68' <_value>]]]]],
# Line 90
[with,
_stloc,
[sha3_64,
[sha3_64, '4' <self.allowances>, [calldataload, '4' <_from>]],
caller],
[sstore,
_stloc,
[seq,
[assert, [ge, [sload, _stloc], [calldataload, '68' <_value>]]],
[sub, [sload, _stloc], [calldataload, '68' <_value>]]]]],
# Line 91
[mstore, 320, [calldataload, '68' <_value>]],
[log3,
320,
32,
100389287136786176327247604509743168900146139575972864366142685224231313322991,
[calldataload, '4' <_from>],
[calldataload, '36' <_to>]],
# Line 92
[mstore, 0, 1],
[seq_unchecked, [return, 0, 32]],
# Line 74
stop]],
# Line 95
[if,
[eq, [mload, 0], '157198259' <approve(address,uint256)>],
[seq,
[assert, [iszero, callvalue]],
/* checking address input */ [uclamplt, [calldataload, 4], [mload, 32]],
# Line 106
[sstore,
[sha3_64,
[sha3_64, '4' <self.allowances>, caller],
[calldataload, '4' <_spender>]],
[calldataload, '36' <_value>]],
# Line 107
[mstore, 320, [calldataload, '36' <_value>]],
[log3,
320,
32,
63486140976153616755203102783360879283472101686154884697241723088393386309925,
caller,
[calldataload, '4' <_spender>]],
# Line 108
[mstore, 0, 1],
[seq_unchecked, [return, 0, 32]],
# Line 95
stop]],
# Line 111
[if,
[eq, [mload, 0], '1086394137' <mint(address,uint256)>],
[seq,
[assert, [iszero, callvalue]],
/* checking address input */ [uclamplt, [calldataload, 4], [mload, 32]],
# Line 120
[assert, [eq, caller, [sload, '6' <self.minter>]]],
# Line 121
[assert, [xor, [calldataload, '4' <_to>], 0]],
# Line 122
[with,
_stloc,
'5' <self.total_supply>,
[sstore,
_stloc,
[seq,
[assert, [ge, [add, [sload, _stloc], [calldataload, '36' <_value>]], [sload, _stloc]]],
[add, [sload, _stloc], [calldataload, '36' <_value>]]]]],
# Line 123
[with,
_stloc,
[sha3_64, '3' <self.balanceOf>, [calldataload, '4' <_to>]],
[sstore,
_stloc,
[seq,
[assert, [ge, [add, [sload, _stloc], [calldataload, '36' <_value>]], [sload, _stloc]]],
[add, [sload, _stloc], [calldataload, '36' <_value>]]]]],
# Line 124
[mstore, 320, [calldataload, '36' <_value>]],
[log3,
320,
32,
100389287136786176327247604509743168900146139575972864366142685224231313322991,
0,
[calldataload, '4' <_to>]],
# Line 111
stop]],
# Line 127
[if,
0,
[seq,
/* _burn(address,uint256) */ [label, priv_1633807128],
/* pop callback pointer */ [mstore, 384, pass],
[mstore, 320, pass],
[mstore, 352, pass],
# Line 135
[assert, [xor, [mload, '320' <_to>], 0]],
# Line 136
[with,
_stloc,
'5' <self.total_supply>,
[sstore,
_stloc,
[seq,
[assert, [ge, [sload, _stloc], [mload, '352' <_value>]]],
[sub, [sload, _stloc], [mload, '352' <_value>]]]]],
# Line 137
[with,
_stloc,
[sha3_64, '3' <self.balanceOf>, [mload, '320' <_to>]],
[sstore,
_stloc,
[seq,
[assert, [ge, [sload, _stloc], [mload, '352' <_value>]]],
[sub, [sload, _stloc], [mload, '352' <_value>]]]]],
# Line 138
[mstore, 416, [mload, '352' <_value>]],
[log3,
416,
32,
100389287136786176327247604509743168900146139575972864366142685224231313322991,
[mload, '320' <_to>],
0],
# Line 127
[jump, [mload, 384]]]],
# Line 141
[if,
[eq, [mload, 0], '1117154408' <burn(uint256)>],
[seq,
[assert, [iszero, callvalue]],
# Line 147
/* Internal Call: _burn */
[pop,
[seq_unchecked,
[seq,
[mstore, 320, 1633807128],
[mstore, 352, caller],
[mstore, 384, [calldataload, '4' <_value>]]],
[mload, 384],
[mload, 352],
[add, pc, 6],
[goto, priv_1633807128],
jumpdest,
0]],
# Line 141
stop]],
# Line 150
[if,
[eq, [mload, 0], '2043438992' <burnFrom(address,uint256)>],
[seq,
[assert, [iszero, callvalue]],
/* checking address input */ [uclamplt, [calldataload, 4], [mload, 32]],
# Line 157
[with,
_stloc,
[sha3_64, [sha3_64, '4' <self.allowances>, [calldataload, '4' <_to>]], caller],
[sstore,
_stloc,
[seq,
[assert, [ge, [sload, _stloc], [calldataload, '36' <_value>]]],
[sub, [sload, _stloc], [calldataload, '36' <_value>]]]]],
# Line 158
/* Internal Call: _burn */
[pop,
[seq_unchecked,
[seq,
[mstore, 320, 1633807128],
[mstore, 352, [calldataload, '4' <_to>]],
[mstore, 384, [calldataload, '36' <_value>]]],
[mload, 384],
[mload, 352],
[add, pc, 6],
[goto, priv_1633807128],
jumpdest,
0]],
# Line 150
stop]],
# Line 12
[if,
[eq, [mload, 0], '117300739' <name()>],
[seq,
[assert, [iszero, callvalue]],
# Line 14
[with,
_pos,
'0' <self.name>,
/* copy byte slice src: [sha3_32, _pos] dst: 384 */
[with,
_pos,
[sha3_32, _pos],
[with,
_opos,
384,
[with,
_actual_len,
[add, [sload, _pos], 32],
[repeat,
288,
0,
3,
[seq,
[if, [gt, [mul, 32, [mload, 288]], _actual_len], break],
[mstore,
[add, _opos, [mul, 32, [mload, 288]]],
[sload, [add, _pos, [mload, 288]]]]]]]]]],
/* Zero pad */
[with,
len,
[mload, 384],
[with,
dst,
[add, 416, len],
[calldatacopy, dst, calldatasize, [sub, [ceil32, len], len]]]],
[mstore, 352, 32],
[seq_unchecked, [return, 352, [ceil32, [add, [mload, 384], 64]]]],
# Line 12
stop]],
# Line 13
[if,
[eq, [mload, 0], '2514000705' <symbol()>],
[seq,
[assert, [iszero, callvalue]],
# Line 15
[with,
_pos,
'1' <self.symbol>,
/* copy byte slice src: [sha3_32, _pos] dst: 384 */
[with,
_pos,
[sha3_32, _pos],
[with,
_opos,
384,
[with,
_actual_len,
[add, [sload, _pos], 32],
[repeat,
288,
0,
2,
[seq,
[if, [gt, [mul, 32, [mload, 288]], _actual_len], break],
[mstore,
[add, _opos, [mul, 32, [mload, 288]]],
[sload, [add, _pos, [mload, 288]]]]]]]]]],
/* Zero pad */
[with,
len,
[mload, 384],
[with,
dst,
[add, 416, len],
[calldatacopy, dst, calldatasize, [sub, [ceil32, len], len]]]],
[mstore, 352, 32],
[seq_unchecked, [return, 352, [ceil32, [add, [mload, 384], 64]]]],
# Line 13
stop]],
# Line 14
[if,
[eq, [mload, 0], '826074471' <decimals()>],
[seq,
[assert, [iszero, callvalue]],
# Line 16
[mstore, 0, [sload, '2' <self.decimals>]],
[seq_unchecked, [return, 0, 32]],
# Line 14
stop]],
# Line 20
[if,
[eq, [mload, 0], '1889567281' <balanceOf(address)>],
[seq,
[assert, [iszero, callvalue]],
/* checking address input */ [uclamplt, [calldataload, 4], [mload, 32]],
# Line 22
[mstore,
0,
[sload, [sha3_64, '3' <self.balanceOf>, [calldataload, '4' <arg0>]]]],
[seq_unchecked, [return, 0, 32]],
# Line 20
stop]],
/* Default function */ [revert, 0, 0]],
0]]]
# @dev Implementation of ERC-20 token standard.
# @author Takayuki Jimba (@yudetamago)
# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
from vyper.interfaces import ERC20
implements: ERC20
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)
# NOTE: By declaring `balanceOf` as public, vyper automatically generates a 'balanceOf()' getter
# method to allow access to account balances.
# The _KeyType will become a required parameter for the getter and it will return _ValueType.
# See: https://vyper.readthedocs.io/en/v0.1.0-beta.8/types.html?highlight=getter#mappings
balanceOf: public(map(address, uint256))
allowances: map(address, map(address, uint256))
total_supply: uint256
minter: address
@public
def __init__(_name: string[64], _symbol: string[32], _decimals: uint256, _supply: uint256):
init_supply: uint256 = _supply * 10 ** _decimals
self.name = _name
self.symbol = _symbol
self.decimals = _decimals
self.balanceOf[msg.sender] = init_supply
self.total_supply = init_supply
self.minter = msg.sender
log.Transfer(ZERO_ADDRESS, msg.sender, init_supply)
@public
@constant
def totalSupply() -> uint256:
"""
@dev Total number of tokens in existence.
"""
return self.total_supply
@public
@constant
def allowance(_owner : address, _spender : address) -> uint256:
"""
@dev Function to check the amount of tokens that an owner allowed to a spender.
@param _owner The address which owns the funds.
@param _spender The address which will spend the funds.
@return An uint256 specifying the amount of tokens still available for the spender.
"""
return self.allowances[_owner][_spender]
@public
def transfer(_to : address, _value : uint256) -> bool:
"""
@dev Transfer token for a specified address
@param _to The address to transfer to.
@param _value The amount to be transferred.
"""
# NOTE: vyper does not allow underflows
# so the following subtraction would revert on insufficient balance
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:
"""
@dev Transfer tokens from one address to another.
Note that while this function emits a Transfer event, this is not required as per the specification,
and other compliant implementations may not emit the event.
@param _from address The address which you want to send tokens from
@param _to address The address which you want to transfer to
@param _value uint256 the amount of tokens to be transferred
"""
# 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:
"""
@dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
Beware that changing an allowance with this method brings the risk that someone may use both the old
and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
@param _spender The address which will spend the funds.
@param _value The amount of tokens to be spent.
"""
self.allowances[msg.sender][_spender] = _value
log.Approval(msg.sender, _spender, _value)
return True
@public
def mint(_to: address, _value: uint256):
"""
@dev Mint an amount of the token and assigns it to an account.
This encapsulates the modification of balances such that the
proper events are emitted.
@param _to The account that will receive the created tokens.
@param _value The amount that will be created.
"""
assert msg.sender == self.minter
assert _to != ZERO_ADDRESS
self.total_supply += _value
self.balanceOf[_to] += _value
log.Transfer(ZERO_ADDRESS, _to, _value)
@private
def _burn(_to: address, _value: uint256):
"""
@dev Internal function that burns an amount of the token of a given
account.
@param _to The account whose tokens will be burned.
@param _value The amount that will be burned.
"""
assert _to != ZERO_ADDRESS
self.total_supply -= _value
self.balanceOf[_to] -= _value
log.Transfer(_to, ZERO_ADDRESS, _value)
@public
def burn(_value: uint256):
"""
@dev Burn an amount of the token of msg.sender.
@param _value The amount that will be burned.
"""
self._burn(msg.sender, _value)
@public
def burnFrom(_to: address, _value: uint256):
"""
@dev Burn an amount of the token from a given account.
@param _to The account whose tokens will be burned.
@param _value The amount that will be burned.
"""
self.allowances[_to][msg.sender] -= _value
self._burn(_to, _value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment