Created
February 4, 2025 04:03
-
-
Save ubinix-warun/263033ed55ee81855635a55d300ca677 to your computer and use it in GitHub Desktop.
convert track_walrus_events.py to ts style REF: https://github.com/MystenLabs/walrus-docs/blob/main/examples/python/track_walrus_events.py
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
system_object: 0x98ebc47370603fe81d9e15491b2f1443d619d1dab720d586e429ed233e1255c1 | |
staking_object: 0x20266a17b4f1a216727f3eef5772f8d486a9e3b5e319af80a5b75809c035561d | |
exchange_objects: | |
- 0x59ab926eb0d94d0d6d6139f11094ea7861914ad2ecffc7411529c60019133997 | |
- 0x89127f53890840ab6c52fca96b4a5cf853d7de52318d236807ad733f976eef7b | |
- 0x9f9b4f113862e8b1a3591d7955fadd7c52ecc07cf24be9e3492ce56eb8087805 | |
- 0xb60118f86ecb38ec79e74586f1bb184939640911ee1d63a84138d080632ee28a |
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
// Track Walrus storage related events on the Sui blockchain | |
import axios from 'axios'; | |
import * as fs from 'fs'; | |
import * as path from 'path'; | |
import * as base64 from 'base64-js'; | |
// Configure these paths to match your system | |
// const FULL_NODE_URL = "https://fullnode.testnet.sui.io:443"; | |
// const PATH_TO_WALRUS = "../CONFIG/bin/walrus"; | |
const FULL_NODE_URL = 'https://fullnode.testnet.sui.io:443'; | |
const PATH_TO_WALRUS_CONFIG = "./config/client_config.yaml"; | |
// Convert a numeric (u256) blob_id to a base64 encoded Blob ID | |
function numToBlobId(blobIdNum: number): string { | |
const extractedBytes: number[] = []; | |
for (let i = 0; i < 32; i++) { | |
extractedBytes.push(blobIdNum & 0xFF); | |
blobIdNum = blobIdNum >> 8; | |
} | |
if (blobIdNum !== 0) { | |
throw new Error('blobIdNum should be zero after extraction'); | |
} | |
const blobIdBytes = new Uint8Array(extractedBytes); | |
const encoded = base64.fromByteArray(blobIdBytes); | |
return encoded.replace(/=+$/, ''); | |
} | |
const configPath = path.resolve(PATH_TO_WALRUS_CONFIG); | |
const configContent = fs.readFileSync(configPath, 'utf-8'); | |
const match = configContent.match(/system_object:[ ]*(.*)/); | |
if (!match) { | |
throw new Error('system_object not found in config'); | |
} | |
const systemObjectId = match[1]; | |
console.log(`System object ID: ${systemObjectId}`); | |
// Query the Walrus system object on Sui | |
const request1 = { | |
jsonrpc: "2.0", | |
id: 1, | |
method: "sui_getObject", | |
params: [ | |
systemObjectId, | |
{ | |
showType: true, | |
showOwner: false, | |
showPreviousTransaction: false, | |
showDisplay: false, | |
showContent: false, | |
showBcs: false, | |
showStorageRebate: false, | |
}, | |
], | |
}; | |
axios.post(FULL_NODE_URL, request1) | |
.then(response1 => { | |
if (response1.status !== 200) throw new Error('Failed to fetch system object'); | |
console.log(response1.data.result) | |
const systemObjectContent = response1.data.result.data; | |
const walrusPackage = systemObjectContent.type.match(/(0x[0-9a-f]+)::system/)[1]; | |
console.log(`Walrus type: ${walrusPackage}`); | |
// Query events for the appropriate Walrus type | |
const request2 = { | |
jsonrpc: "2.0", | |
id: 1, | |
method: "suix_queryEvents", | |
params: [ | |
{ MoveModule: { package: walrusPackage, module: "blob" } }, | |
null, | |
100, | |
true, // Indicates descending order | |
], | |
}; | |
return axios.post(FULL_NODE_URL, request2); | |
}) | |
.then(response2 => { | |
if (response2.status !== 200) throw new Error('Failed to fetch events'); | |
console.log(response2.data.result) | |
const events = response2.data.result.data; | |
events.forEach((event: { id: { txDigest: any; }; type: string | any[]; parsedJson: any; timestampMs: string; }) => { | |
// Parse the Walrus event | |
const txDigest = event.id.txDigest; | |
const eventType = event.type.slice(68 + 13); // Skip the package & module prefix | |
const parsedEvent = event.parsedJson; | |
const blobId = numToBlobId(parseInt(parsedEvent.blob_id)); | |
const timestampMs = parseInt(event.timestampMs); | |
const timeDate = new Date(timestampMs); | |
// For registered blobs get their size in bytes | |
const size = eventType === "BlobRegistered" ? `${parsedEvent.size}B` : ""; | |
if (typeof eventType === "string") { | |
console.log(`${timeDate} ${eventType.padEnd(15)} ${size.padStart(10)} ${blobId} Tx:${txDigest.padEnd(48)}`); | |
} | |
}); | |
}) | |
.catch(error => { | |
console.error('Error:', error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment