Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
split logic out, so one can run it with Github Actions (which doesn't…
Browse files Browse the repository at this point in the history
… accept CLI arguments) or as a CLI program (which accepts arguments) with node dist/cli.js <filename>
  • Loading branch information
randomshinichi committed Jan 19, 2024
1 parent 5aad79a commit 9ae1282
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 91 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"@actions/exec": "^1.1.1",
"@actions/github": "^5.1.1",
"@solana/web3.js": "^1.73.2",
"@types/minimist": "^1.2.5",
"csv-writer": "^1.6.0",
"minimist": "^1.2.8",
"node-downloader-helper": "^2.1.6",
"node-fetch": "^2.6.6"
},
Expand Down
12 changes: 12 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { validateValidatedTokensCsv } from "./logic";
import minimist from "minimist";
// CLI entrypoint which accepts an argument
(async () => {
try {
const argv = minimist(process.argv.slice(2));
await validateValidatedTokensCsv(argv._[0]);
}
catch (error: any) {
console.log(error.message)
}
})();
90 changes: 90 additions & 0 deletions src/logic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as core from "@actions/core";
import { exec } from "@actions/exec";
import { canOnlyAddOneToken, detectDuplicateSymbol, detectDuplicateMints as detectDuplicateMints, validMintAddress, noEditsToPreviousLinesAllowed } from "./utils/validate";
import { ValidatedTokensData } from "./types/types";
import { indexToLineNumber } from "./utils/validate";
import { parse } from "csv-parse/sync";
import fs from "fs";

export async function validateValidatedTokensCsv(filename: string) {
const [records, recordsRaw] = parseCsv(filename);

const recordsPreviousRaw = await gitPreviousVersion("validated-tokens.csv");
fs.writeFileSync(".validated-tokens-0.csv", recordsPreviousRaw);
const [recordsPrevious, _] = parseCsv(".validated-tokens-0.csv")

let duplicateSymbols;
let duplicateMints;
let attemptsToAddMultipleTokens;
let invalidMintAddresses;
let notCommunityValidated;
let noEditsAllowed;

duplicateSymbols = detectDuplicateSymbol(recordsPrevious, records);
duplicateMints = detectDuplicateMints(records);
attemptsToAddMultipleTokens = canOnlyAddOneToken(recordsPrevious, records)
invalidMintAddresses = validMintAddress(records);
noEditsAllowed = noEditsToPreviousLinesAllowed(recordsPrevious, records);
// notCommunityValidated = validCommunityValidated(records);

console.log("No More Duplicate Symbols:", duplicateSymbols);
console.log("Duplicate Mints:", duplicateMints);
console.log("Attempts to Add Multiple Tokens:", attemptsToAddMultipleTokens);
console.log("Invalid Mint Addresses:", invalidMintAddresses);
console.log("Not Community Validated:", notCommunityValidated);
console.log("Edits to Existing Tokens:", noEditsAllowed);
}

// Get previous version of validated-tokens.csv from last commit
async function gitPreviousVersion(path: string): Promise<any> {
let prevVersion = "";
let gitCmdError = "";

try {
await exec("git", ["show", `origin/main:${path}`], {
listeners: {
stdout: (data: Buffer) => {
prevVersion += data.toString();
},
stderr: (data: Buffer) => {
gitCmdError += data.toString();
},
},
silent: true
});
} catch (error: any) {
core.setFailed(error.message);
}

if (gitCmdError) {
core.setFailed(gitCmdError);
}
return prevVersion;
}

function parseCsv(filename: string): [ValidatedTokensData[], string] {
const recordsRaw = fs.readFileSync(filename, "utf8")
const r = parse(recordsRaw, {
columns: true,
skip_empty_lines: true,
});
const records = csvToRecords(r);
return [records, recordsRaw];
}

function csvToRecords(r: any): ValidatedTokensData[] {
const records: ValidatedTokensData[] = [];
r.forEach((record: any, i: number) => {
const rec: ValidatedTokensData = {
Name: record.Name,
Symbol: record.Symbol,
Mint: record.Mint,
Decimals: record.Decimals,
LogoURI: record.LogoURI,
"Community Validated": JSON.parse(record["Community Validated"]),
Line: indexToLineNumber(i)
};
records.push(rec);
});
return records;
}
94 changes: 3 additions & 91 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,12 @@
import * as core from "@actions/core";
import { exec } from "@actions/exec";
import { canOnlyAddOneToken, detectDuplicateSymbol, detectDuplicateMints as detectDuplicateMints, validMintAddress, noEditsToPreviousLinesAllowed } from "./utils/validate";
import { ValidatedTokensData } from "./types/types";
import { indexToLineNumber } from "./utils/validate";
import { parse } from "csv-parse/sync";
import fs from "fs";

