-
Notifications
You must be signed in to change notification settings - Fork 88
chore(js): Add plugin & instruction planner defaults #128
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
cb08667
328c15a
d5e9b97
e7f70ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,30 +1,49 @@ | ||||||
| import { ClientWithPayer, pipe } from '@solana/kit'; | ||||||
| import { addSelfPlanAndSendFunctions, SelfPlanAndSendFunctions } from '@solana/kit/program-client-core'; | ||||||
|
|
||||||
| import { CreateMintInstructionPlanInput, getCreateMintInstructionPlan } from './createMint'; | ||||||
| import { | ||||||
| CreateMintInstructionPlanConfig, | ||||||
| CreateMintInstructionPlanInput, | ||||||
| getCreateMintInstructionPlan, | ||||||
| } from './createMint'; | ||||||
| import { | ||||||
| TokenPlugin as GeneratedTokenPlugin, | ||||||
| TokenPluginInstructions as GeneratedTokenPluginInstructions, | ||||||
| TokenPluginRequirements as GeneratedTokenPluginRequirements, | ||||||
| tokenProgram as generatedTokenProgram, | ||||||
| } from './generated'; | ||||||
| import { getMintToATAInstructionPlan, MintToATAInstructionPlanInput } from './mintToATA'; | ||||||
| import { getTransferToATAInstructionPlan, TransferToATAInstructionPlanInput } from './transferToATA'; | ||||||
| import { | ||||||
| getMintToATAInstructionPlanAsync, | ||||||
| MintToATAInstructionPlanAsyncInput, | ||||||
| MintToATAInstructionPlanConfig, | ||||||
| } from './mintToATA'; | ||||||
| import { | ||||||
| getTransferToATAInstructionPlanAsync, | ||||||
| TransferToATAInstructionPlanAsyncInput, | ||||||
| TransferToATAInstructionPlanConfig, | ||||||
| } from './transferToATA'; | ||||||
| import { MakeOptional } from './types'; | ||||||
|
|
||||||
| export type TokenPluginRequirements = GeneratedTokenPluginRequirements & ClientWithPayer; | ||||||
|
|
||||||
| export type TokenPlugin = Omit<GeneratedTokenPlugin, 'instructions'> & { instructions: TokenPluginInstructions }; | ||||||
|
|
||||||
| export type TokenPluginInstructions = GeneratedTokenPluginInstructions & { | ||||||
| /** Create a new token mint. */ | ||||||
| createMint: ( | ||||||
| input: MakeOptional<CreateMintInstructionPlanInput, 'payer'>, | ||||||
| input: MakeOptional<CreateMintInstructionPlanInput, 'payer' | 'mintAuthority'>, | ||||||
| config?: CreateMintInstructionPlanConfig, | ||||||
| ) => ReturnType<typeof getCreateMintInstructionPlan> & SelfPlanAndSendFunctions; | ||||||
| /** Mint tokens to an owner's ATA (created if needed). */ | ||||||
| mintToATA: ( | ||||||
| input: MakeOptional<MintToATAInstructionPlanInput, 'payer'>, | ||||||
| ) => ReturnType<typeof getMintToATAInstructionPlan> & SelfPlanAndSendFunctions; | ||||||
| input: MakeOptional<MintToATAInstructionPlanAsyncInput, 'payer' | 'mintAuthority'>, | ||||||
| config?: MintToATAInstructionPlanConfig, | ||||||
| ) => Promise<Awaited<ReturnType<typeof getMintToATAInstructionPlanAsync>>> & SelfPlanAndSendFunctions; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these can just be:
Suggested change
Because |
||||||
| /** Transfer tokens to a recipient's ATA (created if needed). */ | ||||||
| transferToATA: ( | ||||||
| input: MakeOptional<TransferToATAInstructionPlanInput, 'payer'>, | ||||||
| ) => ReturnType<typeof getTransferToATAInstructionPlan> & SelfPlanAndSendFunctions; | ||||||
| input: MakeOptional<TransferToATAInstructionPlanAsyncInput, 'payer' | 'authority'>, | ||||||
| config?: TransferToATAInstructionPlanConfig, | ||||||
| ) => Promise<Awaited<ReturnType<typeof getTransferToATAInstructionPlanAsync>>> & SelfPlanAndSendFunctions; | ||||||
| }; | ||||||
|
|
||||||
| export function tokenProgram() { | ||||||
|
|
@@ -35,25 +54,44 @@ export function tokenProgram() { | |||||
| ...c.token, | ||||||
| instructions: { | ||||||
| ...c.token.instructions, | ||||||
| createMint: input => | ||||||
| createMint: (input, config) => | ||||||
| addSelfPlanAndSendFunctions( | ||||||
| client, | ||||||
| getCreateMintInstructionPlan({ ...input, payer: input.payer ?? client.payer }), | ||||||
| getCreateMintInstructionPlan( | ||||||
| { | ||||||
| ...input, | ||||||
| payer: input.payer ?? client.payer, | ||||||
| mintAuthority: input.mintAuthority ?? client.payer.address, | ||||||
|
||||||
| }, | ||||||
| config, | ||||||
| ), | ||||||
| ), | ||||||
| mintToATA: input => | ||||||
| mintToATA: (input, config) => | ||||||
| addSelfPlanAndSendFunctions( | ||||||
| client, | ||||||
| getMintToATAInstructionPlan({ ...input, payer: input.payer ?? client.payer }), | ||||||
| getMintToATAInstructionPlanAsync( | ||||||
| { | ||||||
| ...input, | ||||||
| payer: input.payer ?? client.payer, | ||||||
| mintAuthority: input.mintAuthority ?? client.payer, | ||||||
| }, | ||||||
| config, | ||||||
| ), | ||||||
| ), | ||||||
| transferToATA: input => | ||||||
| transferToATA: (input, config) => | ||||||
| addSelfPlanAndSendFunctions( | ||||||
| client, | ||||||
| getTransferToATAInstructionPlan({ ...input, payer: input.payer ?? client.payer }), | ||||||
| getTransferToATAInstructionPlanAsync( | ||||||
| { | ||||||
| ...input, | ||||||
| payer: input.payer ?? client.payer, | ||||||
| authority: input.authority ?? client.payer, | ||||||
| }, | ||||||
| config, | ||||||
| ), | ||||||
| ), | ||||||
| }, | ||||||
| }, | ||||||
| })); | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import { | |
| getTransferCheckedInstruction, | ||
| TOKEN_PROGRAM_ADDRESS, | ||
| } from './generated'; | ||
| import { MakeOptional } from './types'; | ||
|
|
||
| export type TransferToATAInstructionPlanInput = { | ||
| /** Funding account (must be a system account). */ | ||
|
|
@@ -30,7 +31,7 @@ export type TransferToATAInstructionPlanInput = { | |
| multiSigners?: Array<TransactionSigner>; | ||
| }; | ||
|
|
||
| type TransferToATAInstructionPlanConfig = { | ||
| export type TransferToATAInstructionPlanConfig = { | ||
| systemProgram?: Address; | ||
| tokenProgram?: Address; | ||
| associatedTokenProgram?: Address; | ||
|
|
@@ -71,21 +72,44 @@ export function getTransferToATAInstructionPlan( | |
| ]); | ||
| } | ||
|
|
||
| type TransferToATAInstructionPlanAsyncInput = Omit<TransferToATAInstructionPlanInput, 'destination'>; | ||
| export type TransferToATAInstructionPlanAsyncInput = MakeOptional< | ||
| TransferToATAInstructionPlanInput, | ||
| 'source' | '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, | ||
| }); | ||
| const tokenProgram = config?.tokenProgram ?? TOKEN_PROGRAM_ADDRESS; | ||
|
|
||
| const destinationAta = | ||
| input.destination ?? | ||
| ( | ||
| await findAssociatedTokenPda({ | ||
| owner: input.recipient, | ||
| tokenProgram, | ||
| mint: input.mint, | ||
| }) | ||
| )[0]; | ||
|
Comment on lines
+86
to
+94
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Let's match the style of the |
||
|
|
||
| let source = input.source; | ||
| if (!source) { | ||
| const authorityAddress: Address = | ||
| typeof input.authority === 'string' ? input.authority : input.authority.address; | ||
| const [sourceAta] = await findAssociatedTokenPda({ | ||
| owner: authorityAddress, | ||
| tokenProgram, | ||
| mint: input.mint, | ||
| }); | ||
| source = sourceAta; | ||
| } | ||
|
|
||
| return getTransferToATAInstructionPlan( | ||
| { | ||
| ...input, | ||
| destination: ataAddress, | ||
| source, | ||
| destination: destinationAta, | ||
| }, | ||
| config, | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; |
Uh oh!
There was an error while loading. Please reload this page.