Skip to content

Commit

Permalink
Merge pull request #275 from Once-Upon/benguyen0214/ou-1756-create-a-…
Browse files Browse the repository at this point in the history
…grabblock-command-in-context-repo-in-1x-branch-that

Create a grabblock command in context repo in 1x branch that
  • Loading branch information
pcowgill authored Apr 1, 2024
2 parents 8403c48 + 4fa2703 commit 145bbfa
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 2 deletions.
91 changes: 91 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"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"
"run:contextualizers": "npm run build && node ./dist/commands/main.js run-contextualizers",
"grab:block": "npm run build && node ./dist/commands/main.js grab-block"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -58,6 +59,7 @@
"testEnvironment": "node"
},
"dependencies": {
"axios": "^1.6.8",
"commander": "^11.1.0",
"dotenv": "^16.3.1",
"handlebars": "^4.7.8",
Expand Down
70 changes: 70 additions & 0 deletions src/commands/grabBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as path from 'path';
import * as fs from 'fs';
import * as zlib from 'zlib';
import axios from 'axios';
import { program } from './main';

export function registerGrabBlockCommand() {
program
.command('grab-block')
.description('Grab a block from API and decode it')
.argument('<blockNumber>', 'block number')
.argument('<network>', 'network name')
.action(async (blockNumber, network, options) => {
const dirPath = path.join(
__dirname,
'..',
'..',
'src',
'transformers',
'test',
'blocks',
network,
);
const txFilePath = path.join(dirPath, `${blockNumber}.json`);
const gzFilePath = path.join(dirPath, `${blockNumber}.json.gz`);
const decodedFilePath = path.join(dirPath, `${blockNumber}_decoded.json`);

const storageUrl = `https://storage.googleapis.com/indexingco_heartbeats/${network}/${blockNumber}.json.gz`;
const decodeUrl = `https://decode.onceupon.xyz`;

try {
console.log(
`Fetching block from Google Storage: ${network}/${blockNumber}`,
);
const response = await axios({
method: 'get',
url: storageUrl,
responseType: 'stream',
});

const writer = fs.createWriteStream(gzFilePath);
response.data.pipe(writer);
await new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});

console.log(`Decompressing: ${gzFilePath}`);
const decompressed = zlib.gunzipSync(fs.readFileSync(gzFilePath));
fs.writeFileSync(txFilePath, decompressed);

console.log(`Sending data to decode service`);
const decodedData = await axios.post(decodeUrl, decompressed, {
headers: { 'Content-Type': 'application/json' },
});

fs.writeFileSync(decodedFilePath, JSON.stringify(decodedData.data));
console.log(`Decoded block saved to ${decodedFilePath}`);

// Remove the gz file and the original json file after saving the decoded data
fs.unlinkSync(gzFilePath);
fs.unlinkSync(txFilePath);

process.exit(0); // Successful exit
} catch (error) {
console.error('Failed to grab and decode the block:', 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 @@ -2,6 +2,7 @@ import { Command } from 'commander';
import { registerCreateContextualizerCommand } from './createContextualizer';
import { registerGrabTransactionCommand } from './grabTransaction';
import { registerRunContextualizersCommand } from './runContextualizers';
import { registerGrabBlockCommand } from './grabBlock';

export const program = new Command();

Expand All @@ -13,5 +14,6 @@ program
registerCreateContextualizerCommand();
registerGrabTransactionCommand();
registerRunContextualizersCommand();
registerGrabBlockCommand();

program.parse(process.argv);
2 changes: 1 addition & 1 deletion src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { shortenTxHash } from '../helpers/utils';
import { Transaction } from '../types';

export const grabTx = async (txHash: string, prefix: string) => {
const srcDir = path.join(__dirname, '..', '..', 'src');
const srcDir = path.join(__dirname, '..', '..', 'src', 'contextualizers');

const txHashShorten = shortenTxHash(txHash);
const fileName = prefix + '-' + txHashShorten;
Expand Down

Large diffs are not rendered by default.

0 comments on commit 145bbfa

Please sign in to comment.