ethereum-emissions-calculator
Advanced tools
Comparing version 3.0.2-beta to 3.0.2
@@ -747,9 +747,9 @@ [ | ||
{ | ||
"timestamp": 1661990400, | ||
"emissionsFactor": 0.0002473976222064128 | ||
"timestamp": 1663200000, | ||
"emissionsFactor": 0.0002522790155063084 | ||
}, | ||
{ | ||
"timestamp": 1663259101, | ||
"emissionsFactor": 0.0000001 | ||
"timestamp": 1663286400, | ||
"emissionsFactor": 0.0 | ||
} | ||
] |
import { EmissionsFactor, TransactionData } from "../types"; | ||
/** returns [emissionsInKGCO2, gasUsed] */ | ||
/** | ||
* for each txn, finds the entry in emissions factor table | ||
* (where the timestamp is greater than or equal to txn timestamp) | ||
* @returns [emissionsInKGCO2, gasUsed] | ||
*/ | ||
export declare const getTransactionEmissions: (txns: TransactionData[], emissionsFactorTable: EmissionsFactor[]) => [number, number]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getTransactionEmissions = void 0; | ||
/** returns [emissionsInKGCO2, gasUsed] */ | ||
/** | ||
* for each txn, finds the entry in emissions factor table | ||
* (where the timestamp is greater than or equal to txn timestamp) | ||
* @returns [emissionsInKGCO2, gasUsed] | ||
*/ | ||
const getTransactionEmissions = (txns, emissionsFactorTable) => { | ||
let datePointer = 0; | ||
return txns.reduce((prev, tx) => { | ||
const txnIsOlder = parseInt(tx.timeStamp) > emissionsFactorTable[datePointer].timestamp; | ||
while (txnIsOlder) { | ||
// if the timestamp is older, try the next entry | ||
while (parseInt(tx.timeStamp) > emissionsFactorTable[datePointer].timestamp) { | ||
if (datePointer === emissionsFactorTable.length - 1) { | ||
@@ -15,9 +19,9 @@ break; | ||
} | ||
const txnEmissions = parseInt(tx.gasUsed) * | ||
emissionsFactorTable[datePointer].emissionsFactor; | ||
const sum = prev[0] + txnEmissions; // danger, floating point errors 0.30000000000000004 | ||
const val = [ | ||
prev[0] + | ||
parseInt(tx.gasUsed) * | ||
emissionsFactorTable[datePointer].emissionsFactor, | ||
Number(sum.toFixed(3)), | ||
prev[1] + parseInt(tx.gasUsed), | ||
]; | ||
console.log("fetched value", val, tx.timeStamp, emissionsFactorTable[datePointer].timestamp, emissionsFactorTable[datePointer].emissionsFactor); | ||
return val; | ||
@@ -24,0 +28,0 @@ }, [0, 0]); |
{ | ||
"name": "ethereum-emissions-calculator", | ||
"version": "3.0.2-beta", | ||
"version": "3.0.2", | ||
"description": "TypeScript utils to calculate the CO2 emissions of an Ethereum wallet. Powered by the Etherscan.io API.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
43840
1282
0