Skip to content

Instantly share code, notes, and snippets.

@halink0803
Created September 27, 2022 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halink0803/f455263cb21e4161597943239e7c1ac8 to your computer and use it in GitHub Desktop.
Save halink0803/f455263cb21e4161597943239e7c1ac8 to your computer and use it in GitHub Desktop.
import { ethereum } from '@graphprotocol/graph-ts';
import { PubSub } from '@google-cloud/pubsub';
import { Block } from '../../generated/schema';
export function handleBlock(block: ethereum.Block): void {
let id = block.hash.toHex()
let blockEntity = new Block(id)
blockEntity.number = block.number
blockEntity.timestamp = block.timestamp
blockEntity.parentHash = block.parentHash.toHex()
blockEntity.author = block.author.toHex()
blockEntity.difficulty = block.difficulty
blockEntity.totalDifficulty = block.totalDifficulty
blockEntity.gasUsed = block.gasUsed
blockEntity.gasLimit = block.gasLimit
blockEntity.receiptsRoot = block.receiptsRoot.toHex()
blockEntity.transactionsRoot = block.transactionsRoot.toHex()
blockEntity.stateRoot = block.stateRoot.toHex()
blockEntity.size = block.size
blockEntity.unclesHash = block.unclesHash.toHex()
publishMessage();
blockEntity.save()
}
// Creates a client; cache this for further use
const pubSubClient = new PubSub;
const topicNameOrId = 'projects/halink0803-202409/topics/halink';
const data = JSON.stringify({ foo: 'bar' });
const publishMessage = async (): Promise<string> => {
// Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
const dataBuffer = Buffer.from(data)
try {
const messageId = await pubSubClient.topic(topicNameOrId).publishMessage({ data: dataBuffer })
console.log(`Message ${messageId} published.`)
return messageId;
} catch (error) {
let result:string;
if ( typeof error === 'string') {
result = error
console.error(`Received error while publishing: ${error}`)
} else if (error instanceof Error) {
console.error(`Received error while publishing: ${error.message}`);
result = error.message
}
process.exitCode = 1;
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment