-
Notifications
You must be signed in to change notification settings - Fork 8
/
erc777.vy
270 lines (222 loc) · 8.58 KB
/
erc777.vy
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Author: Sören Steiger, github.com/ssteiger
# License: MIT
# ERC777 Token Standard
# https://eips.ethereum.org/EIPS/eip-777
# Interface for ERC1820 registry contract
# https://eips.ethereum.org/EIPS/eip-1820
contract ERC1820Registry:
def setInterfaceImplementer(
_addr: address,
_interfaceHash: bytes32,
_implementer: address,
): modifying
def getInterfaceImplementer(
_addr: address,
_interfaceHash: bytes32,
) -> address: modifying
# Interface for ERC777Tokens sender contracts
contract ERC777TokensSender:
def tokensToSend(
_operator: address,
_from: address,
_to: address,
_amount: uint256,
_data: bytes[256],
_operatorData: bytes[256]
): modifying
# Interface for ERC777Tokens recipient contracts
contract ERC777TokensRecipient:
def tokensReceived(
_operator: address,
_from: address,
_to: address,
_amount: uint256,
_data: bytes[256],
_operatorData: bytes[256]
): modifying
Sent: event({
_operator: indexed(address), # Address which triggered the send.
_from: indexed(address), # Token holder.
_to: indexed(address), # Token recipient.
_amount: uint256, # Number of tokens to send.
_data: bytes[256], # Information provided by the token holder.
_operatorData: bytes[256] # Information provided by the operator.
})
Minted: event({
_operator: indexed(address), # Address which triggered the mint.
_to: indexed(address), # Recipient of the tokens.
_amount: uint256, # Number of tokens minted.
_data: bytes[256], # Information provided for the recipient.
_operatorData: bytes[256] # Information provided by the operator.
})
Burned: event({
_operator: indexed(address), # Address which triggered the burn.
_from: indexed(address), # Token holder whose tokens are burned.
_amount: uint256, # Token holder whose tokens are burned.
_data: bytes[256], # Information provided by the token holder.
_operatorData: bytes[256] # Information provided by the operator.
})
AuthorizedOperator: event({
_operator: indexed(address), # Address which became an operator of tokenHolder.
_holder: indexed(address) # Address of a token holder which authorized the operator address as an operator.
})
RevokedOperator: event({
_operator: indexed(address), # Address which was revoked as an operator of tokenHolder.
_holder: indexed(address) # Address of a token holder which revoked the operator address as an operator.
})
erc1820Registry: ERC1820Registry
erc1820RegistryAddress: constant(address) = 0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24
# TODO: decide which size to use
NO_OF_DEFAULT_OPERATORS: constant(uint256) = 5
MAX_LENGTH_NAME: constant(uint256) = 64
MAX_LENGTH_SYMBOL: constant(uint256) = 32
name: public(string[MAX_LENGTH_NAME])
symbol: public(string[MAX_LENGTH_SYMBOL])
totalSupply: public(uint256)
granularity: public(uint256)
balanceOf: public(map(address, uint256))
defaultOperatorsList: address[NO_OF_DEFAULT_OPERATORS]
defaultOperatorsMap: map(address, bool)
operators: map(address, map(address, bool))
@public
def __init__(
_name: string[MAX_LENGTH_NAME],
_symbol: string[MAX_LENGTH_SYMBOL],
_totalSupply: uint256,
_granularity: uint256,
_defaultOperators: address[NO_OF_DEFAULT_OPERATORS]
):
self.name = _name
self.symbol = _symbol
self.totalSupply = _totalSupply
# The granularity value MUST be greater than or equal to 1
assert _granularity >= 1
self.granularity = _granularity
self.defaultOperatorsList = _defaultOperators
for i in range(NO_OF_DEFAULT_OPERATORS):
assert _defaultOperators[i] != ZERO_ADDRESS
self.defaultOperatorsMap[_defaultOperators[i]] = True
self.erc1820Registry = ERC1820Registry(erc1820RegistryAddress)
self.erc1820Registry.setInterfaceImplementer(self, keccak256("ERC777Token"), self)
@private
def _checkForERC777TokensInterface_Sender(
_operator: address,
_from: address,
_to: address,
_amount: uint256,
_data: bytes[256]="",
_operatorData: bytes[256]=""
):
implementer: address = self.erc1820Registry.getInterfaceImplementer(_from, keccak256("ERC777TokensSender"))
if implementer != ZERO_ADDRESS:
ERC777TokensSender(_from).tokensToSend(_operator, _from, _to, _amount, _data, _operatorData)
@private
def _checkForERC777TokensInterface_Recipient(
_operator: address,
_from: address,
_to: address,
_amount: uint256,
_data: bytes[256]="",
_operatorData: bytes[256]=""
):
implementer: address = self.erc1820Registry.getInterfaceImplementer(_to, keccak256("ERC777TokensRecipient"))
if implementer != ZERO_ADDRESS:
ERC777TokensRecipient(_to).tokensReceived(_operator, _from, _to, _amount, _data, _operatorData)
@private
def _transferFunds(
_operator: address,
_from: address,
_to: address,
_amount: uint256,
_data: bytes[256]="",
_operatorData: bytes[256]=""
):
# any minting, sending or burning of tokens MUST be a multiple of the granularity value.
assert _amount % self.granularity == 0
# check for 'tokensToSend' hook
if _from.is_contract:
self._checkForERC777TokensInterface_Sender(_operator, _from, _to, _amount, _data, _operatorData)
self.balanceOf[_from] -= _amount
self.balanceOf[_to] += _amount
# check for 'tokensReceived' hook
# but only if transfer is not a burn
if _to != ZERO_ADDRESS:
if _to.is_contract:
self._checkForERC777TokensInterface_Recipient(_operator, _from, _to, _amount, _data, _operatorData)
@public
@constant
def defaultOperators() -> address[NO_OF_DEFAULT_OPERATORS]:
return self.defaultOperatorsList
@public
@constant
def isOperatorFor(_operator: address, _holder: address) -> bool:
return (self.operators[_holder])[_operator] or self.defaultOperatorsMap[_operator] or _operator == _holder
@public
def authorizeOperator(_operator: address):
(self.operators[msg.sender])[_operator] = True
log.AuthorizedOperator(_operator, msg.sender)
@public
def revokeOperator(_operator: address):
# MUST revert if it is called to revoke the holder as an operator for itself
assert _operator != msg.sender
(self.operators[msg.sender])[_operator] = False
log.RevokedOperator(_operator, msg.sender)
@public
def send(_to: address, _amount: uint256, _data: bytes[256]=""):
assert _to != ZERO_ADDRESS
operatorData: bytes[256]=""
self._transferFunds(msg.sender, msg.sender, _to, _amount, _data, operatorData)
log.Sent(msg.sender, msg.sender, _to, _amount, _data, operatorData)
@public
def operatorSend(
_from: address,
_to: address,
_amount: uint256,
_data: bytes[256]="",
_operatorData: bytes[256]=""
):
assert _to != ZERO_ADDRESS
assert self.isOperatorFor(msg.sender, _from)
self._transferFunds(msg.sender, _from, _to, _amount, _data, _operatorData)
log.Sent(msg.sender, _from, _to, _amount, _data, _operatorData)
@public
def burn(_amount: uint256, _data: bytes[256]=""):
operatorData: bytes[256]=""
self._transferFunds(msg.sender, msg.sender, ZERO_ADDRESS, _amount, _data, operatorData)
self.totalSupply -= _amount
log.Burned(msg.sender, msg.sender, _amount, _data, operatorData)
@public
def operatorBurn(
_from: address,
_amount: uint256,
_data: bytes[256]="",
_operatorData: bytes[256]=""
):
# _from: Token holder whose tokens will be burned (or 0x0 to set from to msg.sender).
fromAddress: address
if _from == ZERO_ADDRESS:
fromAddress = msg.sender
else:
fromAddress = _from
assert self.isOperatorFor(msg.sender, fromAddress)
self._transferFunds(msg.sender, fromAddress, ZERO_ADDRESS, _amount, _data, _operatorData)
self.totalSupply -= _amount
log.Burned(msg.sender, fromAddress, _amount, _data, _operatorData)
# NOTE: ERC777 intentionally does not define specific functions to mint tokens.
@public
def mint(
_to: address,
_amount: uint256,
_operatorData: bytes[256]=""
):
assert _to != ZERO_ADDRESS
# any minting, sending or burning of tokens MUST be a multiple of the granularity value.
assert _amount % self.granularity == 0
# only operators are allowed to mint
assert self.defaultOperatorsMap[msg.sender]
self.balanceOf[_to] += _amount
self.totalSupply += _amount
data: bytes[256]=""
if _to.is_contract:
self._checkForERC777TokensInterface_Recipient(msg.sender, ZERO_ADDRESS, _to, _amount, data, _operatorData)
log.Minted(msg.sender, _to, _amount, data, _operatorData)