From 715347e1c79339d4331d519e41e9dd806df22c1c Mon Sep 17 00:00:00 2001 From: ponyjackal Date: Fri, 26 Apr 2024 08:58:41 -0700 Subject: [PATCH 1/3] feat: add detect-contextualizers --- package.json | 3 ++- src/commands/detectContextualizers.ts | 31 +++++++++++++++++++++++++++ src/commands/main.ts | 2 ++ src/commands/utils.ts | 10 +++++++++ src/helpers/utils.ts | 5 ++++- 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/commands/detectContextualizers.ts diff --git a/package.json b/package.json index 2c1137b2..5fc77ce5 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "grab:transaction": "npm run build && node ./dist/commands/main.js grab-transaction", "create:contextualizer": "npm run build && node ./dist/commands/main.js create-contextualizer", "run:contextualizers": "npm run build && node ./dist/commands/main.js run-contextualizers", - "grab:block": "npm run build && node ./dist/commands/main.js grab-block" + "grab:block": "npm run build && node ./dist/commands/main.js grab-block", + "detect:contextualizers": "npm run build && node ./dist/commands/main.js detect-contextualizers" }, "repository": { "type": "git", diff --git a/src/commands/detectContextualizers.ts b/src/commands/detectContextualizers.ts new file mode 100644 index 00000000..2522137b --- /dev/null +++ b/src/commands/detectContextualizers.ts @@ -0,0 +1,31 @@ +import { program } from './main'; +import { getTransaction } from './utils'; +import { Transaction } from '../types'; +import { contextualizer } from '../contextualizers'; + +export function registerDetectContextualizersCommand() { + program + .command('detect-contextualizers') + .description('Run contextualizers for a certain transaction') + .option('-h, --hash ', 'Transaction hash') + .action(async (options) => { + let transaction: Transaction; + try { + console.log(`Fetching a transaction`); + transaction = await getTransaction(options.hash); + + const txResult = contextualizer.contextualize(transaction); + if (!txResult.from) { + console.error( + `No matching protocol contextualizer on ${transaction.hash}`, + ); + } + + console.log('Successfully ran contextualizers'); + process.exit(0); // Successful exit + } catch (error) { + console.error('Running contextualizers failed:', error); + process.exit(1); // Exit with error + } + }); +} diff --git a/src/commands/main.ts b/src/commands/main.ts index ac24158a..2147db8b 100644 --- a/src/commands/main.ts +++ b/src/commands/main.ts @@ -3,6 +3,7 @@ import { registerCreateContextualizerCommand } from './createContextualizer'; import { registerGrabTransactionCommand } from './grabTransaction'; import { registerRunContextualizersCommand } from './runContextualizers'; import { registerGrabBlockCommand } from './grabBlock'; +import { registerDetectContextualizersCommand } from './detectContextualizers'; export const program = new Command(); @@ -15,5 +16,6 @@ registerCreateContextualizerCommand(); registerGrabTransactionCommand(); registerRunContextualizersCommand(); registerGrabBlockCommand(); +registerDetectContextualizersCommand(); program.parse(process.argv); diff --git a/src/commands/utils.ts b/src/commands/utils.ts index ef04157f..1dff1a35 100644 --- a/src/commands/utils.ts +++ b/src/commands/utils.ts @@ -26,6 +26,16 @@ export const grabTx = async (txHash: string, prefix: string) => { fs.writeFileSync(txFilePath, JSON.stringify(transaction, null, 2)); }; +export const getTransaction = async (txHash: string) => { + const defaultApiUrl = 'https://api.onceupon.gg'; + const API_URL = process.env.API_URL || defaultApiUrl; + const transaction = await fetch( + `${API_URL}/v2/transactions/${txHash}?withContext=false`, + ).then((res) => res.json()); + + return transaction; +}; + export const fetchTransactions = async ( limit: number, ): Promise => { diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 66ac5b1d..403d24a7 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -237,9 +237,12 @@ export const makeContextualize = ( children: Record Transaction>, ) => { return (transaction: Transaction): Transaction => { - for (const childContextualizer of Object.values(children)) { + for (const [contextualizerName, childContextualizer] of Object.entries( + children, + )) { const result = childContextualizer(transaction); if (result.context?.summaries?.en.title) { + console.log('contextualizer: ', contextualizerName); return result; } } From 6b496c6372d15fbd1fd9188e3a0c28d6c9ab70a6 Mon Sep 17 00:00:00 2001 From: ponyjackal Date: Fri, 26 Apr 2024 09:00:32 -0700 Subject: [PATCH 2/3] feat: update run-contextualizers --- src/commands/runContextualizers.ts | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/commands/runContextualizers.ts b/src/commands/runContextualizers.ts index 4ba20f9e..7f55035f 100644 --- a/src/commands/runContextualizers.ts +++ b/src/commands/runContextualizers.ts @@ -1,8 +1,7 @@ import { program } from './main'; import { fetchTransactions } from './utils'; import { Transaction } from '../types'; -import { protocolContextualizer } from '../contextualizers/protocol'; -import { heuristicContextualizer } from '../contextualizers/heuristics'; +import { contextualizer } from '../contextualizers'; export function registerRunContextualizersCommand() { program @@ -22,10 +21,10 @@ export function registerRunContextualizersCommand() { try { console.log(`Running contextualizers`); transactions.forEach((transaction) => { - // run protocol contextualizer + // run contextualizers console.log(`Running protocol contextualizer`); try { - const txResult = protocolContextualizer.contextualize(transaction); + const txResult = contextualizer.contextualize(transaction); if (!txResult.from) { console.error( `No matching protocol contextualizer on ${transaction.hash}`, @@ -37,22 +36,6 @@ export function registerRunContextualizersCommand() { err, ); } - - // run heuristic contextualizer - try { - console.log(`Running heuristic contextualizer`); - const txResult = heuristicContextualizer.contextualize(transaction); - if (!txResult.from) { - console.error( - `No matching heuristic contextualizers on ${transaction.hash}`, - ); - } - } catch (err) { - console.error( - `failed to run heuristic contextualizer on ${transaction.hash}: `, - err, - ); - } }); console.log('Successfully ran contextualizers'); From 22fdc0e5292d6ece553f69c7b7d1a6eef5936478 Mon Sep 17 00:00:00 2001 From: ponyjackal Date: Mon, 29 Apr 2024 11:47:08 -0700 Subject: [PATCH 3/3] feat: add debug mode to detect contextualiers --- src/commands/detectContextualizers.ts | 2 +- src/helpers/utils.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/commands/detectContextualizers.ts b/src/commands/detectContextualizers.ts index 2522137b..50c0a398 100644 --- a/src/commands/detectContextualizers.ts +++ b/src/commands/detectContextualizers.ts @@ -14,7 +14,7 @@ export function registerDetectContextualizersCommand() { console.log(`Fetching a transaction`); transaction = await getTransaction(options.hash); - const txResult = contextualizer.contextualize(transaction); + const txResult = contextualizer.contextualize(transaction, true); if (!txResult.from) { console.error( `No matching protocol contextualizer on ${transaction.hash}`, diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 403d24a7..988b1d57 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -234,15 +234,20 @@ function formatSection(section: ContextSummaryVariableType) { } export const makeContextualize = ( - children: Record Transaction>, + children: Record< + string, + (transaction: Transaction, isDebug: boolean) => Transaction + >, ) => { - return (transaction: Transaction): Transaction => { + return (transaction: Transaction, isDebug = false): Transaction => { for (const [contextualizerName, childContextualizer] of Object.entries( children, )) { - const result = childContextualizer(transaction); + const result = childContextualizer(transaction, isDebug); if (result.context?.summaries?.en.title) { - console.log('contextualizer: ', contextualizerName); + if (isDebug) { + console.log('contextualizer: ', contextualizerName); + } return result; } }