Skip to content

Token Tracking

Aaron Li edited this page Jul 31, 2021 · 2 revisions

ONE Wallet tracks the wallet's ownership of tokens (EIP-20, 721, 1155) on the smart contract itself. This was first implemented in #52 then subsequently moved to an independent file as the main smart contract becomes too large.

Background

The approach taken by ONE Wallet is uncommon. Most wallets rely on tracking solutions off-chain. For example, MetaMask asks users to manually add the contract address of each token, only then the wallet would check the balance of the token, recognize the user's ownership, and allow operations on the tokens, if the user holds any balance for that token. Some other wallets rely on APIs provided by some centralized index servers. For example, Etherscan provides public APIs for tracking ERC-20 and NFT (both ERC-721 and 1155) tokens. OpenSea provides more comprehensive APIs for tracking NFTs, including the information about the token contract itself, the distribution of token owners for any token, and each user's list of owned token assets. Wallets such as Coinbase Wallet and TrustWallet rely on such APIs to automatically lookup and update user's token ownerships and balances. However, such APIs have limitations and often has undocumented, non-standard customizations. For example, OpenSea's API retrieve token contract's information only once. It ignores subsequent updates on metadata and/or name and symbols. The API server also caches image and video data that are provided in token metadata, and stores downsampled versions on centralized storage services (Google Storage). Only the cached versions are served via the API, the original, raw metadata became hidden from API user. Moreover, the APIs uses non-standard contract interface to retrieve name, symbol, and contract metadata on ERC-721 and ERC-1155 contracts.

Analysis

Nonetheless, reliance on these APIs are not unreasonable. On Ethereum, the gas cost is high. Many solutions already exist for tracking tokens off-chain, though they maintained by relatively few centralized operators. In our view, wallets on Ethereum rely on those centralized API servers for two primary reasons:

  • Most wallets are not smart contract wallets. They are client-side based so they have to use those APIs.
  • Smart contract wallets need to stay competitive. The gas cost of smart contract wallet was already a key issue on Ethereum. Incurring extra cost on receiving / sending tokens for the sole purpose of tracking tokens would seem unnecessary, and may lead to a loss on the user base if competitors's wallets are cheaper to operate.

ONE Wallet's Approach

Here, ONE Wallet faces a very different set of settings. Since ONE Wallet is running on Harmony network exclusively at this time, it does not have similar constraints on gas cost. The gas cost for tracking tokens is nearly negligible and will very likely stay so, given the high efficiency of the blockchain. On the other hand, Harmony network does not have established central operators offering APIs for tracking tokens. Existing solutions, such as Harmony Block Explorer, does poorly on tracking arbitrary tokens and does not seem to be tracking EIP-721 and 1155 tokens correctly (example).

Even if there were reliable APIs offered by central operators, tracking tokens on-chain would still provide far superior user experience. Based on our implementation (see above), tracking immediate occur when the user receive a EIP-721 or 1155 token, or when the user sends a EIP-20 token. The user is not required to wait or take any action for this to occur. The tracking implementation only uses very few bytes per token. For each token, it stores the token id (32 bytes), token contract address (20 bytes), and token type (1 byte), and computes a 32-byte key based on these three components.

The benefits are clear:

  • when the user restore their wallets at a different client (or device), the client automatically restore the list of tokens being tracked by obtaining a copy from the smart contract. In comparison, users of other wallets would have to add each token back one-by-one.
  • Most importantly, when recovery is triggered, ONE Wallet can send all tokens owned by the user to the recovery address, not just ONEs.

It is also important to note that the tracking does not need to be robust or 100% correct. The tracking mechanism only tracks which tokens are of interest to the wallet user. It does not track the balance. The tracking is guaranteed not to miss a token (except EIP-20 tokens which the wallet only received but never sends out). They client should retrieve the balance of each token by querying the blockchain. The client can also add or subtract any token from the tracking list as the client sees fit. The smart contract also provides interfaces for the user to add, subtract, override the tracking list if they wish to.

Clone this wiki locally