Impermanent Loss in DeFi

Impermanent Loss (IL) is a concept in decentralized finance (DeFi) that occurs when providing liquidity to automated market maker (AMM) pools. It refers to the temporary loss of funds experienced by liquidity providers (LPs) due to price volatility of the assets in the pool. This loss is “impermanent” because it only materializes if the LP withdraws their funds when the asset prices have changed. If the prices return to their original state, the loss disappears.

 How Impermanent Loss Occurs

In an AMM pool, liquidity providers deposit pairs of tokens (e.g., ETH and USDT) into a pool. The pool uses a constant product formula (e.g.  x  x y = k  to determine the price of the assets. When the price of one asset changes relative to the other, arbitrageurs trade in the pool to restore equilibrium, which shifts the ratio of the two assets in the pool. This shift causes LPs to end up with a different value of assets than if they had simply held the tokens.

 Example of Impermanent Loss

Let’s assume a liquidity pool with two assets: ETH and USDT. The pool follows the constant product formula  x x y = k , where:

 x = amount of ETH in the pool

 y = amount of USDT in the pool

k = constant product

 Initial Conditions

– Initial price of ETH: $1,000

– You deposit 1 ETH and 1,000 USDT into the pool.

– Total value of your deposit: $2,000 (1 ETH × $1,000 + 1,000 USDT × $1).

– The pool has:

  – 10 ETH

  – 10,000 USDT

  – Constant product   k = 10 x10,000 = 100,000 .

Your share of the pool: 10% (you deposited 1 ETH and 1,000 USDT out of 10 ETH and 10,000 USDT).

 Scenario: Price of ETH Increases to $2,000

1. Arbitrageurs Trade in the Pool:

   – When the external price of ETH rises to $2,000, arbitrageurs buy ETH from the pool until the pool price matches the external price.

   – The new ratio of ETH to USDT in the pool will adjust to reflect the new price.

2. New Pool Balances:

   – Let the new amount of ETH in the pool be  x’  and USDT be  y’ .

   – The constant product formula x’ x y’ = 100,000 must hold.

   – The new price of ETH in the pool is  y’/x’ =2,000 (since 1 ETH = 2,000 USDT).

   Solving the equations:

   x’ x y’ = 100,000 

  y’/x’ =2,000 implies y’ = 2,000x’

   Substituting  y’ = 2,000x’  into the constant product formula:

      x’ x(2,000x’)= 100,000

   2,000x’^2 = 100,000 

  x’^2= 50 

   x’ = √50 ≈ 7.071 ETH

   y’ = 2,000 x 7.071 USDT

3. Your Share of the Pool:

   – Your share is 10% of the new pool balances.

   – You now have:

     – ETH:  0.10 x 7.071 0.7071 ETH} 

     – USDT:  0.10 x14,142 1,414.2 USDT

   – Total value of your share:

     0.7071 ETH x2,000 + 1,414.2 USDT = 1,414.2 + 1,414.2 = 2,828.4USDT

4. Value if You Had Held the Tokens:

   – If you had simply held your 1 ETH and 1,000 USDT, the value would be:

     1 ETH x2,000 + 1,000 USDT = 2,000 + 1,000 = 3,000 USDT   

5. Impermanent Loss Calculation:

  Impermanent Loss} =value in pool-value if heldvalue if held 

   = 2,828.4-3,000

   = -171.6

  ≈-5.72%

 Key Takeaways

– Impermanent loss occurs when the price of the assets in the pool changes.

– The greater the price change, the higher the impermanent loss.

– LPs are compensated for this risk through trading fees, but they must weigh the fees against potential losses.

– If the price returns to its original state, the loss disappears.

 Formula for Impermanent Loss

The impermanent loss can also be calculated using the following formula:

Impermanent Loss(IL) = [(2x√price ratio)/(1+price ratio )]-1

Where:

 Price Ratio = New Price/ Original Price

In the example above:

 Price Ratio = 2,000/1,000  = 2

Impermanent Loss}= [(2x√2)/(1+2) ]- 1 

= (2×1.4142)/3 – 1 

= 2.8284/3 – 1 

= 0.9428 – 1 

= -0.0572  or , -5.72%

This matches the earlier calculation.

