-
Notifications
You must be signed in to change notification settings - Fork 70
Add an instruction plan to transfer to an ATA #111
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './generated'; | ||
| export * from './createMint'; | ||
| export * from './mintToATA'; | ||
| export * from './transferToATA'; |
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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { | ||
| InstructionPlan, | ||
| sequentialInstructionPlan, | ||
| Address, | ||
| TransactionSigner, | ||
| } from '@solana/kit'; | ||
| import { | ||
| findAssociatedTokenPda, | ||
| getCreateAssociatedTokenIdempotentInstruction, | ||
| getTransferCheckedInstruction, | ||
| TOKEN_PROGRAM_ADDRESS, | ||
| } from './generated'; | ||
|
|
||
| type TransferToATAInstructionPlanInput = { | ||
| /** Funding account (must be a system account). */ | ||
| payer: TransactionSigner; | ||
| /** The token mint to transfer. */ | ||
| mint: Address; | ||
| /** The source account for the transfer. */ | ||
| source: Address; | ||
| /** The source account's owner/delegate or its multisignature account. */ | ||
| authority: Address | TransactionSigner; | ||
| /** Associated token account address to transfer to. | ||
| * Will be created if it does not already exist. | ||
| * Note: Use {@link getTransferToATAInstructionPlanAsync} instead to derive this automatically. | ||
| * Note: Use {@link findAssociatedTokenPda} to derive the associated token account address. | ||
| */ | ||
| destination: Address; | ||
| /** Wallet address for the destination. */ | ||
| recipient: Address; | ||
| /** The amount of tokens to transfer. */ | ||
| amount: number | bigint; | ||
| /** Expected number of base 10 digits to the right of the decimal place. */ | ||
| decimals: number; | ||
| multiSigners?: Array<TransactionSigner>; | ||
| }; | ||
|
|
||
| type TransferToATAInstructionPlanConfig = { | ||
| systemProgram?: Address; | ||
| tokenProgram?: Address; | ||
| associatedTokenProgram?: Address; | ||
| }; | ||
|
|
||
| export function getTransferToATAInstructionPlan( | ||
| input: TransferToATAInstructionPlanInput, | ||
| config?: TransferToATAInstructionPlanConfig | ||
| ): InstructionPlan { | ||
| return sequentialInstructionPlan([ | ||
| getCreateAssociatedTokenIdempotentInstruction( | ||
| { | ||
| payer: input.payer, | ||
| ata: input.destination, | ||
| owner: input.recipient, | ||
| mint: input.mint, | ||
| systemProgram: config?.systemProgram, | ||
| tokenProgram: config?.tokenProgram, | ||
| }, | ||
| { | ||
| programAddress: config?.associatedTokenProgram, | ||
| } | ||
| ), | ||
| getTransferCheckedInstruction( | ||
| { | ||
| source: input.source, | ||
| mint: input.mint, | ||
| destination: input.destination, | ||
| authority: input.authority, | ||
| amount: input.amount, | ||
| decimals: input.decimals, | ||
| multiSigners: input.multiSigners, | ||
| }, | ||
| { | ||
| programAddress: config?.tokenProgram, | ||
| } | ||
| ), | ||
| ]); | ||
| } | ||
|
|
||
| type TransferToATAInstructionPlanAsyncInput = Omit< | ||
| TransferToATAInstructionPlanInput, | ||
| 'destination' | ||
| >; | ||
|
|
||
| export async function getTransferToATAInstructionPlanAsync( | ||
| input: TransferToATAInstructionPlanAsyncInput, | ||
| config?: TransferToATAInstructionPlanConfig | ||
| ): Promise<InstructionPlan> { | ||
| const [ataAddress] = await findAssociatedTokenPda({ | ||
| owner: input.recipient, | ||
| tokenProgram: config?.tokenProgram ?? TOKEN_PROGRAM_ADDRESS, | ||
| mint: input.mint, | ||
| }); | ||
| return getTransferToATAInstructionPlan( | ||
| { | ||
| ...input, | ||
| destination: ataAddress, | ||
| }, | ||
| config | ||
| ); | ||
| } |
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
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.
If you make a
sendAndConfirmtest helper that accepts instruction plans (or expose it directly in the testClient), you could directly consume your own instruction plan helpers in the tests.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.
I've added
client.sendTransactionPlan(...)and used it here. Also refactored uses of the transaction executor I added before to use that instead.I looked at doing the same for planner, but it needs a payer. I think it'd make sense to add
payerto the client, and have it receive an airdrop automatically and then be the fee payer in the transaction planner. But I think that makes sense to do separately since it'll be a lot of unrelated changes.