EIPs/EIPS/eip-token-standard.md

127 lines
3.6 KiB
Markdown
Raw Normal View History

2017-04-24 13:37:03 +00:00
## Preamble
EIP: <to be assigned> (I Suggest 20)
Title: <EIP title>
Author: Fabian Vogelsteller <fabian@ethereum.org>, Vitalik Buterin <vitalik.buterin@ethereum.org>
Type: Informational
Status: Draft
Created: 2015-11-19
## Simple Summary
2017-05-03 13:38:54 +00:00
A standard interface for tokens.
2017-04-24 13:37:03 +00:00
## Abstract
2017-05-03 13:38:54 +00:00
The following standard allows for the implementation of a standard API for tokens within their smart contracts:
it primarily provides basic functionality to transfer tokens and allow them to be approved to be spend by another on-chain third party.
2017-04-24 13:37:03 +00:00
## Motivation
2017-05-03 13:38:54 +00:00
A standard interface allows any tokens on Ethereum to be re-used by other applications: from wallets to decentralized exchanges.
2017-04-24 13:37:03 +00:00
## Specification
## Token
### Methods
**NOTE**: An important point is that callers should handle `false` from `returns (bool success)`. Callers should not assume that `false` is never returned!
#### totalSupply
``` js
function totalSupply() constant returns (uint256 totalSupply)
```
Get the total token supply
2017-04-24 13:42:04 +00:00
2017-04-24 13:37:03 +00:00
#### balanceOf
``` js
function balanceOf(address _owner) constant returns (uint256 balance)
```
Get the account balance of another account with address `_owner`
2017-04-24 13:42:04 +00:00
2017-04-24 13:37:03 +00:00
#### transfer
``` js
function transfer(address _to, uint256 _value) returns (bool success)
```
2017-05-03 13:38:54 +00:00
Transfer `_value` amount of tokens to address `_to`
2017-04-24 13:42:04 +00:00
2017-04-24 13:37:03 +00:00
#### transferFrom
``` js
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
```
Send `_value` amount of tokens from address `_from` to address `_to`
The `transferFrom` method is used for a withdraw workflow, allowing contracts to send tokens on your behalf, for example to "deposit" to a contract address and/or to charge fees in sub-currencies; the command should fail unless the `_from` account has deliberately authorized the sender of the message via some mechanism; we propose these standardized APIs for approval:
2017-04-24 13:42:04 +00:00
2017-04-24 13:37:03 +00:00
#### approve
``` js
function approve(address _spender, uint256 _value) returns (bool success)
```
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.
2017-05-03 13:38:54 +00:00
To prevent attack vectors like the one described here: https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/
2017-04-24 13:37:03 +00:00
make sure to set the allowance to 0 before setting it to another value for the same spender.
2017-04-24 13:42:04 +00:00
2017-04-24 13:37:03 +00:00
#### allowance
``` js
function allowance(address _owner, address _spender) constant returns (uint256 remaining)
```
Returns the amount which `_spender` is still allowed to withdraw from `_owner`
2017-04-24 13:42:04 +00:00
2017-04-24 13:37:03 +00:00
### Events
2017-04-24 13:42:04 +00:00
2017-04-24 13:37:03 +00:00
#### Transfer
``` js
event Transfer(address indexed _from, address indexed _to, uint256 _value)
```
Triggered when tokens are transferred.
2017-04-24 13:42:04 +00:00
2017-04-24 13:37:03 +00:00
#### Approval
``` js
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
```
Triggered whenever `approve(address _spender, uint256 _value)` is called.
## Implementation
2017-05-03 13:38:54 +00:00
There are already plenty of ERC20-compliant tokens deployed on the Ethereum network and is the most widely used standard.
Different implementations have been written by various teams that have different trade-offs: from gas saving to improved security.
#### Example implementations are available at
2017-04-24 13:37:03 +00:00
- https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/StandardToken.sol
- https://github.com/ConsenSys/Tokens/blob/master/Token_Contracts/contracts/StandardToken.sol
2017-05-03 13:38:54 +00:00
#### Implementation adding the force 0 before calling approve again:
2017-04-24 13:37:03 +00:00
- https://github.com/Giveth/minime/blob/master/MiniMeToken.sol
## Copyright
Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).