@thru/token-program 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,168 @@
1
+ # @thru/token-program
2
+
3
+ TypeScript bindings for the Thru on-chain token program. Provides instruction builders, account parsers, address derivation, and formatting utilities for creating and managing tokens on the Thru network.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @thru/token-program
9
+ ```
10
+
11
+ Peer dependencies: `@thru/helpers`, `@thru/thru-sdk`.
12
+
13
+ ## Basic Usage
14
+
15
+ ### Create a new token mint
16
+
17
+ ```typescript
18
+ import {
19
+ createInitializeMintInstruction,
20
+ deriveMintAddress,
21
+ } from '@thru/token-program';
22
+
23
+ const { address, bytes, derivedSeed } = deriveMintAddress(
24
+ mintAuthorityAddress,
25
+ seedHex,
26
+ tokenProgramAddress
27
+ );
28
+
29
+ const instruction = createInitializeMintInstruction({
30
+ mintAccountBytes: bytes,
31
+ decimals: 6,
32
+ mintAuthorityBytes: authorityBytes,
33
+ ticker: 'MYTOKEN',
34
+ seedHex,
35
+ stateProof,
36
+ });
37
+ ```
38
+
39
+ ### Initialize a token account
40
+
41
+ ```typescript
42
+ import {
43
+ createInitializeAccountInstruction,
44
+ deriveTokenAccountAddress,
45
+ } from '@thru/token-program';
46
+
47
+ const { bytes: tokenAccountBytes, derivedSeed } = deriveTokenAccountAddress(
48
+ ownerAddress,
49
+ mintAddress,
50
+ tokenProgramAddress
51
+ );
52
+
53
+ const instruction = createInitializeAccountInstruction({
54
+ tokenAccountBytes,
55
+ mintAccountBytes,
56
+ ownerAccountBytes,
57
+ seedBytes: derivedSeed,
58
+ stateProof,
59
+ });
60
+ ```
61
+
62
+ ### Transfer tokens
63
+
64
+ ```typescript
65
+ import { createTransferInstruction } from '@thru/token-program';
66
+
67
+ const instruction = createTransferInstruction({
68
+ sourceAccountBytes,
69
+ destinationAccountBytes,
70
+ amount: 1_000_000n,
71
+ });
72
+ ```
73
+
74
+ ### Parse on-chain account data
75
+
76
+ ```typescript
77
+ import { parseMintAccountData, parseTokenAccountData } from '@thru/token-program';
78
+
79
+ const mintInfo = parseMintAccountData(account);
80
+ // { decimals, supply, creator, mintAuthority, freezeAuthority, ticker, ... }
81
+
82
+ const tokenInfo = parseTokenAccountData(account);
83
+ // { mint, owner, amount, isFrozen }
84
+ ```
85
+
86
+ ### Format token amounts for display
87
+
88
+ ```typescript
89
+ import { formatRawAmount } from '@thru/token-program';
90
+
91
+ formatRawAmount(1_500_000n, 6); // "1.5"
92
+ formatRawAmount(1_000_000n, 6); // "1"
93
+ ```
94
+
95
+ ## Key Capabilities
96
+
97
+ - **Instruction builders** -- `createInitializeMintInstruction`, `createInitializeAccountInstruction`, `createMintToInstruction`, `createTransferInstruction`
98
+ - **Address derivation** -- `deriveMintAddress`, `deriveTokenAccountAddress`, `deriveWalletSeed`
99
+ - **Account parsing** -- `parseMintAccountData`, `parseTokenAccountData` decode raw on-chain data into typed objects
100
+ - **Formatting utilities** -- `formatRawAmount`, `bytesToHex`, `hexToBytes`
101
+ - **ABI codegen** -- instruction payloads are built using auto-generated builders from the token program ABI
102
+
103
+ ## API Reference
104
+
105
+ ### Instructions
106
+
107
+ Each instruction builder returns an `InstructionData` function that accepts an `AccountLookupContext` and resolves to the serialized instruction bytes.
108
+
109
+ | Function | Description |
110
+ |---|---|
111
+ | `createInitializeMintInstruction(args)` | Create a new token mint with ticker, decimals, and authorities |
112
+ | `createInitializeAccountInstruction(args)` | Create a token account for a given owner and mint |
113
+ | `createMintToInstruction(args)` | Mint new tokens to a destination account |
114
+ | `createTransferInstruction(args)` | Transfer tokens between accounts |
115
+ | `buildTokenInstructionBytes(variant, payload)` | Low-level helper to wrap a payload in a token instruction envelope |
116
+
117
+ ### Derivation
118
+
119
+ | Function | Description |
120
+ |---|---|
121
+ | `deriveMintAddress(authority, seed, programAddress)` | Derive the deterministic address for a token mint |
122
+ | `deriveTokenAccountAddress(owner, mint, programAddress, seed?)` | Derive the deterministic address for a token account |
123
+ | `deriveWalletSeed(walletAddress, extraSeeds?)` | Derive a seed from a wallet address |
124
+
125
+ ### Account Parsing
126
+
127
+ | Function | Description |
128
+ |---|---|
129
+ | `parseMintAccountData(account)` | Parse raw account data into `MintAccountInfo` |
130
+ | `parseTokenAccountData(account)` | Parse raw account data into `TokenAccountInfo` |
131
+ | `isAccountNotFoundError(err)` | Check if an error represents a missing account (code 5) |
132
+
133
+ ### Types
134
+
135
+ ```typescript
136
+ interface MintAccountInfo {
137
+ decimals: number;
138
+ supply: bigint;
139
+ creator: string;
140
+ mintAuthority: string;
141
+ freezeAuthority: string | null;
142
+ hasFreezeAuthority: boolean;
143
+ ticker: string;
144
+ }
145
+
146
+ interface TokenAccountInfo {
147
+ mint: string;
148
+ owner: string;
149
+ amount: bigint;
150
+ isFrozen: boolean;
151
+ }
152
+ ```
153
+
154
+ ### Constants
155
+
156
+ | Constant | Value | Description |
157
+ |---|---|---|
158
+ | `PUBKEY_LENGTH` | `32` | Length of a public key in bytes |
159
+ | `TICKER_MAX_LENGTH` | `8` | Maximum ticker string length |
160
+ | `ZERO_PUBKEY` | `Uint8Array(32)` | 32 zero bytes, used as a null public key |
161
+
162
+ ## Build
163
+
164
+ ```bash
165
+ pnpm build # Build with tsup (CJS + ESM + .d.ts)
166
+ pnpm dev # Watch mode
167
+ pnpm clean # Remove dist/
168
+ ```