Yos Riady software craftsman

Smart Contract Extensibility with Wrapped Tokens

Smart Contract Extensibility with Wrapped Tokens

Smart contracts’ open source and immutable nature enables permissionless innovation in the space. People and companies can innovate on top of protocols without worrying about the rules of the game changing later on. Any Developer can innovate and build new dApps freely in a permissionless manner, without needing anybody’s approval.

When a dApp (decentralized app) is built off of the Ethereum Blockchain it usually implements its own ERC20 Token. Think Augur’s REP Token, or Bancor’s BNT Token. ERC20 is a token standard developed after the release of ETH that defines how tokens are transferred and how to keep a consistent record of those transfers among tokens in the Ethereum Network.

Decentralized services can be composed together to harness their particular strengths. The question is: How can developers extend the capability of an existing ERC20 token contract? In this article, let’s examine a design pattern around smart contract extensibility: Wrapped Tokens.

Wrapped Tokens

Wrapped Tokens is a design pattern where you ‘wrap’ or ‘transform’ an existing crypto asset or Token (ETH, ERC20, BTC) into a new Wrapped Token with additional functionality.

The translation between Token and Wrapped Token is often reversible; users can transform between the two versions at any time. The wrapping process usually involves users locking their Tokens in a smart contract, which then mints an equivalent amount of Wrapped Tokens for the user. Users can trade their Wrapped Tokens to the smart contract to receive their original Tokens back.

Now, let’s look at a few examples of this pattern in the wild.

Wrapping Ether: WETH

Because decentralized platforms running on Ethereum use smart contracts to facilitate trades directly between users, every user needs to have the same standardized format for every token they trade. This format is ERC-20.

Ether or ETH is the native currency built on the Ethereum blockchain. ETH doesn’t conform to the ERC-20 standard. It was built before the ERC20 standard existed.

WETH is wrapped Ether that supports the ERC20 interface. With WETH, you are able to trade ETH for other ERC20 tokens on exchanges and DApps.

When you “wrap” ETH, you’re trading it a smart contract for an equal token called WETH. If you want to get plain ETH back you need to “unwrap” it by trading it back for plain ETH. ETH and WETH are always exchanged at a 1:1 ratio. The Ether collateral is securely locked within the smart contract until it’s traded for WETH at some point in the future.

You can try wrapping ETH on the 0x portal or the OpenSea NFT marketplace. Note that Wrapping and unwrapping are transactions that cost gas.

Here’s the interface of a WETH smart contract:

contract IWETH is ERC20 {
  event Deposit(address indexed sender, uint256 amount);
  event Withdrawal(address indexed recipient, uint256 amount);

  function deposit() public payable;

  function withdraw(uint256 amount) public;

  function withdraw(uint256 amount, address user) public;
}

Here’s a sample WETH implementation:

contract WETH is IWETH {
  string public name = "Wrapped Ether";
  string public symbol = "WETH";
  uint8  public decimals = 18;

  function deposit() public payable {
    _mint(msg.sender, msg.value);
    emit Deposit(msg.sender, msg.value);
  }

  function withdraw(uint amount) public {
    require(balanceOf(msg.sender) >= amount);
    address payable recipient = msg.sender;
    _burn(msg.sender, amount);
    recipient.transfer(amount);
    emit Withdrawal(recipient, amount);
  }

  function withdraw(uint amount, address payable recipient) public {
    require(balanceOf(msg.sender) >= amount);
    recipient.transfer(amount);
    _burn(msg.sender, amount);
    emit Withdrawal(recipient, amount);
  }
}

Note the following:

  • The WETH contract inherits from ERC20, which gives it ERC20 functions such as balanceOf, transfer, and transferFrom.
  • The IWETH interface defines the functions deposit and withdraw to wrap and unwrap ETH respectively.
  • In the deposit function, any Ether sent via payable _mints an equivalent msg.value amount of WETH.
  • In the withdraw function, any WETH burned via _burn returns an equivalent amount to the recipient.

In summary, wrapping Ether enables it to be traded just like any ERC20 token and access the rest of the decentralized exchange and finance ecosystem.

Cryptoassets that can be wrapped are not limited to Ether. There’s also Wrapped Bitcoin (WBTC) which standardizes Bitcoin to the ERC20 format. In the future, expect to see other cryptoassets native to other blockchains on Ethereum as a Wrapped Token.

There’s also Veil Ether which is a WETH-like with the extra ability to deposit ETH and approve in a single transaction.

Wrapping ERC20 tokens

The Wrapped Tokens pattern can be applied to add custom functionality to an existing ERC20 token. Let’s look at some examples of extending existing ERC20 tokens.

Horizon Games’ ERC20 Meta Wrapper

Horizon Games’ ERC20 Meta Wrapper adds meta-transaction methods to any token compliant with the ERC-20 standard.

When you deposit ERC-20 tokens (e.g. DAI) in the wrapper contract, it will give you back metaTokens (e.g. MetaDAI) with a 1:1 ratio. These metaToken have native meta-transaction functionalities, which allow you to transfer tokens without doing an on-chain transaction yourself, but by simply signing a message and broadcasting this message to “executors”. You can also “approve” addresses to transfer tokens on your behalf with a signed message instead of calling the ERC-20 approve() function.

Normally, transffering ERC20 tokens cost gas paid in Ether. With metaTokens, users can pay directly in any ERC20 token they wish. Hence, at a high level, users could transfer DAI by paying the transaction fee in DAI as well, never needing to possess ETH.

Compound cTokens

Compound is an open-source protocol for algorithmic, efficient Money Markets on the Ethereum blockchain. It lets users supply assets to the protocol and earn interest.

Every asset supported by the Compound protocol is integrated through a cToken smart contract, which is an ERC20 compliant token holding representation of balances supplied to the protocol. This Wrapped Token are the primary way of interacting with the Compound Protocol.

Each ERC20 token available on Compound has a correspondong CErc20 token. For example, BAT has a corresponding cBAT and ZRX has a corresponding cZRX. Each cToken contract creates its own money market. When a user mints, redeems, borrows, repays a borrow, liquidates a borrow, or transfers cTokens, she will do so using the cToken contract.

Bonding Curves

Bonding curves utilizes a similar ‘wrapping’ technique to mint and burn continuous tokens. It uses a curve to mint different amounts based on the the curve-bonded token’s supply.

Summary

Smart contracts’ open source and immutable nature enables permissionless innovation. With the Wrapped Tokens design pattern, developers can extend the capability of an existing cryptoasset and make it interoperable with other decentralized applications.

Author

Yos is a software craftsman based in Singapore. Learn more.

📬 Subscribe to my newsletter

Get notified of my latest articles by providing your email below.




Here's something you might be interested in...

Going Serverless book

Have you heard about the Serverless programming model? The Going Serverless book teaches you how to build scalable applications with the Serverless framework and AWS Lambda. You'll learn how to design, develop, test, deploy, and secure Serverless applications from planning to production.

Find out more →