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

Create a log workflow for identifying which contextualizer #343

Merged
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 31 additions & 0 deletions src/commands/detectContextualizers.ts
Original file line number Diff line number Diff line change
@@ -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 <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, true);
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
}
});
}
2 changes: 2 additions & 0 deletions src/commands/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -15,5 +16,6 @@ registerCreateContextualizerCommand();
registerGrabTransactionCommand();
registerRunContextualizersCommand();
registerGrabBlockCommand();
registerDetectContextualizersCommand();

program.parse(process.argv);
23 changes: 3 additions & 20 deletions src/commands/runContextualizers.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}`,
Expand All @@ -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');
Expand Down
10 changes: 10 additions & 0 deletions src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transaction[]> => {
Expand Down
16 changes: 12 additions & 4 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,20 @@ function formatSection(section: ContextSummaryVariableType) {
}

export const makeContextualize = (
children: Record<string, (transaction: Transaction) => Transaction>,
children: Record<
string,
(transaction: Transaction, isDebug: boolean) => Transaction
>,
) => {
return (transaction: Transaction): Transaction => {
for (const childContextualizer of Object.values(children)) {
const result = childContextualizer(transaction);
return (transaction: Transaction, isDebug = false): Transaction => {
for (const [contextualizerName, childContextualizer] of Object.entries(
children,
)) {
const result = childContextualizer(transaction, isDebug);
if (result.context?.summaries?.en.title) {
if (isDebug) {
console.log('contextualizer: ', contextualizerName);
}
return result;
}
}
Expand Down
Loading