ERC-1438 Avatar component and universal wallet (dApp Components (avatar) & Universal Wallet)
Stagnant · Accounts & wallets · created 2018-09-21
A proposal that stores avatar and social badge information on the blockchain so it can be reused across multiple dApps, and that lets a single wallet query, transfer, approve, and pay with both ETH and ERC20 tokens.
Why it is needed
Most Ethereum dApps are developed by copying open source contracts as-is and attaching them to each individual project. Information such as avatars and social badges created this way is locked inside that dApp, cannot be reused by other services, and the same functionality ends up being rebuilt from scratch for each service, wasting resources. In addition, although there are many ERC20 tokens, many of them do not support an approveAndCall method that handles approval and execution in a single step, which makes it hard for wallets or payment services to handle ETH and multiple ERC20 tokens in a unified way. To reduce this fragmentation, this proposal presents a reusable avatar component and a universal wallet that handles ERC20 and ETH together.
What to implement
function totalSupply() public constant returns (uint);: Returns the total supply of the token.function balanceOf(address tokenOwner) public constant returns (uint balance);: Returns the amount of tokens held by the specified address.function allowance(address tokenOwner, address spender) public constant returns (uint remaining);: Returns the remaining amount of tokens the owner has approved for a specific spender.function transfer(address to, uint tokens) public returns (bool success);: Transfers the caller's tokens to another address.function approve(address spender, uint tokens) public returns (bool success);: Approves a specified address to spend a specified amount of the caller's tokens.function transferFrom(address from, address to, uint tokens) public returns (bool success);: An approved address transfers someone else's tokens on their behalf.event Transfer(address indexed from, address indexed to, uint tokens);: An event emitted when tokens are transferred.event Approval(address indexed tokenOwner, address indexed spender, uint tokens);: An event emitted when token usage is approved.function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success): Approves token usage while simultaneously calling the spender's receiveApproval function.function receiveApproval(address from, uint256 tokens, address token, bytes data) public;: Defines the processing logic the spender executes when approval is made via approveAndCall.constructor(bytes _msgPack) _Base(_msgPack) public {}: On contract creation, receives msgPack data containing information such as the avatar and passes it to the base contract.function () public payable {}: A fallback function that allows the contract to receive ETH.function balanceOf(address _erc20) public constant returns (uint balance): Returns the wallet's ETH balance if _erc20 is 0, otherwise returns the balance of that ERC20 token.function transfer(address _erc20, address _to, uint _tokens) onlyOwner public returns (bool success): Only the wallet owner can call this; it transfers ETH or a specified ERC20 token to another address.function approve(address _erc20, address _spender, uint _tokens) onlyOwner public returns (bool success): Only the wallet owner can call this; it approves another address to use a specified ERC20 token.function pay(address _store, uint _tokens, uint256[] _options) onlyOwner public: Looks up the erc20 and spender information of the store contract, then approves or transfers and requests payment along with an options array.function pay(address _store, uint _tokens, bytes _msgPack) onlyOwner public: Looks up the erc20 and spender information of the store contract, then approves or transfers and requests payment along with msgPack data.
In depth
Avatar store (1.1)
An avatar store is created after the ERC20 currency has first been set. Operators can customize both the categories of assets used for avatars and the viewer script that renders those assets on screen. In other words, each store is free to decide what kinds of avatar parts it sells and how they are displayed.
Asset and user data storage (1.2)
An avatar's assets and user information are stored not in contract state but in the event log portion of the blockchain. Assets are created in SVG format and compressed with gzip, and avatar information is recorded as JSON data compressed with msgpack. Leaving this in the event log means other dApps can also read the same log to fetch and reuse the avatar assets and information.
Standard ERC20 interface (2.1)
ERC20Interface is the conventional ERC20 interface, consisting of the six functions totalSupply, balanceOf, allowance, transfer, approve, and transferFrom, plus the two events Transfer and Approval. UniversalWallet communicates with each token contract through this interface.
approveAndCall and its limitations (2.2, 2.3)
Some ERC20 contracts additionally provide an approveAndCall(spender, tokens, data) function that handles approval and a function call in a single transaction. This function sets the approval value, emits an Approval event, and then immediately calls receiveApproval on the specified spender contract. However, the source text points out that not many ERC20 tokens implement approveAndCall this way. On the spender side, receiveApproval(from, tokens, token, data) from the ApproveAndCallFallBack interface is implemented to receive the approval notification.
UniversalWallet's lookup, transfer, and approval (2.4)
In its balanceOf, transfer, and approve functions, UniversalWallet branches to operate on ETH if the _erc20 address passed in is 0, and on that ERC20 token otherwise. balanceOf can be called by anyone, but transfer and approve carry an onlyOwner restriction, so only the wallet owner can execute them. transfer checks via balanceOf that the balance is sufficient before executing.
Processing payment requests via the pay function (2.4)
The pay function is overloaded in two versions: one that takes uint256[] _options and one that takes bytes _msgPack. Both look up the erc20 and spender addresses from the target _store contract for payment, then transfer ETH if erc20 is 0 or approve that token to the spender otherwise. They then call the _store's receiveApproval along with _options or _msgPack, handing payment processing off to the store. Both functions are restricted with onlyOwner.
Proposal status
This ERC is in Stagnant status, a proposal that is no longer actively discussed and has stalled. Since it has not been finalized as a standard, the interface described here cannot be considered as having been adopted as is.