-
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
Open
amilz
wants to merge
5
commits into
solana-program:main
Choose a base branch
from
amilz:chore/add-defaults-to-js-helpers
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb08667
feat(js): add optional ata/source/destination derivation and plugin d…
amilz 328c15a
refactor(js): extract MakeOptional type and pass config through plugin
amilz d5e9b97
chore: test types
amilz e7f70ad
refactor(js): remove authority defaults and replace offline tests
amilz a347914
refactor(js): simplify return types and normalize destinationAta style
amilz 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; |
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,96 @@ | ||
| import { Account, generateKeyPairSigner } from '@solana/kit'; | ||
| import { createDefaultLocalhostRpcClient } from '@solana/kit-plugins'; | ||
| import test from 'ava'; | ||
| import { AccountState, fetchToken, findAssociatedTokenPda, Token, TOKEN_PROGRAM_ADDRESS, tokenProgram } from '../src'; | ||
| import { | ||
| createMint, | ||
| createTokenPdaWithAmount, | ||
| generateKeyPairSignerWithSol, | ||
| createDefaultSolanaClient, | ||
| } from './_setup'; | ||
|
|
||
| test('plugin mintToATA defaults payer and auto-derives ATA', async t => { | ||
| // Given a mint account, its mint authority and a token owner. | ||
| const client = await createDefaultLocalhostRpcClient().use(tokenProgram()); | ||
| const mintAuthority = await generateKeyPairSigner(); | ||
| const owner = await generateKeyPairSigner(); | ||
| const mint = await generateKeyPairSigner(); | ||
|
|
||
| // And a mint created via the plugin. | ||
| await client.token.instructions | ||
| .createMint({ newMint: mint, decimals: 2, mintAuthority: mintAuthority.address }) | ||
| .sendTransaction(); | ||
|
|
||
| // When we mint to the owner via the plugin (payer defaulted, ATA derived). | ||
| await client.token.instructions | ||
| .mintToATA({ | ||
| mint: mint.address, | ||
| owner: owner.address, | ||
| mintAuthority, | ||
| amount: 1_000n, | ||
| decimals: 2, | ||
| }) | ||
| .sendTransaction(); | ||
amilz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Then we expect the derived ATA to exist with the correct balance. | ||
| const [ata] = await findAssociatedTokenPda({ | ||
| mint: mint.address, | ||
| owner: owner.address, | ||
| tokenProgram: TOKEN_PROGRAM_ADDRESS, | ||
| }); | ||
|
|
||
| t.like(await fetchToken(client.rpc, ata), <Account<Token>>{ | ||
| address: ata, | ||
| data: { | ||
| mint: mint.address, | ||
| owner: owner.address, | ||
| amount: 1000n, | ||
| state: AccountState.Initialized, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test('plugin transferToATA defaults payer and auto-derives source + destination', async t => { | ||
| // Given a mint account and ownerA's ATA with 100 tokens. | ||
| const baseClient = createDefaultSolanaClient(); | ||
| const [payer, mintAuthority, ownerA, ownerB] = await Promise.all([ | ||
| generateKeyPairSignerWithSol(baseClient), | ||
| generateKeyPairSigner(), | ||
| generateKeyPairSigner(), | ||
| generateKeyPairSigner(), | ||
| ]); | ||
| const decimals = 2; | ||
| const mint = await createMint(baseClient, payer, mintAuthority.address, decimals); | ||
| await createTokenPdaWithAmount(baseClient, payer, mintAuthority, mint, ownerA.address, 100n, decimals); | ||
|
|
||
| // When ownerA transfers 50 tokens to ownerB via the plugin (payer defaulted, source + destination derived). | ||
| const client = await createDefaultLocalhostRpcClient().use(tokenProgram()); | ||
| await client.token.instructions | ||
| .transferToATA({ | ||
| mint, | ||
| authority: ownerA, | ||
| recipient: ownerB.address, | ||
| amount: 50n, | ||
| decimals, | ||
| }) | ||
| .sendTransaction(); | ||
|
|
||
| // Then we expect both ATAs to have the correct balances. | ||
| const [sourceAta] = await findAssociatedTokenPda({ | ||
| owner: ownerA.address, | ||
| mint, | ||
| tokenProgram: TOKEN_PROGRAM_ADDRESS, | ||
| }); | ||
| const [destAta] = await findAssociatedTokenPda({ | ||
| owner: ownerB.address, | ||
| mint, | ||
| tokenProgram: TOKEN_PROGRAM_ADDRESS, | ||
| }); | ||
|
|
||
| t.like(await fetchToken(client.rpc, sourceAta), <Account<Token>>{ | ||
| data: { amount: 50n }, | ||
| }); | ||
| t.like(await fetchToken(client.rpc, destAta), <Account<Token>>{ | ||
| data: { amount: 50n }, | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.