Skip to content

Commit

Permalink
refactor getTransactions
Browse files Browse the repository at this point in the history
  • Loading branch information
SimasG committed Jan 9, 2023
1 parent 73ffd01 commit db8e219
Show file tree
Hide file tree
Showing 18 changed files with 13,109 additions and 29 deletions.
13,028 changes: 13,027 additions & 1 deletion data/EmissionsFactors.json

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions dist/calculateEmissions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/calculateEmissions.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/getTransactions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/getTransactions.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions dist/script/generateData.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/script/generateData.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions dist/script/generateEmissionsFactorTable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/script/generateEmissionsFactorTable.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"dependencies": {
"@types/csvtojson": "^2.0.0",
"convert-csv-to-json": "^1.5.0",
"csvtojson": "^2.0.10",
"dotenv": "^16.0.3",
"fs-extra": "^11.1.0"
},
"devDependencies": {
"@types/csvtojson": "^2.0.0",
"@types/express": "^4.17.15",
"@types/fs-extra": "^9.0.13",
"@types/node": "^18.11.18",
Expand Down
13 changes: 4 additions & 9 deletions src/calculateEmissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let timestamps: string[] = [];
let emissionsFactor = "";
let emissionsFactors: string[] = [];

// get an array of the tx timestamps from the address
// getting an array of the tx timestamps from the address
const getTimestamps = (tx: txResult) => {
for (let j = 1; j < emissionsFactorsObj.length; j++) {
if (
Expand All @@ -29,7 +29,7 @@ const getTimestamps = (tx: txResult) => {
return timestamps;
};

// get an array of emissions factors from timestamps
// getting an array of emissions factors from timestamps
const getEmissionsFactors = (timestamp: string) => {
for (let k = 0; k < emissionsFactorsObj.length; k++) {
if (emissionsFactorsObj[k].UnixTimeStamp === timestamp) {
Expand All @@ -40,8 +40,8 @@ const getEmissionsFactors = (timestamp: string) => {
return emissionsFactors;
};

// calculate total address emissions
const calculateEmissions = async (addr: string) => {
// calculating total address emissions
export const calculateEmissions = async (addr: string) => {
const transactions: tx = await getTransactions(addr);

let txEmissions = 0;
Expand All @@ -66,11 +66,6 @@ const calculateEmissions = async (addr: string) => {
return totalEmissionsKg;
};

const emissions = await calculateEmissions(
"0x6B3595068778DD592e39A122f4f5a5cF09C90fE2"
);

// 0x619353127678b95C023530df08BCB638870cFDdA -> mine
// 0xF417ACe7b13c0ef4fcb5548390a450A4B75D3eB3 -> woj.eth
// 0x6B3595068778DD592e39A122f4f5a5cF09C90fE2 -> sushi
console.log("emissions:", emissions);
2 changes: 1 addition & 1 deletion src/getTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Fetching array of historical transactions of a given address
**/
export const getTransactions = async (addr: string) => {
const response = await fetch(
`https://api.etherscan.io/api?module=account&action=txlist&address=${addr}&startblock=0&endblock=99999999&page=1&offset=10000&sort=asc&apikey=${process.env.IAQEQUWUR1GGPD1RAFIDPUDNVDNCWUMPBI}`
`https://api.etherscan.io/api?module=account&action=txlist&address=${addr}&startblock=0&endblock=99999999&page=1&offset=10000&sort=asc&apikey=${process.env.ETHERSCAN_API_KEY}`
);
const data = await response.json();
return data;
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { calculateEmissions } from "./calculateEmissions.js";

const emissions = await calculateEmissions(
"0x6B3595068778DD592e39A122f4f5a5cF09C90fE2"
);
console.log("emissions:", emissions);
8 changes: 4 additions & 4 deletions src/scripts/generateData.ts → src/script/generateData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { gasUsedItem, hashrateItem } from "../types.js";
/*
Generates json files from `GasedUsed.csv` & `Hashrate.csv`
**/
const gasUsedJSON: gasUsedItem[] = await csv()
const gasUsedJSONObj: gasUsedItem[] = await csv()
.fromFile("data/GasUsed.csv")
.then((jsonObj) => {
console.log(jsonObj);
return jsonObj;
});

fs.outputFileSync("data/GasUsed.json", JSON.stringify(gasUsedJSON));
fs.outputFileSync("data/GasUsed.json", JSON.stringify(gasUsedJSONObj));

const hashrateJSON: hashrateItem[] = await csv()
const hashrateJSONObj: hashrateItem[] = await csv()
.fromFile("data/Hashrate.csv")
.then((jsonObj) => {
return jsonObj;
});
fs.outputFileSync("data/Hashrate.json", JSON.stringify(hashrateJSON));
fs.outputFileSync("data/Hashrate.json", JSON.stringify(hashrateJSONObj));
File renamed without changes.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"resolveJsonModule": true
},
"include": ["src/**/*", "src/scripts/**/*"]
"include": ["src/**/*", "src/script/**/*"]
}

0 comments on commit db8e219

Please sign in to comment.