@streamflow/stream 10.0.0-alpha.p313.3ce4d29 → 10.0.0-alpha.p313.43acc5b

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 CHANGED
@@ -19,6 +19,10 @@ You can also `getOne` stream and `get` multiple streams.
19
19
  - [Create a Token Lock](#create-a-token-lock)
20
20
  - [Streamflow app token lock criteria](#streamflow-app-token-lock-criteria)
21
21
  - [Example for creating a token lock](#example-for-creating-a-token-lock)
22
+ - [Create a Price-based Stream (Aligned)](#create-a-price-based-stream-aligned)
23
+ - [Example for creating a Price-based Vesting Stream](#example-for-creating-a-price-based-vesting-stream)
24
+ - [Create a Price-based Lock](#create-a-price-based-lock)
25
+ - [Fetching Aligned Stream Data](#fetching-aligned-stream-data)
22
26
  - [General Stream creation](#general-stream-creation)
23
27
  - [Create multiple streams at once](#create-multiple-streams-at-once)
24
28
  - [Identifying created Streams](#identifying-created-streams)
@@ -72,7 +76,7 @@ import { StreamflowSolana } from "@streamflow/stream";
72
76
  const solanaClient = new StreamflowSolana.SolanaStreamClient(
73
77
  "https://api.mainnet-beta.solana.com"
74
78
  );
75
-
79
+ ```
76
80
 
77
81
  ## Universal parameters
78
82
 
@@ -99,7 +103,7 @@ const solanaInteractParams = {
99
103
 
100
104
  ## Create a Vesting Stream
101
105
 
102
- Vesting Streams are a type of Stream used to linearly unlock a given amount of tokens over a period of time. You can specify the amount, cliff, start time and releasePeriod along with other configuration that controls who can transfer or cancel the contract. For a Stream to be considered Vesting in the Streamflow App it has to match the criteria from the `isVesting` helper function (or `buildStreamType` returns `StreamType.Vesting`). The stream must not allow top-ups, the cliff amount must not be too close to the total amount and it must not be a Dynamic token lock.
106
+ Vesting Streams are a type of Stream used to linearly unlock a given amount of tokens over a period of time. You can specify the amount, cliff, start time and releasePeriod along with other configuration that controls who can transfer or cancel the contract. For a Stream to be considered Vesting in the Streamflow App it has to match the criteria from the `isVesting` helper function (or `buildStreamType` returns `StreamType.Vesting`). The cliff amount must not be too close to the total amount and it must not be a Dynamic token lock.
103
107
 
104
108
  For more details creating a Stream with arbitrary configuration see [General Stream creation](#general-stream-creation).
105
109
  For creating multiple Streams at once see [Create Multiple Streams](#create-multiple-streams-at-once).
@@ -214,6 +218,147 @@ try {
214
218
  const { ixs } = await client.prepareCreateStreamInstructions(createTokenLockParams, solanaCreateParams)
215
219
  ```
216
220
 
221
+ ## Create a Price-based Stream (Aligned)
222
+
223
+ Price-based Streams (also known as Aligned or Dynamic Streams) are a type of Stream where the unlock rate dynamically adjusts based on the price of a token as reported by a price oracle. The unlock percentage scales between `minPercentage` and `maxPercentage` based on whether the current price is at or below `minPrice` or at or above `maxPrice`. This allows for vesting schedules that accelerate or decelerate based on token price performance.
224
+
225
+ Price-based Streams require all the standard stream parameters plus additional price-based configuration parameters. The unlock rate is recalculated periodically based on the oracle price, making the vesting schedule responsive to market conditions.
226
+
227
+ Streamflow's custom oracles can be fetched through the Oracle API
228
+ - Endpoint -> `https://oracle-api.streamflow.finance/oracle/{mint}`
229
+ - [API Docs](https://oracle-api.streamflow.finance/docs)
230
+
231
+ For more details creating a Stream with arbitrary configuration see [General Stream creation](#general-stream-creation).
232
+
233
+ ### Example for creating a Price-based Vesting Stream
234
+
235
+ The following code will create a Price-based Stream that adjusts its unlock rate based on token price. When the price is at or below `minPrice`, it unlocks at `minPercentage` rate. When the price is at or above `maxPrice`, it unlocks at `maxPercentage` rate. For prices between these bounds, the unlock rate scales proportionally.
236
+
237
+ ```javascript
238
+ const tokenDecimals = 9;
239
+ const totalAmount = getBN(1000, tokenDecimals);
240
+ const cliffAmount = getBN(100, tokenDecimals);
241
+
242
+ const day = 60 * 60 * 24; // in seconds
243
+ const twoWeeks = day * 14;
244
+
245
+ const mint = "DNw99999M7e24g99999999WJirKeZ5fQc6KY999999gK"
246
+ const remainingAmount = totalAmount.sub(cliffAmount);
247
+ const baseAmountPerPeriod = remainingAmount.divn(twoWeeks);
248
+
249
+ const priceOracleAddress = await fetch(`https://oracle-api.streamflow.finance/oracle/${mint}`).then(res => res.json()).then(data => data.address);
250
+
251
+ const createPriceBasedParams: Types.ICreateAlignedStreamData = {
252
+ recipient: "4ih00075bKjVg000000tLdk4w42NyG3Mv0000dc0M00", // Recipient address.
253
+ tokenId: mint, // Token mint address.
254
+ start: 1643363040, // Timestamp (in seconds) when the stream starts.
255
+ amount: totalAmount,
256
+ period: day, // 1 day for daily unlock updates
257
+ cliff: 1643363040, // Cliff timestamp in seconds.
258
+ cliffAmount: cliffAmount, // Amount unlocked at the "cliff" timestamp.
259
+ amountPerPeriod: baseAmountPerPeriod, // Base release rate (will be adjusted based on price).
260
+ name: "Price-based Vesting for Jane Doe.", // The stream name or subject.
261
+ transferableBySender: false, // not possible for aligned streams
262
+ automaticWithdrawal: false, // not possible for aligned streams
263
+ // Price-based configuration parameters - NOTE: all of these must be present in the creation order for the created Stream to be price-based
264
+ minPrice: 0.05, // Minimum price threshold. If current price <= minPrice, minPercentage is used.
265
+ maxPrice: 1.0, // Maximum price threshold. If current price >= maxPrice, maxPercentage is used.
266
+ minPercentage: 1, // Minimum unlock percentage (in basis points, e.g., 1 = 0.01%). Used when price <= minPrice.
267
+ maxPercentage: 1000, // Maximum unlock percentage (in basis points, e.g., 1000 = 10%). Used when price >= maxPrice.
268
+ // If either of these are omitted or oracleType is set to "none" the resulting Stream will behave like a regular Stream
269
+ oracleType: "test", // "test" is passed if you are using Streamflow's price oracles, you can also pass "pyth" if you are passing a Pyth price oracle to priceOracle
270
+ priceOracle: priceOracleAddress, // Address of the price oracle, either a Streamflow oracle address, or a Pyth oracle address.
271
+ skipInitial: false, // [optional] Whether to skip initial unlock amount update when the stream starts. Defaults to false.
272
+ tickSize: 1, // [optional] Tick size for percentage calculations when price is between minPrice and maxPrice. Defaults to 1.
273
+ };
274
+
275
+ try {
276
+ const { ixs, tx, metadata } = await client.create(createPriceBasedParams, solanaCreateParams);
277
+ } catch (exception) {
278
+ }
279
+
280
+ const { ixs } = await client.prepareCreateStreamInstructions(createPriceBasedParams, solanaCreateParams)
281
+ ```
282
+ ## Create a Price-based Lock
283
+
284
+ You can also create a Stream that behaves similar to a token Lock and unlocks at a specific price. There are some technical limitations that cause a small portion of the tokens to be unlocked over time that depends on the contract amount and number of decimals, but for most cases the time it would take for the whole contract to be unlocked would be measured in decades or even hundreds of years. You can achieve this with the following settings:
285
+
286
+ ```javascript
287
+ const targetPrice = 100; // Price at which you want tokens to unlock
288
+ const minPrice = targetPrice - 1;
289
+ const maxPrice = targetPrice;
290
+ const minPercentage = 0;
291
+ const maxPercentage = 100;
292
+ const totalAmount = new BN(1000000);
293
+ const amountPerPeriod = totalAmount;
294
+ const cliffAmount = new BN(0); // No cliff for price-based lock
295
+
296
+ const createPriceBasedParams: Types.ICreateAlignedStreamData = {
297
+ recipient: "4ih00075bKjVg000000tLdk4w42NyG3Mv0000dc0M00", // Recipient address.
298
+ tokenId: mint, // Token mint address.
299
+ start: 1643363040, // Timestamp (in seconds) when the stream starts.
300
+ amount: totalAmount,
301
+ period: 30, // 30 seconds so in case the target price is reached, the oracle has 30 seconds to fetch the price and update the contract release rate
302
+ cliff: 1643363040, // Cliff timestamp in seconds (should match start for price-based lock).
303
+ cliffAmount: cliffAmount, // Amount unlocked at the "cliff" timestamp.
304
+ amountPerPeriod: amountPerPeriod, // Will be updated to 1 as long as price below targetPrice,
305
+ // meaning that tokens are unlocked at rate of 1 smallest denominator of the token per each period (if token has 9 decimals that is 0.000000001 token every 30 seconds)
306
+ name: "Price-based Lock for Jane Doe.",
307
+ transferableBySender: false, // not possible for aligned streams
308
+ automaticWithdrawal: false, // not possible for aligned streams
309
+ // Price-based configuration parameters - NOTE: all of these must be present in the creation order for the created Stream to be price-based
310
+ minPrice, // Minimum price threshold. If current price <= minPrice, minPercentage is used.
311
+ maxPrice, // Maximum price threshold. If current price >= maxPrice, maxPercentage is used.
312
+ minPercentage, // Minimum unlock percentage (in basis points, e.g., 1 = 0.01%). Used when price <= minPrice.
313
+ maxPercentage, // Maximum unlock percentage (in basis points, e.g., 1000 = 10%). Used when price >= maxPrice.
314
+ // If either of these are omitted or oracleType is set to "none" the resulting Stream will behave like a regular Stream
315
+ oracleType: "test", // "test" is passed if you are using Streamflow's price oracles, you can also pass "pyth" if you are passing a Pyth price oracle to priceOracle
316
+ priceOracle: priceOracleAddress, // Address of the price oracle, either a Streamflow oracle address, or a Pyth oracle address.
317
+ skipInitial: false, // In this case it should not be set to true
318
+ // These parameters make sense for a price-based token lock
319
+ floorPrice: 20, // lower bound targetPrice - at or below this price tokens will be unlocked
320
+ expiryTime: 1643364640, // If the targetPrice has not been reached by this point in time, the tokens will be unlocked
321
+ expiryPercentage: maxPercentage, // The percentage unlock rate of tokens after expiryTime, it should be maxPercentage or 100
322
+
323
+ };
324
+ ```
325
+
326
+ ### Fetching Aligned Stream Data
327
+
328
+ When you fetch a price-based stream using `getOne()`, `get()` or `searchStreams()`, it automatically returns an `AlignedContract` instance if the stream is price-based. You can check if a stream is aligned and access its price-based properties:
329
+
330
+ ```javascript
331
+ import { isAligned, getNumberFromBN } from "@streamflow/stream";
332
+
333
+ const tokenDecimals = 9; // Token decimals for the stream's token
334
+
335
+ const stream = await client.getOne({
336
+ id: "AAAAyotqTZZMAAAAmsD1JAgksT8NVAAAASfrGB5RAAAA",
337
+ });
338
+
339
+ // Check if the stream is price-based (aligned)
340
+ if (isAligned(stream)) {
341
+ console.log("Min Price:", stream.minPrice);
342
+ console.log("Max Price:", stream.maxPrice);
343
+ console.log("Min Percentage:", stream.minPercentage);
344
+ console.log("Max Percentage:", stream.maxPercentage);
345
+ console.log("Tick Size:", stream.tickSize);
346
+ console.log("Oracle Type:", stream.oracleType); // "test", "pyth", or "none"
347
+ console.log("Price Oracle Address:", stream.priceOracle);
348
+ console.log("Initial Price:", stream.initialPrice);
349
+ console.log("Last Price:", stream.lastPrice);
350
+ console.log("Last Amount Update Time:", stream.lastAmountUpdateTime);
351
+ console.log("Initial Amount Per Period:", getNumberFromBN(stream.initialAmountPerPeriod, tokenDecimals));
352
+ console.log("Expiry Time:", stream.expiryTime);
353
+ console.log("Expiry Percentage:", stream.expiryPercentage);
354
+ console.log("Floor Price:", stream.floorPrice);
355
+ console.log("Proxy Address:", stream.proxyAddress);
356
+ }
357
+ ```
358
+
359
+ All standard stream properties (like `amount`, `withdrawnAmount`, `unlocked()`, etc.) are also available on aligned streams, as `AlignedContract` extends the base `Contract` class.
360
+
361
+ > **Note:** Price-based Streams use a separate proxy program (Aligned Unlocks) to manage the dynamic unlock logic. The unlock rate is recalculated periodically based on the oracle price. Automatic withdrawal and rate updates are not supported for price-based streams.
217
362
 
218
363
  ## General Stream creation
219
364
 
@@ -426,7 +571,7 @@ console.log(remaining);
426
571
  ```javascript
427
572
  const data: IGetAllData = {
428
573
  address: "99h00075bKjVg000000tLdk4w42NyG3Mv0000dc0M99",
429
- type: StreamType.All, // StreamType.Vesting, StreamType.Lock, StreamType.Payment
574
+ type: StreamType.All, // StreamType.Vesting, StreamType.Lock
430
575
  direction: StreamDirection.All, // StreamDirection.Outgoing, StreamDirection.Incoming
431
576
  };
432
577