-
Notifications
You must be signed in to change notification settings - Fork 65
Add an instruction plan for creating a mint #109
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
Open
mcintyre94
wants to merge
1
commit into
main
Choose a base branch
from
create-mint-plan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { getCreateAccountInstruction } from '@solana-program/system'; | ||
| import { | ||
| Address, | ||
| InstructionPlan, | ||
| OptionOrNullable, | ||
| sequentialInstructionPlan, | ||
| TransactionSigner, | ||
| } from '@solana/kit'; | ||
| import { | ||
| getInitializeMint2Instruction, | ||
| getMintSize, | ||
| TOKEN_PROGRAM_ADDRESS, | ||
| } from './generated'; | ||
|
|
||
| // RPC `getMinimumBalanceForRentExemption` for 82 bytes, which is token mint size | ||
| // Hardcoded to avoid requiring an RPC request each time | ||
| const minimumBalanceForMint = 1461600; | ||
|
|
||
| export type CreateMintInstructionPlanInput = { | ||
| /** Funding account (must be a system account). */ | ||
| payer: TransactionSigner; | ||
| /** New mint account to create. */ | ||
| newMint: TransactionSigner; | ||
| /** Number of base 10 digits to the right of the decimal place. */ | ||
| decimals: number; | ||
| /** The authority/multisignature to mint tokens. */ | ||
| mintAuthority: Address; | ||
| /** The optional freeze authority/multisignature of the mint. */ | ||
| freezeAuthority?: OptionOrNullable<Address>; | ||
| /** | ||
| * Optional override for the amount of Lamports to fund the mint account with. | ||
| * @default 1461600 | ||
| * */ | ||
| mintAccountLamports?: number; | ||
| }; | ||
|
|
||
| type CreateMintInstructionPlanConfig = { | ||
| systemProgramAddress?: Address; | ||
| tokenProgramAddress?: Address; | ||
| }; | ||
|
|
||
| export function createMintInstructionPlan( | ||
| params: CreateMintInstructionPlanInput, | ||
| config?: CreateMintInstructionPlanConfig | ||
| ): InstructionPlan { | ||
| return sequentialInstructionPlan([ | ||
| getCreateAccountInstruction( | ||
| { | ||
| payer: params.payer, | ||
| newAccount: params.newMint, | ||
| lamports: params.mintAccountLamports ?? minimumBalanceForMint, | ||
| space: getMintSize(), | ||
| programAddress: config?.tokenProgramAddress ?? TOKEN_PROGRAM_ADDRESS, | ||
| }, | ||
| { | ||
| programAddress: config?.systemProgramAddress, | ||
| } | ||
| ), | ||
| getInitializeMint2Instruction( | ||
| { | ||
| mint: params.newMint.address, | ||
| decimals: params.decimals, | ||
| mintAuthority: params.mintAuthority, | ||
| freezeAuthority: params.freezeAuthority, | ||
| }, | ||
| { | ||
| programAddress: config?.tokenProgramAddress, | ||
| } | ||
| ), | ||
| ]); | ||
| } | ||
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 +1,2 @@ | ||
| export * from './generated'; | ||
| export * from './createMint'; |
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,77 @@ | ||
| import { generateKeyPairSigner, Account, some, none } from '@solana/kit'; | ||
| import test from 'ava'; | ||
| import { fetchMint, Mint, createMintInstructionPlan } from '../src'; | ||
| import { | ||
| createDefaultSolanaClient, | ||
| generateKeyPairSignerWithSol, | ||
| createDefaultTransactionPlanner, | ||
| createDefaultTransactionPlanExecutor, | ||
| } from './_setup'; | ||
|
|
||
| test('it creates and initializes a new mint account', async (t) => { | ||
| // Given an authority and a mint account. | ||
| const client = createDefaultSolanaClient(); | ||
| const authority = await generateKeyPairSignerWithSol(client); | ||
| const mint = await generateKeyPairSigner(); | ||
|
|
||
| // When we create and initialize a mint account at this address. | ||
| const instructionPlan = createMintInstructionPlan({ | ||
| payer: authority, | ||
| newMint: mint, | ||
| decimals: 2, | ||
| mintAuthority: authority.address, | ||
| }); | ||
|
|
||
| const transactionPlanner = createDefaultTransactionPlanner(client, authority); | ||
| const transactionPlan = await transactionPlanner(instructionPlan); | ||
| const transactionPlanExecutor = createDefaultTransactionPlanExecutor(client); | ||
| await transactionPlanExecutor(transactionPlan); | ||
|
|
||
| // Then we expect the mint account to exist and have the following data. | ||
| const mintAccount = await fetchMint(client.rpc, mint.address); | ||
| t.like(mintAccount, <Account<Mint>>{ | ||
| address: mint.address, | ||
| data: { | ||
| mintAuthority: some(authority.address), | ||
| supply: 0n, | ||
| decimals: 2, | ||
| isInitialized: true, | ||
| freezeAuthority: none(), | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test('it creates a new mint account with a freeze authority', async (t) => { | ||
| // Given an authority and a mint account. | ||
| const client = createDefaultSolanaClient(); | ||
| const [payer, mintAuthority, freezeAuthority, mint] = await Promise.all([ | ||
| generateKeyPairSignerWithSol(client), | ||
| generateKeyPairSigner(), | ||
| generateKeyPairSigner(), | ||
| generateKeyPairSigner(), | ||
| ]); | ||
|
|
||
| // When we create and initialize a mint account at this address. | ||
| const instructionPlan = createMintInstructionPlan({ | ||
| payer: payer, | ||
| newMint: mint, | ||
| decimals: 2, | ||
| mintAuthority: mintAuthority.address, | ||
| freezeAuthority: freezeAuthority.address, | ||
| }); | ||
|
|
||
| const transactionPlanner = createDefaultTransactionPlanner(client, payer); | ||
| const transactionPlan = await transactionPlanner(instructionPlan); | ||
| const transactionPlanExecutor = createDefaultTransactionPlanExecutor(client); | ||
| await transactionPlanExecutor(transactionPlan); | ||
|
|
||
| // Then we expect the mint account to exist and have the following data. | ||
| const mintAccount = await fetchMint(client.rpc, mint.address); | ||
| t.like(mintAccount, <Account<Mint>>{ | ||
| address: mint.address, | ||
| data: { | ||
| mintAuthority: some(mintAuthority.address), | ||
| freezeAuthority: some(freezeAuthority.address), | ||
| }, | ||
| }); | ||
| }); |
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.
To flag this, I think it's wasteful/annoying to require you to pass
Rpc<GetMinimumBalanceForRentExemption>API and re-calculate this every time, given this value doesn't change (without a server change).Can change to do that though!
I added an optional
minimumBalanceForMintOverrideoverride in case you want to use this with a Solana network with different logic, or to account for a hypothetical change in future before the library is updated, but intended most usage to just not have to think about it.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.
Yeah I think that's a great idea. IIRC we even discussed the possibility of adding a sync helper like that to Kit. Not sure where we landed on that though.
I also like the override parameter. What do you think of calling it
mintBalanceormintLamportsinstead? I'm not a fan of "minimum balance" here because the created account will have this exact balance after the transaction. Also "override" might be redundant since it's optional.Uh oh!
There was an error while loading. Please reload this page.
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 like
mintLamports, and good to drop the override!Edit: Went with
mintAccountLamportsThere 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.
On the Kit sync helper, my issue is anza-xyz/kit#777
I don't think we really discussed it but I still think this would be worth having. Feels like the risk of this changing frequently is very low.