Skip to content

Commit

Permalink
Simplify token imports (#34)
Browse files Browse the repository at this point in the history
* refactor: removed token

* refactor: renamed variables

* refactor: consolidate token export

* docs: update

* docs: typo
  • Loading branch information
nickfrosty authored Feb 17, 2025
1 parent be3110d commit dc635bb
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 185 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-chicken-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gill": minor
---

removed token client but keep the program address
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,32 +487,36 @@ To import any of these program clients:
```typescript
import { ... } from "gill/programs";
import { ... } from "gill/programs/token";
import { ... } from "gill/programs/token22";
```

> Note: Some client re-exported client program clients have a naming collision. As a result, they
> may be re-exported under a subpath of `gill/programs`. For example, `gill/programs/token22` and
> `gill/programs/token`.
> may be re-exported under a subpath of `gill/programs`. For example, `gill/programs/token`.
The program clients included inside `gill` are:

- [System program](https://github.com/solana-program/system) - re-exported from
`@solana-program/system`
- [Compute Budget program](https://github.com/solana-program/compute-budget) - re-exported from
`@solana-program/compute-budget`
- [Memo program](https://github.com/solana-program/memo) - re-exported from `@solana-program/memo`
- [Token program](https://github.com/solana-program/token) - re-exported from
`@solana-program/token`
- [Token Extension program (aka Token22)](https://github.com/solana-program/token-2022) -
re-exported from `@solana-program/token-2022`
- [Address Lookup Table program](https://github.com/solana-program/address-lookup-table) -
re-exported from `@solana-program/address-lookup-table`
- [Token Metadata program](https://github.com/metaplex-foundation/mpl-token-metadata) from Metaplex
(only the v3 functionality)
- System program - re-exported from
[`@solana-program/system`](https://github.com/solana-program/system)
- Compute Budget program- re-exported from
[`@solana-program/compute-budget`](https://github.com/solana-program/compute-budget)
- Memo program - re-exported from [`@solana-program/memo`](https://github.com/solana-program/memo)
- Token Program and Token Extensions program (aka Token22) - re-exported from
[`@solana-program/token-2022`](https://github.com/solana-program/token-2022), which is a fully
backwards compatible client with the original Token Program
- Address Lookup Table program - re-exported from
[`@solana-program/address-lookup-table`](https://github.com/solana-program/address-lookup-table)
- Token Metadata program from Metaplex (only the v3 functionality) - generated via Codama their IDL
([source](https://github.com/metaplex-foundation/mpl-token-metadata))

If one of the existing clients are not being exported from `gill/programs` or a subpath therein, you
can of course manually add their compatible client to your repo.

> Note: Since the Token Extensions program client is fully compatible with the original Token
> Program client, `gill` only ships the `@solana-program/token-2022` client and the
> `TOKEN_PROGRAM_ADDRESS` in order to remove all that redundant code from the library.
>
> To use the original Token Program, simply pass the `TOKEN_PROGRAM_ADDRESS` as the the program
> address for any instructions
### Other compatible program clients

From the [solana-program](https://github.com/solana-program/token) GitHub organization, formerly
Expand Down
7 changes: 5 additions & 2 deletions examples/esrun/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import {
address,
} from "gill";
import { loadKeypairSignerFromFile } from "gill/node";
import { buildCreateTokenTransaction, buildMintTokensTransaction } from "gill/programs";
import { TOKEN_2022_PROGRAM_ADDRESS } from "gill/programs/token22";
import {
buildCreateTokenTransaction,
buildMintTokensTransaction,
TOKEN_2022_PROGRAM_ADDRESS,
} from "gill/programs/token";

/** Turn on debug mode */
global.__GILL_DEBUG_LEVEL__ = "debug";
Expand Down
36 changes: 20 additions & 16 deletions packages/gill/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,32 +487,36 @@ To import any of these program clients:
```typescript
import { ... } from "gill/programs";
import { ... } from "gill/programs/token";
import { ... } from "gill/programs/token22";
```

> Note: Some client re-exported client program clients have a naming collision. As a result, they
> may be re-exported under a subpath of `gill/programs`. For example, `gill/programs/token22` and
> `gill/programs/token`.
> may be re-exported under a subpath of `gill/programs`. For example, `gill/programs/token`.
The program clients included inside `gill` are:

- [System program](https://github.com/solana-program/system) - re-exported from
`@solana-program/system`
- [Compute Budget program](https://github.com/solana-program/compute-budget) - re-exported from
`@solana-program/compute-budget`
- [Memo program](https://github.com/solana-program/memo) - re-exported from `@solana-program/memo`
- [Token program](https://github.com/solana-program/token) - re-exported from
`@solana-program/token`
- [Token Extension program (aka Token22)](https://github.com/solana-program/token-2022) -
re-exported from `@solana-program/token-2022`
- [Address Lookup Table program](https://github.com/solana-program/address-lookup-table) -
re-exported from `@solana-program/address-lookup-table`
- [Token Metadata program](https://github.com/metaplex-foundation/mpl-token-metadata) from Metaplex
(only the v3 functionality)
- System program - re-exported from
[`@solana-program/system`](https://github.com/solana-program/system)
- Compute Budget program- re-exported from
[`@solana-program/compute-budget`](https://github.com/solana-program/compute-budget)
- Memo program - re-exported from [`@solana-program/memo`](https://github.com/solana-program/memo)
- Token Program and Token Extensions program (aka Token22) - re-exported from
[`@solana-program/token-2022`](https://github.com/solana-program/token-2022), which is a fully
backwards compatible client with the original Token Program
- Address Lookup Table program - re-exported from
[`@solana-program/address-lookup-table`](https://github.com/solana-program/address-lookup-table)
- Token Metadata program from Metaplex (only the v3 functionality) - generated via Codama their IDL
([source](https://github.com/metaplex-foundation/mpl-token-metadata))

If one of the existing clients are not being exported from `gill/programs` or a subpath therein, you
can of course manually add their compatible client to your repo.

> Note: Since the Token Extensions program client is fully compatible with the original Token
> Program client, `gill` only ships the `@solana-program/token-2022` client and the
> `TOKEN_PROGRAM_ADDRESS` in order to remove all that redundant code from the library.
>
> To use the original Token Program, simply pass the `TOKEN_PROGRAM_ADDRESS` as the the program
> address for any instructions
### Other compatible program clients

From the [solana-program](https://github.com/solana-program/token) GitHub organization, formerly
Expand Down
16 changes: 5 additions & 11 deletions packages/gill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,16 @@
"import": "./dist/node/index.node.mjs",
"require": "./dist/node/index.node.cjs"
},
"./programs/token": {
"types": "./dist/programs/token/index.d.ts",
"import": "./dist/programs/token/index.node.mjs",
"require": "./dist/programs/token/index.node.cjs"
},
"./programs": {
"types": "./dist/programs/index.d.ts",
"import": "./dist/programs/index.node.mjs",
"require": "./dist/programs/index.node.cjs"
},
"./programs/token": {
"types": "./dist/programs/token.d.ts",
"import": "./dist/programs/token.node.mjs",
"require": "./dist/programs/token.node.cjs"
},
"./programs/token22": {
"types": "./dist/programs/token22.d.ts",
"import": "./dist/programs/token22.node.mjs",
"require": "./dist/programs/token22.node.cjs"
},
"./types": "./dist/index.d.ts"
},
"browser": {
Expand Down Expand Up @@ -98,7 +93,6 @@
"@solana-program/compute-budget": "^0.6.1",
"@solana-program/memo": "^0.6.1",
"@solana-program/system": "^0.6.2",
"@solana-program/token": "^0.4.1",
"@solana-program/token-2022": "^0.3.4",
"@solana/accounts": "^2.0.0",
"@solana/addresses": "^2.0.0",
Expand Down
32 changes: 8 additions & 24 deletions packages/gill/src/__tests__/create-token-instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { getCreateMetadataAccountV3Instruction } from "../programs/token-metadat
import {
getCreateTokenInstructions,
GetCreateTokenInstructionsArgs,
} from "../programs/create-token-instructions";
TOKEN_PROGRAM_ADDRESS,
} from "../programs/token";

import { TOKEN_PROGRAM_ADDRESS, getInitializeMintInstruction } from "@solana-program/token";
import {
TOKEN_2022_PROGRAM_ADDRESS,
getMintSize,
getInitializeMintInstruction as getInitializeMintInstructionToken22,
getInitializeMintInstruction,
getInitializeTokenMetadataInstruction,
getInitializeMetadataPointerInstruction,
} from "@solana-program/token-2022";
Expand All @@ -37,12 +37,6 @@ jest.mock("@solana-program/system", () => ({
getCreateAccountInstruction: jest.fn(),
}));

jest.mock("@solana-program/token", () => ({
// preserve all real implementations to only change the desired ones
...jest.requireActual("@solana-program/token"),
getInitializeMintInstruction: jest.fn(),
}));

jest.mock("@solana-program/token-2022", () => ({
// preserve all real implementations to only change the desired ones
...jest.requireActual("@solana-program/token-2022"),
Expand All @@ -65,7 +59,6 @@ describe("getCreateTokenInstructions", () => {
let mockInitializeMintInstruction: IInstruction;
let mockCreateMetadataInstruction: IInstruction;

let mockInitializeMintToken22Instruction: IInstruction;
let mockInitializeMetadataPointerInstruction: IInstruction;
let mockInitializeTokenMetadataInstruction: IInstruction;

Expand All @@ -91,13 +84,9 @@ describe("getCreateTokenInstructions", () => {
data: new Uint8Array([1]),
};
mockInitializeMintInstruction = {
programAddress: "token" as Address,
programAddress: "tokenProgram" as Address,
data: new Uint8Array([2]),
};
mockInitializeMintToken22Instruction = {
programAddress: "token22" as Address,
data: new Uint8Array([3]),
};
mockCreateMetadataInstruction = {
programAddress: "metadata" as Address,
data: new Uint8Array([4]),
Expand All @@ -116,9 +105,6 @@ describe("getCreateTokenInstructions", () => {
(getCreateMetadataAccountV3Instruction as jest.Mock).mockReturnValue(
mockCreateMetadataInstruction,
);
(getInitializeMintInstructionToken22 as jest.Mock).mockReturnValue(
mockInitializeMintToken22Instruction,
);
(getInitializeMetadataPointerInstruction as jest.Mock).mockReturnValue(
mockInitializeMetadataPointerInstruction,
);
Expand Down Expand Up @@ -347,7 +333,7 @@ describe("getCreateTokenInstructions", () => {
}),
);

expect(getInitializeMintInstructionToken22).toHaveBeenCalledWith(
expect(getInitializeMintInstruction).toHaveBeenCalledWith(
expect.objectContaining({
mint: mockMint.address,
}),
Expand All @@ -366,7 +352,7 @@ describe("getCreateTokenInstructions", () => {

getCreateTokenInstructions(args);

expect(getInitializeMintInstructionToken22).toHaveBeenCalledWith(
expect(getInitializeMintInstruction).toHaveBeenCalledWith(
expect.objectContaining({
mint: mockMint.address,
decimals: 6,
Expand All @@ -387,7 +373,7 @@ describe("getCreateTokenInstructions", () => {

getCreateTokenInstructions(args);

expect(getInitializeMintInstructionToken22).toHaveBeenCalledWith(
expect(getInitializeMintInstruction).toHaveBeenCalledWith(
expect.objectContaining({
mintAuthority: mockMintAuthority.address,
freezeAuthority: mockFreezeAuthority.address,
Expand Down Expand Up @@ -415,7 +401,6 @@ describe("getCreateTokenInstructions", () => {

expect(instructions).toHaveLength(4);
expect(instructions[1]).toBe(mockInitializeMetadataPointerInstruction);
expect(instructions[2]).toBe(mockInitializeMintToken22Instruction);
expect(instructions[3]).toBe(mockInitializeTokenMetadataInstruction);

expect(getInitializeMetadataPointerInstruction).toHaveBeenCalledWith(
Expand All @@ -426,7 +411,7 @@ describe("getCreateTokenInstructions", () => {
}),
);

expect(getInitializeMintInstructionToken22).toHaveBeenCalledWith(
expect(getInitializeMintInstruction).toHaveBeenCalledWith(
expect.objectContaining({
mint: mockMint.address,
mintAuthority: mockPayer.address,
Expand Down Expand Up @@ -462,7 +447,6 @@ describe("getCreateTokenInstructions", () => {

expect(instructions).toHaveLength(4);
expect(instructions[1]).toBe(mockInitializeMetadataPointerInstruction);
expect(instructions[2]).toBe(mockInitializeMintToken22Instruction);
expect(instructions[3]).toBe(mockInitializeTokenMetadataInstruction);

expect(getInitializeMetadataPointerInstruction).toHaveBeenCalledWith(
Expand Down
11 changes: 7 additions & 4 deletions packages/gill/src/__tests__/mint-tokens-instructions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { Address } from "@solana/addresses";
import type { KeyPairSigner } from "@solana/signers";
import {
getCreateAssociatedTokenIdempotentInstruction,
getMintToInstruction,
TOKEN_2022_PROGRAM_ADDRESS,
} from "@solana-program/token-2022";
import { getMintTokensInstructions, GetMintTokensInstructionsArgs } from "../programs";
import type { KeyPairSigner } from "@solana/signers";
import type { Address } from "@solana/addresses";
import { TOKEN_PROGRAM_ADDRESS } from "@solana-program/token";
import {
getMintTokensInstructions,
GetMintTokensInstructionsArgs,
TOKEN_PROGRAM_ADDRESS,
} from "../programs/token";

// Mock the imported functions
jest.mock("@solana-program/token-2022", () => ({
Expand Down
3 changes: 1 addition & 2 deletions packages/gill/src/__typetests__/create-token-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import type {
TransactionMessageWithBlockhashLifetime,
} from "@solana/transaction-messages";
import { signTransactionMessageWithSigners } from "@solana/signers";

import { GetCreateTokenInstructionsArgs, buildCreateTokenTransaction } from "../programs";
import { GetCreateTokenInstructionsArgs, buildCreateTokenTransaction } from "../programs/token";

// [DESCRIBE] buildCreateTokenTransaction
async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/gill/src/__typetests__/mint-tokens-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import type {
TransactionMessageWithBlockhashLifetime,
} from "@solana/transaction-messages";
import { signTransactionMessageWithSigners } from "@solana/signers";

import { buildMintTokensTransaction } from "../programs";
import { buildMintTokensTransaction } from "../programs/token";

// [DESCRIBE] buildMintTokensTransaction
async () => {
Expand Down
6 changes: 0 additions & 6 deletions packages/gill/src/programs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ export * from "@solana-program/address-lookup-table";
export * from "@solana-program/compute-budget";
export * from "./compute-budget";

export * from "./token-shared";

/**
* Codama generated clients, stored internally in this package
* (and associated helpers for them)
*/
export * from "./token-metadata";
export * from "./create-token-instructions";
export * from "./create-token-transaction";
export * from "./mint-tokens-instructions";
export * from "./mint-tokens-transaction";
4 changes: 2 additions & 2 deletions packages/gill/src/programs/token-metadata/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { KeyPairSigner } from "@solana/signers";
import { checkedAddress } from "../../core/utils";

/**
* Derive the Token Metadata address from a token's Mint address
* Derive the Token Metadata address from a token's mint address
*
* @param `mint` - `Address` or `KeyPairSigner` of the token Mint
* @param `mint` - `Address` or `KeyPairSigner` of the token mint
*/
export async function getTokenMetadataAddress(mint: Address | KeyPairSigner): Promise<Address> {
return (
Expand Down
8 changes: 0 additions & 8 deletions packages/gill/src/programs/token.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { Address } from "@solana/addresses";
import type { KeyPairSigner } from "@solana/signers";
import { findAssociatedTokenPda, TOKEN_PROGRAM_ADDRESS } from "@solana-program/token";
import { findAssociatedTokenPda } from "@solana-program/token-2022";
import { TOKEN_2022_PROGRAM_ADDRESS } from "@solana-program/token-2022";
import { checkedAddress } from "../core/utils";
import { checkedAddress } from "../../core/utils";

export const TOKEN_PROGRAM_ADDRESS =
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;

/**
* Derive the associated token account (ata) address for an owner and mint/tokenProgram
Expand Down
4 changes: 4 additions & 0 deletions packages/gill/src/programs/token/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "@solana-program/token-2022";
export * from "./instructions";
export * from "./transactions";
export * from "./addresses";
Loading

0 comments on commit dc635bb

Please sign in to comment.