Created
March 8, 2024 03:20
-
-
Save tubackkhoa/509b9d0a0a334d54ad4a9f3c4e6d3361 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { Registry, decodeTxRaw } from '@cosmjs/proto-signing'; | |
| import { TextProposal } from 'cosmjs-types/cosmos/gov/v1beta1/gov'; | |
| import { fromAscii } from '@cosmjs/encoding'; | |
| import { | |
| defaultRegistryTypes as defaultStargateTypes, | |
| Event, | |
| logs, | |
| StargateClient, | |
| } from '@cosmjs/stargate'; | |
| import { JsonObject, fromBinary, wasmTypes } from '@cosmjs/cosmwasm-stargate'; | |
| export const customRegistry = new Registry([ | |
| ...defaultStargateTypes, | |
| ...wasmTypes, | |
| ]); | |
| customRegistry.register('/cosmos.gov.v1beta1.TextProposal', TextProposal); | |
| export const decodeProto = (value: JsonObject) => { | |
| const typeUrl = value.type_url || value.typeUrl; | |
| if (typeUrl) { | |
| // decode proto | |
| return decodeProto(customRegistry.decode({ typeUrl, value: value.value })); | |
| } | |
| for (const k in value) { | |
| if (typeof value[k] === 'string') { | |
| try { | |
| value[k] = fromBinary(value[k]); | |
| } catch {} | |
| } | |
| if (typeof value[k] === 'object') value[k] = decodeProto(value[k]); | |
| } | |
| if (value.msg instanceof Uint8Array) | |
| value.msg = JSON.parse(fromAscii(value.msg)); | |
| return value; | |
| }; | |
| const parseWasmEvents = (events: readonly Event[]) => { | |
| const wasmEvents = events.filter((e) => e.type.startsWith('wasm')); | |
| const attrs = []; | |
| for (const wasmEvent of wasmEvents) { | |
| let attr: { [key: string]: string }; | |
| for (const { key, value } of wasmEvent.attributes) { | |
| if (key === '_contract_address') { | |
| if (attr) attrs.push(attr); | |
| attr = {}; | |
| } | |
| attr[key] = value; | |
| } | |
| attrs.push(attr); | |
| } | |
| return attrs; | |
| }; | |
| (async () => { | |
| const client = await StargateClient.connect('wss://rpc.orai.io'); | |
| const { rawLog, tx } = await client.getTx( | |
| '9B435E4014DEBA5AB80D4BB8F52D766A6C14BFCAC21F821CDB96F4ABB4E29B17' | |
| ); | |
| const { body } = decodeTxRaw(tx); | |
| const messages = body.messages.map(decodeProto); | |
| const logs: logs.Log[] = JSON.parse(rawLog); | |
| const data = logs.map((log) => { | |
| const index = log.msg_index ?? 0; | |
| const attrs = parseWasmEvents(log.events); | |
| return { attrs, msg: messages[index] }; | |
| }); | |
| console.dir(data, { depth: null }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment