Skip to content

Commit 4df23d1

Browse files
new release (#128)
2 parents a2215d1 + ef271b4 commit 4df23d1

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

.changeset/two-times-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eth-tech-tree": minor
3+
---
4+
5+
Add "Total Gas Used" to Progress and Leader Board views

src/utils/helpers.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import os from "os";
21
import fs from "fs";
3-
import { IChallenge } from "../types";
2+
import os from "os";
43
import { fetchChallenges, getEnsAddress } from "../modules/api";
54
import { Choice } from "../tasks/parse-command-arguments-and-options";
5+
import { IChallenge } from "../types";
66

77
export function wait(ms: number) {
88
return new Promise((resolve) => setTimeout(resolve, ms));
@@ -71,6 +71,15 @@ export const calculatePoints = (completedChallenges: Array<{ challenge: IChallen
7171
}, 0);
7272
}
7373

74+
export const calculateTotalGasUsed = (completedChallenges: Array<{ challenge: IChallenge | undefined, completion?: { gasReport?: Array<{ functionName: string, gasUsed: number }> } }>): number => {
75+
return completedChallenges
76+
.reduce((total, challengeItem) => {
77+
const gasReport = challengeItem.completion?.gasReport;
78+
const challengeGasUsed = gasReport?.reduce((challengeTotal: number, report: { functionName: string, gasUsed: number }) => challengeTotal + report.gasUsed, 0) || 0;
79+
return total + challengeGasUsed;
80+
}, 0);
81+
}
82+
7483
export const searchChallenges = async (term: string = "") => {
7584
const challenges = (await fetchChallenges()).filter((challenge: IChallenge) => challenge.enabled);
7685
const choices = challenges.map((challenge: IChallenge) => ({

src/utils/leaderboard-view.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { TreeNode } from "../types";
21
import chalk from "chalk";
2+
import { TreeNode } from "../types";
33
import { stripAnsi } from "./helpers";
44

55
interface LeaderboardEntry {
@@ -39,7 +39,7 @@ export class LeaderboardView {
3939

4040
private getEntryLabel(entry: LeaderboardEntry): string {
4141
const identifier = entry.ens || entry.address;
42-
return chalk.white(`${this.getRankFormatting(entry.rank)} | ${this.formatSpacing(chalk.yellow(entry.points.toLocaleString()), 8)} | ${chalk.green(identifier)}`);
42+
return chalk.white(`${this.getRankFormatting(entry.rank)} | ${this.formatSpacing(chalk.yellow(entry.points.toLocaleString()), 8)} | ${this.formatSpacing(chalk.cyan(entry.totalGasUsed.toLocaleString()), 14)} | ${chalk.green(identifier)}`);
4343
}
4444

4545
private getRankFormatting(rank: number): string {
@@ -108,6 +108,6 @@ Total Gas Used: ${chalk.green(entry.totalGasUsed.toLocaleString())}
108108
}
109109
}
110110

111-
return chalk.bold(`${statement}\n\nTop Players\n Rank | Points | Player`);
111+
return chalk.bold(`${statement}\n\nTop Players\n${this.formatSpacing("Rank", 7, false)} | ${this.formatSpacing("Points", 8)} | ${this.formatSpacing("Total Gas Used", 12)} | Player`);
112112
}
113113
}

src/utils/progress-view.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { IUser, IChallenge, TreeNode } from "../types";
21
import chalk from "chalk";
3-
import { calculatePoints } from "./helpers";
2+
import { IChallenge, IUser, TreeNode } from "../types";
3+
import { calculatePoints, calculateTotalGasUsed } from "./helpers";
44

55
export class ProgressView {
66
constructor(
@@ -18,6 +18,7 @@ export class ProgressView {
1818
.filter(c => c.challenge);
1919

2020
const points = calculatePoints(completedChallenges);
21+
const totalGasUsed = calculateTotalGasUsed(completedChallenges);
2122
const completionRate = (completedChallenges.length / this.challenges.filter(c => c.enabled).length * 100).toFixed(1);
2223

2324
// Create completed challenges node with all completed challenges as children
@@ -44,17 +45,17 @@ export class ProgressView {
4445
label: "Progress",
4546
name: "stats",
4647
children: [...challengeNodes],
47-
message: this.buildStatsMessage(points, completionRate)
48+
message: this.buildStatsMessage(points, completionRate, totalGasUsed)
4849
};
4950

5051
return statsNode;
5152
}
5253

53-
private buildStatsMessage(points: number, completionRate: string): string {
54+
private buildStatsMessage(points: number, completionRate: string, totalGasUsed: number): string {
5455
const totalChallenges = this.challenges.filter(c => c.enabled).length;
5556
const completedChallenges = this.userState.challenges.filter(c => c.status === "success").length;
5657
return `Address: ${chalk.green(this.userState.ens || this.userState.address)}
57-
${chalk.yellow(`Points Earned: ${points.toLocaleString()}`)}
58+
${chalk.yellow(`Points Earned: ${points.toLocaleString()}\n${chalk.cyan(`Total Gas Used: ${totalGasUsed.toLocaleString()}`)}`)}
5859
5960
Challenges Completed: ${chalk.blueBright(`${completedChallenges}/${totalChallenges} (${completionRate}%)`)}
6061
${completedChallenges ? "Details:" : ""}`;

0 commit comments

Comments
 (0)