import { validateValidatedTokensCsv } from "./logic";
// Github Actions entrypoint
(async () => {
try {
await validateValidatedTokensCsv();
await validateValidatedTokensCsv("validated-tokens.csv");
}
catch (error: any) {
core.setFailed(error.message);
console.log(error.message)
}
})();

async function validateValidatedTokensCsv() {
const [records, recordsRaw] = parseCsv("validated-tokens.csv");

const recordsPreviousRaw = await gitPreviousVersion("validated-tokens.csv");
fs.writeFileSync(".validated-tokens-0.csv", recordsPreviousRaw);
const [recordsPrevious, _] = parseCsv(".validated-tokens-0.csv")

let duplicateSymbols;
let duplicateMints;
let attemptsToAddMultipleTokens;
let invalidMintAddresses;
let notCommunityValidated;
let noEditsAllowed;

duplicateSymbols = detectDuplicateSymbol(recordsPrevious, records);
duplicateMints = detectDuplicateMints(records);
attemptsToAddMultipleTokens = canOnlyAddOneToken(recordsPrevious, records)
invalidMintAddresses = validMintAddress(records);
noEditsAllowed = noEditsToPreviousLinesAllowed(recordsPrevious, records);
// notCommunityValidated = validCommunityValidated(records);

console.log("No More Duplicate Symbols:", duplicateSymbols);
console.log("Duplicate Mints:", duplicateMints);
console.log("Attempts to Add Multiple Tokens:", attemptsToAddMultipleTokens);
console.log("Invalid Mint Addresses:", invalidMintAddresses);
console.log("Not Community Validated:", notCommunityValidated);
console.log("Edits to Existing Tokens:", noEditsAllowed);
}

// Get previous version of validated-tokens.csv from last commit
async function gitPreviousVersion(path: string): Promise<any> {
let prevVersion = "";
let gitCmdError = "";

try {
await exec("git", ["show", `origin/main:${path}`], {
listeners: {
stdout: (data: Buffer) => {
prevVersion += data.toString();
},
stderr: (data: Buffer) => {
gitCmdError += data.toString();
},
},
silent: true
});
} catch (error: any) {
core.setFailed(error.message);
}

if (gitCmdError) {
core.setFailed(gitCmdError);
}
return prevVersion;
}

function parseCsv(filename: string): [ValidatedTokensData[], string] {
const recordsRaw = fs.readFileSync(filename, "utf8")
const r = parse(recordsRaw, {
columns: true,
skip_empty_lines: true,
});
const records = csvToRecords(r);
return [records, recordsRaw];
}

function csvToRecords(r: any): ValidatedTokensData[] {
const records: ValidatedTokensData[] = [];
r.forEach((record: any, i: number) => {
const rec: ValidatedTokensData = {
Name: record.Name,
Symbol: record.Symbol,
Mint: record.Mint,
Decimals: record.Decimals,
LogoURI: record.LogoURI,
"Community Validated": JSON.parse(record["Community Validated"]),
Line: indexToLineNumber(i)
};
records.push(rec);
});
return records;
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@
dependencies:
"@types/node" "*"

"@types/minimist@^1.2.5":
version "1.2.5"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.5.tgz#ec10755e871497bcd83efe927e43ec46e8c0747e"
integrity sha512-hov8bUuiLiyFPGyFPE1lwWhmzYbirOXQNNo40+y3zow8aFVTeyn3VWL0VFFfdNddA8S4Vf0Tc062rzyNr7Paag==

"@types/node-fetch@^2.6.2":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.2.tgz#d1a9c5fd049d9415dce61571557104dec3ec81da"
Expand Down Expand Up @@ -463,6 +468,11 @@ mime-types@^2.1.12:
dependencies:
mime-db "1.52.0"

minimist@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

ms@2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
Expand Down

0 comments on commit 9ae1282

Please sign in to comment.