@iqprotocol/energy
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -5,46 +5,130 @@ 'use strict'; | ||
var Energy = /*#__PURE__*/function () { | ||
function Energy() {} | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
Energy.halfLife = function halfLife(params) { | ||
var initialValue = params.initialValue, | ||
halfLifePeriod = params.halfLifePeriod, | ||
t0 = params.t0, | ||
t1 = params.t1; | ||
var period = t1 - t0; | ||
if (period < 0) { | ||
throw new Error('Invalid period'); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
if (period === 0) { | ||
return initialValue; | ||
} | ||
return initialValue * Math.pow(0.5, period / halfLifePeriod); | ||
return target; | ||
}; | ||
Energy.calculateEnergy = function calculateEnergy(params) { | ||
var balance = params.balance, | ||
prevEnergy = params.prevEnergy, | ||
halfLifePeriod = params.halfLifePeriod, | ||
t0 = params.t0, | ||
t1 = params.t1; | ||
return _extends.apply(this, arguments); | ||
} | ||
if (balance < 0) { | ||
throw new Error('Invalid balance'); | ||
} | ||
/* eslint-disable no-extra-parens */ | ||
var ONE = 1n << 144n; | ||
var LOG_ONE_HALF = 15457698658747239244624307340191628289589491n; // log(0.5) * 2 ** 144 | ||
return balance + this.halfLife({ | ||
initialValue: prevEnergy - balance, | ||
halfLifePeriod: halfLifePeriod, | ||
var MAX_SAFE_UINT112 = 5192296858534827628530496329220095n; // 2 ** 112 - 1; | ||
var halfLife = function halfLife(params) { | ||
var gapHalvingPeriod = params.gapHalvingPeriod, | ||
t0 = params.t0, | ||
t1 = params.t1; | ||
var initialValue = params.initialValue; | ||
if (initialValue < 0) { | ||
throw new Error('Initial value underflow'); | ||
} | ||
if (initialValue > MAX_SAFE_UINT112) { | ||
throw new Error('Initial value overflow'); | ||
} | ||
var period = BigInt(t1 - t0); | ||
if (period < 0n) { | ||
throw new Error('Negative period'); | ||
} | ||
if (period === 0n) { | ||
return initialValue; | ||
} | ||
initialValue >>= BigInt(period) / BigInt(gapHalvingPeriod); | ||
if (initialValue === 0n) { | ||
return initialValue; | ||
} | ||
period %= BigInt(gapHalvingPeriod); | ||
var x = BigInt(LOG_ONE_HALF * period) / BigInt(gapHalvingPeriod); | ||
var z = BigInt(initialValue); | ||
var i = ONE; | ||
var sum = 0n; | ||
while (z !== 0n) { | ||
sum += z; | ||
z = z * x / i; | ||
i += ONE; | ||
sum -= z; | ||
z = z * x / i; | ||
i += ONE; | ||
} | ||
return BigInt.asUintN(112, sum); | ||
}; | ||
var calculateLinearEnergy = function calculateLinearEnergy(_ref) { | ||
var power = _ref.power, | ||
initialValue = _ref.initialValue, | ||
gapHalvingPeriod = _ref.gapHalvingPeriod, | ||
t0 = _ref.t0, | ||
t1 = _ref.t1; | ||
var period = BigInt(t1 - t0); | ||
if (period < 0n) { | ||
throw new Error('Negative period'); | ||
} | ||
return initialValue + power * period / BigInt(gapHalvingPeriod * 4); | ||
}; | ||
var calculateEnergyCap = function calculateEnergyCap(_ref2) { | ||
var power = _ref2.power, | ||
initialValue = _ref2.initialValue, | ||
gapHalvingPeriod = _ref2.gapHalvingPeriod, | ||
t0 = _ref2.t0, | ||
t1 = _ref2.t1; | ||
if (power > initialValue) { | ||
return power - halfLife({ | ||
initialValue: power - initialValue, | ||
gapHalvingPeriod: gapHalvingPeriod, | ||
t0: t0, | ||
t1: t1 | ||
}); | ||
} | ||
return power + halfLife({ | ||
initialValue: initialValue - power, | ||
gapHalvingPeriod: gapHalvingPeriod, | ||
t0: t0, | ||
t1: t1 | ||
}); | ||
}; | ||
var calculateEffectiveEnergy = function calculateEffectiveEnergy(params) { | ||
var energyCap = calculateEnergyCap(_extends({}, params, { | ||
initialValue: params.energyCap | ||
})); | ||
var linearEnergy = calculateLinearEnergy(_extends({}, params, { | ||
initialValue: params.energy | ||
})); | ||
var energy = linearEnergy < energyCap ? linearEnergy : energyCap; // min(linearEnergy, energyCap) | ||
return { | ||
energyCap: energyCap, | ||
linearEnergy: linearEnergy, | ||
energy: energy | ||
}; | ||
}; | ||
return Energy; | ||
}(); | ||
exports.Energy = Energy; | ||
exports.calculateEffectiveEnergy = calculateEffectiveEnergy; | ||
exports.calculateEnergyCap = calculateEnergyCap; | ||
exports.calculateLinearEnergy = calculateLinearEnergy; | ||
exports.halfLife = halfLife; | ||
//# sourceMappingURL=energy.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Energy=function(){function e(){}return e.halfLife=function(e){var r=e.initialValue,i=e.halfLifePeriod,t=e.t1-e.t0;if(t<0)throw new Error("Invalid period");return 0===t?r:r*Math.pow(.5,t/i)},e.calculateEnergy=function(e){var r=e.balance,i=e.prevEnergy,t=e.halfLifePeriod,n=e.t0,a=e.t1;if(r<0)throw new Error("Invalid balance");return r+this.halfLife({initialValue:i-r,halfLifePeriod:t,t0:n,t1:a})},e}(); | ||
"use strict";function r(){return(r=Object.assign||function(r){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(r[i]=e[i])}return r}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});var n=1n<<144n,e=15457698658747239244624307340191628289589491n,i=5192296858534827628530496329220095n,t=function(r){var t=r.gapHalvingPeriod,a=r.t0,l=r.t1,o=r.initialValue;if(o<0)throw new Error("Initial value underflow");if(o>i)throw new Error("Initial value overflow");var g=BigInt(l-a);if(g<0n)throw new Error("Negative period");if(0n===g)return o;if(0n===(o>>=BigInt(g)/BigInt(t)))return o;g%=BigInt(t);for(var u=BigInt(e*g)/BigInt(t),p=BigInt(o),f=n,v=0n;0n!==p;)v+=p,v-=p=p*u/f,p=p*u/(f+=n),f+=n;return BigInt.asUintN(112,v)},a=function(r){var n=r.power,e=r.initialValue,i=r.gapHalvingPeriod,t=BigInt(r.t1-r.t0);if(t<0n)throw new Error("Negative period");return e+n*t/BigInt(4*i)},l=function(r){var n=r.power,e=r.initialValue,i=r.gapHalvingPeriod,a=r.t0,l=r.t1;return n>e?n-t({initialValue:n-e,gapHalvingPeriod:i,t0:a,t1:l}):n+t({initialValue:e-n,gapHalvingPeriod:i,t0:a,t1:l})};exports.calculateEffectiveEnergy=function(n){var e=l(r({},n,{initialValue:n.energyCap})),i=a(r({},n,{initialValue:n.energy}));return{energyCap:e,linearEnergy:i,energy:i<e?i:e}},exports.calculateEnergyCap=l,exports.calculateLinearEnergy=a,exports.halfLife=t; | ||
//# sourceMappingURL=energy.cjs.production.min.js.map |
@@ -1,45 +0,126 @@ | ||
var Energy = /*#__PURE__*/function () { | ||
function Energy() {} | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
Energy.halfLife = function halfLife(params) { | ||
var initialValue = params.initialValue, | ||
halfLifePeriod = params.halfLifePeriod, | ||
t0 = params.t0, | ||
t1 = params.t1; | ||
var period = t1 - t0; | ||
if (period < 0) { | ||
throw new Error('Invalid period'); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
if (period === 0) { | ||
return initialValue; | ||
} | ||
return initialValue * Math.pow(0.5, period / halfLifePeriod); | ||
return target; | ||
}; | ||
Energy.calculateEnergy = function calculateEnergy(params) { | ||
var balance = params.balance, | ||
prevEnergy = params.prevEnergy, | ||
halfLifePeriod = params.halfLifePeriod, | ||
t0 = params.t0, | ||
t1 = params.t1; | ||
return _extends.apply(this, arguments); | ||
} | ||
if (balance < 0) { | ||
throw new Error('Invalid balance'); | ||
} | ||
/* eslint-disable no-extra-parens */ | ||
var ONE = 1n << 144n; | ||
var LOG_ONE_HALF = 15457698658747239244624307340191628289589491n; // log(0.5) * 2 ** 144 | ||
return balance + this.halfLife({ | ||
initialValue: prevEnergy - balance, | ||
halfLifePeriod: halfLifePeriod, | ||
var MAX_SAFE_UINT112 = 5192296858534827628530496329220095n; // 2 ** 112 - 1; | ||
var halfLife = function halfLife(params) { | ||
var gapHalvingPeriod = params.gapHalvingPeriod, | ||
t0 = params.t0, | ||
t1 = params.t1; | ||
var initialValue = params.initialValue; | ||
if (initialValue < 0) { | ||
throw new Error('Initial value underflow'); | ||
} | ||
if (initialValue > MAX_SAFE_UINT112) { | ||
throw new Error('Initial value overflow'); | ||
} | ||
var period = BigInt(t1 - t0); | ||
if (period < 0n) { | ||
throw new Error('Negative period'); | ||
} | ||
if (period === 0n) { | ||
return initialValue; | ||
} | ||
initialValue >>= BigInt(period) / BigInt(gapHalvingPeriod); | ||
if (initialValue === 0n) { | ||
return initialValue; | ||
} | ||
period %= BigInt(gapHalvingPeriod); | ||
var x = BigInt(LOG_ONE_HALF * period) / BigInt(gapHalvingPeriod); | ||
var z = BigInt(initialValue); | ||
var i = ONE; | ||
var sum = 0n; | ||
while (z !== 0n) { | ||
sum += z; | ||
z = z * x / i; | ||
i += ONE; | ||
sum -= z; | ||
z = z * x / i; | ||
i += ONE; | ||
} | ||
return BigInt.asUintN(112, sum); | ||
}; | ||
var calculateLinearEnergy = function calculateLinearEnergy(_ref) { | ||
var power = _ref.power, | ||
initialValue = _ref.initialValue, | ||
gapHalvingPeriod = _ref.gapHalvingPeriod, | ||
t0 = _ref.t0, | ||
t1 = _ref.t1; | ||
var period = BigInt(t1 - t0); | ||
if (period < 0n) { | ||
throw new Error('Negative period'); | ||
} | ||
return initialValue + power * period / BigInt(gapHalvingPeriod * 4); | ||
}; | ||
var calculateEnergyCap = function calculateEnergyCap(_ref2) { | ||
var power = _ref2.power, | ||
initialValue = _ref2.initialValue, | ||
gapHalvingPeriod = _ref2.gapHalvingPeriod, | ||
t0 = _ref2.t0, | ||
t1 = _ref2.t1; | ||
if (power > initialValue) { | ||
return power - halfLife({ | ||
initialValue: power - initialValue, | ||
gapHalvingPeriod: gapHalvingPeriod, | ||
t0: t0, | ||
t1: t1 | ||
}); | ||
} | ||
return power + halfLife({ | ||
initialValue: initialValue - power, | ||
gapHalvingPeriod: gapHalvingPeriod, | ||
t0: t0, | ||
t1: t1 | ||
}); | ||
}; | ||
var calculateEffectiveEnergy = function calculateEffectiveEnergy(params) { | ||
var energyCap = calculateEnergyCap(_extends({}, params, { | ||
initialValue: params.energyCap | ||
})); | ||
var linearEnergy = calculateLinearEnergy(_extends({}, params, { | ||
initialValue: params.energy | ||
})); | ||
var energy = linearEnergy < energyCap ? linearEnergy : energyCap; // min(linearEnergy, energyCap) | ||
return { | ||
energyCap: energyCap, | ||
linearEnergy: linearEnergy, | ||
energy: energy | ||
}; | ||
}; | ||
return Energy; | ||
}(); | ||
export { Energy }; | ||
export { calculateEffectiveEnergy, calculateEnergyCap, calculateLinearEnergy, halfLife }; | ||
//# sourceMappingURL=energy.esm.js.map |
@@ -1,15 +0,21 @@ | ||
export declare class Energy { | ||
static halfLife(params: { | ||
initialValue: number; | ||
halfLifePeriod: number; | ||
t0: number; | ||
t1: number; | ||
}): number; | ||
static calculateEnergy(params: { | ||
balance: number; | ||
prevEnergy: number; | ||
halfLifePeriod: number; | ||
t0: number; | ||
t1: number; | ||
}): number; | ||
} | ||
export declare type HalfLifeParams = { | ||
initialValue: bigint; | ||
gapHalvingPeriod: number; | ||
t0: number; | ||
t1: number; | ||
}; | ||
export declare type EnergyCalculationParams = HalfLifeParams & { | ||
power: bigint; | ||
}; | ||
export declare type EffectiveEnergyCalculationParams = Omit<EnergyCalculationParams, 'initialValue'> & { | ||
energy: bigint; | ||
energyCap: bigint; | ||
}; | ||
export declare const halfLife: (params: HalfLifeParams) => bigint; | ||
export declare const calculateLinearEnergy: ({ power, initialValue, gapHalvingPeriod, t0, t1, }: EnergyCalculationParams) => bigint; | ||
export declare const calculateEnergyCap: ({ power, initialValue, gapHalvingPeriod, t0, t1, }: EnergyCalculationParams) => bigint; | ||
export declare const calculateEffectiveEnergy: (params: EffectiveEnergyCalculationParams) => { | ||
energyCap: bigint; | ||
linearEnergy: bigint; | ||
energy: bigint; | ||
}; |
{ | ||
"name": "@iqprotocol/energy", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"license": "UNLICENSED", | ||
@@ -21,3 +21,3 @@ "scripts": { | ||
}, | ||
"gitHead": "33a4329d2db14b9aed2b714507abd83bd5d530f9" | ||
"gitHead": "1b31a31b58f27795061c34c1442fa6c49da79833" | ||
} |
# @iqprotocol/energy | ||
This package is part of [IQ Protocol JS SDK.](https://github.com/iqalliance/iq-sdk-js) | ||
The package provided various utility functions for *energy* calculation. | ||
## Installation | ||
```bash | ||
yarn add @iqprotocol/energy | ||
``` | ||
## Energy & Power | ||
In IQ protocol *Power* is the reinterpreted pTokens balance that is responsible for *Energy* generation. Simply `1 pToken = 1 Unit of Power`. | ||
Whereas the Energy is a spendable asset that is generated over time by the Power. | ||
**Linear Energy** the actual energy value at the specific time. As the name implies, it changes linearly over time. | ||
**Energy Cap** is the potential maximum possible energy value at specific time. It corresponds to the IQ protocol *Proof Of Hold*. | ||
**Effective Energy** is the current available energy. It is the lowest value between the current linear energy value and energy cap. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25946
247
21
1