convex.core.util.Economics Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of convex-core Show documentation
Show all versions of convex-core Show documentation
Convex core libraries and common utilities
The newest version!
package convex.core.util;
import convex.core.Constants;
/**
* Utility function for Convex Cryptoeconomics
*/
public class Economics {
/**
* Computes the marginal exchange rate between assets A and B with pool quantities,
* such that a constant liquidity pool c = a * b is maintained.
*
* @param a Quantity of Asset A
* @param b Quantity of Asset B
* @return Price of A in terms of B
*/
public static double swapRate(long a, long b) {
if ((a<=0)||(b<=0)) throw new IllegalArgumentException("Pool quantities must be positive");
return (double)b/(double)a;
}
/**
* Computes the smallest price for d units of Asset A in terms of units of Asset B
* such that a constant liquidity pool c = a * b is increased
*
* @param a Quantity of Asset A in Pool
* @param b Quantity of Asset B in Pool
* @param delta Quantity of Unit A to buy (negative = sell)
* @return Price of A in terms of B
*/
public static long swapPrice(long delta,long a, long b) {
if ((a<=0)||(b<=0)) throw new IllegalArgumentException("Pool quantities must be positive");
if (delta>=a) throw new IllegalArgumentException("Trying to buy entire pool!");
long newB=Utils.mulDiv(a, b, a-delta);
long result=(newB-b)+1; // strict increase
return result;
}
public static double stakeDecay(long time, long peerTime) {
if (peerTime>=time) return 1.0;
double delay=time-peerTime;
delay-=Constants.PEER_DECAY_DELAY;
if (delay<0) return 1.0;
return Math.max(0.001, Math.exp(-delay/Constants.PEER_DECAY_TIME));
}
}