@streamflow/stream 2.1.1 → 3.0.0-test.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 +108 -71
- package/dist/{sdk.d.ts → Stream.d.ts} +2 -2
- package/dist/{sdk.js → Stream.js} +24 -37
- package/dist/StreamClient.d.ts +107 -0
- package/dist/StreamClient.js +747 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +4 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +8 -5
- package/dist/instructions.d.ts +97 -0
- package/dist/instructions.js +210 -0
- package/dist/layout.d.ts +18 -0
- package/dist/layout.js +73 -0
- package/dist/types.d.ts +91 -36
- package/dist/utils.d.ts +6 -6
- package/dist/utils.js +32 -74
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
**Important: Security audit for the v2 is underway.**
|
|
2
|
-
|
|
3
1
|
Docs: https://streamflow.finance/js-sdk/
|
|
4
2
|
|
|
5
3
|
# Streamflow
|
|
@@ -8,86 +6,134 @@ Streamflow is a token distribution platform powered by a streaming payments' pro
|
|
|
8
6
|
|
|
9
7
|
There are several ways to use **Streamflow**:
|
|
10
8
|
|
|
11
|
-
- **(easiest) [app.streamflow.finance](https://app.streamflow.finance)** (React application that uses JS SDK directly)
|
|
12
|
-
- **[JS SDK](https://github.com/streamflow-finance/js-sdk)** to interact with the protocol => [NPM package](https://www.npmjs.com/package/@streamflow/stream
|
|
9
|
+
- **(easiest) [app.streamflow.finance](https://app.streamflow.finance?utm_medium=github.com&utm_source=referral&utm_campaign=js-sdk-repo)** (React application that uses JS SDK directly)
|
|
10
|
+
- **[JS SDK](https://github.com/streamflow-finance/js-sdk)** to interact with the protocol => [NPM package](https://www.npmjs.com/package/@streamflow/stream)
|
|
13
11
|
- **[Rust SDK](https://github.com/streamflow-finance/rust-sdk)** to integrate within a Solana program => [Rust Crate](https://docs.rs/streamflow-sdk/)
|
|
14
12
|
|
|
15
|
-
|
|
13
|
+
**Security audit passed ✅**
|
|
14
|
+
|
|
15
|
+
Protocol audit available [here](https://github.com/streamflow-finance/rust-sdk/blob/main/protocol_audit.pdf).
|
|
16
|
+
Partner oracle audit available here [here](https://github.com/streamflow-finance/rust-sdk/blob/main/partner_oracle_audit.pdf).
|
|
17
|
+
|
|
18
|
+
## JS SDK to interact with Streamflow protocol.
|
|
16
19
|
|
|
17
|
-
This
|
|
20
|
+
This package allows you to `create`, `createMultiple`, `withdraw`, `cancel`, `topup` and `transfer` SPL token stream.
|
|
18
21
|
|
|
19
22
|
You can also `getOne` stream and `get` multiple streams.
|
|
20
23
|
|
|
21
24
|
---
|
|
22
25
|
|
|
23
|
-
|
|
26
|
+
## Installation
|
|
24
27
|
|
|
25
28
|
`npm i @streamflow/stream @solana/web3.js @project-serum/anchor`
|
|
26
29
|
|
|
27
30
|
> _Anchor is needed for the `Wallet` type. We plan on removing this dependency in upcoming releases._
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
```javascript
|
|
32
|
-
import { Connection } from "@solana/web3.js";
|
|
33
|
-
import { Wallet } from "@project-serum/anchor/src/provider";
|
|
34
|
-
import BN from "bn.js";
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
### Usage
|
|
38
|
-
|
|
39
|
-
#### Imports
|
|
32
|
+
## Import SDK
|
|
40
33
|
|
|
41
34
|
Most common imports:
|
|
42
35
|
|
|
43
36
|
```javascript
|
|
44
|
-
import
|
|
37
|
+
import {
|
|
38
|
+
StreamClient,
|
|
45
39
|
Stream,
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
40
|
+
CreateParams,
|
|
41
|
+
CreateMultiParams,
|
|
42
|
+
WithdrawParams,
|
|
43
|
+
TransferParams,
|
|
44
|
+
TopupParams,
|
|
45
|
+
CancelParams,
|
|
46
|
+
GetAllParams,
|
|
53
47
|
StreamDirection,
|
|
54
48
|
StreamType,
|
|
55
49
|
Cluster,
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
TxResponse,
|
|
51
|
+
CreateResponse,
|
|
52
|
+
BN,
|
|
53
|
+
getBN,
|
|
54
|
+
getNumberFromBN,
|
|
58
55
|
} from "@streamflow/stream";
|
|
59
56
|
```
|
|
60
57
|
|
|
61
58
|
_Check the SDK for other types and utility functions._
|
|
62
59
|
|
|
63
|
-
|
|
60
|
+
## Create StreamClient instance
|
|
61
|
+
|
|
62
|
+
Before creating and manipulating streams StreamClient instance must be created.
|
|
63
|
+
All streams functions are methods on this instance.
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
import { StreamClient } from "@streamflow/stream";
|
|
67
|
+
|
|
68
|
+
const StreamClient = new StreamClient(
|
|
69
|
+
"https://api.mainnet-beta.solana.com",
|
|
70
|
+
Cluster.Mainnet,
|
|
71
|
+
"confirmed"
|
|
72
|
+
);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Create stream
|
|
64
76
|
|
|
65
77
|
```javascript
|
|
66
78
|
const createStreamParams = {
|
|
67
|
-
|
|
68
|
-
sender: wallet, // Wallet signing the transaction, creating and sending the stream.
|
|
79
|
+
sender: wallet, // Wallet/Keypair signing the transaction, creating and sending the stream.
|
|
69
80
|
recipient: "4ih00075bKjVg000000tLdk4w42NyG3Mv0000dc0M00", // Solana recipient address.
|
|
70
81
|
mint: "DNw99999M7e24g99999999WJirKeZ5fQc6KY999999gK", // SPL Token mint.
|
|
71
82
|
start: 1643363040, // Timestamp (in seconds) when the stream/token vesting starts.
|
|
72
|
-
depositedAmount:
|
|
83
|
+
depositedAmount: getBN(1000000000000, 9), // Deposited amount of tokens (using smallest denomination).
|
|
73
84
|
period: 1, // Time step (period) in seconds per which the unlocking occurs.
|
|
74
85
|
cliff: 1643363160, // Vesting contract "cliff" timestamp in seconds.
|
|
75
86
|
cliffAmount: new BN(100000000000), // Amount (smallest denomination) unlocked at the "cliff" timestamp.
|
|
76
|
-
amountPerPeriod:
|
|
87
|
+
amountPerPeriod: getBN(5000000000, 9), // Release rate: how many tokens are unlocked per each period.
|
|
77
88
|
name: "Transfer to Jane Doe.", // The stream name or subject.
|
|
78
89
|
canTopup: false, // setting to FALSE will effectively create a vesting contract.
|
|
79
90
|
cancelableBySender: true, // Whether or not sender can cancel the stream.
|
|
80
91
|
cancelableByRecipient: false, // Whether or not recipient can cancel the stream.
|
|
81
|
-
transferableBySender: true, // Whether or not sender can transfer the stream
|
|
82
|
-
transferableByRecipient: false, // Whether or not recipient can transfer the stream
|
|
83
|
-
automaticWithdrawal: true, // [WIP] Whether or not a 3rd party (e.g. cron job, "cranker") can initiate a token withdraw/transfer.
|
|
92
|
+
transferableBySender: true, // Whether or not sender can transfer the stream.
|
|
93
|
+
transferableByRecipient: false, // Whether or not recipient can transfer the stream.
|
|
94
|
+
automaticWithdrawal: true, // [WIP] Whether or not a 3rd party (e.g. cron job, "cranker") can initiate a token withdraw/transfer.
|
|
84
95
|
withdrawalFrequency: 10, // [WIP] Relevant when automatic withdrawal is enabled. If greater than 0 our withdrawor will take care of withdrawals. If equal to 0 our withdrawor will skip, but everyone else can initiate withdrawals.
|
|
85
|
-
partner:
|
|
86
|
-
cluster: Cluster.Mainnet, // (optional) Cluster (default is Cluster.Mainnet).
|
|
96
|
+
partner: null, // (optional) Partner's wallet address (string | null).
|
|
87
97
|
};
|
|
88
98
|
|
|
89
99
|
try {
|
|
90
|
-
const { tx,
|
|
100
|
+
const { ixs, tx, metadata } = await StreamClient.create(createStreamParams);
|
|
101
|
+
} catch (exception) {
|
|
102
|
+
// handle exception
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Create multiple streams at once
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
const recipients = [
|
|
110
|
+
{
|
|
111
|
+
recipient: "4ih00075bKjVg000000tLdk4w42NyG3Mv0000dc0M00", // Solana recipient address.
|
|
112
|
+
depositedAmount: getBN(1000000000000, 9), // Deposited amount of tokens (in the smallest units).
|
|
113
|
+
name: "January Payroll", // The stream name/subject.
|
|
114
|
+
},
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
const createMultiStreamsParams = {
|
|
118
|
+
sender: wallet, // Wallet/Keypair signing the transaction, creating and sending the stream.
|
|
119
|
+
recipientsData: recipients, // Array of Solana recipient address.
|
|
120
|
+
mint: "DNw99999M7e24g99999999WJirKeZ5fQc6KY999999gK", // SPL Token mint.
|
|
121
|
+
start: 1643363040, // Timestamp (in seconds) when the stream/token vesting starts.
|
|
122
|
+
period: 1, // Time step (period) in seconds per which the unlocking occurs.
|
|
123
|
+
cliff: 1643363160, // Vesting contract "cliff" timestamp in seconds.
|
|
124
|
+
cliffAmount: getBN(100000000000, 9), // Amount unlocked at the "cliff" timestamp.
|
|
125
|
+
amountPerPeriod: getBN(5000000000, 9), // Release rate: how many tokens are unlocked per each period.
|
|
126
|
+
canTopup: true, // setting to FALSE will effectively create a vesting contract.
|
|
127
|
+
cancelableBySender: true, // Whether or not sender can cancel the stream.
|
|
128
|
+
cancelableByRecipient: false, // Whether or not recipient can cancel the stream.
|
|
129
|
+
transferableBySender: true, // Whether or not sender can transfer the stream.
|
|
130
|
+
transferableByRecipient: false, // Whether or not recipient can transfer the stream.
|
|
131
|
+
automaticWithdrawal: false, // Whether or not a 3rd party can initiate withdraw in the name of recipient (currently not used, set it to FALSE).
|
|
132
|
+
partner: null, // (optional) Partner's wallet address (string | null).
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
const { txs } = await StreamClient.createMultiple(createMultiStreamsParams);
|
|
91
137
|
} catch (exception) {
|
|
92
138
|
// handle exception
|
|
93
139
|
}
|
|
@@ -97,100 +143,89 @@ try {
|
|
|
97
143
|
|
|
98
144
|
Please note that transaction fees for the scheduled transfers are paid upfront by the stream creator (sender).\_
|
|
99
145
|
|
|
100
|
-
|
|
146
|
+
## Withdraw from stream
|
|
101
147
|
|
|
102
148
|
```javascript
|
|
103
149
|
const withdrawStreamParams = {
|
|
104
|
-
|
|
105
|
-
invoker: wallet, // Wallet signing the transaction.
|
|
150
|
+
invoker: wallet, // Wallet/Keypair signing the transaction.
|
|
106
151
|
id: "AAAAyotqTZZMAAAAmsD1JAgksT8NVAAAASfrGB5RAAAA", // Identifier of a stream to be withdrawn from.
|
|
107
|
-
amount:
|
|
108
|
-
cluster: Cluster.Mainnet, // Cluster (optional, default is Cluster.Mainnet).
|
|
152
|
+
amount: getBN(100000000000, 9), // Requested amount to withdraw. If stream is completed, the whole amount will be withdrawn.
|
|
109
153
|
};
|
|
110
154
|
|
|
111
155
|
try {
|
|
112
|
-
const { tx } = await
|
|
156
|
+
const { ixs, tx } = await StreamClient.withdraw(withdrawStreamParams);
|
|
113
157
|
} catch (exception) {
|
|
114
158
|
// handle exception
|
|
115
159
|
}
|
|
116
160
|
```
|
|
117
161
|
|
|
118
|
-
|
|
162
|
+
## Topup stream
|
|
119
163
|
|
|
120
164
|
```javascript
|
|
121
165
|
const topupStreamParams = {
|
|
122
|
-
|
|
123
|
-
invoker: wallet, // Wallet signing the transaction.
|
|
166
|
+
invoker: wallet, // Wallet/Keypair signing the transaction.
|
|
124
167
|
id: "AAAAyotqTZZMAAAAmsD1JAgksT8NVAAAASfrGB5RAAAA", // Identifier of a stream to be topped up.
|
|
125
|
-
amount:
|
|
126
|
-
cluster: Cluster.Mainnet, // Cluster (optional, default is Cluster.Mainnet).
|
|
168
|
+
amount: getBN(100000000000, 9), // Specified amount to topup (increases deposited amount).
|
|
127
169
|
};
|
|
128
170
|
|
|
129
171
|
try {
|
|
130
|
-
const { tx } = await
|
|
172
|
+
const { ixs, tx } = await StreamClient.topup(topupStreamParams);
|
|
131
173
|
} catch (exception) {
|
|
132
174
|
// handle exception
|
|
133
175
|
}
|
|
134
176
|
```
|
|
135
177
|
|
|
136
|
-
|
|
178
|
+
## Transfer stream to another recipient
|
|
137
179
|
|
|
138
180
|
```javascript
|
|
139
181
|
const data = {
|
|
140
|
-
|
|
141
|
-
invoker: wallet, // Wallet signing the transaction.
|
|
182
|
+
invoker: wallet, // Wallet/Keypair signing the transaction.
|
|
142
183
|
id: "AAAAyotqTZZMAAAAmsD1JAgksT8NVAAAASfrGB5RAAAA",
|
|
143
184
|
recipientId: "99h00075bKjVg000000tLdk4w42NyG3Mv0000dc0M99", // Identifier of a stream to be transferred.
|
|
144
|
-
cluster: Cluster.Mainnet, // Cluster (optional, default is Cluster.Mainnet).
|
|
145
185
|
};
|
|
146
186
|
|
|
147
187
|
try {
|
|
148
|
-
const { tx } = await
|
|
188
|
+
const { tx } = await StreamClient.transfer(data);
|
|
149
189
|
} catch (exception) {
|
|
150
190
|
// handle exception
|
|
151
191
|
}
|
|
152
192
|
```
|
|
153
193
|
|
|
154
|
-
|
|
194
|
+
## Cancel stream
|
|
155
195
|
|
|
156
196
|
```javascript
|
|
157
197
|
const cancelStreamParams = {
|
|
158
|
-
|
|
159
|
-
invoker: wallet, // Wallet signing the transaction.
|
|
198
|
+
invoker: wallet, // Wallet/Keypair signing the transaction.
|
|
160
199
|
id: "AAAAyotqTZZMAAAAmsD1JAgksT8NVAAAASfrGB5RAAAA", // Identifier of a stream to be canceled.
|
|
161
|
-
cluster: Cluster.Mainnet, // Cluster (optional, default is Cluster.Mainnet).
|
|
162
200
|
};
|
|
163
201
|
|
|
164
202
|
try {
|
|
165
|
-
const { tx } = await
|
|
203
|
+
const { ixs, tx } = await StreamClient.cancel(cancelStreamParams);
|
|
166
204
|
} catch (exception) {
|
|
167
205
|
// handle exception
|
|
168
206
|
}
|
|
169
207
|
```
|
|
170
208
|
|
|
171
|
-
|
|
209
|
+
## Get one stream by its ID
|
|
172
210
|
|
|
173
211
|
```javascript
|
|
174
212
|
try {
|
|
175
|
-
const stream = await
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
});
|
|
213
|
+
const stream = await StreamClient.getOne(
|
|
214
|
+
"AAAAyotqTZZMAAAAmsD1JAgksT8NVAAAASfrGB5RAAAA" // Identifier of a stream that is fetched.
|
|
215
|
+
);
|
|
179
216
|
} catch (exception) {
|
|
180
217
|
// handle exception
|
|
181
218
|
}
|
|
182
219
|
```
|
|
183
220
|
|
|
184
|
-
|
|
221
|
+
## Get multiple streams for a specific wallet address
|
|
185
222
|
|
|
186
223
|
```javascript
|
|
187
224
|
try {
|
|
188
|
-
const streams =
|
|
189
|
-
connection: connection, // Connection to the cluster.
|
|
225
|
+
const streams = StreamClient.get({
|
|
190
226
|
wallet: wallet, // Wallet signing the transaction.
|
|
191
227
|
type: StreamType.All, // (optional) Type, default is StreamType.All
|
|
192
228
|
direction: StreamDirection.All, // (optional) Direction, default is StreamDirection.All)
|
|
193
|
-
cluster: Cluster.Mainnet, // (optional) Cluster, default is Cluster.Mainnet).
|
|
194
229
|
});
|
|
195
230
|
} catch (exception) {
|
|
196
231
|
// handle exception
|
|
@@ -199,8 +234,10 @@ try {
|
|
|
199
234
|
|
|
200
235
|
## Additional notes
|
|
201
236
|
|
|
237
|
+
Default import `import Stream from '@streamflow/stream'` is an import of older SDK version that uses Anchor and doesn't support raw instructions, nor multi streams creation.
|
|
238
|
+
|
|
202
239
|
Streamflow protocol program ID (devnet): `HqDGZjaVRXJ9MGRQEw7qDc2rAr6iH1n1kAQdCZaCMfMZ`
|
|
203
|
-
|
|
240
|
+
|
|
204
241
|
Streamflow protocol program ID (mainnet): `strmRqUCoQUgGUan5YhzUZa6KqdzwX5L6FpUxfmKg5m`
|
|
205
242
|
|
|
206
243
|
**All BN amounts are denominated in their smallest units.**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Stream as StreamData, CreateStreamParams,
|
|
1
|
+
import { Stream as StreamData, GetStreamsParams, CreateStreamParams, CreateStreamResponse, WithdrawStreamParams, CancelStreamParams, TransactionResponse, TopupStreamParams, TransferStreamParams, GetStreamParams } from "./types";
|
|
2
2
|
export default class Stream {
|
|
3
3
|
/**
|
|
4
4
|
* Creates a new stream/vesting contract.
|
|
@@ -22,7 +22,7 @@ export default class Stream {
|
|
|
22
22
|
* @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
|
|
23
23
|
* @param {boolean} [data.automaticWithdrawal = false] - Whether or not a 3rd party can initiate withdraw in the name of recipient.
|
|
24
24
|
* @param {number} [data.withdrawalFrequency = 0] - Relevant when automatic withdrawal is enabled. If greater than 0 our withdrawor will take care of withdrawals. If equal to 0 our withdrawor will skip, but everyone else can initiate withdrawals.
|
|
25
|
-
* @param {string} [data.partner =
|
|
25
|
+
* @param {string | null} [data.partner = null] - Partner's wallet address (optional).
|
|
26
26
|
* @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
|
|
27
27
|
*/
|
|
28
28
|
static create({ connection, sender, recipient, mint, start, depositedAmount, period, cliff, cliffAmount, amountPerPeriod, name, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal, withdrawalFrequency, partner, cluster, }: CreateStreamParams): Promise<CreateStreamResponse>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// Version 1 of the SDK that uses Anchor.
|
|
2
3
|
var __assign = (this && this.__assign) || function () {
|
|
3
4
|
__assign = Object.assign || function(t) {
|
|
4
5
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
@@ -75,8 +76,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
75
76
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
76
77
|
};
|
|
77
78
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
78
|
-
var anchor_1 = require("@project-serum/anchor");
|
|
79
79
|
var spl_token_1 = require("@solana/spl-token");
|
|
80
|
+
var buffer_1 = require("buffer");
|
|
81
|
+
var anchor_1 = require("@project-serum/anchor");
|
|
82
|
+
var spl_token_2 = require("@solana/spl-token");
|
|
80
83
|
var web3_js_1 = require("@solana/web3.js");
|
|
81
84
|
var types_1 = require("./types");
|
|
82
85
|
var utils_1 = require("./utils");
|
|
@@ -112,13 +115,13 @@ var Stream = /** @class */ (function () {
|
|
|
112
115
|
* @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
|
|
113
116
|
* @param {boolean} [data.automaticWithdrawal = false] - Whether or not a 3rd party can initiate withdraw in the name of recipient.
|
|
114
117
|
* @param {number} [data.withdrawalFrequency = 0] - Relevant when automatic withdrawal is enabled. If greater than 0 our withdrawor will take care of withdrawals. If equal to 0 our withdrawor will skip, but everyone else can initiate withdrawals.
|
|
115
|
-
* @param {string} [data.partner =
|
|
118
|
+
* @param {string | null} [data.partner = null] - Partner's wallet address (optional).
|
|
116
119
|
* @param {ClusterExtended} [data.cluster = Cluster.Mainnet] - Cluster: devnet, mainnet-beta, testnet or local (optional).
|
|
117
120
|
*/
|
|
118
121
|
Stream.create = function (_a) {
|
|
119
|
-
var connection = _a.connection, sender = _a.sender, recipient = _a.recipient, mint = _a.mint, start = _a.start, depositedAmount = _a.depositedAmount, period = _a.period, cliff = _a.cliff, cliffAmount = _a.cliffAmount, amountPerPeriod = _a.amountPerPeriod, name = _a.name, canTopup = _a.canTopup, cancelableBySender = _a.cancelableBySender, cancelableByRecipient = _a.cancelableByRecipient, transferableBySender = _a.transferableBySender, transferableByRecipient = _a.transferableByRecipient, _b = _a.automaticWithdrawal, automaticWithdrawal = _b === void 0 ? false : _b, _c = _a.withdrawalFrequency, withdrawalFrequency = _c === void 0 ? 0 : _c, _d = _a.partner, partner = _d === void 0 ?
|
|
122
|
+
var connection = _a.connection, sender = _a.sender, recipient = _a.recipient, mint = _a.mint, start = _a.start, depositedAmount = _a.depositedAmount, period = _a.period, cliff = _a.cliff, cliffAmount = _a.cliffAmount, amountPerPeriod = _a.amountPerPeriod, name = _a.name, canTopup = _a.canTopup, cancelableBySender = _a.cancelableBySender, cancelableByRecipient = _a.cancelableByRecipient, transferableBySender = _a.transferableBySender, transferableByRecipient = _a.transferableByRecipient, _b = _a.automaticWithdrawal, automaticWithdrawal = _b === void 0 ? false : _b, _c = _a.withdrawalFrequency, withdrawalFrequency = _c === void 0 ? 0 : _c, _d = _a.partner, partner = _d === void 0 ? "" : _d, _e = _a.cluster, cluster = _e === void 0 ? types_1.Cluster.Mainnet : _e;
|
|
120
123
|
return __awaiter(this, void 0, void 0, function () {
|
|
121
|
-
var program, mintPublicKey, recipientPublicKey, metadata, _f, escrowTokens, senderTokens, recipientTokens, streamflowTreasuryTokens, partnerPublicKey, partnerTokens, signers,
|
|
124
|
+
var program, mintPublicKey, recipientPublicKey, metadata, _f, escrowTokens, senderTokens, recipientTokens, streamflowTreasuryTokens, partnerPublicKey, partnerTokens, signers, nameUtf8EncodedArr, tx;
|
|
122
125
|
return __generator(this, function (_g) {
|
|
123
126
|
switch (_g.label) {
|
|
124
127
|
case 0:
|
|
@@ -126,7 +129,7 @@ var Stream = /** @class */ (function () {
|
|
|
126
129
|
mintPublicKey = new web3_js_1.PublicKey(mint);
|
|
127
130
|
recipientPublicKey = new web3_js_1.PublicKey(recipient);
|
|
128
131
|
metadata = web3_js_1.Keypair.generate();
|
|
129
|
-
return [4 /*yield*/, anchor_1.web3.PublicKey.findProgramAddress([Buffer.from("strm"), metadata.publicKey.toBuffer()], program.programId)];
|
|
132
|
+
return [4 /*yield*/, anchor_1.web3.PublicKey.findProgramAddress([buffer_1.Buffer.from("strm"), metadata.publicKey.toBuffer()], program.programId)];
|
|
130
133
|
case 1:
|
|
131
134
|
_f = __read.apply(void 0, [_g.sent(), 1]), escrowTokens = _f[0];
|
|
132
135
|
return [4 /*yield*/, ata(mintPublicKey, sender.publicKey)];
|
|
@@ -138,19 +141,21 @@ var Stream = /** @class */ (function () {
|
|
|
138
141
|
return [4 /*yield*/, ata(mintPublicKey, constants_1.STREAMFLOW_TREASURY_PUBLIC_KEY)];
|
|
139
142
|
case 4:
|
|
140
143
|
streamflowTreasuryTokens = _g.sent();
|
|
141
|
-
partnerPublicKey =
|
|
144
|
+
partnerPublicKey = partner
|
|
145
|
+
? new web3_js_1.PublicKey(partner)
|
|
146
|
+
: sender.publicKey;
|
|
142
147
|
return [4 /*yield*/, ata(mintPublicKey, partnerPublicKey)];
|
|
143
148
|
case 5:
|
|
144
149
|
partnerTokens = _g.sent();
|
|
145
150
|
signers = [metadata];
|
|
146
|
-
|
|
151
|
+
nameUtf8EncodedArr = encoder.encode(name);
|
|
147
152
|
return [4 /*yield*/, program.rpc.create(
|
|
148
153
|
// Order of the parameters must match the ones in the program
|
|
149
|
-
new
|
|
154
|
+
new spl_token_1.u64(start), depositedAmount, new spl_token_1.u64(period), amountPerPeriod, new spl_token_1.u64(cliff), cliffAmount, cancelableBySender, cancelableByRecipient, automaticWithdrawal, transferableBySender, transferableByRecipient, canTopup, nameUtf8EncodedArr, new spl_token_1.u64(automaticWithdrawal ? withdrawalFrequency : period), {
|
|
150
155
|
accounts: {
|
|
151
156
|
sender: sender.publicKey,
|
|
152
157
|
senderTokens: senderTokens,
|
|
153
|
-
recipient: recipient,
|
|
158
|
+
recipient: new web3_js_1.PublicKey(recipient),
|
|
154
159
|
metadata: metadata.publicKey,
|
|
155
160
|
escrowTokens: escrowTokens,
|
|
156
161
|
recipientTokens: recipientTokens,
|
|
@@ -158,12 +163,12 @@ var Stream = /** @class */ (function () {
|
|
|
158
163
|
streamflowTreasuryTokens: streamflowTreasuryTokens,
|
|
159
164
|
partner: partnerPublicKey,
|
|
160
165
|
partnerTokens: partnerTokens,
|
|
161
|
-
mint: mint,
|
|
162
|
-
feeOracle: constants_1.
|
|
166
|
+
mint: new web3_js_1.PublicKey(mint),
|
|
167
|
+
feeOracle: constants_1.FEE_ORACLE_PUBLIC_KEY,
|
|
163
168
|
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
164
169
|
timelockProgram: program.programId,
|
|
165
|
-
tokenProgram:
|
|
166
|
-
associatedTokenProgram:
|
|
170
|
+
tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
|
|
171
|
+
associatedTokenProgram: spl_token_2.ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
167
172
|
withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
|
|
168
173
|
systemProgram: web3_js_1.SystemProgram.programId,
|
|
169
174
|
},
|
|
@@ -219,7 +224,7 @@ var Stream = /** @class */ (function () {
|
|
|
219
224
|
partner: data.partner,
|
|
220
225
|
partnerTokens: partnerTokens,
|
|
221
226
|
mint: data.mint,
|
|
222
|
-
tokenProgram:
|
|
227
|
+
tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
|
|
223
228
|
},
|
|
224
229
|
})];
|
|
225
230
|
case 4:
|
|
@@ -273,7 +278,7 @@ var Stream = /** @class */ (function () {
|
|
|
273
278
|
partner: data.partner,
|
|
274
279
|
partnerTokens: partnerTokens,
|
|
275
280
|
mint: data.mint,
|
|
276
|
-
tokenProgram:
|
|
281
|
+
tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
|
|
277
282
|
},
|
|
278
283
|
})];
|
|
279
284
|
case 4:
|
|
@@ -321,8 +326,8 @@ var Stream = /** @class */ (function () {
|
|
|
321
326
|
metadata: stream,
|
|
322
327
|
mint: mint,
|
|
323
328
|
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
324
|
-
tokenProgram:
|
|
325
|
-
associatedTokenProgram:
|
|
329
|
+
tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
|
|
330
|
+
associatedTokenProgram: spl_token_2.ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
326
331
|
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
327
332
|
},
|
|
328
333
|
})];
|
|
@@ -375,7 +380,7 @@ var Stream = /** @class */ (function () {
|
|
|
375
380
|
partner: partner,
|
|
376
381
|
partnerTokens: partnerTokens,
|
|
377
382
|
mint: mint,
|
|
378
|
-
tokenProgram:
|
|
383
|
+
tokenProgram: spl_token_2.TOKEN_PROGRAM_ID,
|
|
379
384
|
withdrawor: constants_1.WITHDRAWOR_PUBLIC_KEY,
|
|
380
385
|
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
381
386
|
},
|
|
@@ -490,27 +495,9 @@ function ata(mint, account) {
|
|
|
490
495
|
return __awaiter(this, void 0, void 0, function () {
|
|
491
496
|
return __generator(this, function (_a) {
|
|
492
497
|
switch (_a.label) {
|
|
493
|
-
case 0: return [4 /*yield*/,
|
|
498
|
+
case 0: return [4 /*yield*/, spl_token_2.Token.getAssociatedTokenAddress(spl_token_2.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_2.TOKEN_PROGRAM_ID, mint, account, true)];
|
|
494
499
|
case 1: return [2 /*return*/, _a.sent()];
|
|
495
500
|
}
|
|
496
501
|
});
|
|
497
502
|
});
|
|
498
503
|
}
|
|
499
|
-
function formatStringToBytesArray(encoder, text) {
|
|
500
|
-
var textCopy = __spreadArray([], __read(text), false);
|
|
501
|
-
var characters = Array.from(textCopy);
|
|
502
|
-
var utf8EncodedBytes = [];
|
|
503
|
-
characters.every(function (char) {
|
|
504
|
-
if (utf8EncodedBytes.length > 64)
|
|
505
|
-
return false;
|
|
506
|
-
var encoded = encoder.encode(char);
|
|
507
|
-
if (utf8EncodedBytes.length + encoded.length > 64)
|
|
508
|
-
return false;
|
|
509
|
-
encoded.forEach(function (elem) { return utf8EncodedBytes.push(new anchor_1.BN(elem)); });
|
|
510
|
-
return true;
|
|
511
|
-
});
|
|
512
|
-
var numberOfBytes = utf8EncodedBytes.length;
|
|
513
|
-
var fill = new Array(64 - numberOfBytes).fill(new anchor_1.BN(0));
|
|
514
|
-
utf8EncodedBytes.push.apply(utf8EncodedBytes, __spreadArray([], __read(fill), false));
|
|
515
|
-
return utf8EncodedBytes;
|
|
516
|
-
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { web3 } from "@project-serum/anchor";
|
|
2
|
+
import { Commitment, ConnectionConfig } from "@solana/web3.js";
|
|
3
|
+
import { Stream as StreamData, CreateParams, CreateMultiParams, WithdrawParams, TopupParams, CancelParams, TransferParams, ClusterExtended, GetAllParams, CreateResponse, CreateMultiResponse, TxResponse } from "./types";
|
|
4
|
+
export default class StreamClient {
|
|
5
|
+
private connection;
|
|
6
|
+
private cluster;
|
|
7
|
+
private programId;
|
|
8
|
+
private commitment;
|
|
9
|
+
/**
|
|
10
|
+
* Create Stream instance
|
|
11
|
+
*/
|
|
12
|
+
constructor(clusterUrl: string, cluster?: ClusterExtended, commitment?: Commitment | ConnectionConfig);
|
|
13
|
+
getConnection(): web3.Connection;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new stream/vesting contract.
|
|
16
|
+
* All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
|
|
17
|
+
* @param {CreateParams} data
|
|
18
|
+
* @param {Wallet | Keypair} data.sender - Wallet signing the transaction. Its address should match the authorized wallet (sender) or transaction will fail.
|
|
19
|
+
* @param {string} data.recipient - Solana address of the recipient. Associated token account will be derived using this address and token mint address.
|
|
20
|
+
* @param {string} data.mint - SPL Token mint.
|
|
21
|
+
* @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
|
|
22
|
+
* @param {u64} data.depositedAmount - Initially deposited amount of tokens (in the smallest units).
|
|
23
|
+
* @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
|
|
24
|
+
* @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
|
|
25
|
+
* @param {u64} data.cliffAmount - Amount unlocked at the "cliff".
|
|
26
|
+
* @param {u64} data.amountPerPeriod - Amount unlocked per each period.
|
|
27
|
+
* @param {string} data.name - Stream name/subject.
|
|
28
|
+
* @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
|
|
29
|
+
* @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
|
|
30
|
+
* @param {boolean} data.cancelableByRecipient - Whether or not recipient can cancel the stream.
|
|
31
|
+
* @param {boolean} data.transferableBySender - Whether or not sender can transfer the stream.
|
|
32
|
+
* @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
|
|
33
|
+
* @param {boolean} data.automaticWithdrawal - Whether or not a 3rd party can initiate withdraw in the name of recipient.
|
|
34
|
+
* @param {number} [data.withdrawalFrequency = 0] - Relevant when automatic withdrawal is enabled. If greater than 0 our withdrawor will take care of withdrawals. If equal to 0 our withdrawor will skip, but everyone else can initiate withdrawals.
|
|
35
|
+
* @param {string | null} [data.partner = null] - Partner's wallet address (optional).
|
|
36
|
+
*/
|
|
37
|
+
create({ sender, recipient, mint, start, depositedAmount, period, cliff, cliffAmount, amountPerPeriod, name, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal, withdrawalFrequency, partner, }: CreateParams): Promise<CreateResponse>;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new stream/vesting contract.
|
|
40
|
+
* All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
|
|
41
|
+
* @param {CreateMultiParams} data
|
|
42
|
+
* @param {Wallet | Keypair} data.sender - Wallet signing the transaction. Its address should match the authorized wallet (sender) or transaction will fail.
|
|
43
|
+
* @param {MultiRecipient[]} data.recipientsData
|
|
44
|
+
* @param {string} data.mint - SPL Token mint.
|
|
45
|
+
* @param {number} data.start - Timestamp (in seconds) when the stream/token vesting starts.
|
|
46
|
+
* @param {number} data.period - Time step (period) in seconds per which the unlocking occurs.
|
|
47
|
+
* @param {number} data.cliff - Vesting contract "cliff" timestamp in seconds.
|
|
48
|
+
* @param {u64} data.cliffAmount - Amount unlocked at the "cliff".
|
|
49
|
+
* @param {u64} data.amountPerPeriod - Amount unlocked per each period.
|
|
50
|
+
* @param {boolean} data.canTopup - TRUE for streams, FALSE for vesting contracts.
|
|
51
|
+
* @param {boolean} data.cancelableBySender - Whether or not sender can cancel the stream.
|
|
52
|
+
* @param {boolean} data.cancelableByRecipient - Whether or not recipient can cancel the stream.
|
|
53
|
+
* @param {boolean} data.transferableBySender - Whether or not sender can transfer the stream.
|
|
54
|
+
* @param {boolean} data.transferableByRecipient - Whether or not recipient can transfer the stream.
|
|
55
|
+
* @param {boolean} data.automaticWithdrawal - Whether or not a 3rd party can initiate withdraw in the name of recipient.
|
|
56
|
+
* @param {number} [data.withdrawalFrequency = 0] - Relevant when automatic withdrawal is enabled. If greater than 0 our withdrawor will take care of withdrawals. If equal to 0 our withdrawor will skip, but everyone else can initiate withdrawals.
|
|
57
|
+
* @param {string | null} [data.partner = null] - Partner's wallet address (optional).
|
|
58
|
+
*/
|
|
59
|
+
createMultiple({ sender, recipientsData, mint, start, period, cliff, cliffAmount, amountPerPeriod, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal, withdrawalFrequency, partner, }: CreateMultiParams): Promise<CreateMultiResponse>;
|
|
60
|
+
/**
|
|
61
|
+
* Attempts withdrawal from the specified stream.
|
|
62
|
+
* @param {WithdrawParams} data
|
|
63
|
+
* @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (recipient) or transaction will fail.
|
|
64
|
+
* @param {string} data.id - Identifier of a stream (escrow account with metadata) to be withdrawn from.
|
|
65
|
+
* @param {u64} data.amount - Requested amount (in the smallest units) to withdraw (while streaming). If stream is completed, the whole amount will be withdrawn.
|
|
66
|
+
*/
|
|
67
|
+
withdraw({ invoker, id, amount, }: WithdrawParams): Promise<TxResponse>;
|
|
68
|
+
/**
|
|
69
|
+
* Attempts canceling the specified stream.
|
|
70
|
+
* @param {CancelParams} data
|
|
71
|
+
* @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (sender or recipient) or transaction will fail.
|
|
72
|
+
* @param {string} data.id - Identifier of a stream (escrow account with metadata) to be canceled.
|
|
73
|
+
*/
|
|
74
|
+
cancel({ invoker, id }: CancelParams): Promise<TxResponse>;
|
|
75
|
+
/**
|
|
76
|
+
* Attempts changing the stream/vesting contract's recipient (effectively transferring the stream/vesting contract).
|
|
77
|
+
* Potential associated token account rent fee (to make it rent-exempt) is paid by the transaction initiator.
|
|
78
|
+
* @param {TransferParams} data
|
|
79
|
+
* @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match authorized wallet (sender or recipient) or transaction will fail.
|
|
80
|
+
* @param {string} data.id - Identifier of a stream (escrow account with metadata) to be transferred.
|
|
81
|
+
* @param {string} data.recipientId - Address of a new recipient.
|
|
82
|
+
*/
|
|
83
|
+
transfer({ invoker, id, recipientId, }: TransferParams): Promise<TxResponse>;
|
|
84
|
+
/**
|
|
85
|
+
* Tops up stream account deposited amount.
|
|
86
|
+
* @param {TopupParams} data
|
|
87
|
+
* @param {Wallet | Keypair} data.invoker - Wallet signing the transaction. It's address should match current stream sender or transaction will fail.
|
|
88
|
+
* @param {string} data.id - Identifier of a stream (escrow account with metadata) to be topped up.
|
|
89
|
+
* @param {u64} data.amount - Specified amount (in the smallest units) to topup (increases deposited amount).
|
|
90
|
+
*/
|
|
91
|
+
topup({ invoker, id, amount, }: TopupParams): Promise<TxResponse>;
|
|
92
|
+
/**
|
|
93
|
+
* Fetch stream data by its id (address).
|
|
94
|
+
* @param {string} id - Identifier of a stream that is fetched.
|
|
95
|
+
*/
|
|
96
|
+
getOne(id: string): Promise<StreamData>;
|
|
97
|
+
/**
|
|
98
|
+
* Fetch streams/contracts by providing direction.
|
|
99
|
+
* Streams are sorted by start time in ascending order.
|
|
100
|
+
* @param {GetAllParams} data
|
|
101
|
+
* @param {PublicKey} data.wallet - PublicKey of the wallet for which the streams/contracts are fetched.
|
|
102
|
+
* @param {StreamType} [data.type = StreamType.All] - It can be one of: stream, vesting, all.
|
|
103
|
+
* @param {StreamDirection} [data.direction = StreamDirection.All] - It can be one of: incoming, outgoing, all.
|
|
104
|
+
*/
|
|
105
|
+
get({ wallet, type, direction, }: GetAllParams): Promise<[string, StreamData][]>;
|
|
106
|
+
private sign;
|
|
107
|
+
}
|