-
Notifications
You must be signed in to change notification settings - Fork 1
Support decoding codama program events #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,9 @@ import { | |||||||||||||||||||||||||||||||||||||||||||||
| isAnchorIdlV01, | ||||||||||||||||||||||||||||||||||||||||||||||
| getInstructionDiscriminatorBytes, | ||||||||||||||||||||||||||||||||||||||||||||||
| findInstructionDiscriminatorByName, | ||||||||||||||||||||||||||||||||||||||||||||||
| getDiscriminator, | ||||||||||||||||||||||||||||||||||||||||||||||
| isRootNode, | ||||||||||||||||||||||||||||||||||||||||||||||
| isAnchorIdl, | ||||||||||||||||||||||||||||||||||||||||||||||
| } from '@subql/common-solana'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { getLogger } from '@subql/node-core'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -21,7 +24,14 @@ import { | |||||||||||||||||||||||||||||||||||||||||||||
| } from '@subql/types-solana'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { isHex } from '@subql/utils'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import bs58 from 'bs58'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { camelCase, DefinedTypeNode, InstructionNode, RootNode } from 'codama'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||
| camelCase, | ||||||||||||||||||||||||||||||||||||||||||||||
| DefinedTypeNode, | ||||||||||||||||||||||||||||||||||||||||||||||
| InstructionNode, | ||||||||||||||||||||||||||||||||||||||||||||||
| pascalCase, | ||||||||||||||||||||||||||||||||||||||||||||||
| RootNode, | ||||||||||||||||||||||||||||||||||||||||||||||
| titleCase, | ||||||||||||||||||||||||||||||||||||||||||||||
| } from 'codama'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { Memoize } from '../utils/decorators'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { allAccounts, getProgramId } from './utils.solana'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -99,14 +109,36 @@ export function decodeInstruction( | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Codama doesn't support Logs so extra work is required to decode logs | ||||||||||||||||||||||||||||||||||||||||||||||
| export function decodeLog(idl: Idl, message: string): DecodedData | null { | ||||||||||||||||||||||||||||||||||||||||||||||
| const msgData = message.replace('Program data: ', ''); | ||||||||||||||||||||||||||||||||||||||||||||||
| const msgBuffer = basedToBuffer(msgData); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Codama IDL, doesn't include events in the IDL but it should decoded to a definedType in the IDL | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!isAnchorIdl(idl)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (msgBuffer.length < 8) { | ||||||||||||||||||||||||||||||||||||||||||||||
| // Not enough data for a discriminator | ||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Split the discriminator and data | ||||||||||||||||||||||||||||||||||||||||||||||
| const logDisc = msgBuffer.subarray(0, 8); | ||||||||||||||||||||||||||||||||||||||||||||||
| const data = msgBuffer.subarray(8); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Attempt to find the matching type by discriminator and decode the data | ||||||||||||||||||||||||||||||||||||||||||||||
| return decodeData(idl, data.toString('base64'), (root) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| return root.program.definedTypes.find( | ||||||||||||||||||||||||||||||||||||||||||||||
| (t) => | ||||||||||||||||||||||||||||||||||||||||||||||
| // Warning this is fragile as there can be various casings of the type names, but because it requires hashing data we can only go one way | ||||||||||||||||||||||||||||||||||||||||||||||
| logDisc.indexOf(getDiscriminator(t.name)) === 0 || | ||||||||||||||||||||||||||||||||||||||||||||||
| logDisc.indexOf(getDiscriminator(pascalCase(t.name))) === 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+128
to
+133
|
||||||||||||||||||||||||||||||||||||||||||||||
| return root.program.definedTypes.find( | |
| (t) => | |
| // Warning this is fragile as there can be various casings of the type names, but because it requires hashing data we can only go one way | |
| logDisc.indexOf(getDiscriminator(t.name)) === 0 || | |
| logDisc.indexOf(getDiscriminator(pascalCase(t.name))) === 0, | |
| ); | |
| // Build a map from all possible discriminators to their type nodes, covering common casing conventions | |
| const discriminatorToType = new Map<string, typeof root.program.definedTypes[0]>(); | |
| for (const t of root.program.definedTypes) { | |
| const nameVariants = [ | |
| t.name, | |
| pascalCase(t.name), | |
| camelCase(t.name), | |
| titleCase(t.name), | |
| ]; | |
| for (const variant of nameVariants) { | |
| const disc = getDiscriminator(variant); | |
| discriminatorToType.set(disc.toString('hex'), t); | |
| } | |
| } | |
| const logDiscHex = logDisc.toString('hex'); | |
| return discriminatorToType.get(logDiscHex); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import is correctly using the 'node:' prefix for the built-in crypto module, which is good practice for Node.js 16+.