Trying to make a ecosystem where you have two tokens (Token X, Token Y) Token X can be minted for $1 worth of Token Y burned. And Token X can be redeemed for $1 worth of Token Y. How would I do this in solidity without using a data feed like chainlink? I'm a horrible coder so advice would be appreciated lol.
2 Answers
If you plan to take the token outside your solidity project's world, there is no way of getting the price of the token without using an oracle service.
-
-
I am not sure whether that can be very successful. Go through the failures of all algorithmic stable coins in existence to understand what not to do. I am no expert to comment further on it. Just know that oracle would give you the accurate value that you need when dealing with financial stuff.– AoSCommented Jul 26, 2022 at 5:05
How to Check Token Price in Solidity Without Using a Direct Price Feed In decentralized finance (DeFi), obtaining token prices is often essential for trading, staking, or providing liquidity. While price feeds like Chainlink are the most common approach, there are scenarios where you may need to calculate the token price directly from an on-chain liquidity pool. This article explores how to calculate a token's price in Solidity without relying on direct price feeds.
Key Idea: Using Liquidity Pool Reserves Liquidity pools in automated market makers (AMMs) like Uniswap or PancakeSwap maintain reserves of two tokens (e.g., Token A and Token B). The ratio of these reserves determines the relative price of the tokens. By using the balance of the token and ETH (or any base token), we can deduce the token's price.
Steps to Calculate Token Price: Get Reserves from the Liquidity Pool Contract
Query the liquidity pool to get the balance of the token and ETH. Use the getReserves() function if the pool follows the Uniswap V2 standard. Fetch ETH Price Using a Reliable Oracle
Use an ETH price feed (e.g., Chainlink) to get the current price of ETH in USD. Calculate Token Price in USD
Combine the liquidity pool data with the ETH price to calculate the token's price in USD.
Example Solidity Implementation Here’s a step-by-step example of calculating the token price:
Get Liquidity Pool Reserves Liquidity pool contracts (like Uniswap V2) provide a getReserves() function that returns the reserves of both tokens in the pool:
interface IUniswapV2Pair { function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast); function token0() external view returns (address); function token1() external view returns (address); }
Fetch ETH Price in USD To get the ETH price in USD, use a price feed from Chainlink or another oracle. For example:
interface IPriceFeed { function latestAnswer() external view returns (int256); }
Token Price Calculation Logic With the above components, we can calculate the token price using the following logic:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0;
interface IUniswapV2Pair { function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast); function token0() external view returns (address); function token1() external view returns (address); }
interface IPriceFeed { function latestAnswer() external view returns (int256); }
contract TokenPriceCalculator { IUniswapV2Pair public liquidityPool; IPriceFeed public ethPriceFeed; address public token;
constructor(address _liquidityPool, address _ethPriceFeed, address _token) {
liquidityPool = IUniswapV2Pair(_liquidityPool);
ethPriceFeed = IPriceFeed(_ethPriceFeed);
token = _token;
}
function getTokenPriceInUSD() external view returns (uint256) {
(uint112 reserve0, uint112 reserve1,) = liquidityPool.getReserves();
address token0 = liquidityPool.token0();
address token1 = liquidityPool.token1();
// Determine the token and ETH reserves
uint112 tokenReserve;
uint112 ethReserve;
if (token0 == token) {
tokenReserve = reserve0;
ethReserve = reserve1;
} else if (token1 == token) {
tokenReserve = reserve1;
ethReserve = reserve0;
} else {
revert("Token not found in the liquidity pool");
}
// Fetch the ETH price in USD from the price feed
int256 ethPriceInUSD = ethPriceFeed.latestAnswer();
require(ethPriceInUSD > 0, "Invalid ETH price");
// Calculate the token price in USD
uint256 tokenPriceInUSD = (uint256(ethPriceInUSD) * ethReserve) / tokenReserve;
return tokenPriceInUSD;
}
}
Considerations Exchange Rate Volatility:
Token prices can fluctuate rapidly in liquidity pools due to low liquidity or high trading activity. Accuracy Depends on ETH Price Feed:
The accuracy of the calculated token price is tied to the reliability of the ETH price oracle. Impermanent Loss:
In smaller liquidity pools, the price may be skewed due to impermanent loss or concentrated liquidity positions.
Conclusion By leveraging liquidity pool reserves and an ETH price feed, you can calculate a token’s price in USD without needing a direct price oracle for the token itself. This approach is highly effective for many DeFi use cases, especially when a reliable price feed for the token is unavailable. With this method, you gain a real-time, on-chain solution for token price determination.
-
1