Skip to content

Commit

Permalink
Merge pull request #27 from Agoric/feat/bundle-install-indexing
Browse files Browse the repository at this point in the history
Feat: MsgInstallBundle indexing
  • Loading branch information
frazarshad authored Jun 14, 2024
2 parents f4a7b8c + 1a4f12b commit 6d29d5d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
14 changes: 14 additions & 0 deletions project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
],
]),
},
Expand Down Expand Up @@ -107,6 +114,13 @@ const project: CosmosProject = {
type: "state_change",
},
},
{
handler: 'handleBundleInstallMessage',
kind: CosmosHandlerKind.Message,
filter: {
type: '/agoric.swingset.MsgInstallBundle',
},
},
],
},
},
Expand Down
10 changes: 10 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}
28 changes: 26 additions & 2 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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,
getStateChangeModule,
resolveBrandNamesAndValues,
getEscrowAddress,
getAddressFromUint8Array,
} from './utils';

import {
Expand Down Expand Up @@ -128,6 +129,29 @@ export async function handleIbcReceivePacketEvent(cosmosEvent: CosmosEvent): Pro
await Promise.allSettled([transferRecord.save(), ibcChannel]);
}

export async function handleBundleInstallMessage(message: CosmosMessage): Promise<void> {
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, 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 || '',
getAddressFromUint8Array(msg.decodedMsg.submitter),
);

await bundleRecord.save();
}

export async function handleStateChangeEvent(cosmosEvent: CosmosEvent): Promise<void> {
const { event, block } = cosmosEvent;

Expand Down
12 changes: 9 additions & 3 deletions src/mappings/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ 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 hashArray = new Uint8Array(shaHash.slice(0, 20));
const address = getAddressFromUint8Array(hashArray);
return address;
};

export const getAddressFromUint8Array = (uint8Array: Uint8Array, chainPrefix: string = 'agoric') => {
const words = bech32.toWords(uint8Array);
const bech32String = bech32.encode(chainPrefix, words);

return bech32String;
};

0 comments on commit 6d29d5d

Please sign in to comment.