@unifiedflow/unified-flow-sdk 1.0.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/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@unifiedflow/unified-flow-sdk",
3
+ "version": "1.0.0",
4
+ "description": "SDK for the Unified Flow program",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
10
+ "dependencies": {
11
+ "@coral-xyz/anchor": "^0.30.0",
12
+ "@solana/spl-token": "^0.4.0",
13
+ "@solana/web3.js": "^1.90.0"
14
+ },
15
+ "devDependencies": {
16
+ "typescript": "^5.0.0"
17
+ }
18
+ }
package/src/client.ts ADDED
@@ -0,0 +1,196 @@
1
+ import { Program, BN, Idl } from "@coral-xyz/anchor";
2
+ import { PublicKey, SystemProgram } from "@solana/web3.js";
3
+ import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync } from "@solana/spl-token";
4
+ import { UnifiedFlow } from "./types";
5
+ import { getConfigPDA, getFeeVaultPDA, getMilestonePDA, getStreamPDA, getVaultATA } from "./pda";
6
+
7
+ export const CHAINLINK_PROGRAM_ID = new PublicKey("HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny");
8
+ export const SOL_USD_FEED = new PublicKey("99B2bTijsU6f1GCT73HmdR7HCFFjGMBcPZY6jZ96ynrR");
9
+
10
+ export interface MilestoneInput {
11
+ amount: BN;
12
+ }
13
+
14
+ export class UnifiedFlowClient {
15
+ constructor(public readonly program: Program<UnifiedFlow>) {}
16
+
17
+ /**
18
+ * Helper to derive the config PDA
19
+ */
20
+ public getConfigPDA(): PublicKey {
21
+ return getConfigPDA(this.program.programId)[0];
22
+ }
23
+
24
+ /**
25
+ * Create a new stream (Linear, Cliff, or Milestone)
26
+ */
27
+ public async createStream(
28
+ creator: PublicKey,
29
+ recipient: PublicKey,
30
+ mint: PublicKey,
31
+ amount: BN,
32
+ startTs: BN,
33
+ cliffTs: BN,
34
+ endTs: BN,
35
+ vestingType: number,
36
+ milestones: MilestoneInput[],
37
+ nonce: BN
38
+ ) {
39
+ const config = this.getConfigPDA();
40
+ const stream = getStreamPDA(creator, recipient, nonce, this.program.programId)[0];
41
+ const vault = getVaultATA(mint, stream);
42
+ const creatorTokenAccount = getAssociatedTokenAddressSync(mint, creator, true);
43
+
44
+ const builder = this.program.methods
45
+ .createStream(amount, startTs, cliffTs, endTs, vestingType, milestones, nonce)
46
+ .accounts({
47
+ creator,
48
+ recipient,
49
+ mint,
50
+ creatorTokenAccount,
51
+ tokenProgram: TOKEN_PROGRAM_ID,
52
+ });
53
+
54
+ if (vestingType === 2 && milestones.length > 0) { // VESTING_TYPE_MILESTONE = 2
55
+ const remainingAccounts = milestones.map((_, i) => ({
56
+ pubkey: getMilestonePDA(stream, i, this.program.programId)[0],
57
+ isWritable: true,
58
+ isSigner: false,
59
+ }));
60
+ builder.remainingAccounts(remainingAccounts);
61
+ }
62
+
63
+ return builder;
64
+ }
65
+
66
+ /**
67
+ * Withdraw unlocked/vested tokens from a stream
68
+ */
69
+ public async withdraw(
70
+ streamPDA: PublicKey,
71
+ recipient: PublicKey,
72
+ mint: PublicKey
73
+ ) {
74
+ const config = this.getConfigPDA();
75
+ const vault = getVaultATA(mint, streamPDA);
76
+ const recipientAta = getAssociatedTokenAddressSync(mint, recipient, true);
77
+ const feeVault = getFeeVaultPDA(this.program.programId)[0];
78
+
79
+ return this.program.methods
80
+ .withdraw()
81
+ .accounts({
82
+ recipient,
83
+ stream: streamPDA,
84
+ recipientAta,
85
+ chainlinkFeed: SOL_USD_FEED,
86
+ tokenProgram: TOKEN_PROGRAM_ID,
87
+ } as any);
88
+ }
89
+
90
+ /**
91
+ * Cancel an active stream and return remaining tokens to the creator
92
+ */
93
+ public async cancel(
94
+ streamPDA: PublicKey,
95
+ creator: PublicKey,
96
+ recipient: PublicKey,
97
+ mint: PublicKey
98
+ ) {
99
+ const config = this.getConfigPDA();
100
+ const vault = getVaultATA(mint, streamPDA);
101
+ const creatorTokenAccount = getAssociatedTokenAddressSync(mint, creator, true);
102
+ const recipientTokenAccount = getAssociatedTokenAddressSync(mint, recipient, true);
103
+
104
+ return this.program.methods
105
+ .cancel()
106
+ .accounts({
107
+ creator,
108
+ stream: streamPDA,
109
+ creatorTokenAccount,
110
+ recipientTokenAccount,
111
+ tokenProgram: TOKEN_PROGRAM_ID,
112
+ } as any);
113
+ }
114
+
115
+ /**
116
+ * Unlock a specific milestone
117
+ */
118
+ public async unlockMilestone(
119
+ streamPDA: PublicKey,
120
+ creator: PublicKey,
121
+ milestoneIndex: number
122
+ ) {
123
+ const milestonePDA = getMilestonePDA(streamPDA, milestoneIndex, this.program.programId)[0];
124
+
125
+ return this.program.methods
126
+ .unlockMilestone()
127
+ .accounts({
128
+ stream: streamPDA,
129
+ milestone: milestonePDA,
130
+ } as any);
131
+ }
132
+
133
+ /**
134
+ * Edit a milestone's amount (increase or decrease)
135
+ */
136
+ public async editMilestone(
137
+ streamPDA: PublicKey,
138
+ creator: PublicKey,
139
+ mint: PublicKey,
140
+ milestoneIndex: number,
141
+ newAmount: BN
142
+ ) {
143
+ const milestonePDA = getMilestonePDA(streamPDA, milestoneIndex, this.program.programId)[0];
144
+ const vault = getVaultATA(mint, streamPDA);
145
+ const creatorTokenAccount = getAssociatedTokenAddressSync(mint, creator, true);
146
+
147
+ return this.program.methods
148
+ .editMilestone(newAmount)
149
+ .accounts({
150
+ stream: streamPDA,
151
+ milestone: milestonePDA,
152
+ creatorTokenAccount,
153
+ tokenProgram: TOKEN_PROGRAM_ID,
154
+ } as any);
155
+ }
156
+
157
+ /**
158
+ * Edit the cliff timestamp of a cliff vesting stream
159
+ */
160
+ public async editCliff(
161
+ streamPDA: PublicKey,
162
+ creator: PublicKey,
163
+ newCliffTs: BN
164
+ ) {
165
+ const config = this.getConfigPDA();
166
+
167
+ return this.program.methods
168
+ .editCliff(newCliffTs)
169
+ .accounts({
170
+ stream: streamPDA,
171
+ } as any);
172
+ }
173
+
174
+ /**
175
+ * Edit a linear stream's end timestamp and/or top up tokens
176
+ */
177
+ public async editLinear(
178
+ streamPDA: PublicKey,
179
+ creator: PublicKey,
180
+ mint: PublicKey,
181
+ newEndTs: BN,
182
+ topupAmount: BN
183
+ ) {
184
+ const config = this.getConfigPDA();
185
+ const vault = getVaultATA(mint, streamPDA);
186
+ const creatorTokenAccount = getAssociatedTokenAddressSync(mint, creator, true);
187
+
188
+ return this.program.methods
189
+ .editLinear(newEndTs, topupAmount)
190
+ .accounts({
191
+ stream: streamPDA,
192
+ creatorTokenAccount,
193
+ tokenProgram: TOKEN_PROGRAM_ID,
194
+ } as any);
195
+ }
196
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./types";
2
+ export * from "./pda";
3
+ export * from "./client";
4
+
5
+ import IDL from "./unified_flow.json";
6
+ export { IDL };
package/src/pda.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import { getAssociatedTokenAddressSync } from "@solana/spl-token";
3
+ import { BN } from "@coral-xyz/anchor";
4
+
5
+ export const UNIFIED_FLOW_PROGRAM_ID = new PublicKey(
6
+ "8M5yieUh7pxwUi1YBByDF82nqoorZwaKi8dBoMVpurFa"
7
+ );
8
+
9
+ export function getConfigPDA(programId: PublicKey = UNIFIED_FLOW_PROGRAM_ID): [PublicKey, number] {
10
+ return PublicKey.findProgramAddressSync([Buffer.from("config")], programId);
11
+ }
12
+
13
+ export function getFeeVaultPDA(programId: PublicKey = UNIFIED_FLOW_PROGRAM_ID): [PublicKey, number] {
14
+ return PublicKey.findProgramAddressSync([Buffer.from("fee_vault")], programId);
15
+ }
16
+
17
+ export function getStreamPDA(
18
+ creator: PublicKey,
19
+ recipient: PublicKey,
20
+ nonce: BN,
21
+ programId: PublicKey = UNIFIED_FLOW_PROGRAM_ID
22
+ ): [PublicKey, number] {
23
+ const nonceBuffer = Buffer.alloc(8);
24
+ nonceBuffer.writeBigUInt64LE(BigInt(nonce.toString()));
25
+
26
+ return PublicKey.findProgramAddressSync(
27
+ [
28
+ Buffer.from("stream"),
29
+ creator.toBuffer(),
30
+ recipient.toBuffer(),
31
+ nonceBuffer,
32
+ ],
33
+ programId
34
+ );
35
+ }
36
+
37
+ export function getMilestonePDA(
38
+ stream: PublicKey,
39
+ index: number,
40
+ programId: PublicKey = UNIFIED_FLOW_PROGRAM_ID
41
+ ): [PublicKey, number] {
42
+ return PublicKey.findProgramAddressSync(
43
+ [Buffer.from("milestone"), stream.toBuffer(), Buffer.from([index])],
44
+ programId
45
+ );
46
+ }
47
+
48
+ export function getVaultATA(
49
+ mint: PublicKey,
50
+ stream: PublicKey
51
+ ): PublicKey {
52
+ return getAssociatedTokenAddressSync(mint, stream, true);
53
+ }