Skip to content

Commit

Permalink
refactor: common types for token builders (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfrosty authored Feb 19, 2025
1 parent 2c573b2 commit e3c8aff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 42 deletions.
31 changes: 10 additions & 21 deletions packages/gill/src/programs/token/transactions/create-token.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
import type { TransactionBuilderInput } from "./types";
import type {
ITransactionMessageWithFeePayer,
TransactionMessageWithBlockhashLifetime,
TransactionVersion,
} from "@solana/transaction-messages";
import { createTransaction } from "../../../core";
import type { CreateTransactionInput, FullTransaction, Simplify } from "../../../types";
import type { FullTransaction, Simplify } from "../../../types";
import { type KeyPairSigner, type TransactionSigner } from "@solana/signers";
import {
getCreateTokenInstructions,
type GetCreateTokenInstructionsArgs,
} from "../instructions/create-token";
import { type KeyPairSigner, type TransactionSigner } from "@solana/signers";
import { createTransaction } from "../../../core";
import { TOKEN_2022_PROGRAM_ADDRESS } from "@solana-program/token-2022";
import { getTokenMetadataAddress } from "../../token-metadata";
import { checkedTokenProgramAddress, TOKEN_PROGRAM_ADDRESS } from "../addresses";

type TransactionInput<
TVersion extends TransactionVersion = "legacy",
TFeePayer extends TransactionSigner = TransactionSigner,
TLifetimeConstraint extends
| TransactionMessageWithBlockhashLifetime["lifetimeConstraint"]
| undefined = undefined,
> = Simplify<
Omit<
CreateTransactionInput<TVersion, TFeePayer, TLifetimeConstraint>,
"version" | "instructions" | "feePayer"
> &
Partial<Pick<CreateTransactionInput<TVersion, TFeePayer, TLifetimeConstraint>, "version">>
>;

type GetCreateTokenTransactionInput = Simplify<
Omit<GetCreateTokenInstructionsArgs, "metadataAddress"> &
Partial<Pick<GetCreateTokenInstructionsArgs, "metadataAddress">>
Expand All @@ -36,7 +23,7 @@ type GetCreateTokenTransactionInput = Simplify<
/**
* Create a transaction that can create a token with metadata
*
* The transaction will has the following defaults:
* The transaction has the following defaults:
* - Default `version` = `legacy`
* - Default `computeUnitLimit`:
* - for TOKEN_PROGRAM_ADDRESS => `60_000`
Expand Down Expand Up @@ -66,15 +53,16 @@ export async function buildCreateTokenTransaction<
TVersion extends TransactionVersion = "legacy",
TFeePayer extends TransactionSigner = TransactionSigner,
>(
args: TransactionInput<TVersion, TFeePayer> & GetCreateTokenTransactionInput,
args: TransactionBuilderInput<TVersion, TFeePayer> & GetCreateTokenTransactionInput,
): Promise<FullTransaction<TVersion, ITransactionMessageWithFeePayer>>;
export async function buildCreateTokenTransaction<
TVersion extends TransactionVersion = "legacy",
TFeePayer extends TransactionSigner = TransactionSigner,
TLifetimeConstraint extends
TransactionMessageWithBlockhashLifetime["lifetimeConstraint"] = TransactionMessageWithBlockhashLifetime["lifetimeConstraint"],
>(
args: TransactionInput<TVersion, TFeePayer, TLifetimeConstraint> & GetCreateTokenTransactionInput,
args: TransactionBuilderInput<TVersion, TFeePayer, TLifetimeConstraint> &
GetCreateTokenTransactionInput,
): Promise<
FullTransaction<
TVersion,
Expand All @@ -87,7 +75,8 @@ export async function buildCreateTokenTransaction<
TFeePayer extends TransactionSigner,
TLifetimeConstraint extends TransactionMessageWithBlockhashLifetime["lifetimeConstraint"],
>(
args: TransactionInput<TVersion, TFeePayer, TLifetimeConstraint> & GetCreateTokenTransactionInput,
args: TransactionBuilderInput<TVersion, TFeePayer, TLifetimeConstraint> &
GetCreateTokenTransactionInput,
) {
args.tokenProgram = checkedTokenProgramAddress(args.tokenProgram);

Expand Down
31 changes: 10 additions & 21 deletions packages/gill/src/programs/token/transactions/mint-tokens.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
import type { TransactionBuilderInput } from "./types";
import type { Address } from "@solana/addresses";
import { type TransactionSigner } from "@solana/signers";
import type {
ITransactionMessageWithFeePayer,
TransactionMessageWithBlockhashLifetime,
TransactionVersion,
} from "@solana/transaction-messages";
import type { FullTransaction, Simplify } from "../../../types";
import { checkedAddress, createTransaction } from "../../../core";
import type { CreateTransactionInput, FullTransaction, Simplify } from "../../../types";
import { type TransactionSigner } from "@solana/signers";
import { checkedTokenProgramAddress, getAssociatedTokenAccountAddress } from "../addresses";
import {
getMintTokensInstructions,
type GetMintTokensInstructionsArgs,
} from "../instructions/mint-tokens";
import { Address } from "@solana/addresses";

type TransactionInput<
TVersion extends TransactionVersion = "legacy",
TFeePayer extends TransactionSigner = TransactionSigner,
TLifetimeConstraint extends
| TransactionMessageWithBlockhashLifetime["lifetimeConstraint"]
| undefined = undefined,
> = Simplify<
Omit<
CreateTransactionInput<TVersion, TFeePayer, TLifetimeConstraint>,
"version" | "instructions" | "feePayer"
> &
Partial<Pick<CreateTransactionInput<TVersion, TFeePayer, TLifetimeConstraint>, "version">>
>;

type GetCreateTokenTransactionInput = Simplify<
Omit<GetMintTokensInstructionsArgs, "ata"> & Partial<Pick<GetMintTokensInstructionsArgs, "ata">>
Expand All @@ -35,7 +22,7 @@ type GetCreateTokenTransactionInput = Simplify<
* Create a transaction that can mint tokens to the desired wallet/owner,
* including creating their ATA if it does not exist
*
* The transaction will has the following defaults:
* The transaction has the following defaults:
* - Default `version` = `legacy`
* - Default `computeUnitLimit` = `31_000`
*
Expand Down Expand Up @@ -69,15 +56,16 @@ export async function buildMintTokensTransaction<
TVersion extends TransactionVersion = "legacy",
TFeePayer extends TransactionSigner = TransactionSigner,
>(
args: TransactionInput<TVersion, TFeePayer> & GetCreateTokenTransactionInput,
args: TransactionBuilderInput<TVersion, TFeePayer> & GetCreateTokenTransactionInput,
): Promise<FullTransaction<TVersion, ITransactionMessageWithFeePayer>>;
export async function buildMintTokensTransaction<
TVersion extends TransactionVersion = "legacy",
TFeePayer extends TransactionSigner = TransactionSigner,
TLifetimeConstraint extends
TransactionMessageWithBlockhashLifetime["lifetimeConstraint"] = TransactionMessageWithBlockhashLifetime["lifetimeConstraint"],
>(
args: TransactionInput<TVersion, TFeePayer, TLifetimeConstraint> & GetCreateTokenTransactionInput,
args: TransactionBuilderInput<TVersion, TFeePayer, TLifetimeConstraint> &
GetCreateTokenTransactionInput,
): Promise<
FullTransaction<
TVersion,
Expand All @@ -90,7 +78,8 @@ export async function buildMintTokensTransaction<
TFeePayer extends TransactionSigner,
TLifetimeConstraint extends TransactionMessageWithBlockhashLifetime["lifetimeConstraint"],
>(
args: TransactionInput<TVersion, TFeePayer, TLifetimeConstraint> & GetCreateTokenTransactionInput,
args: TransactionBuilderInput<TVersion, TFeePayer, TLifetimeConstraint> &
GetCreateTokenTransactionInput,
) {
args.tokenProgram = checkedTokenProgramAddress(args.tokenProgram);
args.mint = checkedAddress(args.mint);
Expand Down
20 changes: 20 additions & 0 deletions packages/gill/src/programs/token/transactions/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { TransactionSigner } from "@solana/signers";
import type {
TransactionMessageWithBlockhashLifetime,
TransactionVersion,
} from "@solana/transaction-messages";
import type { CreateTransactionInput, Simplify } from "../../../types";

export type TransactionBuilderInput<
TVersion extends TransactionVersion = "legacy",
TFeePayer extends TransactionSigner = TransactionSigner,
TLifetimeConstraint extends
| TransactionMessageWithBlockhashLifetime["lifetimeConstraint"]
| undefined = undefined,
> = Simplify<
Omit<
CreateTransactionInput<TVersion, TFeePayer, TLifetimeConstraint>,
"version" | "instructions" | "feePayer"
> &
Partial<Pick<CreateTransactionInput<TVersion, TFeePayer, TLifetimeConstraint>, "version">>
>;

0 comments on commit e3c8aff

Please sign in to comment.