You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since handling prices is something almost every smart contract developer must do, why not have a small helper library to make working with them easier?
📝 Details
There are many scenarios when comparing prices becomes necessary, such as when swapping tokens or ensuring Oracle prices from different providers are within some acceptable range. Math libraries already make all of this possible by, for example, being able to accurately multiply and divide, but
This requires the developer to implement the subsequent calculations on their own, which can introduce potential vulnerabilities.
It may be more desirable to directly view a more meaningful value, for example, the relative change between two integers.
Here is an example of a library that includes 5 functions that could be useful when working with prices:
pragma solidity0.8.26;
libraryPriceMath {
/**@notice Returns the absolute change between two unsigned integers */function absDiff(uint256a, uint256b) internalpurereturns (uint256) {
return a > b ? a - b : b - a;
}
/**@notice Returns the relative change between two unsigned integers * @dev Maximum change = 99.99% (9999 return value) Minimum change(excluding no change) = .01% (1 return value) * @param a The base Value * @param b The new value * @return The percentage change from a to b, scaled by 1e4 (basis points) */function relChange(uint256a, uint256b) internalpurereturns (uint256) {
uint diff =absDiff(a, b);
return (diff *1e4) / a;
}
/** * @notice Returns the signed relative change between two unsigned integers * @dev This function returns how much b has changed relative to the value of a * @dev A positive result indicates an increase, while a negative result indicates a decrease * @dev Maximum positive change = 99.99% (9999 return value) Maximum negative change = -99.99% (-9999 return value) * @param a The base value * @param b The new value * @return The percentage change from a to b, scaled by 1e4 (basis points) */function signedRelChange(
uint256a,
uint256b
) internalpurereturns (int256) {
if (a ==0) {
return b >0?int256(type(int256).max) : int256(0);
}
int256 diff =int256(b) -int256(a);
return (diff *1e4) /int256(a);
}
/** * @notice Increases an amount by a given percentage * @dev The percentage is expressed as basis points (1/100th of a percent) * @param a The base amount * @param p The percentage to increase by, in basis points (1% = 100, 100% = 10000) * @return The amount increased by the specified percentage */function addPerc(uint256a, uint256p) internalpurereturns (uint256) {
if (p ==0) return a;
return a + ((a * p) /10000);
}
/** * @notice Decreases an amount by a given percentage * @dev The percentage is expressed as basis points (1/100th of a percent) * @dev Can revert/overflow if value of `a * p` exceeds `type(uint256).max` * @param a The base amount * @param p The percentage to decrease by, in basis points (1% = 100, 100% = 10000) * @return The amount decreased by the specified percentage */function subPerc(uint256a, uint256p) internalpurereturns (uint256) {
if (p ==0) return a;
return a - ((a * p) /10000);
}
}
The text was updated successfully, but these errors were encountered:
🧐 Motivation
Since handling prices is something almost every smart contract developer must do, why not have a small helper library to make working with them easier?
📝 Details
There are many scenarios when comparing prices becomes necessary, such as when swapping tokens or ensuring Oracle prices from different providers are within some acceptable range. Math libraries already make all of this possible by, for example, being able to accurately multiply and divide, but
Here is an example of a library that includes 5 functions that could be useful when working with prices:
The text was updated successfully, but these errors were encountered: