@zebec-network/zebec-stream-sdk 3.0.0-dev.2 → 3.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/dist/services/streamService.d.ts +5 -4
- package/dist/services/streamService.js +20 -15
- package/dist/types.d.ts +0 -3
- package/dist/utils.js +3 -2
- package/package.json +1 -1
|
@@ -4,11 +4,12 @@ import { TransactionPayload } from "@zebec-network/solana-common";
|
|
|
4
4
|
import { type ZebecStreamIdl } from "../artifacts";
|
|
5
5
|
import type { CancelStreamParams, ChangeStreamReceiverParams, CreateStreamParams, InitializeStreamConfigParams, RpcNetwork, StreamConfigInfo, StreamMetadataInfo, TokenMetadata, UpdateStreamConfigParams, WhiteListTokensParams, WithdrawStreamParams } from "../types";
|
|
6
6
|
export declare class ZebecStreamService {
|
|
7
|
+
readonly streamConfigName: string;
|
|
7
8
|
readonly provider: Provider;
|
|
8
9
|
readonly network: RpcNetwork;
|
|
9
10
|
readonly program: Program<ZebecStreamIdl>;
|
|
10
|
-
constructor(provider: Provider, network: RpcNetwork, program: Program<ZebecStreamIdl>);
|
|
11
|
-
static create(provider: Provider, network: RpcNetwork): ZebecStreamService;
|
|
11
|
+
constructor(streamConfigName: string, provider: Provider, network: RpcNetwork, program: Program<ZebecStreamIdl>);
|
|
12
|
+
static create(streamConfigName: string, provider: Provider, network: RpcNetwork): ZebecStreamService;
|
|
12
13
|
private _createPayload;
|
|
13
14
|
get connection(): Connection;
|
|
14
15
|
get programId(): PublicKey;
|
|
@@ -38,7 +39,7 @@ export declare class ZebecStreamService {
|
|
|
38
39
|
}[];
|
|
39
40
|
feeVault: PublicKey;
|
|
40
41
|
}): Promise<TransactionInstruction>;
|
|
41
|
-
getCreateStreamInstruction(feePayer: PublicKey, receiver: PublicKey, senderAta: PublicKey, streamToken: PublicKey, withdrawAccount: PublicKey, sender: PublicKey, streamMetadata: PublicKey, streamData: {
|
|
42
|
+
getCreateStreamInstruction(streamConfig: PublicKey, feePayer: PublicKey, receiver: PublicKey, senderAta: PublicKey, streamToken: PublicKey, withdrawAccount: PublicKey, sender: PublicKey, streamMetadata: PublicKey, streamData: {
|
|
42
43
|
amount: BN;
|
|
43
44
|
automaticWithdrawal: boolean;
|
|
44
45
|
cancelableByRecipient: boolean;
|
|
@@ -58,7 +59,7 @@ export declare class ZebecStreamService {
|
|
|
58
59
|
getPauseResumeStreamInstruction(streamMetadata: PublicKey, user: PublicKey): Promise<TransactionInstruction>;
|
|
59
60
|
getCancelStreamInstruction(feePayer: PublicKey, otherParty: PublicKey, otherPartyAta: PublicKey, signer: PublicKey, signerAta: PublicKey, streamMetadata: PublicKey, streamToken: PublicKey, streamVault: PublicKey, streamVaultAta: PublicKey): Promise<TransactionInstruction>;
|
|
60
61
|
getWithdrawStreamInstruction(receiver: PublicKey, receiverAta: PublicKey, streamMetadata: PublicKey, streamToken: PublicKey, streamVault: PublicKey, streamVaultAta: PublicKey, withdrawer: PublicKey, feePayer: PublicKey): Promise<TransactionInstruction>;
|
|
61
|
-
getWhitelistTokensInstruction(admin: PublicKey, tokens: PublicKey[]): Promise<TransactionInstruction>;
|
|
62
|
+
getWhitelistTokensInstruction(admin: PublicKey, streamConfig: PublicKey, tokens: PublicKey[]): Promise<TransactionInstruction>;
|
|
62
63
|
getChangeStreamReceiverInstruction(streamMetadata: PublicKey, newRecipient: PublicKey, signer: PublicKey): Promise<TransactionInstruction>;
|
|
63
64
|
initializeStreamConfig(params: InitializeStreamConfigParams): Promise<TransactionPayload>;
|
|
64
65
|
updateStreamConfig(params: UpdateStreamConfigParams): Promise<TransactionPayload>;
|
|
@@ -4,6 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ZebecStreamService = void 0;
|
|
7
|
+
const assert_1 = __importDefault(require("assert"));
|
|
8
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
7
9
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
8
10
|
const mpl_token_metadata_1 = require("@metaplex-foundation/mpl-token-metadata");
|
|
9
11
|
const umi_bundle_defaults_1 = require("@metaplex-foundation/umi-bundle-defaults");
|
|
@@ -12,22 +14,22 @@ const spl_token_1 = require("@solana/spl-token");
|
|
|
12
14
|
const web3_js_1 = require("@solana/web3.js");
|
|
13
15
|
const core_utils_1 = require("@zebec-network/core-utils");
|
|
14
16
|
const solana_common_1 = require("@zebec-network/solana-common");
|
|
15
|
-
const assert_1 = __importDefault(require("assert"));
|
|
16
|
-
const bignumber_js_1 = require("bignumber.js");
|
|
17
17
|
const artifacts_1 = require("../artifacts");
|
|
18
18
|
const constants_1 = require("../constants");
|
|
19
19
|
const pda_1 = require("../pda");
|
|
20
20
|
const utils_1 = require("../utils");
|
|
21
21
|
class ZebecStreamService {
|
|
22
|
+
streamConfigName;
|
|
22
23
|
provider;
|
|
23
24
|
network;
|
|
24
25
|
program;
|
|
25
|
-
constructor(provider, network, program) {
|
|
26
|
+
constructor(streamConfigName, provider, network, program) {
|
|
27
|
+
this.streamConfigName = streamConfigName;
|
|
26
28
|
this.provider = provider;
|
|
27
29
|
this.network = network;
|
|
28
30
|
this.program = program;
|
|
29
31
|
}
|
|
30
|
-
static create(provider, network) {
|
|
32
|
+
static create(streamConfigName, provider, network) {
|
|
31
33
|
const connection = provider.connection;
|
|
32
34
|
const rpcEndpoint = connection.rpcEndpoint;
|
|
33
35
|
const connNetwork = rpcEndpoint.includes("devnet")
|
|
@@ -42,7 +44,7 @@ class ZebecStreamService {
|
|
|
42
44
|
throw new Error(`InvalidOperation: Network mismatch. network and connection network should be same. network: ${network}, connection: ${connNetwork}`);
|
|
43
45
|
}
|
|
44
46
|
const program = new anchor_1.Program(artifacts_1.ZEBEC_STREAM_IDL, provider);
|
|
45
|
-
return new ZebecStreamService(provider, network, program);
|
|
47
|
+
return new ZebecStreamService(streamConfigName, provider, network, program);
|
|
46
48
|
}
|
|
47
49
|
async _createPayload(payerKey, instructions, signers, addressLookupTableAccounts) {
|
|
48
50
|
const errorMap = new Map();
|
|
@@ -101,7 +103,7 @@ class ZebecStreamService {
|
|
|
101
103
|
})
|
|
102
104
|
.instruction();
|
|
103
105
|
}
|
|
104
|
-
async getCreateStreamInstruction(feePayer, receiver, senderAta, streamToken, withdrawAccount, sender, streamMetadata, streamData) {
|
|
106
|
+
async getCreateStreamInstruction(streamConfig, feePayer, receiver, senderAta, streamToken, withdrawAccount, sender, streamMetadata, streamData) {
|
|
105
107
|
(0, assert_1.default)(streamData.streamName.length === constants_1.STREAM_NAME_BUFFER_SIZE, `Stream name buffer must be of size ${constants_1.STREAM_NAME_BUFFER_SIZE}`);
|
|
106
108
|
return this.program.methods
|
|
107
109
|
.createStream({
|
|
@@ -129,6 +131,7 @@ class ZebecStreamService {
|
|
|
129
131
|
withdrawAccount,
|
|
130
132
|
sender,
|
|
131
133
|
streamMetadata,
|
|
134
|
+
streamConfig,
|
|
132
135
|
})
|
|
133
136
|
.instruction();
|
|
134
137
|
}
|
|
@@ -172,13 +175,14 @@ class ZebecStreamService {
|
|
|
172
175
|
})
|
|
173
176
|
.instruction();
|
|
174
177
|
}
|
|
175
|
-
async getWhitelistTokensInstruction(admin, tokens) {
|
|
178
|
+
async getWhitelistTokensInstruction(admin, streamConfig, tokens) {
|
|
176
179
|
return this.program.methods
|
|
177
180
|
.whitelistTokens({
|
|
178
181
|
tokens,
|
|
179
182
|
})
|
|
180
|
-
.
|
|
183
|
+
.accountsPartial({
|
|
181
184
|
admin,
|
|
185
|
+
streamConfig,
|
|
182
186
|
})
|
|
183
187
|
.instruction();
|
|
184
188
|
}
|
|
@@ -197,7 +201,7 @@ class ZebecStreamService {
|
|
|
197
201
|
if (!admin) {
|
|
198
202
|
throw new Error("Either provide admin or create provider with public key");
|
|
199
203
|
}
|
|
200
|
-
const [streamConfig] = (0, pda_1.deriveStreamConfigPda)(
|
|
204
|
+
const [streamConfig] = (0, pda_1.deriveStreamConfigPda)(this.streamConfigName, this.programId);
|
|
201
205
|
const baseFee = new anchor_1.BN((0, core_utils_1.percentToBps)(params.config.baseFeePercent));
|
|
202
206
|
const frequencies = params.config.frequencies.map((frequency) => new anchor_1.BN(frequency));
|
|
203
207
|
const platformFee = new anchor_1.BN((0, core_utils_1.percentToBps)(params.config.platformFeePercent));
|
|
@@ -216,7 +220,7 @@ class ZebecStreamService {
|
|
|
216
220
|
platformFee,
|
|
217
221
|
withdrawAccount,
|
|
218
222
|
feeTiers,
|
|
219
|
-
configName:
|
|
223
|
+
configName: this.streamConfigName,
|
|
220
224
|
feeVault,
|
|
221
225
|
});
|
|
222
226
|
return this._createPayload(admin, [ix]);
|
|
@@ -226,7 +230,7 @@ class ZebecStreamService {
|
|
|
226
230
|
if (!admin) {
|
|
227
231
|
throw new Error("Either provide admin or create provider with public key");
|
|
228
232
|
}
|
|
229
|
-
const [streamConfig] = (0, pda_1.deriveStreamConfigPda)(
|
|
233
|
+
const [streamConfig] = (0, pda_1.deriveStreamConfigPda)(this.streamConfigName, this.programId);
|
|
230
234
|
const baseFee = new anchor_1.BN((0, core_utils_1.percentToBps)(params.config.baseFeePercent));
|
|
231
235
|
const frequencies = params.config.frequencies.map((frequency) => new anchor_1.BN(frequency));
|
|
232
236
|
const platformFee = new anchor_1.BN((0, core_utils_1.percentToBps)(params.config.platformFeePercent));
|
|
@@ -245,7 +249,7 @@ class ZebecStreamService {
|
|
|
245
249
|
platformFee,
|
|
246
250
|
withdrawAccount,
|
|
247
251
|
feeTiers,
|
|
248
|
-
configName:
|
|
252
|
+
configName: this.streamConfigName,
|
|
249
253
|
feeVault,
|
|
250
254
|
});
|
|
251
255
|
return this._createPayload(admin, [ix]);
|
|
@@ -255,7 +259,7 @@ class ZebecStreamService {
|
|
|
255
259
|
const sender = (0, anchor_1.translateAddress)(params.sender);
|
|
256
260
|
const feePayer = params.feePayer ? (0, anchor_1.translateAddress)(params.feePayer) : sender;
|
|
257
261
|
const streamToken = (0, anchor_1.translateAddress)(params.streamToken);
|
|
258
|
-
const [streamConfig] = (0, pda_1.deriveStreamConfigPda)(
|
|
262
|
+
const [streamConfig] = (0, pda_1.deriveStreamConfigPda)(this.streamConfigName, this.programId);
|
|
259
263
|
const streamConfigAccount = await this.program.account.streamConfig.fetch(streamConfig);
|
|
260
264
|
const withdrawer = streamConfigAccount.withdrawAccount;
|
|
261
265
|
const autoWithdrawFrequencies = new Set(Array.from(streamConfigAccount.frequencies.map((f) => f.toNumber())));
|
|
@@ -292,7 +296,7 @@ class ZebecStreamService {
|
|
|
292
296
|
// add instruction to transfer fee to fee vault
|
|
293
297
|
const transferFeeToVaultIx = (0, spl_token_1.createTransferInstruction)(senderFeeTokenAta, feeVaultAta, sender, BigInt(parsedFeeAmount));
|
|
294
298
|
ixs.push(transferFeeToVaultIx);
|
|
295
|
-
const createStreamIx = await this.getCreateStreamInstruction(feePayer, receiver, senderAta, streamToken, withdrawer, sender, streamMetadataKeypair.publicKey, {
|
|
299
|
+
const createStreamIx = await this.getCreateStreamInstruction(streamConfig, feePayer, receiver, senderAta, streamToken, withdrawer, sender, streamMetadataKeypair.publicKey, {
|
|
296
300
|
amount: new anchor_1.BN(amount),
|
|
297
301
|
automaticWithdrawal: params.automaticWithdrawal,
|
|
298
302
|
cancelableByRecipient: params.cancelableByRecipient,
|
|
@@ -359,8 +363,9 @@ class ZebecStreamService {
|
|
|
359
363
|
}
|
|
360
364
|
async whiteListTokens(params) {
|
|
361
365
|
const admin = (0, anchor_1.translateAddress)(params.admin);
|
|
366
|
+
const [streamConfig] = (0, pda_1.deriveStreamConfigPda)(this.streamConfigName, this.programId);
|
|
362
367
|
const tokens = params.tokens.map((token) => (0, anchor_1.translateAddress)(token));
|
|
363
|
-
const ix = await this.getWhitelistTokensInstruction(admin, tokens);
|
|
368
|
+
const ix = await this.getWhitelistTokensInstruction(admin, streamConfig, tokens);
|
|
364
369
|
return this._createPayload(admin, [ix]);
|
|
365
370
|
}
|
|
366
371
|
async changeStreamReceiver(params) {
|
package/dist/types.d.ts
CHANGED
|
@@ -63,7 +63,6 @@ export type TokenMetadata = {
|
|
|
63
63
|
} | null;
|
|
64
64
|
};
|
|
65
65
|
export type CreateStreamParams = {
|
|
66
|
-
configName: string;
|
|
67
66
|
feePayer?: Address;
|
|
68
67
|
receiver: Address;
|
|
69
68
|
sender: Address;
|
|
@@ -95,7 +94,6 @@ export type InitializeStreamConfigParams = {
|
|
|
95
94
|
frequencies: number[];
|
|
96
95
|
platformFeePercent: Numeric;
|
|
97
96
|
withdrawAccount: Address;
|
|
98
|
-
configName: string;
|
|
99
97
|
feeTiers: FeeTier[];
|
|
100
98
|
feeVault: Address;
|
|
101
99
|
};
|
|
@@ -112,7 +110,6 @@ export type UpdateStreamConfigParams = {
|
|
|
112
110
|
frequencies: number[];
|
|
113
111
|
platformFeePercent: Numeric;
|
|
114
112
|
withdrawAccount: Address;
|
|
115
|
-
configName: string;
|
|
116
113
|
feeTiers: FeeTier[];
|
|
117
114
|
feeVault: Address;
|
|
118
115
|
};
|
package/dist/utils.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getTokenUsdPrice = getTokenUsdPrice;
|
|
4
4
|
exports.calculateFeeTokenAmountForStream = calculateFeeTokenAmountForStream;
|
|
5
5
|
exports.getFeeRateForUsdAmount = getFeeRateForUsdAmount;
|
|
6
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
6
7
|
const core_utils_1 = require("@zebec-network/core-utils");
|
|
7
8
|
const constants_1 = require("./constants");
|
|
8
9
|
async function getTokenUsdPrice(tokenSymbol, network) {
|
|
@@ -25,14 +26,14 @@ async function calculateFeeTokenAmountForStream(streamTokenSymbol, streamTokenAm
|
|
|
25
26
|
if (oneFeeTokenPriceInUsd === 0) {
|
|
26
27
|
throw new Error(`Fee token price is zero for symbol: ${feeTokenSymbol}, network: ${network}`);
|
|
27
28
|
}
|
|
28
|
-
const streamTokenAmountUsdPrice = new BigNumber(streamTokenAmount).times(oneStreamTokenPriceInUsd);
|
|
29
|
+
const streamTokenAmountUsdPrice = new bignumber_js_1.BigNumber(streamTokenAmount).times(oneStreamTokenPriceInUsd);
|
|
29
30
|
const feeRateInBps = getFeeRateForUsdAmount(streamTokenAmountUsdPrice, feeTiers);
|
|
30
31
|
const feeAmountUsdPrice = streamTokenAmountUsdPrice.times(feeRateInBps).div(10000);
|
|
31
32
|
const feeTokenAmount = feeAmountUsdPrice.div(oneFeeTokenPriceInUsd);
|
|
32
33
|
return feeTokenAmount;
|
|
33
34
|
}
|
|
34
35
|
function getFeeRateForUsdAmount(amount, feeTiers) {
|
|
35
|
-
feeTiers.sort((a, b) => BigNumber(a.minThreshold).comparedTo(BigNumber(b.minThreshold)) ?? 0);
|
|
36
|
+
feeTiers.sort((a, b) => (0, bignumber_js_1.BigNumber)(a.minThreshold).comparedTo((0, bignumber_js_1.BigNumber)(b.minThreshold)) ?? 0);
|
|
36
37
|
const tier = feeTiers.find((tier) => amount.gte(tier.minThreshold) && amount.lt(tier.maxThreshold));
|
|
37
38
|
if (!tier) {
|
|
38
39
|
throw new Error(`No fee tier found for amount: ${amount}`);
|
package/package.json
CHANGED