How to build DeFi on IC?

blockpunk
ICP League
Published in
6 min readDec 7, 2021

DEFI originated from Ethereum

Most of the DEFI applications that we know presently are based on EVM virtual machines. Therefore, most of the DEFI-related contents and technical materials that are popular are related to EVM and Solidity. The first step in building DEFI applications on IC is to understand the difference between EVMs and IC. This document will introduce the differences between IC and EVM from five aspects:

  • Differences between EVM and IC structures
  • Performance differences
  • Storage
  • Reverse Gas Model
  • Development Languages

Structure

As we all know, Transactions exists in the blockchain structure of Ethereum, which is a classic structure in blockchain industry; however, IC adopted completely different technology, there is no main chain data structure in IC.

For example, the main token on IC, ICP, runs in a Canister called Ledger, which is a very important Canister on IC (i.e., the smart contract on IC), and all its functions and data are implemented inside this Canister.

To build DEFI applications on IC, it is critical to understand the mechanism of Ledger Canister, how its original token, ICP, makes transfers, and how transactions are confirmed.

In addition, Ethereum and IC are similar in structure as they both use “account structure”. The difference is that IC’s account structure consists of two types of accounts, “Principal ID” and “Account ID”, both of which are user accounts. The “Account ID” is equivalent to the sub-account of “Principal ID”, it is used as the user’s identity to record information about the number of ICPs in the user’s account.

Performance

Ethereum confirms a transaction in a block between 12–15 seconds. The throughput of Ethereum is measured to be between 12 and 15 batches of transactions per second. Both of above make it difficult to apply such performance requirements for applications with high immediacy requirements.

In contrast, there are two types of transactions on IC, of which the query type can reach about 200 ms and the transaction type can reach about 2s. Such performance is relatively close to the performance of the Internet, which provides the basis for building most types of Internet experience applications on IC.

Based on these performance advantages, IC will be the top 1 option for DEFI applications below to implement building activities:

  1. DEX applications
  2. Experience enhanced version of GameFI
  3. NFT

Storage

Ethereum is the first blockchain to have smart contracts. But its storage, which is also primarily stored on the main chain, is over 9T of data at this point. As described in the previous Structure section, all of ETH’s smart contract data and transaction data together make up a total volume of 9T on the main chain, which is very large.

In contrast, there is no main chain data on IC. Each Canister (smart contract) is stored independently, and the storage limit for a single Canister is currently 8G.

When you see this number, you may have a question: will the upper limit of IC storage be a limitation for smart contract development, while there is no need to consider the upper limit of data storage when developing an Ethereum contract?

First of all, every storage has an upper limit. The so-called no data storage cap of Ethereum is because the storage of each contract is created through transactions, and the main chain will store the data of transactions. Therefore, transaction data will not be capped in the main chain, there will only be increasing blocks and blocks of transaction data. But the aggregation of all smart contracts will create a huge amount of data, which is currently seen as a total volume of 9T on the main chain.

The EVM mechanism has limited capability in storage and can only store structured data, and the size of a single byte data cannot exceed 32 bytes. But IC is capable of multiple types of data storage, similar like Blob. Therefore, applications like Tiktok, which requires video storage, can run on IC. Such applications cannot be built on EVM, but it is perfectly feasible on IC.

The most discussed solution for scaling in the Ethereum community is expanding via sharding. IC do not have similar problems in this area, and while the storage limit for a single Canister is 8G, there are different solutions for expanding storage on IC. For example, to achieve the effect of scaling, the storage can be classified according to business type and index structure technology. In addition, business data can be allocated into multiple Canisters by hash sharding, range sharding technology, by which way to reach unlimit scaling target. At present, there are more than 10,000 Canisters on the IC, and in the long run, its capacity of expansion will be much higher than that of the Ethereum.

For example, an IPFS evangelist mentioned earlier that NFT that is not stored on IPFS is not a NFT of your own, which graphically expressed that the storage content of NFT built on EVM is stripped to other infrastructure settings, such as IPFS. So it is a big extra burden for developers to link two different public chains for one application. Therefore, the development of applications, such as NFT and GameFi, will be perfectly supported by IC.

Reverse Gas Model

While using any Dapp on EVM, any of your interaction requires you to pay ETH as a Gas fee. A problem often encountered is the explosion of Gas fees brought about by the soaring price of ETH, which has greatly increased the barrier to entry for Ethereum.

First, we start by introducing the two original tokens on IC, ICP and Cycles.

ICP is the primary token on IC and is the primary token for governance, voting, etc. on IC. But IC also has another primary token, Cycles, which is the Gas token on IC.

So what is Reverse Gas Model? Firstly, let’s introduce how Cycles are obtained. The only way to get Cycles is to burn ICPs to get Cycles, and this burning is one-way, i.e. Cycles cannot be converted to ICPs.

A key factor in this process is what is the price at which ICPs can be exchanged for Cycles? The price is floating and is determined by the price of ICP. Cycles have a stable value and can be considered a stable currency, and the ICP price divided by the stable value of Cycles is the number of Cycles that can be exchanged for ICP. And the overhead of any Canister call is Cycles-priced. On IC, people will no longer encounter problems like the Gas fee speedly increased when ETH price pump high.

Users on Ethereum need to buy ETH to enter Dapp before they can interact with the application, while the payer of Cycles on IC is the publisher of smart contracts, so users do not need to hold ICP to be able to use the application, which greatly reduces the threshold of entry for users.

Development Language

Developing smart contracts with Solidity is similar to developing smart contracts with JavaScript, both of which have ordinary expressiveness, limited data types, and poor support for complex data structures. A problem that developers often encounter is Stack too deep, depending on the complexity of the expression inside the function. A function with more than 16 local variables will not work, so developers need to concern a lot to control the number of variables.

Theoretically, any language that can be compiled into WASM can be written on IC, and so far, the most popular languages on IC are Rust and Motoko. Both have better expressiveness and better data type support.

  • Motoko is quite simplified, but its category library is not adequate.
  • Rust is difficult to get started with, but with diverse category library.

If your team has experience with Rust, you can choose Rust. If you are new to Rust, Motoko is your best start point to kickoff.

--

--

blockpunk
ICP League

Co-founder of ICP League & Ourea Group, obsessed with Social Tokens, DAO & NFT.