@iqprotocol/energy
Advanced tools
Comparing version 0.6.0 to 0.6.1
@@ -1,2 +0,2 @@ | ||
export declare type HalfLifeParams = { | ||
type HalfLifeParams = { | ||
initialValue: bigint; | ||
@@ -7,13 +7,13 @@ gapHalvingPeriod: number; | ||
}; | ||
export declare type EnergyCalculationParams = HalfLifeParams & { | ||
type EnergyCalculationParams = HalfLifeParams & { | ||
power: bigint; | ||
}; | ||
export declare type EffectiveEnergyCalculationParams = Omit<EnergyCalculationParams, 'initialValue'> & { | ||
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) => { | ||
declare const halfLife: (params: HalfLifeParams) => bigint; | ||
declare const calculateLinearEnergy: ({ power, initialValue, gapHalvingPeriod, t0, t1, }: EnergyCalculationParams) => bigint; | ||
declare const calculateEnergyCap: ({ power, initialValue, gapHalvingPeriod, t0, t1, }: EnergyCalculationParams) => bigint; | ||
declare const calculateEffectiveEnergy: (params: EffectiveEnergyCalculationParams) => { | ||
energyCap: bigint; | ||
@@ -23,1 +23,2 @@ linearEnergy: bigint; | ||
}; | ||
export { HalfLifeParams, EnergyCalculationParams, EffectiveEnergyCalculationParams, halfLife, calculateLinearEnergy, calculateEnergyCap, calculateEffectiveEnergy }; |
@@ -0,8 +1,67 @@ | ||
'use strict'; | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./energy.cjs.production.min.js') | ||
} else { | ||
module.exports = require('./energy.cjs.development.js') | ||
} | ||
/* eslint-disable no-extra-parens */ | ||
const ONE = 1n << 144n; | ||
const LOG_ONE_HALF = 15457698658747239244624307340191628289589491n; // log(0.5) * 2 ** 144 | ||
const MAX_SAFE_UINT112 = 5192296858534827628530496329220095n; // 2 ** 112 - 1; | ||
const halfLife = (params) => { | ||
const { gapHalvingPeriod, t0, t1 } = params; | ||
let { initialValue } = params; | ||
if (initialValue < 0) { | ||
throw new Error('Initial value underflow'); | ||
} | ||
if (initialValue > MAX_SAFE_UINT112) { | ||
throw new Error('Initial value overflow'); | ||
} | ||
let 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); | ||
const x = BigInt(LOG_ONE_HALF * period) / BigInt(gapHalvingPeriod); | ||
let z = BigInt(initialValue); | ||
let i = ONE; | ||
let 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); | ||
}; | ||
const calculateLinearEnergy = ({ power, initialValue, gapHalvingPeriod, t0, t1, }) => { | ||
const period = BigInt(t1 - t0); | ||
if (period < 0n) { | ||
throw new Error('Negative period'); | ||
} | ||
return initialValue + (power * period) / BigInt(gapHalvingPeriod * 4); | ||
}; | ||
const calculateEnergyCap = ({ power, initialValue, gapHalvingPeriod, t0, t1, }) => { | ||
if (power > initialValue) { | ||
return power - halfLife({ initialValue: power - initialValue, gapHalvingPeriod, t0, t1 }); | ||
} | ||
return power + halfLife({ initialValue: initialValue - power, gapHalvingPeriod, t0, t1 }); | ||
}; | ||
const calculateEffectiveEnergy = (params) => { | ||
const energyCap = calculateEnergyCap({ ...params, initialValue: params.energyCap }); | ||
const linearEnergy = calculateLinearEnergy({ ...params, initialValue: params.energy }); | ||
const energy = linearEnergy < energyCap ? linearEnergy : energyCap; // min(linearEnergy, energyCap) | ||
return { energyCap, linearEnergy, energy }; | ||
}; | ||
exports.calculateEffectiveEnergy = calculateEffectiveEnergy; | ||
exports.calculateEnergyCap = calculateEnergyCap; | ||
exports.calculateLinearEnergy = calculateLinearEnergy; | ||
exports.halfLife = halfLife; |
{ | ||
"name": "@iqprotocol/energy", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"license": "UNLICENSED", | ||
"scripts": { | ||
"start": "tsdx watch --tsconfig tsconfig.build.json --verbose --noClean", | ||
"build": "tsdx build --tsconfig tsconfig.build.json", | ||
"test": "tsdx test --group=unit", | ||
"start": "rollup -cw", | ||
"build": "rollup -c", | ||
"test": "jest --group=unit", | ||
"prepublish": "npm run build" | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/energy.esm.js", | ||
"module": "dist/index.esm.js", | ||
"typings": "dist/index.d.ts", | ||
@@ -21,3 +21,3 @@ "files": [ | ||
}, | ||
"gitHead": "f464db766f57cc190e2e526ca204ff10a346b270" | ||
"gitHead": "f893c5c153a16a9ca7ab0872e14bbe95cb1ccf86" | ||
} |
@@ -11,2 +11,18 @@ # Energy Utils | ||
## Usage | ||
```ts | ||
import { calculateEnergyCap } from '@iqprotocol/energy'; | ||
const params = { | ||
power: 100n, | ||
initialValue: 50n, // power at t0 | ||
gapHalvingPeriod: 86400, | ||
t0: 1629906900, | ||
t1: 1629906900 + 86400 | ||
}; | ||
const energyCap = calculateEnergyCap(params); // 75n | ||
``` | ||
## Energy & Power | ||
@@ -13,0 +29,0 @@ |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
37
1
7295
5
145
1