-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support event parsing in shank (#31)
* feat: support event parsing in shank * docs(changeset): support shank event parsing * build: bump up kinobi-lite
- Loading branch information
1 parent
7a523e4
commit 30f749f
Showing
10 changed files
with
200 additions
and
20 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@solanafm/explorer-kit": patch | ||
"@solanafm/explorer-kit-idls": patch | ||
--- | ||
|
||
support shank event parsing |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { createAnchorEventParser } from "./anchor"; | ||
export { createShankEventParser } from "./shank"; |
60 changes: 60 additions & 0 deletions
60
packages/explorerkit-translator/src/parsers/v2/event/shank.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Idl } from "@solanafm/kinobi-lite"; | ||
import { convertBNToNumberInObject } from "@solanafm/utils"; | ||
|
||
import { EventParserInterface, ParserOutput, ParserType } from "../../.."; | ||
import { mapDataTypeToName } from "../../../helpers/idl"; | ||
import { KinobiTreeGenerator } from "../../../helpers/KinobiTreeGenerator"; | ||
import { IdlItem } from "../../../types/IdlItem"; | ||
import { FMShankSerializer, KinobiTreeGeneratorType } from "../../../types/KinobiTreeGenerator"; | ||
|
||
export const createShankEventParser: (idlItem: IdlItem) => EventParserInterface = (idlItem: IdlItem) => { | ||
const idl = idlItem.idl as Idl; | ||
const eventsLayout = new KinobiTreeGenerator(idl).constructLayout(KinobiTreeGeneratorType.EVENTS); | ||
|
||
const parseEvents = (eventData: string, mapTypes?: boolean): ParserOutput => { | ||
try { | ||
if (eventsLayout) { | ||
let dataBuffer: Buffer = Buffer.from(eventData, "base64"); | ||
let eventSerializer: FMShankSerializer | undefined = undefined; | ||
if (dataBuffer.byteLength > 0) { | ||
// Let's assume all the events have enums as u8 for now, if we need to support other discriminants we will need to add them from Kinobi | ||
const borshDiscriminant = Buffer.from(dataBuffer).readUint8(0); | ||
eventSerializer = eventsLayout.get(borshDiscriminant); | ||
} | ||
|
||
if (eventSerializer) { | ||
const decodedEventData = eventSerializer.serializer?.deserialize(dataBuffer); | ||
|
||
if (decodedEventData && decodedEventData[0]) { | ||
const filteredIdlEvent = | ||
idl.events?.filter( | ||
(event) => event.name.toLowerCase() === eventSerializer.instructionName.toLowerCase() | ||
) ?? []; | ||
|
||
if (mapTypes) { | ||
decodedEventData[0] = mapDataTypeToName(decodedEventData[0], filteredIdlEvent[0]?.fields); | ||
} | ||
|
||
decodedEventData[0] = convertBNToNumberInObject(decodedEventData[0]); | ||
|
||
return { | ||
name: eventSerializer.instructionName, | ||
data: convertBNToNumberInObject(decodedEventData[0]), | ||
type: ParserType.EVENT, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
} | ||
}; | ||
|
||
return { | ||
eventsLayout, | ||
parseEvents, | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ export enum KinobiTreeGeneratorType { | |
ACCOUNTS, | ||
INSTRUCTIONS, | ||
TYPES, | ||
EVENTS, | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.