-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathERC20.vif
90 lines (64 loc) · 3.4 KB
/
ERC20.vif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Vyper Port of MyToken
# THIS CONTRACT HAS NOT BEEN AUDITED!
# ERC20 details at:
# https://theethereum.wiki/w/index.php/ERC20_Token_Standard
# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
# Events of the token.
Transfer: event({_from: indexed(address), _to: indexed(address), _value: uint256})
Approval: event({_owner: indexed(address), _spender: indexed(address), _value: uint256})
# Variables of the token.
# name: public(bytes32)
name:IFL(public(bytes32),this)
symbol:IFL(public(bytes32),this)
totalSupply:IFL(public(uint256),this)
decimals: IFL(public(int128),this)
balances: IFL(int128[address],this)
allowed: IFL(int128[address][address],this)
@public
def __init__(_name: bytes32, _symbol: bytes32, _decimals: uint256, _initialSupply: uint256):
self.name = _name
self.symbol = _symbol
self.decimals = _decimals
self.totalSupply =_initialSupply * convert(10, 'uint256') ** _decimals
self.balances[msg.sender] = convert(self.totalSupply, 'int128')
# What is the balance of a particular account?
@public
@constant
@IFL_this
def balanceOf(_owner:IFL(address,owner)) -> uint256:
return convert(self.balances[endorse(_owner, this)], 'uint256')
# Send `_value` tokens to `_to` from your account
@public
@IFL_this
def transfer(_to: IFL(address,owner), _amount: IFL((int128(uint256)),owner)) -> bool:
if self.balances[endorse(msg.sender, this)] >= _amount and self.balances[endorse(_to, this)] + endorse(_amount, this) >= self.balances[endorse(_to, this)]:
self.balances[endorse(msg.sender, this)] -= endorse(_amount, this) # Subtract from the sender
self.balances[endorse(_to, this)] += endorse(_amount, this) # Add the same to the recipient
log.Transfer(msg.sender, _to, convert(_amount, 'uint256')) # log transfer event.
return True
else:
return False
# Transfer allowed tokens from a specific account to another.
@public
@IFL_this
# def transferFrom(_from: address, _to: address, _value: int128(uint256)) -> bool:
def transferFrom(_from: IFL(address,payer), _to: IFL(address,receiver), _value: IFL((int128(uint256)),payer)) -> bool:
if endorse(_value, this) <= self.allowed[endorse(_from, this)][endorse(msg.sender, this)] and endorse(_value, this) <= self.balances[endorse(_from, this)]:
self.balances[endorse(_from, this)] -= endorse(_value, this) # decrease balance of from address.
self.allowed[endorse(_from, this)][endorse(msg.sender, this)] -= endorse(_value, this) # decrease allowance.
self.balances[endorse(_to, this)] += endorse(_value, this) # incease balance of to address.
log.Transfer(_from, _to, convert(_value, 'uint256')) # log transfer event.
return True
else:
return False
# Allow _spender to withdraw from your account, multiple times, up to the _value amount.
# If this function is called again it overwrites the current allowance with _value.
@public
def approve(_spender: IFL(address,payer), _amount:IFL((int128(uint256)),payer)) -> bool:
self.allowed[endorse(msg.sender, this)][endorse(_spender, this)] = endorse(_amount, this)
log.Approval(msg.sender, _spender, convert(_amount, 'uint256'))
return True
# Get the allowence an address has to spend anothers' token.
@public
def allowance(_owner:IFL(address,owner), _spender:IFL(address,spender)) -> uint256:
return convert(self.allowed[endorse(_owner, this)][endorse(_spender, this)], 'uint256')