-
Notifications
You must be signed in to change notification settings - Fork 1.9k
ts: setup associated token program idl #1939
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
armaniferrante
merged 3 commits into
otter-sec:master
from
heavy-duty:danmt/setup-associated-token-program-coder
Jun 1, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
22 changes: 22 additions & 0 deletions
22
tests/custom-coder/programs/spl-associated-token/Cargo.toml
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,22 @@ | ||
| [package] | ||
| name = "spl-associated-token" | ||
| version = "0.1.0" | ||
| description = "Created with Anchor" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "lib"] | ||
| name = "spl_associated_token" | ||
|
|
||
| [features] | ||
| no-entrypoint = [] | ||
| no-idl = [] | ||
| no-log-ix-name = [] | ||
| cpi = ["no-entrypoint"] | ||
| default = [] | ||
|
|
||
| [profile.release] | ||
| overflow-checks = true | ||
|
|
||
| [dependencies] | ||
| anchor-lang = "0.24.2" |
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,2 @@ | ||
| [target.bpfel-unknown-unknown.dependencies.std] | ||
| features = [] |
33 changes: 33 additions & 0 deletions
33
tests/custom-coder/programs/spl-associated-token/src/lib.rs
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,33 @@ | ||
| // This file is autogenerated with https://github.com/acheroncrypto/native-to-anchor | ||
|
|
||
| use anchor_lang::prelude::*; | ||
|
|
||
| declare_id!("4dUGnkre6uBhX1abB4ofkoecGN4aDXdiWSaWLUjVw6bh"); | ||
|
|
||
| #[program] | ||
| pub mod spl_associated_token { | ||
| use super::*; | ||
|
|
||
| pub fn create(ctx: Context<Create>) -> Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Accounts)] | ||
| pub struct Create<'info> { | ||
| #[account(mut)] | ||
| authority: Signer<'info>, | ||
| #[account(mut)] | ||
| /// CHECK: | ||
| associated_account: AccountInfo<'info>, | ||
| /// CHECK: | ||
| owner: AccountInfo<'info>, | ||
| /// CHECK: | ||
| mint: AccountInfo<'info>, | ||
| /// CHECK: | ||
| system_program: AccountInfo<'info>, | ||
| /// CHECK: | ||
| token_program: AccountInfo<'info>, | ||
| /// CHECK: | ||
| rent: AccountInfo<'info>, | ||
| } |
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,73 @@ | ||
| import * as anchor from "@project-serum/anchor"; | ||
| import { Native, Spl } from "@project-serum/anchor"; | ||
| import { Keypair, PublicKey } from "@solana/web3.js"; | ||
| import * as assert from "assert"; | ||
| import BN from "bn.js"; | ||
|
|
||
| describe("spl-associated-token-coder", () => { | ||
| // Configure the client to use the local cluster. | ||
| const provider = anchor.AnchorProvider.env(); | ||
| anchor.setProvider(provider); | ||
|
|
||
| // Client. | ||
| const program = Spl.associatedToken(); | ||
| const systemProgram = Native.system(); | ||
| const tokenProgram = Spl.token(); | ||
|
|
||
| it("Creates an account", async () => { | ||
| // arrange | ||
| const mintKeypair = Keypair.generate(); | ||
| const mintDecimals = 6; | ||
| const mintSize = tokenProgram.coder.accounts.size( | ||
| tokenProgram.idl.accounts[0] | ||
| ); | ||
| const mintRentExemption = | ||
| await provider.connection.getMinimumBalanceForRentExemption(mintSize); | ||
| const [associatedToken] = await PublicKey.findProgramAddress( | ||
| [ | ||
| provider.publicKey.toBuffer(), | ||
| tokenProgram.programId.toBuffer(), | ||
| mintKeypair.publicKey.toBuffer(), | ||
| ], | ||
| program.programId | ||
| ); | ||
|
|
||
| // act | ||
| await program.methods | ||
| .create() | ||
| .accounts({ | ||
| authority: provider.wallet.publicKey, | ||
| mint: mintKeypair.publicKey, | ||
| owner: provider.wallet.publicKey, | ||
| associatedAccount: associatedToken, | ||
| }) | ||
| .preInstructions( | ||
| await Promise.all([ | ||
| systemProgram.methods | ||
| .createAccount( | ||
| new BN(mintRentExemption), | ||
| new BN(mintSize), | ||
| tokenProgram.programId | ||
| ) | ||
| .accounts({ | ||
| from: provider.wallet.publicKey, | ||
| to: mintKeypair.publicKey, | ||
| }) | ||
| .instruction(), | ||
| tokenProgram.methods | ||
| .initializeMint(mintDecimals, provider.wallet.publicKey, null) | ||
| .accounts({ | ||
| mint: mintKeypair.publicKey, | ||
| }) | ||
| .instruction(), | ||
| ]) | ||
| ) | ||
| .signers([mintKeypair]) | ||
| .rpc(); | ||
| // assert | ||
| const tokenAccount = await tokenProgram.account.token.fetch( | ||
| associatedToken | ||
| ); | ||
| assert.ok(tokenAccount.mint.equals(mintKeypair.publicKey)); | ||
| }); | ||
| }); |
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,42 @@ | ||
| import { AccountsCoder } from "../index.js"; | ||
| import { Idl, IdlTypeDef } from "../../idl.js"; | ||
| import { accountSize } from "../common"; | ||
|
|
||
| export class SplAssociatedTokenAccountsCoder<A extends string = string> | ||
| implements AccountsCoder | ||
| { | ||
| constructor(private idl: Idl) {} | ||
|
|
||
| public async encode<T = any>(accountName: A, account: T): Promise<Buffer> { | ||
| switch (accountName) { | ||
| default: { | ||
| throw new Error(`Invalid account name: ${accountName}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public decode<T = any>(accountName: A, ix: Buffer): T { | ||
| return this.decodeUnchecked(accountName, ix); | ||
| } | ||
|
|
||
| public decodeUnchecked<T = any>(accountName: A, ix: Buffer): T { | ||
| switch (accountName) { | ||
| default: { | ||
| throw new Error(`Invalid account name: ${accountName}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO: this won't use the appendData. | ||
| public memcmp(accountName: A, _appendData?: Buffer): any { | ||
| switch (accountName) { | ||
| default: { | ||
| throw new Error(`Invalid account name: ${accountName}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public size(idlAccount: IdlTypeDef): number { | ||
| return accountSize(this.idl, idlAccount) ?? 0; | ||
| } | ||
| } |
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,14 @@ | ||
| import { EventCoder } from "../index.js"; | ||
| import { Idl } from "../../idl.js"; | ||
| import { Event } from "../../program/event"; | ||
| import { IdlEvent } from "../../idl"; | ||
|
|
||
| export class SplAssociatedTokenEventsCoder implements EventCoder { | ||
| constructor(_idl: Idl) {} | ||
|
|
||
| decode<E extends IdlEvent = IdlEvent, T = Record<string, string>>( | ||
| _log: string | ||
| ): Event<E, T> | null { | ||
| throw new Error("SPL associated token program does not have events"); | ||
| } | ||
| } |
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,26 @@ | ||
| import { Idl } from "../../idl.js"; | ||
| import { Coder } from "../index.js"; | ||
| import { SplAssociatedTokenInstructionCoder } from "./instruction.js"; | ||
| import { SplAssociatedTokenStateCoder } from "./state.js"; | ||
| import { SplAssociatedTokenAccountsCoder } from "./accounts.js"; | ||
| import { SplAssociatedTokenEventsCoder } from "./events.js"; | ||
| import { SplAssociatedTokenTypesCoder } from "./types.js"; | ||
|
|
||
| /** | ||
| * Coder for the SPL token program. | ||
| */ | ||
| export class SplAssociatedTokenCoder implements Coder { | ||
| readonly instruction: SplAssociatedTokenInstructionCoder; | ||
| readonly accounts: SplAssociatedTokenAccountsCoder; | ||
| readonly state: SplAssociatedTokenStateCoder; | ||
| readonly events: SplAssociatedTokenEventsCoder; | ||
| readonly types: SplAssociatedTokenTypesCoder; | ||
|
|
||
| constructor(idl: Idl) { | ||
| this.instruction = new SplAssociatedTokenInstructionCoder(idl); | ||
| this.accounts = new SplAssociatedTokenAccountsCoder(idl); | ||
| this.events = new SplAssociatedTokenEventsCoder(idl); | ||
| this.state = new SplAssociatedTokenStateCoder(idl); | ||
| this.types = new SplAssociatedTokenTypesCoder(idl); | ||
| } | ||
| } |
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,22 @@ | ||
| import camelCase from "camelcase"; | ||
| import { Idl } from "../../idl.js"; | ||
| import { InstructionCoder } from "../index.js"; | ||
|
|
||
| export class SplAssociatedTokenInstructionCoder implements InstructionCoder { | ||
| constructor(_: Idl) {} | ||
|
|
||
| encode(ixName: string, _: any): Buffer { | ||
| switch (camelCase(ixName)) { | ||
| case "create": { | ||
| return Buffer.alloc(0); | ||
| } | ||
| default: { | ||
| throw new Error(`Invalid instruction: ${ixName}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| encodeState(_ixName: string, _ix: any): Buffer { | ||
| throw new Error("SPL associated token does not have state"); | ||
| } | ||
| } | ||
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,13 @@ | ||
| import { StateCoder } from "../index.js"; | ||
| import { Idl } from "../../idl"; | ||
|
|
||
| export class SplAssociatedTokenStateCoder implements StateCoder { | ||
| constructor(_idl: Idl) {} | ||
|
|
||
| encode<T = any>(_name: string, _account: T): Promise<Buffer> { | ||
| throw new Error("SPL associated token does not have state"); | ||
| } | ||
| decode<T = any>(_ix: Buffer): T { | ||
| throw new Error("SPL associated token does not have state"); | ||
| } | ||
| } |
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,13 @@ | ||
| import { TypesCoder } from "../index.js"; | ||
| import { Idl } from "../../idl.js"; | ||
|
|
||
| export class SplAssociatedTokenTypesCoder implements TypesCoder { | ||
| constructor(_idl: Idl) {} | ||
|
|
||
| encode<T = any>(_name: string, _type: T): Buffer { | ||
| throw new Error("SPL associated token does not have user-defined types"); | ||
| } | ||
| decode<T = any>(_name: string, _typeData: Buffer): T { | ||
| throw new Error("SPL associated token does not have user-defined types"); | ||
| } | ||
| } |
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.