-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from Once-Upon/benguyen0214/ou-1756-create-a-…
…grabblock-command-in-context-repo-in-1x-branch-that Create a grabblock command in context repo in 1x branch that
- Loading branch information
Showing
6 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.