@usepiper/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/LICENSE +21 -0
- package/README.md +368 -0
- package/dist/constants.d.ts +19 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +21 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/piper.d.ts +130 -0
- package/dist/piper.d.ts.map +1 -0
- package/dist/piper.js +253 -0
- package/dist/piper.js.map +1 -0
- package/dist/types.d.ts +156 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +74 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +147 -0
- package/dist/utils.js.map +1 -0
- package/package.json +55 -0
package/dist/piper.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Piper SDK — Core Class
|
|
3
|
+
*
|
|
4
|
+
* Static methods that append Move calls to a Sui `Transaction`.
|
|
5
|
+
* Each method maps directly to a public function in the piper::stream Move module.
|
|
6
|
+
*
|
|
7
|
+
* Move function signatures (from piper-move/sources/stream.move):
|
|
8
|
+
*
|
|
9
|
+
* create_stream<T>(coin: Coin<T>, flow_rate: u64, recipient: address, clock: &Clock, ctx: &mut TxContext): Stream<T>
|
|
10
|
+
* create_on_demand_stream<T>(coin: Coin<T>, recipient: address, authorized_spender: address, clock: &Clock, ctx: &mut TxContext): Stream<T>
|
|
11
|
+
* tick<T>(stream: &mut Stream<T>, clock: &Clock, ctx: &mut TxContext)
|
|
12
|
+
* pay<T>(stream: &mut Stream<T>, amount: u64, clock: &Clock, ctx: &mut TxContext)
|
|
13
|
+
* revoke<T>(stream: &mut Stream<T>, ctx: &mut TxContext): Coin<T>
|
|
14
|
+
* top_up<T>(stream: &mut Stream<T>, coin: Coin<T>)
|
|
15
|
+
* add_split<T>(stream: &mut Stream<T>, recipient: address, percent: u8, ctx: &mut TxContext)
|
|
16
|
+
* remove_split<T>(stream: &mut Stream<T>, index: u64, ctx: &mut TxContext)
|
|
17
|
+
* destroy_empty<T>(stream: Stream<T>)
|
|
18
|
+
*/
|
|
19
|
+
import { PIPER_TESTNET_PACKAGE_ID, target } from './constants.js';
|
|
20
|
+
/**
|
|
21
|
+
* Resolves a value to a `TransactionArgument`.
|
|
22
|
+
* If the value is already a `TransactionArgument`, returns it directly.
|
|
23
|
+
* If it's a string (object ID), wraps it with `tx.object()`.
|
|
24
|
+
*/
|
|
25
|
+
function resolveObject(tx, value) {
|
|
26
|
+
if (typeof value === 'string') {
|
|
27
|
+
return tx.object(value);
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The main Piper SDK class.
|
|
33
|
+
*
|
|
34
|
+
* All methods are static — there's no instance state.
|
|
35
|
+
* Call `Piper.setPackageId()` once at startup (or use the testnet default),
|
|
36
|
+
* then use any method to append Move calls to a `Transaction`.
|
|
37
|
+
*/
|
|
38
|
+
export class Piper {
|
|
39
|
+
/** The configured Move package ID */
|
|
40
|
+
static _packageId = PIPER_TESTNET_PACKAGE_ID;
|
|
41
|
+
// ===== Configuration =====
|
|
42
|
+
/**
|
|
43
|
+
* Sets the Move package ID for all subsequent calls.
|
|
44
|
+
* Defaults to the Piper testnet package.
|
|
45
|
+
*/
|
|
46
|
+
static setPackageId(packageId) {
|
|
47
|
+
Piper._packageId = packageId;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Returns the currently configured package ID.
|
|
51
|
+
* @throws if no package ID has been set and the default has been cleared.
|
|
52
|
+
*/
|
|
53
|
+
static getPackageId() {
|
|
54
|
+
if (!Piper._packageId) {
|
|
55
|
+
throw new Error('Piper package ID not set. Call Piper.setPackageId() before using SDK methods.');
|
|
56
|
+
}
|
|
57
|
+
return Piper._packageId;
|
|
58
|
+
}
|
|
59
|
+
// ===== Stream Creation =====
|
|
60
|
+
/**
|
|
61
|
+
* Creates a continuous (time-based) stream.
|
|
62
|
+
*
|
|
63
|
+
* Move: `piper::stream::create_stream<T>(coin, flow_rate, recipient, clock, ctx): Stream<T>`
|
|
64
|
+
*
|
|
65
|
+
* @returns A `TransactionResult` representing the created `Stream<T>` object.
|
|
66
|
+
* You must transfer or share this object in the same transaction.
|
|
67
|
+
*/
|
|
68
|
+
static createContinuousStream(tx, config) {
|
|
69
|
+
const packageId = Piper.getPackageId();
|
|
70
|
+
return tx.moveCall({
|
|
71
|
+
target: target(packageId, 'create_stream'),
|
|
72
|
+
typeArguments: [config.coinType],
|
|
73
|
+
arguments: [
|
|
74
|
+
resolveObject(tx, config.coin),
|
|
75
|
+
tx.pure.u64(BigInt(config.flowRate)),
|
|
76
|
+
tx.pure.address(config.recipient),
|
|
77
|
+
tx.object.clock(),
|
|
78
|
+
],
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Creates an on-demand (pay-per-use) stream.
|
|
83
|
+
*
|
|
84
|
+
* Move: `piper::stream::create_on_demand_stream<T>(coin, recipient, authorized_spender, clock, ctx): Stream<T>`
|
|
85
|
+
*
|
|
86
|
+
* @returns A `TransactionResult` representing the created `Stream<T>` object.
|
|
87
|
+
* You must transfer or share this object in the same transaction.
|
|
88
|
+
*/
|
|
89
|
+
static createOnDemandStream(tx, config) {
|
|
90
|
+
const packageId = Piper.getPackageId();
|
|
91
|
+
return tx.moveCall({
|
|
92
|
+
target: target(packageId, 'create_on_demand_stream'),
|
|
93
|
+
typeArguments: [config.coinType],
|
|
94
|
+
arguments: [
|
|
95
|
+
resolveObject(tx, config.coin),
|
|
96
|
+
tx.pure.address(config.recipient),
|
|
97
|
+
tx.pure.address(config.authorizedSpender),
|
|
98
|
+
tx.object.clock(),
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Shares a stream object so it can be autonomously ticked by anyone.
|
|
104
|
+
* This is the recommended way to handle newly created continuous streams.
|
|
105
|
+
*
|
|
106
|
+
* @param tx The Transaction
|
|
107
|
+
* @param stream The TransactionResult from createContinuousStream
|
|
108
|
+
* @param coinType The type of the coin in the stream
|
|
109
|
+
*/
|
|
110
|
+
static shareStream(tx, stream, coinType) {
|
|
111
|
+
const packageId = Piper.getPackageId();
|
|
112
|
+
tx.moveCall({
|
|
113
|
+
target: '0x2::transfer::public_share_object',
|
|
114
|
+
typeArguments: [`${packageId}::stream::Stream<${coinType}>`],
|
|
115
|
+
arguments: [stream],
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
// ===== Stream Actions =====
|
|
119
|
+
/**
|
|
120
|
+
* Triggers a payment tick on a continuous stream.
|
|
121
|
+
* Permissionless — anyone can call this.
|
|
122
|
+
*
|
|
123
|
+
* Move: `piper::stream::tick<T>(stream, clock, ctx)`
|
|
124
|
+
*
|
|
125
|
+
* The Move function handles all coin transfers internally.
|
|
126
|
+
* Funds are sent directly to the primary recipient and any split recipients on-chain.
|
|
127
|
+
*/
|
|
128
|
+
static tick(tx, config) {
|
|
129
|
+
const packageId = Piper.getPackageId();
|
|
130
|
+
tx.moveCall({
|
|
131
|
+
target: target(packageId, 'tick'),
|
|
132
|
+
typeArguments: [config.coinType],
|
|
133
|
+
arguments: [
|
|
134
|
+
resolveObject(tx, config.streamId),
|
|
135
|
+
tx.object.clock(),
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Performs an on-demand payment from a pay-per-use stream.
|
|
141
|
+
* Must be called by the `authorizedSpender` — the transaction must be
|
|
142
|
+
* signed by the spender's key.
|
|
143
|
+
*
|
|
144
|
+
* Move: `piper::stream::pay<T>(stream, amount, clock, ctx)`
|
|
145
|
+
*/
|
|
146
|
+
static pay(tx, config) {
|
|
147
|
+
const packageId = Piper.getPackageId();
|
|
148
|
+
tx.moveCall({
|
|
149
|
+
target: target(packageId, 'pay'),
|
|
150
|
+
typeArguments: [config.coinType],
|
|
151
|
+
arguments: [
|
|
152
|
+
resolveObject(tx, config.streamId),
|
|
153
|
+
tx.pure.u64(BigInt(config.amount)),
|
|
154
|
+
tx.object.clock(),
|
|
155
|
+
],
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Revokes a stream and returns the remaining balance as a `Coin<T>`.
|
|
160
|
+
* Only the stream creator can call this (enforced on-chain).
|
|
161
|
+
*
|
|
162
|
+
* Move: `piper::stream::revoke<T>(stream, clock, ctx): Coin<T>`
|
|
163
|
+
*
|
|
164
|
+
* @returns A `TransactionResult` representing the remaining `Coin<T>`.
|
|
165
|
+
* You must transfer or consume this coin in the same transaction.
|
|
166
|
+
*/
|
|
167
|
+
static revoke(tx, config) {
|
|
168
|
+
const packageId = Piper.getPackageId();
|
|
169
|
+
return tx.moveCall({
|
|
170
|
+
target: target(packageId, 'revoke'),
|
|
171
|
+
typeArguments: [config.coinType],
|
|
172
|
+
arguments: [
|
|
173
|
+
resolveObject(tx, config.streamId),
|
|
174
|
+
tx.object.clock(),
|
|
175
|
+
],
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Adds additional funds to an existing stream.
|
|
180
|
+
* Anyone can top up a stream.
|
|
181
|
+
*
|
|
182
|
+
* Move: `piper::stream::top_up<T>(stream, coin)`
|
|
183
|
+
*/
|
|
184
|
+
static topUp(tx, config) {
|
|
185
|
+
const packageId = Piper.getPackageId();
|
|
186
|
+
tx.moveCall({
|
|
187
|
+
target: target(packageId, 'top_up'),
|
|
188
|
+
typeArguments: [config.coinType],
|
|
189
|
+
arguments: [
|
|
190
|
+
resolveObject(tx, config.streamId),
|
|
191
|
+
resolveObject(tx, config.coin),
|
|
192
|
+
],
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
// ===== Split Management =====
|
|
196
|
+
/**
|
|
197
|
+
* Adds a percentage-based split recipient to a stream.
|
|
198
|
+
* Only the stream creator can manage splits (enforced on-chain).
|
|
199
|
+
* Total split percentages cannot exceed 100%.
|
|
200
|
+
*
|
|
201
|
+
* Move: `piper::stream::add_split<T>(stream, recipient, percent, ctx)`
|
|
202
|
+
*/
|
|
203
|
+
static addSplit(tx, config) {
|
|
204
|
+
const packageId = Piper.getPackageId();
|
|
205
|
+
tx.moveCall({
|
|
206
|
+
target: target(packageId, 'add_split'),
|
|
207
|
+
typeArguments: [config.coinType],
|
|
208
|
+
arguments: [
|
|
209
|
+
resolveObject(tx, config.streamId),
|
|
210
|
+
tx.pure.address(config.recipient),
|
|
211
|
+
tx.pure.u8(config.percent),
|
|
212
|
+
],
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Removes a split by index.
|
|
217
|
+
* Only the stream creator can manage splits (enforced on-chain).
|
|
218
|
+
*
|
|
219
|
+
* Move: `piper::stream::remove_split<T>(stream, index, ctx)`
|
|
220
|
+
*/
|
|
221
|
+
static removeSplit(tx, config) {
|
|
222
|
+
const packageId = Piper.getPackageId();
|
|
223
|
+
tx.moveCall({
|
|
224
|
+
target: target(packageId, 'remove_split'),
|
|
225
|
+
typeArguments: [config.coinType],
|
|
226
|
+
arguments: [
|
|
227
|
+
resolveObject(tx, config.streamId),
|
|
228
|
+
tx.pure.u64(BigInt(config.index)),
|
|
229
|
+
],
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
// ===== Destruction =====
|
|
233
|
+
/**
|
|
234
|
+
* Destroys an inactive stream with zero balance.
|
|
235
|
+
* Deletes the `Stream` object from on-chain storage.
|
|
236
|
+
*
|
|
237
|
+
* Move: `piper::stream::destroy_empty<T>(stream)`
|
|
238
|
+
*
|
|
239
|
+
* Note: The stream is consumed by value — if you pass a string ID,
|
|
240
|
+
* the SDK resolves it as an owned object.
|
|
241
|
+
*/
|
|
242
|
+
static destroyEmpty(tx, config) {
|
|
243
|
+
const packageId = Piper.getPackageId();
|
|
244
|
+
tx.moveCall({
|
|
245
|
+
target: target(packageId, 'destroy_empty'),
|
|
246
|
+
typeArguments: [config.coinType],
|
|
247
|
+
arguments: [
|
|
248
|
+
resolveObject(tx, config.streamId),
|
|
249
|
+
],
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
//# sourceMappingURL=piper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"piper.js","sourceRoot":"","sources":["../src/piper.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH,OAAO,EAAE,wBAAwB,EAAmB,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAanF;;;;GAIG;AACH,SAAS,aAAa,CACpB,EAAe,EACf,KAAmC;IAEnC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,MAAM,OAAO,KAAK;IAChB,qCAAqC;IAC7B,MAAM,CAAC,UAAU,GAAW,wBAAwB,CAAC;IAE7D,4BAA4B;IAE5B;;;OAGG;IACH,MAAM,CAAC,YAAY,CAAC,SAAiB;QACnC,KAAK,CAAC,UAAU,GAAG,SAAS,CAAC;IAC/B,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,YAAY;QACjB,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CACb,+EAA+E,CAChF,CAAC;QACJ,CAAC;QACD,OAAO,KAAK,CAAC,UAAU,CAAC;IAC1B,CAAC;IAED,8BAA8B;IAE9B;;;;;;;OAOG;IACH,MAAM,CAAC,sBAAsB,CAC3B,EAAe,EACf,MAAoC;QAEpC,MAAM,SAAS,GAAG,KAAK,CAAC,YAAY,EAAE,CAAC;QAEvC,OAAO,EAAE,CAAC,QAAQ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,CAAC;YACV,MAAM,EAAE,MAAM,CAAC,SAAS,EAAE,cAAc,CAAC;YACzC,aAAa,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC;YAChC,SAAS,EAAE;gBACT,aAAa,CAAC,EAAE,EAAE,MAAM,CAAC,QAAQ,CAAC;gBAClC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;aAClC;SACF,CAAC,CAAC;IACL,CAAC;IAED,0BAA0B;IAE1B;;;;;;;;OAQG;IACH,MAAM,CAAC,YAAY,CAAC,EAAe,EAAE,MAA0B;QAC7D,MAAM,SAAS,GAAG,KAAK,CAAC,YAAY,EAAE,CAAC;QAEvC,EAAE,CAAC,QAAQ,CAAC;YACV,MAAM,EAAE,MAAM,CAAC,SAAS,EAAE,eAAe,CAAC;YAC1C,aAAa,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC;YAChC,SAAS,EAAE;gBACT,aAAa,CAAC,EAAE,EAAE,MAAM,CAAC,QAAQ,CAAC;aACnC;SACF,CAAC,CAAC;IACL,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Piper SDK Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* TypeScript interfaces for all SDK config objects and on-chain event types.
|
|
5
|
+
*/
|
|
6
|
+
import type { TransactionArgument } from '@mysten/sui/transactions';
|
|
7
|
+
/** Configuration for creating a continuous (time-based) stream */
|
|
8
|
+
export interface CreateContinuousStreamConfig {
|
|
9
|
+
/** The Coin<T> object to deposit — a TransactionArgument or object ID string */
|
|
10
|
+
coin: TransactionArgument | string;
|
|
11
|
+
/** Full coin type, e.g. '0x2::sui::SUI' */
|
|
12
|
+
coinType: string;
|
|
13
|
+
/** Tokens (in smallest units) to stream per second */
|
|
14
|
+
flowRate: number | bigint;
|
|
15
|
+
/** Sui address of the primary recipient */
|
|
16
|
+
recipient: string;
|
|
17
|
+
}
|
|
18
|
+
/** Configuration for creating an on-demand (pay-per-use) stream */
|
|
19
|
+
export interface CreateOnDemandStreamConfig {
|
|
20
|
+
/** The Coin<T> object to deposit — a TransactionArgument or object ID string */
|
|
21
|
+
coin: TransactionArgument | string;
|
|
22
|
+
/** Full coin type, e.g. '0x2::sui::SUI' */
|
|
23
|
+
coinType: string;
|
|
24
|
+
/** Sui address that receives payments */
|
|
25
|
+
recipient: string;
|
|
26
|
+
/** Sui address allowed to call `pay` */
|
|
27
|
+
authorizedSpender: string;
|
|
28
|
+
}
|
|
29
|
+
/** Configuration for ticking a continuous stream */
|
|
30
|
+
export interface TickConfig {
|
|
31
|
+
/** The Stream<T> object ID or TransactionArgument */
|
|
32
|
+
streamId: string | TransactionArgument;
|
|
33
|
+
/** Full coin type of the stream */
|
|
34
|
+
coinType: string;
|
|
35
|
+
}
|
|
36
|
+
/** Configuration for an on-demand payment */
|
|
37
|
+
export interface PayConfig {
|
|
38
|
+
/** The Stream<T> object ID or TransactionArgument */
|
|
39
|
+
streamId: string | TransactionArgument;
|
|
40
|
+
/** Full coin type of the stream */
|
|
41
|
+
coinType: string;
|
|
42
|
+
/** Amount to deduct (in smallest units) */
|
|
43
|
+
amount: number | bigint;
|
|
44
|
+
}
|
|
45
|
+
/** Configuration for revoking a stream */
|
|
46
|
+
export interface RevokeConfig {
|
|
47
|
+
/** The Stream<T> object ID or TransactionArgument */
|
|
48
|
+
streamId: string | TransactionArgument;
|
|
49
|
+
/** Full coin type of the stream */
|
|
50
|
+
coinType: string;
|
|
51
|
+
}
|
|
52
|
+
/** Configuration for topping up a stream */
|
|
53
|
+
export interface TopUpConfig {
|
|
54
|
+
/** The Stream<T> object ID or TransactionArgument */
|
|
55
|
+
streamId: string | TransactionArgument;
|
|
56
|
+
/** Full coin type of the stream */
|
|
57
|
+
coinType: string;
|
|
58
|
+
/** The Coin<T> object to deposit — a TransactionArgument or object ID string */
|
|
59
|
+
coin: TransactionArgument | string;
|
|
60
|
+
}
|
|
61
|
+
/** Configuration for adding a split to a stream */
|
|
62
|
+
export interface AddSplitConfig {
|
|
63
|
+
/** The Stream<T> object ID or TransactionArgument */
|
|
64
|
+
streamId: string | TransactionArgument;
|
|
65
|
+
/** Full coin type of the stream */
|
|
66
|
+
coinType: string;
|
|
67
|
+
/** Sui address that will receive this split */
|
|
68
|
+
recipient: string;
|
|
69
|
+
/** Percentage share (1–100) */
|
|
70
|
+
percent: number;
|
|
71
|
+
}
|
|
72
|
+
/** Configuration for removing a split from a stream */
|
|
73
|
+
export interface RemoveSplitConfig {
|
|
74
|
+
/** The Stream<T> object ID or TransactionArgument */
|
|
75
|
+
streamId: string | TransactionArgument;
|
|
76
|
+
/** Full coin type of the stream */
|
|
77
|
+
coinType: string;
|
|
78
|
+
/** Index of the split to remove */
|
|
79
|
+
index: number;
|
|
80
|
+
}
|
|
81
|
+
/** Configuration for destroying an empty stream */
|
|
82
|
+
export interface DestroyEmptyConfig {
|
|
83
|
+
/** The Stream<T> object ID or TransactionArgument */
|
|
84
|
+
streamId: string | TransactionArgument;
|
|
85
|
+
/** Full coin type of the stream */
|
|
86
|
+
coinType: string;
|
|
87
|
+
}
|
|
88
|
+
/** Emitted when a new stream is created */
|
|
89
|
+
export interface StreamCreatedEvent {
|
|
90
|
+
stream_id: string;
|
|
91
|
+
sender: string;
|
|
92
|
+
recipient: string;
|
|
93
|
+
flow_rate: string;
|
|
94
|
+
initial_balance: string;
|
|
95
|
+
}
|
|
96
|
+
/** Emitted on every tick or on-demand payment to the primary recipient */
|
|
97
|
+
export interface PaymentSentEvent {
|
|
98
|
+
stream_id: string;
|
|
99
|
+
recipient: string;
|
|
100
|
+
amount: string;
|
|
101
|
+
timestamp: string;
|
|
102
|
+
}
|
|
103
|
+
/** Emitted when a stream is revoked by its creator */
|
|
104
|
+
export interface StreamRevokedEvent {
|
|
105
|
+
stream_id: string;
|
|
106
|
+
remaining_balance: string;
|
|
107
|
+
}
|
|
108
|
+
/** Emitted when additional funds are added to a stream */
|
|
109
|
+
export interface StreamToppedUpEvent {
|
|
110
|
+
stream_id: string;
|
|
111
|
+
amount: string;
|
|
112
|
+
new_balance: string;
|
|
113
|
+
}
|
|
114
|
+
/** Emitted when a split payment is distributed */
|
|
115
|
+
export interface SplitPaymentSentEvent {
|
|
116
|
+
stream_id: string;
|
|
117
|
+
recipient: string;
|
|
118
|
+
amount: string;
|
|
119
|
+
percent: number;
|
|
120
|
+
}
|
|
121
|
+
/** Emitted when a split is added to a stream */
|
|
122
|
+
export interface SplitAddedEvent {
|
|
123
|
+
stream_id: string;
|
|
124
|
+
recipient: string;
|
|
125
|
+
percent: number;
|
|
126
|
+
}
|
|
127
|
+
/** Emitted when a split is removed from a stream */
|
|
128
|
+
export interface SplitRemovedEvent {
|
|
129
|
+
stream_id: string;
|
|
130
|
+
recipient: string;
|
|
131
|
+
percent: number;
|
|
132
|
+
}
|
|
133
|
+
/** Union type of all Piper events */
|
|
134
|
+
export type PiperEvent = {
|
|
135
|
+
type: 'StreamCreated';
|
|
136
|
+
data: StreamCreatedEvent;
|
|
137
|
+
} | {
|
|
138
|
+
type: 'PaymentSent';
|
|
139
|
+
data: PaymentSentEvent;
|
|
140
|
+
} | {
|
|
141
|
+
type: 'StreamRevoked';
|
|
142
|
+
data: StreamRevokedEvent;
|
|
143
|
+
} | {
|
|
144
|
+
type: 'StreamToppedUp';
|
|
145
|
+
data: StreamToppedUpEvent;
|
|
146
|
+
} | {
|
|
147
|
+
type: 'SplitPaymentSent';
|
|
148
|
+
data: SplitPaymentSentEvent;
|
|
149
|
+
} | {
|
|
150
|
+
type: 'SplitAdded';
|
|
151
|
+
data: SplitAddedEvent;
|
|
152
|
+
} | {
|
|
153
|
+
type: 'SplitRemoved';
|
|
154
|
+
data: SplitRemovedEvent;
|
|
155
|
+
};
|
|
156
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names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M,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,kDAAkD;AAClD,MAAM,WAAW,qBAAqB;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,gDAAgD;AAChD,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,oDAAoD;AACpD,MAAM,WAAW,iBAAiB;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,qCAAqC;AACrC,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,IAAI,EAAE,kBAAkB,CAAA;CAAE,GACnD;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,IAAI,EAAE,gBAAgB,CAAA;CAAE,GAC/C;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,IAAI,EAAE,kBAAkB,CAAA;CAAE,GACnD;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,IAAI,EAAE,mBAAmB,CAAA;CAAE,GACrD;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,IAAI,EAAE,qBAAqB,CAAA;CAAE,GACzD;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,IAAI,EAAE,eAAe,CAAA;CAAE,GAC7C;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,IAAI,EAAE,iBAAiB,CAAA;CAAE,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Piper SDK — Utility Functions
|
|
3
|
+
*
|
|
4
|
+
* Helper functions for extracting stream IDs from transaction results,
|
|
5
|
+
* calculating flow rates, and parsing Piper events.
|
|
6
|
+
*/
|
|
7
|
+
import type { PiperEvent } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Extracts the created `Stream` object ID from a transaction result.
|
|
10
|
+
*
|
|
11
|
+
* Looks for a `StreamCreated` event in the transaction events and returns
|
|
12
|
+
* the `stream_id` field. Use this after calling `createContinuousStream`
|
|
13
|
+
* or `createOnDemandStream`.
|
|
14
|
+
*
|
|
15
|
+
* @param result - The transaction execution result. Must include events
|
|
16
|
+
* (use `include: { events: true }` when executing).
|
|
17
|
+
* @returns The stream object ID as a hex string.
|
|
18
|
+
* @throws If no `StreamCreated` event is found in the result.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const result = await client.signAndExecuteTransaction({
|
|
23
|
+
* signer: keypair,
|
|
24
|
+
* transaction: tx,
|
|
25
|
+
* include: { events: true },
|
|
26
|
+
* });
|
|
27
|
+
* const streamId = extractStreamId(result);
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function extractStreamId(result: {
|
|
31
|
+
events?: Array<{
|
|
32
|
+
type: string;
|
|
33
|
+
parsedJson?: Record<string, unknown>;
|
|
34
|
+
}>;
|
|
35
|
+
}): string;
|
|
36
|
+
/**
|
|
37
|
+
* Computes the flow rate needed to stream a total amount evenly over a duration.
|
|
38
|
+
*
|
|
39
|
+
* @param totalAmount - Total tokens to stream (in smallest units, e.g. MIST for SUI).
|
|
40
|
+
* @param durationSeconds - Duration in seconds.
|
|
41
|
+
* @returns The per-second flow rate as a `bigint`.
|
|
42
|
+
* @throws If `durationSeconds` is zero or negative.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // 100 USDC over 30 days (USDC has 6 decimals)
|
|
47
|
+
* const flowRate = calculateFlowRate(100_000_000n, 30 * 24 * 3600);
|
|
48
|
+
* // flowRate = 38n (≈ 38 microUSDC per second)
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function calculateFlowRate(totalAmount: bigint, durationSeconds: number): bigint;
|
|
52
|
+
/**
|
|
53
|
+
* Parses all Piper events from a transaction result into typed event objects.
|
|
54
|
+
*
|
|
55
|
+
* @param result - The transaction execution result with events included.
|
|
56
|
+
* @returns An array of typed `PiperEvent` objects.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const events = parsePiperEvents(result);
|
|
61
|
+
* for (const event of events) {
|
|
62
|
+
* if (event.type === 'PaymentSent') {
|
|
63
|
+
* console.log(`Payment of ${event.data.amount} to ${event.data.recipient}`);
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function parsePiperEvents(result: {
|
|
69
|
+
events?: Array<{
|
|
70
|
+
type: string;
|
|
71
|
+
parsedJson?: Record<string, unknown>;
|
|
72
|
+
}>;
|
|
73
|
+
}): PiperEvent[];
|
|
74
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,UAAU,EAQX,MAAM,YAAY,CAAC;AAapB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE;IACtC,MAAM,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,CAAC,CAAC;CACxE,GAAG,MAAM,CA4BT;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,CAC/B,WAAW,EAAE,MAAM,EACnB,eAAe,EAAE,MAAM,GACtB,MAAM,CAKR;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE;IACvC,MAAM,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,CAAC,CAAC;CACxE,GAAG,UAAU,EAAE,CAiDf"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Piper SDK — Utility Functions
|
|
3
|
+
*
|
|
4
|
+
* Helper functions for extracting stream IDs from transaction results,
|
|
5
|
+
* calculating flow rates, and parsing Piper events.
|
|
6
|
+
*/
|
|
7
|
+
/** Event type suffixes used to identify Piper events in transaction results */
|
|
8
|
+
const EVENT_TYPES = {
|
|
9
|
+
StreamCreated: '::events::StreamCreated',
|
|
10
|
+
PaymentSent: '::events::PaymentSent',
|
|
11
|
+
StreamRevoked: '::events::StreamRevoked',
|
|
12
|
+
StreamToppedUp: '::events::StreamToppedUp',
|
|
13
|
+
SplitPaymentSent: '::events::SplitPaymentSent',
|
|
14
|
+
SplitAdded: '::events::SplitAdded',
|
|
15
|
+
SplitRemoved: '::events::SplitRemoved',
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Extracts the created `Stream` object ID from a transaction result.
|
|
19
|
+
*
|
|
20
|
+
* Looks for a `StreamCreated` event in the transaction events and returns
|
|
21
|
+
* the `stream_id` field. Use this after calling `createContinuousStream`
|
|
22
|
+
* or `createOnDemandStream`.
|
|
23
|
+
*
|
|
24
|
+
* @param result - The transaction execution result. Must include events
|
|
25
|
+
* (use `include: { events: true }` when executing).
|
|
26
|
+
* @returns The stream object ID as a hex string.
|
|
27
|
+
* @throws If no `StreamCreated` event is found in the result.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const result = await client.signAndExecuteTransaction({
|
|
32
|
+
* signer: keypair,
|
|
33
|
+
* transaction: tx,
|
|
34
|
+
* include: { events: true },
|
|
35
|
+
* });
|
|
36
|
+
* const streamId = extractStreamId(result);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function extractStreamId(result) {
|
|
40
|
+
const events = result.events;
|
|
41
|
+
if (!events || events.length === 0) {
|
|
42
|
+
throw new Error('No events found in transaction result. Did you include events in the response? ' +
|
|
43
|
+
'Use `include: { events: true }` when executing the transaction.');
|
|
44
|
+
}
|
|
45
|
+
const streamEvent = events.find((e) => e.type.endsWith(EVENT_TYPES.StreamCreated));
|
|
46
|
+
if (!streamEvent || !streamEvent.parsedJson) {
|
|
47
|
+
throw new Error('No StreamCreated event found in transaction result. ' +
|
|
48
|
+
'Make sure the transaction includes a createContinuousStream or createOnDemandStream call.');
|
|
49
|
+
}
|
|
50
|
+
const streamId = streamEvent.parsedJson['stream_id'];
|
|
51
|
+
if (!streamId) {
|
|
52
|
+
throw new Error('StreamCreated event found but stream_id field is missing.');
|
|
53
|
+
}
|
|
54
|
+
return streamId;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Computes the flow rate needed to stream a total amount evenly over a duration.
|
|
58
|
+
*
|
|
59
|
+
* @param totalAmount - Total tokens to stream (in smallest units, e.g. MIST for SUI).
|
|
60
|
+
* @param durationSeconds - Duration in seconds.
|
|
61
|
+
* @returns The per-second flow rate as a `bigint`.
|
|
62
|
+
* @throws If `durationSeconds` is zero or negative.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* // 100 USDC over 30 days (USDC has 6 decimals)
|
|
67
|
+
* const flowRate = calculateFlowRate(100_000_000n, 30 * 24 * 3600);
|
|
68
|
+
* // flowRate = 38n (≈ 38 microUSDC per second)
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function calculateFlowRate(totalAmount, durationSeconds) {
|
|
72
|
+
if (durationSeconds <= 0) {
|
|
73
|
+
throw new Error('durationSeconds must be greater than zero.');
|
|
74
|
+
}
|
|
75
|
+
return totalAmount / BigInt(durationSeconds);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Parses all Piper events from a transaction result into typed event objects.
|
|
79
|
+
*
|
|
80
|
+
* @param result - The transaction execution result with events included.
|
|
81
|
+
* @returns An array of typed `PiperEvent` objects.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const events = parsePiperEvents(result);
|
|
86
|
+
* for (const event of events) {
|
|
87
|
+
* if (event.type === 'PaymentSent') {
|
|
88
|
+
* console.log(`Payment of ${event.data.amount} to ${event.data.recipient}`);
|
|
89
|
+
* }
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export function parsePiperEvents(result) {
|
|
94
|
+
const events = result.events;
|
|
95
|
+
if (!events)
|
|
96
|
+
return [];
|
|
97
|
+
const parsed = [];
|
|
98
|
+
for (const event of events) {
|
|
99
|
+
const json = event.parsedJson;
|
|
100
|
+
if (!json)
|
|
101
|
+
continue;
|
|
102
|
+
if (event.type.endsWith(EVENT_TYPES.StreamCreated)) {
|
|
103
|
+
parsed.push({
|
|
104
|
+
type: 'StreamCreated',
|
|
105
|
+
data: json,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else if (event.type.endsWith(EVENT_TYPES.PaymentSent)) {
|
|
109
|
+
parsed.push({
|
|
110
|
+
type: 'PaymentSent',
|
|
111
|
+
data: json,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
else if (event.type.endsWith(EVENT_TYPES.StreamRevoked)) {
|
|
115
|
+
parsed.push({
|
|
116
|
+
type: 'StreamRevoked',
|
|
117
|
+
data: json,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
else if (event.type.endsWith(EVENT_TYPES.StreamToppedUp)) {
|
|
121
|
+
parsed.push({
|
|
122
|
+
type: 'StreamToppedUp',
|
|
123
|
+
data: json,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
else if (event.type.endsWith(EVENT_TYPES.SplitPaymentSent)) {
|
|
127
|
+
parsed.push({
|
|
128
|
+
type: 'SplitPaymentSent',
|
|
129
|
+
data: json,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
else if (event.type.endsWith(EVENT_TYPES.SplitAdded)) {
|
|
133
|
+
parsed.push({
|
|
134
|
+
type: 'SplitAdded',
|
|
135
|
+
data: json,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
else if (event.type.endsWith(EVENT_TYPES.SplitRemoved)) {
|
|
139
|
+
parsed.push({
|
|
140
|
+
type: 'SplitRemoved',
|
|
141
|
+
data: json,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return parsed;
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH,+EAA+E;AAC/E,MAAM,WAAW,GAAG;IAClB,aAAa,EAAE,yBAAyB;IACxC,WAAW,EAAE,uBAAuB;IACpC,aAAa,EAAE,yBAAyB;IACxC,cAAc,EAAE,0BAA0B;IAC1C,gBAAgB,EAAE,4BAA4B;IAC9C,UAAU,EAAE,sBAAsB;IAClC,YAAY,EAAE,wBAAwB;CAC9B,CAAC;AAEX;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,eAAe,CAAC,MAE/B;IACC,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IAC7B,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CACb,iFAAiF;YAC/E,iEAAiE,CACpE,CAAC;IACJ,CA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