ERC-823 Token Exchange Standard
Stagnant · Tokens · created 2018-01-06
A standard that lets an ERC20 token holder exchange their tokens for another registered ERC20 token, and immediately spend the exchanged tokens as payment.
Why it is needed
The ERC20 standard itself has no function for converting one token into another. As a result, exchanging tokens requires relying on a separate token converter, and existing converters work by burning the original token during the exchange, reducing total supply. A design where tokens are destroyed cannot be treated like currency, and it also weakens the incentive to create new tokens. In addition, spending the exchanged tokens immediately as payment requires handling the exchange and the spend as two separate steps. Without such a standard, every project implements exchange logic differently, making it difficult for a compatible exchange mechanism between ERC20 tokens to take hold.
What to implement
function exchangeToken(address _targetContract, uint _amount) public returns(bool success, uint creditedAmount): A function that requests the exchange service contract to exchange held tokens for tokens of a target contract.function exchangeAndSpend(address _targetContract, uint _amount,address _to) public returns(bool success): A function that, after exchanging tokens, requests that the converted amount be immediately spent to the _to address.function __exchangerCallback(address _targetContract,address _exchanger, uint _amount) public returns(bool success): A callback function called by the exchange service contract to deduct _amount from the balance of the person who requested the exchange.event Exchange(address _from, address _ targetContract, uint _amount): An event on the sender side contract recording that a new token exchange has occurred.event ExchangeSpent(address _from, address _targetContract, address _to, uint _amount): An event on the sender side contract recording a case where a spend occurred simultaneously with an exchange.function __targetExchangeCallback (uint _to, uint _amount) public returns(bool success): A callback function called by the exchange service contract to credit _amount of the target token to the exchanger's address, completing the exchange.function __targetExchangeAndSpendCallback (address _from, address _to, uint _amount) public returns(bool success): A callback function called by the exchange service contract to credit the token and immediately transfer it to the _to address, completing both the exchange and the spend together.event Exchange(address _from, address _with, uint _amount): An event on the receiver side contract recording that a new token exchange has occurred.event ExchangeSpent(address _from, address _ targetContract, address _to, uint _amount): An event on the receiver side contract recording a case where a spend occurred simultaneously with an exchange.function registerToken(address _token) public returns(bool success): A function that lets a token contract owner register their token with the exchange service so it can participate in exchanges.function exchangeToken(address _targetContract, uint _amount, address _from) public returns(bool success, uint creditedAmount): A function that looks up the exchange rate to calculate the exchange amount and calls the sender and receiver callbacks to carry out the actual exchange.function exchangeAndSpend(address _targetContract, uint _amount, address _from, address _to) public returns(bool success): A function that looks up the exchange rate to calculate the exchange amount and calls the sender callback and the receiver's spend callback to handle the exchange and spend together.event Exchange( address _from, address _by, uint _value ,address _target ): An event at the exchange service contract level recording that a new exchange has occurred.event ExchangeAndSpent ( address _from, address _by, uint _value ,address _target ,address _to): An event at the exchange service contract level recording a transaction where a spend occurred simultaneously with an exchange.
In depth
Sender Interface
The interface implemented by an ERC20 contract that wants to exchange its own tokens for another token. The exchangedWith mapping accumulates how much (uint) has been exchanged with which target contract (address), and the exchangedBy mapping records the exchange amount per address of the person who requested the exchange. exchangeToken(_targetContract, _amount) calls the exchange service contract to initiate the exchange and returns success and creditedAmount. exchangeAndSpend(_targetContract, _amount, _to) requests that the converted amount be spent to the _to address at the same time as the exchange. __exchangerCallback(_targetContract, _exchanger, _amount) is a callback that only the exchange service contract can call, deducting _amount from the exchanger's balance. This process is recorded via the Exchange and ExchangeSpent events.
Receiver Interface
The interface implemented by an ERC20 contract that receives tokens exchanged from another token. The exchangesReceived mapping records how much (uint) has been received and held from which token (address). __targetExchangeCallback(_to, _amount) can only be called by the exchange service contract, and credits _amount of the target token to the exchanger's address, completing the exchange. __targetExchangeAndSpendCallback(_from, _to, _amount) credits the amount and simultaneously transfers the token to the _to address, finishing the exchange and spend in one step. Both callbacks record their results via the Exchange and ExchangeSpent events.
Exchange Service Contract
A separate contract that acts as the intermediary between the sender and receiver sides. It uses an oracle to obtain the authenticated exchange rate. The registeredTokens array is the list of tokens registered to participate in exchanges, and a token must be registered in this list before it can be exchanged. registerToken(_token) is the function a token contract owner uses to register their own token. exchangeToken(_targetContract, _amount, _from) looks up the exchange rate to calculate the converted amount, then calls the sender's __exchangerCallback and the receiver's __targetExchangeCallback in sequence to complete the exchange. exchangeAndSpend(_targetContract, _amount, _from, _to) follows the same process but also includes the spend, calling __targetExchangeAndSpendCallback. The result is recorded via the Exchange and ExchangeAndSpent events.
A design that does not burn tokens
Existing token converters worked by burning the original token during exchange, reducing total supply. This standard instead does not burn the exchanged token, keeping it held inside the target contract so it can be reused or preserved later. The specification describes this as closer to how currency is handled compared to existing token converters, and states that this structure is an exchange standard that can be applied consistently across all ERC20 tokens.
Backward compatibility with ERC20
Because this proposal simply extends the ERC20 standard, it is fully backward compatible. A token implementing this standard still follows all the existing functionality of the ERC20 standard, and the specification explains that this standard merely adds exchange and spend functionality on top of it. However, it should be noted that the specification's status is Stagnant, a draft proposed in 2018 that has not progressed further and has stalled.
Things to watch out for
- Restricting who may call the callback functions: __exchangerCallback, __targetExchangeCallback, and __targetExchangeAndSpendCallback must only be callable by the exchange service contract. Otherwise an arbitrary caller could manipulate balances.
- Handling false return values: Callers of functions that return success must handle the possibility that false is returned, and must not always assume true.
- Allow exchange only for registered tokens: Before an exchange takes place, the target token must be registered in the exchange service contract's registeredTokens.