Skip to content
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

Add a function to narrow an IInstruction to have a given program address #2234

Merged
merged 1 commit into from
Mar 4, 2024

Conversation

mcintyre94
Copy link
Contributor

@mcintyre94 mcintyre94 commented Mar 1, 2024

This is another function that is helpful for using generated clients.

The generated client includes a function to parse an IInstruction:

export function parseTransferSolInstruction<
  TProgram extends string,
  TAccountMetas extends readonly IAccountMeta[]
>(
  instruction: IInstruction<TProgram> & <snip>
): ParsedTransferSolInstruction<TProgram, TAccountMetas>

The problem is that if we pass an IInstruction from a deserialized transaction to this parse function, then it has TProgram = string, and we end up with ParsedTransferSolInstruction<TProgram = string>, ie we lose the known program address in the type. This can cause issues when we use this type and expect it to include a more specific type.

This PR adds a function to narrow an instruction type to include a known program address as TProgram. If we do this before calling eg this parse function, then we can maintain the program address in the type correctly.

Eg:

const instruction: IInstruction<string> // from eg a deserialized transaction
const address = '1234' as Address

if(instruction.programAddress === address) {
  const parsed = parseInstruction(instruction); // ParsedInstruction<string>
}

if(isInstructionWithKnownProgramAddress(instruction, address)) {
  const parsed = parseInstruction(instruction); // ParsedInstruction<'1234'>
}

packages/instructions/src/instruction.ts Outdated Show resolved Hide resolved
packages/errors/src/messages.ts Outdated Show resolved Hide resolved
@mcintyre94 mcintyre94 force-pushed the instruction-narrow-address branch from ca9fd72 to 01c7a0d Compare March 1, 2024 17:48
Copy link
Collaborator

@steveluscher steveluscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sort of prefer something like isInstructionForProgram rather than isInstructionWithKnownProgramAddress, but no big deal either way! I just think it's less clear what it means to be ‘with’ a program address.

@mcintyre94 mcintyre94 force-pushed the instruction-narrow-address branch from 01c7a0d to 1cd0162 Compare March 4, 2024 12:40
@mcintyre94 mcintyre94 force-pushed the instruction-narrow-address branch from 1cd0162 to f94276b Compare March 4, 2024 17:10
@mcintyre94 mcintyre94 merged commit fb655dd into master Mar 4, 2024
6 checks passed
@mcintyre94 mcintyre94 deleted the instruction-narrow-address branch March 4, 2024 17:21
Copy link
Contributor

github-actions bot commented Mar 7, 2024

🎉 This PR is included in version 1.91.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants