From 2ed2579251a0c9a893ef31dc15e3e5db1ef8c85b Mon Sep 17 00:00:00 2001 From: Fraz Arshad Date: Thu, 13 Jun 2024 20:55:01 +0500 Subject: [PATCH 1/3] feat: added handler to index MsgInstallBundle --- project.ts | 14 ++++++++++++++ schema.graphql | 10 ++++++++++ src/mappings/mappingHandlers.ts | 27 +++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/project.ts b/project.ts index a7a42050..2a2128bf 100644 --- a/project.ts +++ b/project.ts @@ -46,6 +46,13 @@ const project: CosmosProject = { file: "./proto/cosmos/gov/v1beta1/gov.proto", messages: ["WeightedVoteOption"], }, + ], + [ + "/agoric.swingset.MsgInstallBundle", + { + file: "./proto/agoric/swingset/msgs.proto", + messages: ["MsgInstallBundle"], + }, ], ]), }, @@ -107,6 +114,13 @@ const project: CosmosProject = { type: "state_change", }, }, + { + handler: 'handleBundleInstallMessage', + kind: CosmosHandlerKind.Message, + filter: { + type: '/agoric.swingset.MsgInstallBundle', + }, + }, ], }, }, diff --git a/schema.graphql b/schema.graphql index 0fd219c6..4596de9a 100644 --- a/schema.graphql +++ b/schema.graphql @@ -303,3 +303,13 @@ type VaultStatesDaily @entity { liquidated: BigInt! liquidatedClosed: BigInt! } + +type BundleInstall @entity { + id: ID! + blockHeight: BigInt! + blockTime: Date! + uncompressedSize: BigInt! + bundle: String! + compressedBundle: String! + submitter: String! +} diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index c97d6167..35bd1aa4 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -1,5 +1,5 @@ -import { StateChangeEvent, IBCChannel, IBCTransfer, TransferType } from '../types'; -import { CosmosEvent } from '@subql/types-cosmos'; +import { StateChangeEvent, IBCChannel, IBCTransfer, TransferType, BundleInstall } from '../types'; +import { CosmosEvent, CosmosMessage } from '@subql/types-cosmos'; import { b64decode, extractStoragePath, @@ -128,6 +128,29 @@ export async function handleIbcReceivePacketEvent(cosmosEvent: CosmosEvent): Pro await Promise.allSettled([transferRecord.save(), ibcChannel]); } +export async function handleBundleInstallMessage(message: CosmosMessage): Promise { + const { msg, block, tx } = message; + + if (msg.typeUrl !== '/agoric.swingset.MsgInstallBundle') { + logger.warn('message type is not /agoric.swingset.MsgInstallBundle'); + return; + } + + // JSON.stringify converts the object from Uint8Array to readable string + const { uncompressedSize, compressedBundle, submitter, bundle } = JSON.parse(JSON.stringify(msg.decodedMsg)); + const bundleRecord = new BundleInstall( + tx.hash, + BigInt(block.header.height), + block.header.time as any, + BigInt(uncompressedSize), + bundle || '', + compressedBundle || '', + submitter, + ); + + await bundleRecord.save(); +} + export async function handleStateChangeEvent(cosmosEvent: CosmosEvent): Promise { const { event, block } = cosmosEvent; From fb833ea04024f79371a3856288c260a398bc97e5 Mon Sep 17 00:00:00 2001 From: Fraz Arshad Date: Thu, 13 Jun 2024 21:17:22 +0500 Subject: [PATCH 2/3] fix: updated logic for handling submitter address --- src/mappings/mappingHandlers.ts | 5 +++-- src/mappings/utils.ts | 11 ++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index 35bd1aa4..86e821f4 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -6,6 +6,7 @@ import { getStateChangeModule, resolveBrandNamesAndValues, getEscrowAddress, + getAddressFromUint8Array, } from './utils'; import { @@ -137,7 +138,7 @@ export async function handleBundleInstallMessage(message: CosmosMessage): Promis } // JSON.stringify converts the object from Uint8Array to readable string - const { uncompressedSize, compressedBundle, submitter, bundle } = JSON.parse(JSON.stringify(msg.decodedMsg)); + const { uncompressedSize, compressedBundle, bundle } = JSON.parse(JSON.stringify(msg.decodedMsg)); const bundleRecord = new BundleInstall( tx.hash, BigInt(block.header.height), @@ -145,7 +146,7 @@ export async function handleBundleInstallMessage(message: CosmosMessage): Promis BigInt(uncompressedSize), bundle || '', compressedBundle || '', - submitter, + getAddressFromUint8Array(msg.decodedMsg.submitter), ); await bundleRecord.save(); diff --git a/src/mappings/utils.ts b/src/mappings/utils.ts index e0a05639..005f1eb8 100644 --- a/src/mappings/utils.ts +++ b/src/mappings/utils.ts @@ -101,10 +101,15 @@ export function dateToDayKey(timestamp: any): number { export const getEscrowAddress = (port: string, channel: string) => { const version = 'ics20-1'; - const chainPrefix = 'agoric'; const stringtoSha = Buffer.from([...Buffer.from(version), 0, ...Buffer.from(`${port}/${channel}`)]); const shaHash = sha256.sha256.array(stringtoSha.toString()); - const bechWords = bech32.toWords(shaHash.slice(0, 20)); - const address = bech32.encode(chainPrefix, bechWords); + const address = getAddressFromUint8Array(shaHash.slice(0, 20)); return address; }; + +export const getAddressFromUint8Array = (uint8Array: Array, chainPrefix: string = 'agoric') => { + const words = bech32.toWords(uint8Array); + const bech32String = bech32.encode(chainPrefix, words); + + return bech32String; +}; From 1a4f12b177832d0f77d964a244b37797a73df029 Mon Sep 17 00:00:00 2001 From: Fraz Arshad Date: Fri, 14 Jun 2024 12:15:42 +0500 Subject: [PATCH 3/3] refactor: type change for util function --- src/mappings/utils.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mappings/utils.ts b/src/mappings/utils.ts index 005f1eb8..7223238e 100644 --- a/src/mappings/utils.ts +++ b/src/mappings/utils.ts @@ -103,11 +103,12 @@ export const getEscrowAddress = (port: string, channel: string) => { const version = 'ics20-1'; const stringtoSha = Buffer.from([...Buffer.from(version), 0, ...Buffer.from(`${port}/${channel}`)]); const shaHash = sha256.sha256.array(stringtoSha.toString()); - const address = getAddressFromUint8Array(shaHash.slice(0, 20)); + const hashArray = new Uint8Array(shaHash.slice(0, 20)); + const address = getAddressFromUint8Array(hashArray); return address; }; -export const getAddressFromUint8Array = (uint8Array: Array, chainPrefix: string = 'agoric') => { +export const getAddressFromUint8Array = (uint8Array: Uint8Array, chainPrefix: string = 'agoric') => { const words = bech32.toWords(uint8Array); const bech32String = bech32.encode(chainPrefix, words);