ICP Developer Guide -Chapter 2

The Building Block of ICP-Canister

Canister is a fundamental computational unit that combines both code and state. It is essentially a smart contract or a container that runs on the Internet Computer blockchain. Canisters are designed to be autonomous, scalable, and interoperable, enabling developers to build decentralized applications (dApps) and services.

Key Features of a Canister:

Code and State:

  1. Autonomous:
    • Canisters operate independently and can interact with other canisters or external systems via messages.
  2. Scalable:
    • The Internet Computer allows canisters to scale horizontally by distributing their workload across multiple nodes in the network.
  3. Interoperable:
    • Canisters can communicate with each other through message passing, enabling complex decentralized applications to be built by composing multiple canisters.
  4. Upgradable:
    • Developers can update the code of a canister without losing its state, making it easier to maintain and improve applications over time.

Cycle

n the Internet Computer (ICP), a Cycle is a computational unit used to pay for the execution of smart contracts (called canisters). It functions similarly to “gas” in Ethereum but is designed to be more predictable and cost-efficient.

Key Features of Cycles in ICP:

  1. Resource-Based Pricing – The cost of computation, storage, and network usage is measured in cycles.
  2. Stable Pricing Model – Unlike Ethereum’s gas, the cost of cycles is tied to real-world resources (compute and storage) rather than being market-driven.
  3. Conversion from ICP Tokens – ICP tokens can be converted into cycles to fund canister execution. The conversion rate is adjusted to maintain price stability. XDR (Special Drawing Rights) is used as a reference unit to determine the cost of converting ICP tokens to cycles in a stable manner.(1T Cycle = 1 XDR ~ USD1.321)
  4. Canister Management – Cycles are stored within canisters and consumed as operations are performed. When a canister runs out of cycles, it stops executing until refueled.

Cycle Usage in ICP:

  • Computation – Each instruction executed by a canister consumes cycles.
  • Storage – Data stored in the canister costs cycles over time.
  • Inter-canister Calls – Messaging between canisters also consumes cycles.
  • Network Operations – Data transmission to and from the Internet Computer incurs cycle costs.

Creation of a Cycle Wallet in Local Network

To create a cycle wallet in the local network, you start the local network in a clean mode using the following command:

dfx start –background –clean

Running the following command first time to create a cycle wallet automatically with 100T cycles in local network, otherwise it will return the cycle wallet id only.

dfx identity get-wallet

The following command will return the balance in the wallet:

dfx wallet balance

Motoko-The native programming language of ICP

Motoko is a programming language specifically designed for the Internet Computer (ICP) blockchain. It is optimized for writing smart contracts (called canisters) that run directly on the ICP network.

Key Features of Motoko

  1. Actor-Based Model – Uses the actor model to handle concurrency and asynchronous execution efficiently.
  2. Type-Safe & Memory-Safe – Strongly typed language that prevents common programming errors.
  3. Designed for Web3 – Integrates directly with the Internet Computer, supporting scalability, persistence, and seamless upgrades.
  4. Automatic Garbage Collection – Handles memory management internally.
  5. WebAssembly Compilation – Runs as WebAssembly (Wasm) for efficient execution on the ICP blockchain.
  6. Interoperability – Can interact with Rust, JavaScript, and other WebAssembly languages.

Create and Deploy Canisters

Creating a canister on the Internet Computer (ICP) involves a few simple steps. Below is a basic guide:

Start local network in background

dfx start –background

Create hello world project

dfx new hello

Select a backend language, you may choose Motoko, Rust, Python or Typescript, as shown below. We choose Motoko for our illustration.

Then select a Frontend language as follows. We choose React for illustration.

Then add extra features as follows, we choose Intenet Identity:

Pressing enter to confirm will create all necessary files and install dependencies, and arrive at the start up screen as shown below. A project with the folder name hello will be created.

The project architecture is as shown below:

Deploying Canister on Local Network

To deploy the hello project you have just created on a local network, use the following command:

dfx deploy

The following screen shows successful deployment, otherwise there will be errors:

You may access the frontend URL via the link generated.

The frontend UI is as illustrated below:

Deploying Canister on the IC Mainnet

The command to deploy the canister on the IC mainnet is

dfx deploy –network ic

If the deployment on the IC blockchain is successful, the output will display the URLs of both the frontend and backend, as shown below:

The frontend URL can be accessed on the desktop browser and the browser of the mobile devices. It does not need to register a domain nor a central server to host the web app, it is fully on chain.

In this example, accessing the frontend URL with the link will display a certificate generator app, as shown below:

Obtaining Cycles

You need cycles to deploy your ICP app on the mainnet. There are two ways to obtain the cycles, one way is to redeem the coupon codes if you are given some free coupons, the other way is to buy icp tokens and convert them to cycles.

Method 1: Redeeming cycles from coupon codes

The command to redeem the cycles with coupon code is as follows:

dfx cycles redeem-faucet-coupon –network ic <COUPON_CODE>

Example: dfx cycles redeem-faucet-coupon –network ic CE3A9-BA578-CD44B

To check cycles balance in the wallet, use the following command:

dfx cycles –network ic balance

Method 2: Converting ICP tokens to cycles

First of all you must create an empty canister using ICP token using the following command:

dfx ledger –ic create-canister <principal-identifier> –amount <ICP tokens>

You may use the following command to obtain the principal identifier:

dfx identity get-principal

Example to get the principal identifier:

Then use the following command to convert ICP tokens to cycles:

dfx ledger –ic create-canister zxyfn-yljyi-bn6dy-ixi7n-jez74-nk723-pvj3m-jykes-dhqon-3ktql-uae –amount 0.3

To check canister status on the mainnet, use the following command:

dfx canister –network ic status <canister_id>

The status is as shown below:

*You can obtain canister id on the ic mainnet using the command: dfx canister id <canister name>–network ic

Appendix: List of dfx commands

List all accounts in the device

dfx identity list

Show current identity

dfx identity whoami

Use a particular identity

dfx identity use <Identity Name>

Get ICP Tokens balance

dfx ledger balance

Top up cycles into cycle wallet or canister by converting ICP Tokens

dfx ledger –network ic top-up <wallet id or canister id> –amount <icp tokens>

Example: Add 4T cycles to your backend canister

dfx ledger top-up –network ic <canister_backend >–amount 4.0

Top up cycles directly

dfx canister –network ic deposit-cycles amount <canister_backend>

Example: Deposit 4T cycles to your backend canister

dfx canister –network ic deposit-cycles 4000000000000 weather_app_backend

To remove your identity in the device

dfx identity remove <identity name>

To get canister basic info

dfx canister –network ic info

To stop local network

dfx stop

To list the controllers of the canister:

dfx canister info ic_minter_backend

ICP Developer Guide -Chapter 1

In the previous post, you learned about the fundamental concepts of the Internet Computer Protocol (ICP), a third-generation blockchain designed to power the next evolution of the internet. At its core, ICP functions as a decentralized cloud, enabling developers to build and deploy applications entirely on-chain without relying on traditional centralized servers like AWS or Google Cloud.

You might be wondering—how is this even possible? To clear up any doubts, I will walk you through the process of creating and deploying an application on the Internet Computer. Unlike conventional web hosting, ICP allows you to launch apps without registering a domain name or provisioning a cloud server, leveraging blockchain-native web hosting for a truly decentralized experience.

Prerequisite

To start coding in IC(Internet Computer) , there are some prerequisites you need to set up or install before you can jump into developing your first app. Following are the prerequisites:

  • Ensure you have the supporting operating system-
    • Windows 10 or 11 with WSL2 installed with Ubuntu Linux v20.04
    • Mac OSX 12 or above
    • Ubuntu Linux v20.04
  • NodeJs v20
  • GitHub Account
  • IC SDK
  • Visual Studio Code IDE
  • Basic programming knowledge- JavaScript, CSS, HTML

Here are the references to install the or set up the prerequisites:

You must install IC SDK before you can start coding. Use the following command in the WSL ubuntu terminal to install the SDK:

·sh -ci “$(curl -fsSL https://internetcomputer.org/install.sh)”

After installation, check its version using the command dfx –version, you should see something like dfx 0.24.3

*·If you are using a machine running Apple silicon, you will need to have Rosetta installed. You can install Rosetta by running softwareupdate –install-rosetta in your terminal.

The next step is to create an account in IC. In ICP, authentication requires a key pair consisting of a private and a public key, while the account itself is identified by a unique principal ID. Additionally, a ledger is needed to store accounts and transactions. This ledger is a smart contract known as a system canister. Each user will have a ledger account identifier, also called an account ID, which is used to hold ICP tokens. Furthermore, a wallet must be created to store cycles and facilitate sending cycles to and from canisters.

Creating ICP Account

To create an account in IC, using the following command:

dfx identity new <identity_name>

·💡Identity names must use alphanumeric characters comprising uppercase and lower letters, numbers and special characters. Example: My_chatb@t

·ℹ️Most importantly, REMEMBER to back up the 24-word account/identity seed phrase. This is essential for restoring your account if you forget your password or need to access it from another device. Additionally, you can create multiple accounts on your device.

Principal ID

Having created your account, you can obtain your principal id using the following commands:

dfx identity use <identity_name>

dfx identity get-principal

Your account’s principal ID will resemble this:

zxyfn-yljyi-bn6dy-ixi7n-jez74-nk723-pvj3m-jykes-dhqon-3ktql-uae

Ledger Account ID

You can also obtain your ledger account id using the following commands:

dfx identity use <identity_name>

dfx ledger account-id

Your Ledger account ID will resemble this :

1370f0ea74b35f33d2a2fee64a7a8c53cd52d6dd82c1cdfe08712dcd863692ab

Importing Account

In case you have changed your device and need to use the same account to develop ICP apps, you may import the 24-word seed phrase you have saved as a plaintext into your new development environment using the following command:

dfx identity import –seed-file <seedfile.txt> <Identity Name>

ICP Token Balance

To check the ICP token balance in ledger account on ICP Main Network, use the following commend:

dfx ledger –network ic balance

·💡–network ic or –ic: Connect to ICP Main Network, without this parameter, it will connect to the local network

Internet Identity

Internet Identity is a decentralized authentication system for the ICP. If you haven’t already, set up an Internet Identity:

  • Go to the Internet Identity portal: https://identity.ic0.app/.
  • Click “Create New” to create a new identity.
  • Follow the prompts to register your device . For Windows 10 user, require to use your mobile phone to scan the QR Code to store the credential information in the mobile phone. For Android device, recommend to use Google Lens to perform Passkey QR code scanning.
  • Note down your Internet Identity number (e.g., 12345).

 ICP Account Address

To receive ICP tokens, you need an ICP account address associated with your Internet Identity. Here’s how to get it:

  • Go to the Network Nervous System (NNS) Dapphttps://nns.ic0.app/.
  • Authenticate using your Internet Identity.
  • Once logged in, navigate to the “Accounts” section.

Plug Wallet

You may also use the Plug Wallet to store your ICP tokens. Plug wallet can be installed as a browser extension on a laptop or can be installed as a mobile app on your phone. You can download Plug Wallet using the link below.

https://plugwallet.ooo/

Network Nervous System

The Network Nervous System (NNS) is the decentralized governance system aka DAO that controls and manages the Internet Computer (ICP), a blockchain-based computing platform developed by the DFINITY Foundation. The NNS is one of the most critical components of the Internet Computer, as it enables the network to operate autonomously and evolve over time through community participation.


Key Functions of the NNS

  1. Governance:
    • The NNS allows ICP token holders to participate in the governance of the Internet Computer by submitting and voting on proposals.
    • Proposals can cover a wide range of topics, such as upgrading the protocol, adjusting network parameters, or funding ecosystem projects.
  2. Token Economics:
    • The NNS manages the ICP utility token, including its minting, burning, and distribution.
    • It also handles the creation of cycles, which are used to pay for computation and storage on the Internet Computer.
  3. Node Management:
    • The NNS oversees the addition, removal, and configuration of node machines that power the Internet Computer.
    • It ensures the network remains secure, scalable, and efficient.
  4. Canister Management:
    • The NNS manages the lifecycle of canisters (smart contracts) on the Internet Computer, including their creation, upgrading, and deletion.
  5. Network Upgrades:
    • The NNS facilitates seamless upgrades to the Internet Computer protocol without requiring hard forks or downtime.
    • This is achieved through a decentralized voting process.