Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves linting for project #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .editorconfig

This file was deleted.

5 changes: 0 additions & 5 deletions .eslintrc.json

This file was deleted.

9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ lib-cov
coverage
test.js

# Editor directories
.vscode

# nyc test coverage
.nyc_output

Expand Down Expand Up @@ -62,6 +65,6 @@ typings/
.next

# Generated files
dist/
transparency-report.json
transparency.json
dist/
transparency-report.json
transparency.json
19 changes: 19 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @ts-check

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
...tseslint.configs.stylistic,
{
rules: {
'semi': ['error', 'always'],
'indent': ['error', 2],
}
},
{
ignores: ['node_modules', 'dist', 'build']
}
);
892 changes: 676 additions & 216 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"main": "./dist/platform.js",
"type": "module",
"scripts": {
"build": "tsc && node ./dist/build.js --save=true",
"test": "tsc && node ./dist/build.js --save=false --verbose=true",
"lint": "eslint src/**/*.ts"
"build": "npm run lint && tsc && node ./dist/build.js --save=true",
"test": "npm run lint && tsc && node ./dist/build.js --save=false --verbose=true",
"lint": "npx eslint src/**/*.ts"
},
"repository": {
"type": "git",
Expand All @@ -20,13 +20,16 @@
},
"homepage": "https://github.com/brave/transparency-report-generator#readme",
"devDependencies": {
"@eslint/js": "^9.9.0",
"@types/eslint__js": "^8.42.3",
"@types/node": "^18.11.18",
"@types/node-fetch": "^2.6.2",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"eslint": "^8.31.0",
"eslint": "^8.57.0",
"eslint-config-standard": "^17.0.0",
"typescript": "^4.9.4"
"typescript": "^4.9.5",
"typescript-eslint": "^8.0.1"
},
"dependencies": {
"node-fetch": "^3.3.0"
Expand Down
8 changes: 4 additions & 4 deletions src/exchange-modules/coinbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function signRequest(
time: number,
SECRET: string,
path: string,
method: string = "GET",
body: string = ""
method = "GET",
body = ""
): string {
const key = Buffer.from(SECRET, "base64");
const hmac = crypto.createHmac("sha256", key);
Expand All @@ -67,7 +67,7 @@ function signRequest(
* https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getorders
*/
async function _getOrders(
since: number = 0,
since = 0,
pageCursor: CoinbaseCursor = {}
): Promise<CoinbaseOrder[]> {
debugLOG(`Requesting orders from Coinbase`);
Expand Down Expand Up @@ -129,7 +129,7 @@ function isValidOrder(order: CoinbaseOrder, since: number): boolean {
}

export async function getOrders(
since: number = 0
since = 0
): Promise<Record<string, TransactionOrder>> {
console.group("Coinbase");
const orders = await _getOrders(since);
Expand Down
87 changes: 43 additions & 44 deletions src/exchange-modules/gemini.ts

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may not be reading this carefully enough, but the changes in this file seem to be beyond linting, as it modifies the flow (by moving code within the if (trades.length > 0) condition).

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function signRequest(secret: string, payload: string): string {
* https://docs.gemini.com/rest-api/#trade-history
*/
export async function getOrders(
timestamp: number = 0
timestamp = 0
): Promise<Record<string, TransactionOrder>> {
console.group("Gemini");
const results: Record<string, TransactionOrder> = {};
Expand All @@ -62,10 +62,11 @@ export async function getOrders(
* and insignificant. By adding 1ms, we will retrieve only trades which
* occurred after these earlier test transactions.
*/
let hasTrades = true;
let afterTimestamp = timestamp || 1649111057653;

while (true) {
let afterDate = new Date(afterTimestamp).toLocaleString();
while (hasTrades) {
const afterDate = new Date(afterTimestamp).toLocaleString();

debugLOG(
`Requesting transactions${timestamp > 0 ? ` since ${afterDate}` : ``}`
Expand Down Expand Up @@ -113,53 +114,51 @@ export async function getOrders(

if (trades.length == 0) {
debugLOG(`No more transactions to retrieve from Gemini`);
break;
} else if (trades.length > 0) {
const firstDate = new Date(trades[0].timestampms).toLocaleString();
const lastDate = new Date(
trades[trades.length - 1].timestampms
).toLocaleString();
debugLOG(
`Retrieved ${trades.length} trades spanning ${lastDate} to ${firstDate}`
);
hasTrades = false;
}

/**
* Because trades are sorted with the most recent first, we can
* add 1ms to the first item's timestamp to ensure we don't get
* duplicate trades in the next request.
* See: https://docs.gemini.com/rest-api/#get-past-trades
*/
afterTimestamp = trades[0].timestamp;

for (const trade of trades) {

if (trade.type !== TradeTypes.Buy) continue;

const id = trade.order_id;
const time = trade.timestampms;
const amount = parseFloat(trade.amount);
if ( trades.length > 0 ) {
const firstDate = new Date(trades[0].timestampms).toLocaleString();
const lastDate = new Date(trades[trades.length - 1].timestampms).toLocaleString();
debugLOG(`Retrieved ${trades.length} trades spanning ${lastDate} to ${firstDate}`);

/**
* A trade for this order has already been encountered.
* Let's push the date back for this order if necessary,
* and increase the total amount of associated BAT too.
* Because trades are sorted with the most recent first, we can
* add 1ms to the first item's timestamp to ensure we don't get
* duplicate trades in the next request.
* See: https://docs.gemini.com/rest-api/#get-past-trades
*/
if (results.hasOwnProperty(id)) {
const newAmount = parseFloat(results[id].BAT) + amount;
results[id].date = Math.min(results[id].date, time);
results[id].BAT = newAmount.toString();
continue;
afterTimestamp = trades[0].timestamp;

for (const trade of trades) {

if (trade.type !== TradeTypes.Buy) continue;

const id = trade.order_id;
const time = trade.timestampms;
const amount = parseFloat(trade.amount);

/**
* A trade for this order has already been encountered.
* Let's push the date back for this order if necessary,
* and increase the total amount of associated BAT too.
*/
if (results[id]) {
const newAmount = parseFloat(results[id].BAT) + amount;
results[id].date = Math.min(results[id].date, time);
results[id].BAT = newAmount.toString();
continue;
}

/**
* This is the first time we've encountered this order.
*/
results[id] = {
date: time,
site: Exchange.Gemini,
BAT: amount.toString(),
};
}

/**
* This is the first time we've encountered this order.
*/
results[id] = {
date: time,
site: Exchange.Gemini,
BAT: amount.toString(),
};
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/exchange-modules/uphold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface UpholdTransaction {
}

export async function getTransactionIDs(
knownIDs: number = 0
knownIDs = 0
): Promise<string[]> {
console.group("Uphold");
debugLOG("Requesting transaction IDs");
Expand Down Expand Up @@ -64,7 +64,7 @@ export async function getTransactionByID(
* This method is provided for parity with the other modules.
*/
export async function getOrders(
timestamp: number = 0
timestamp = 0
): Promise<Record<string, TransactionOrder>> {
const transactions = await getTransactionIDs();
const results: Record<string, TransactionOrder> = {};
Expand Down
16 changes: 6 additions & 10 deletions src/modules/brave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,14 @@ export interface CryptoCompareData {
conversionSymbol: string;
}

interface MauDauResponse {
[date: string]: {
browser_mau_adjusted: number;
browser_mau_historical: number;
browser_dau_monthly_avg: number;
};
}
type MauDauResponse = Record<string, {
browser_mau_adjusted: number;
browser_mau_historical: number;
browser_dau_monthly_avg: number;
}>;

// TODO: Sparse data; nothing after 2022-04-01.
export async function getRewardsPayoutRecordHistory(): Promise<
RewardsInstanceCount[]
> {
export async function getRewardsPayoutRecordHistory(): Promise<RewardsInstanceCount[]> {
debugLOG("Requesting count of Rewards instances");
const endpoint = `https://ads-serve.brave.com/v1/stat/payout`;
const headers = { Authorization: `Bearer ${ADS_SERVER_STATS_CREDENTIAL}` };
Expand Down
6 changes: 2 additions & 4 deletions src/modules/braveBATInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ interface GrowthRecord {
total: number;
}

interface CategoryGrowthStats {
[key: string]: Record<string, number>;
}
type CategoryGrowthStats = Record<string, Record<string, number>>;

export async function getCreatorGrowth() {
debugLOG("Requesting creator growth from BraveBAT.info");
Expand All @@ -25,7 +23,7 @@ export async function getCreatorGrowth() {
/**
* Make sure records are in chronological order so that
* when we iterate, the most recent data is always last.
*/
* */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
* */
*/

records.sort((a, b) => {
const aDate = new Date(a.record_date);
const bDate = new Date(b.record_date);
Expand Down
Loading