@craftec/flowcraft-sdk 0.1.0

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,100 @@
1
+ # @craftec/flowcraft-sdk
2
+
3
+ SDK for Flowcraft subscription streaming protocol on Solana.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @craftec/flowcraft-sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { Connection, Keypair, PublicKey } from "@solana/web3.js";
15
+ import { Wallet } from "@coral-xyz/anchor";
16
+ import { FlowcraftClient } from "@craftec/flowcraft-sdk";
17
+
18
+ // Initialize client
19
+ const connection = new Connection("https://api.devnet.solana.com");
20
+ const wallet = new Wallet(yourKeypair);
21
+ const client = new FlowcraftClient(connection, wallet);
22
+
23
+ // Create a pool
24
+ const { signature, pool, vault } = await client.createPool(payer, owner, {
25
+ name: "premium",
26
+ mint: mintPubkey,
27
+ });
28
+
29
+ // Subscribe to a pool
30
+ const { signature, stream } = await client.subscribe(subscriber, {
31
+ pool: poolPubkey,
32
+ tier: "monthly",
33
+ amount: 100_000_000, // in smallest units
34
+ durationSeconds: 30 * 24 * 60 * 60, // 30 days
35
+ subscriberTokenAccount,
36
+ treasuryTokenAccount,
37
+ });
38
+
39
+ // Calculate vested amount (off-chain)
40
+ const stats = await client.getPoolAggregateStats(poolPubkey);
41
+ console.log("Total Vested:", stats.totalVested.toString());
42
+ console.log("Claimable:", stats.totalClaimable.toString());
43
+
44
+ // Batch claim from multiple streams
45
+ const { transactions, totalStreams } = await client.buildClaimAllTransactions(
46
+ owner.publicKey,
47
+ poolPubkey,
48
+ ownerTokenAccount,
49
+ 20 // batch size
50
+ );
51
+
52
+ // Sign all transactions at once
53
+ const signed = await wallet.signAllTransactions(transactions);
54
+
55
+ // Send in parallel
56
+ await Promise.all(signed.map(tx => connection.sendTransaction(tx)));
57
+ ```
58
+
59
+ ## API Reference
60
+
61
+ ### FlowcraftClient
62
+
63
+ Main client class for interacting with the Flowcraft protocol.
64
+
65
+ #### Pool Operations
66
+ - `createPool(payer, owner, params)` - Create a new subscription pool
67
+ - `fetchPool(pool)` - Fetch pool data
68
+ - `getPoolInfo(pool)` - Get pool with computed info
69
+ - `getPoolAggregateStats(pool)` - Get aggregate vesting stats for all streams
70
+
71
+ #### Subscription Operations
72
+ - `subscribe(subscriber, params)` - Subscribe to a pool
73
+ - `addSegment(payer, params)` - Add a new segment to existing subscription
74
+ - `upgradeSegment(subscriber, params)` - Upgrade/downgrade a segment
75
+ - `cancelSegment(subscriber, params)` - Cancel a segment
76
+
77
+ #### Claiming
78
+ - `claim(owner, params)` - Claim from single stream
79
+ - `claimBatch(owner, params)` - Claim from multiple streams in one tx
80
+ - `buildClaimAllTransactions(...)` - Build batch claim txs for all streams
81
+
82
+ #### Utilities
83
+ - `getStreamInfoRealTime(stream)` - Get stream with real-time vesting calculation
84
+ - `getRealTimeClaimable(stream)` - Get current claimable amount
85
+
86
+ ## Constants
87
+
88
+ ```typescript
89
+ import { PROGRAM_ID, RATE_DECIMALS } from "@craftec/flowcraft-sdk";
90
+
91
+ // Program ID (devnet/mainnet)
92
+ console.log(PROGRAM_ID.toBase58());
93
+
94
+ // Rate decimals for vesting calculation (10^9)
95
+ console.log(RATE_DECIMALS); // 1000000000
96
+ ```
97
+
98
+ ## License
99
+
100
+ MIT
@@ -0,0 +1,148 @@
1
+ import { Connection, PublicKey, Keypair } from "@solana/web3.js";
2
+ import { Program, AnchorProvider, Wallet, BN, Idl } from "@coral-xyz/anchor";
3
+ import { Config, Pool, Stream, PoolInfo, StreamInfo, PoolAggregateStats, StreamWithAddress, CreatePoolParams, SubscribeParams, AddSegmentParams, ClaimParams, CancelSegmentParams, UpgradeSegmentParams } from "./types";
4
+ export declare const IDL: Idl;
5
+ export declare class FlowcraftClient {
6
+ readonly program: Program;
7
+ readonly provider: AnchorProvider;
8
+ readonly programId: PublicKey;
9
+ constructor(connection: Connection, wallet: Wallet, programId?: PublicKey, idl?: Idl);
10
+ /**
11
+ * Get the config PDA
12
+ */
13
+ getConfigPda(): [PublicKey, number];
14
+ /**
15
+ * Get the pool PDA for an owner and name
16
+ */
17
+ getPoolPda(owner: PublicKey, name: string): [PublicKey, number];
18
+ /**
19
+ * Get the stream PDA for a pool and subscriber
20
+ */
21
+ getStreamPda(pool: PublicKey, subscriber: PublicKey): [PublicKey, number];
22
+ /**
23
+ * Get the vault PDA for a pool
24
+ */
25
+ getVaultPda(pool: PublicKey): [PublicKey, number];
26
+ /**
27
+ * Fetch the global config
28
+ */
29
+ fetchConfig(): Promise<Config | null>;
30
+ /**
31
+ * Fetch a pool by address
32
+ */
33
+ fetchPool(pool: PublicKey): Promise<Pool | null>;
34
+ /**
35
+ * Fetch a pool by owner and name
36
+ */
37
+ fetchPoolByOwner(owner: PublicKey, name: string): Promise<Pool | null>;
38
+ /**
39
+ * Fetch a stream by address
40
+ */
41
+ fetchStream(stream: PublicKey): Promise<Stream | null>;
42
+ /**
43
+ * Fetch a stream by pool and subscriber
44
+ */
45
+ fetchStreamBySubscriber(pool: PublicKey, subscriber: PublicKey): Promise<Stream | null>;
46
+ /**
47
+ * Fetch pool with computed info
48
+ */
49
+ getPoolInfo(pool: PublicKey): Promise<PoolInfo | null>;
50
+ /**
51
+ * Fetch stream with computed info
52
+ */
53
+ getStreamInfo(stream: PublicKey): Promise<StreamInfo | null>;
54
+ /**
55
+ * Initialize the protocol config (one-time setup)
56
+ */
57
+ initializeConfig(admin: Keypair, treasury: PublicKey, feeBps: number): Promise<string>;
58
+ /**
59
+ * Update protocol config (admin only)
60
+ */
61
+ updateConfig(admin: Keypair, newTreasury?: PublicKey, newFeeBps?: number, newAdmin?: PublicKey): Promise<string>;
62
+ /**
63
+ * Create a new subscription pool
64
+ * @param payer - Keypair that pays for rent (signs the transaction)
65
+ * @param owner - PublicKey of pool owner (can be any account, including PDA)
66
+ * @param params - Pool creation parameters
67
+ */
68
+ createPool(payer: Keypair, owner: PublicKey, params: CreatePoolParams): Promise<{
69
+ signature: string;
70
+ pool: PublicKey;
71
+ vault: PublicKey;
72
+ }>;
73
+ /**
74
+ * Subscribe to a pool (creates stream with first segment)
75
+ */
76
+ subscribe(subscriber: Keypair, params: SubscribeParams): Promise<{
77
+ signature: string;
78
+ stream: PublicKey;
79
+ }>;
80
+ /**
81
+ * Add a new segment to an existing subscription
82
+ */
83
+ addSegment(payer: Keypair, params: AddSegmentParams): Promise<string>;
84
+ /**
85
+ * Claim vested tokens (pool owner only)
86
+ */
87
+ claim(owner: Keypair, params: ClaimParams): Promise<string>;
88
+ /**
89
+ * Claim vested tokens from multiple streams in a single transaction
90
+ */
91
+ claimBatch(owner: Keypair, params: {
92
+ pool: PublicKey;
93
+ streams: PublicKey[];
94
+ ownerTokenAccount: PublicKey;
95
+ }): Promise<string>;
96
+ /**
97
+ * Build batch claim transactions for all streams in a pool
98
+ * Returns transactions ready to be signed with signAllTransactions
99
+ */
100
+ buildClaimAllTransactions(owner: PublicKey, pool: PublicKey, ownerTokenAccount: PublicKey, batchSize?: number): Promise<{
101
+ transactions: any[];
102
+ totalStreams: number;
103
+ }>;
104
+ /**
105
+ * Cancel a segment (subscriber only)
106
+ */
107
+ cancelSegment(subscriber: Keypair, params: CancelSegmentParams): Promise<string>;
108
+ /**
109
+ * Upgrade or downgrade a segment
110
+ */
111
+ upgradeSegment(caller: Keypair, params: UpgradeSegmentParams): Promise<string>;
112
+ /**
113
+ * Check if a pool exists
114
+ */
115
+ poolExists(owner: PublicKey, name: string): Promise<boolean>;
116
+ /**
117
+ * Check if a subscription stream exists
118
+ */
119
+ subscriptionExists(pool: PublicKey, subscriber: PublicKey): Promise<boolean>;
120
+ /**
121
+ * Get claimable amount for a stream
122
+ */
123
+ getClaimable(stream: PublicKey): Promise<BN>;
124
+ /**
125
+ * Check if subscription is expired
126
+ */
127
+ isSubscriptionExpired(stream: PublicKey): Promise<boolean>;
128
+ /**
129
+ * Fetch all streams for a pool using getProgramAccounts
130
+ * Stream account layout: [8 discriminator][32 pool][32 subscriber]...
131
+ * Filter by pool pubkey at offset 8
132
+ */
133
+ fetchStreamsByPool(pool: PublicKey): Promise<StreamWithAddress[]>;
134
+ /**
135
+ * Calculate real-time aggregate statistics for a pool
136
+ * Fetches all streams and calculates vesting off-chain
137
+ */
138
+ getPoolAggregateStats(pool: PublicKey): Promise<PoolAggregateStats>;
139
+ /**
140
+ * Get real-time claimable amount for a stream (calculated off-chain)
141
+ */
142
+ getRealTimeClaimable(streamAddress: PublicKey): Promise<BN>;
143
+ /**
144
+ * Get detailed stream info with real-time vesting calculation
145
+ */
146
+ getStreamInfoRealTime(streamAddress: PublicKey): Promise<StreamInfo | null>;
147
+ }
148
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,SAAS,EACT,OAAO,EAER,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,OAAO,EACP,cAAc,EACd,MAAM,EACN,EAAE,EACF,GAAG,EACJ,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EACL,MAAM,EACN,IAAI,EACJ,MAAM,EACN,QAAQ,EACR,UAAU,EAEV,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,WAAW,EACX,mBAAmB,EACnB,oBAAoB,EACrB,MAAM,SAAS,CAAC;AAYjB,eAAO,MAAM,GAAG,EAAE,GAAqB,CAAC;AAExC,qBAAa,eAAe;IAC1B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;IAClC,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;gBAG5B,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,MAAM,EACd,SAAS,GAAE,SAAsB,EACjC,GAAG,GAAE,GAAS;IAahB;;OAEG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