@streamflow/stream 7.5.2 → 7.5.3-alpha.cffa362.20250508190445
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/cjs/BaseStreamClient-CQOC8m8V.d.cts +542 -0
- package/dist/cjs/aptos/index.cjs +460 -0
- package/dist/cjs/aptos/index.cjs.map +1 -0
- package/dist/cjs/aptos/index.d.cts +14 -0
- package/dist/cjs/evm/index.cjs +1917 -0
- package/dist/cjs/evm/index.cjs.map +1 -0
- package/dist/cjs/evm/index.d.cts +13 -0
- package/dist/cjs/index-CYu6i--S.d.cts +136 -0
- package/dist/cjs/index-DUVcpIhD.d.cts +203 -0
- package/dist/cjs/index-b7Qo5mCj.d.cts +664 -0
- package/dist/cjs/index-fNLnPKzv.d.cts +213 -0
- package/dist/cjs/index.cjs +7205 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +192 -0
- package/dist/cjs/solana/descriptor/streamflow_aligned_unlocks.cjs +4 -0
- package/dist/cjs/solana/descriptor/streamflow_aligned_unlocks.cjs.map +1 -0
- package/dist/cjs/solana/descriptor/streamflow_aligned_unlocks.d.cts +1234 -0
- package/dist/cjs/solana/index.cjs +3763 -0
- package/dist/cjs/solana/index.cjs.map +1 -0
- package/dist/cjs/solana/index.d.cts +17 -0
- package/dist/cjs/sui/index.cjs +1023 -0
- package/dist/cjs/sui/index.cjs.map +1 -0
- package/dist/cjs/sui/index.d.cts +13 -0
- package/dist/esm/BaseStreamClient-CQOC8m8V.d.ts +542 -0
- package/dist/esm/aptos/index.d.ts +14 -4
- package/dist/esm/aptos/index.js +447 -4
- package/dist/esm/aptos/index.js.map +1 -0
- package/dist/esm/evm/index.d.ts +13 -5
- package/dist/esm/evm/index.js +1904 -5
- package/dist/esm/evm/index.js.map +1 -0
- package/dist/esm/index-BEo0O2IG.d.ts +664 -0
- package/dist/esm/index-CWmb3-x9.d.ts +136 -0
- package/dist/esm/index-Cgv37xlG.d.ts +213 -0
- package/dist/esm/index-D6QYg8hn.d.ts +203 -0
- package/dist/esm/index.d.ts +192 -10
- package/dist/esm/index.js +7112 -10
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/solana/descriptor/idl/streamflow_aligned_unlocks.json +917 -1368
- package/dist/esm/solana/descriptor/streamflow_aligned_unlocks.d.ts +3 -1
- package/dist/esm/solana/descriptor/streamflow_aligned_unlocks.js +3 -1
- package/dist/esm/solana/descriptor/streamflow_aligned_unlocks.js.map +1 -0
- package/dist/esm/solana/index.d.ts +17 -7
- package/dist/esm/solana/index.js +3707 -7
- package/dist/esm/solana/index.js.map +1 -0
- package/dist/esm/sui/index.d.ts +13 -4
- package/dist/esm/sui/index.js +1013 -4
- package/dist/esm/sui/index.js.map +1 -0
- package/package.json +57 -27
- package/dist/cjs/__tests__/aptos/streamClient.spec.js +0 -111
- package/dist/cjs/__tests__/common/GenericStreamClient.spec.js +0 -45
- package/dist/cjs/aptos/StreamClient.js +0 -226
- package/dist/cjs/aptos/constants.js +0 -27
- package/dist/cjs/aptos/index.js +0 -38
- package/dist/cjs/aptos/types.js +0 -69
- package/dist/cjs/aptos/utils.js +0 -34
- package/dist/cjs/aptos/wallet.js +0 -34
- package/dist/cjs/common/BaseStreamClient.js +0 -23
- package/dist/cjs/common/GenericStreamClient.js +0 -102
- package/dist/cjs/common/constants.js +0 -10
- package/dist/cjs/common/contractUtils.js +0 -77
- package/dist/cjs/common/types.js +0 -141
- package/dist/cjs/common/utils.js +0 -69
- package/dist/cjs/evm/StreamClient.js +0 -238
- package/dist/cjs/evm/abi.js +0 -1257
- package/dist/cjs/evm/constants.js +0 -38
- package/dist/cjs/evm/ercAbi.js +0 -224
- package/dist/cjs/evm/index.js +0 -40
- package/dist/cjs/evm/types.js +0 -66
- package/dist/cjs/evm/utils.js +0 -11
- package/dist/cjs/index.js +0 -43
- package/dist/cjs/solana/StreamClient.js +0 -1042
- package/dist/cjs/solana/constants.js +0 -95
- package/dist/cjs/solana/descriptor/idl/streamflow_aligned_unlocks.json +0 -1437
- package/dist/cjs/solana/descriptor/streamflow_aligned_unlocks.js +0 -2
- package/dist/cjs/solana/index.js +0 -40
- package/dist/cjs/solana/instructionTypes.js +0 -2
- package/dist/cjs/solana/instructions.js +0 -298
- package/dist/cjs/solana/layout.js +0 -182
- package/dist/cjs/solana/lib/derive-accounts.js +0 -17
- package/dist/cjs/solana/lib/utils.js +0 -127
- package/dist/cjs/solana/timelockIDL.js +0 -422
- package/dist/cjs/solana/types.js +0 -93
- package/dist/cjs/sui/StreamClient.js +0 -416
- package/dist/cjs/sui/constants.js +0 -42
- package/dist/cjs/sui/index.js +0 -37
- package/dist/cjs/sui/types.js +0 -68
- package/dist/cjs/sui/utils.js +0 -46
- package/dist/cjs/sui/wallet.js +0 -40
- package/dist/esm/__tests__/aptos/streamClient.spec.d.ts +0 -1
- package/dist/esm/__tests__/aptos/streamClient.spec.js +0 -106
- package/dist/esm/__tests__/common/GenericStreamClient.spec.d.ts +0 -1
- package/dist/esm/__tests__/common/GenericStreamClient.spec.js +0 -40
- package/dist/esm/aptos/StreamClient.d.ts +0 -53
- package/dist/esm/aptos/StreamClient.js +0 -226
- package/dist/esm/aptos/constants.d.ts +0 -8
- package/dist/esm/aptos/constants.js +0 -24
- package/dist/esm/aptos/types.d.ts +0 -109
- package/dist/esm/aptos/types.js +0 -105
- package/dist/esm/aptos/utils.d.ts +0 -7
- package/dist/esm/aptos/utils.js +0 -30
- package/dist/esm/aptos/wallet.d.ts +0 -9
- package/dist/esm/aptos/wallet.js +0 -33
- package/dist/esm/common/BaseStreamClient.d.ts +0 -22
- package/dist/esm/common/BaseStreamClient.js +0 -19
- package/dist/esm/common/GenericStreamClient.d.ts +0 -72
- package/dist/esm/common/GenericStreamClient.js +0 -101
- package/dist/esm/common/constants.d.ts +0 -4
- package/dist/esm/common/constants.js +0 -4
- package/dist/esm/common/contractUtils.d.ts +0 -63
- package/dist/esm/common/contractUtils.js +0 -61
- package/dist/esm/common/types.d.ts +0 -353
- package/dist/esm/common/types.js +0 -135
- package/dist/esm/common/utils.d.ts +0 -32
- package/dist/esm/common/utils.js +0 -59
- package/dist/esm/evm/StreamClient.d.ts +0 -32
- package/dist/esm/evm/StreamClient.js +0 -237
- package/dist/esm/evm/abi.d.ts +0 -111
- package/dist/esm/evm/abi.js +0 -1255
- package/dist/esm/evm/constants.d.ts +0 -20
- package/dist/esm/evm/constants.js +0 -35
- package/dist/esm/evm/ercAbi.d.ts +0 -39
- package/dist/esm/evm/ercAbi.js +0 -222
- package/dist/esm/evm/types.d.ts +0 -98
- package/dist/esm/evm/types.js +0 -102
- package/dist/esm/evm/utils.d.ts +0 -1
- package/dist/esm/evm/utils.js +0 -8
- package/dist/esm/solana/StreamClient.d.ts +0 -150
- package/dist/esm/solana/StreamClient.js +0 -1017
- package/dist/esm/solana/constants.d.ts +0 -70
- package/dist/esm/solana/constants.js +0 -92
- package/dist/esm/solana/instructionTypes.d.ts +0 -101
- package/dist/esm/solana/instructionTypes.js +0 -1
- package/dist/esm/solana/instructions.d.ts +0 -142
- package/dist/esm/solana/instructions.js +0 -265
- package/dist/esm/solana/layout.d.ts +0 -27
- package/dist/esm/solana/layout.js +0 -155
- package/dist/esm/solana/lib/derive-accounts.d.ts +0 -4
- package/dist/esm/solana/lib/derive-accounts.js +0 -11
- package/dist/esm/solana/lib/utils.d.ts +0 -22
- package/dist/esm/solana/lib/utils.js +0 -117
- package/dist/esm/solana/timelockIDL.d.ts +0 -22
- package/dist/esm/solana/timelockIDL.js +0 -420
- package/dist/esm/solana/types.d.ts +0 -180
- package/dist/esm/solana/types.js +0 -142
- package/dist/esm/sui/StreamClient.d.ts +0 -82
- package/dist/esm/sui/StreamClient.js +0 -417
- package/dist/esm/sui/constants.d.ts +0 -10
- package/dist/esm/sui/constants.js +0 -39
- package/dist/esm/sui/types.d.ts +0 -167
- package/dist/esm/sui/types.js +0 -104
- package/dist/esm/sui/utils.d.ts +0 -11
- package/dist/esm/sui/utils.js +0 -39
- package/dist/esm/sui/wallet.d.ts +0 -17
- package/dist/esm/sui/wallet.js +0 -38
|
@@ -1,1042 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Latest version of the SDK that does not use Anchor. It supports raw instructions.
|
|
3
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
-
if (k2 === undefined) k2 = k;
|
|
5
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
-
}
|
|
9
|
-
Object.defineProperty(o, k2, desc);
|
|
10
|
-
}) : (function(o, m, k, k2) {
|
|
11
|
-
if (k2 === undefined) k2 = k;
|
|
12
|
-
o[k2] = m[k];
|
|
13
|
-
}));
|
|
14
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
-
}) : function(o, v) {
|
|
17
|
-
o["default"] = v;
|
|
18
|
-
});
|
|
19
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
-
if (mod && mod.__esModule) return mod;
|
|
21
|
-
var result = {};
|
|
22
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
-
__setModuleDefault(result, mod);
|
|
24
|
-
return result;
|
|
25
|
-
};
|
|
26
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
27
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
|
-
};
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.SolanaStreamClient = void 0;
|
|
31
|
-
const bn_js_1 = __importDefault(require("bn.js"));
|
|
32
|
-
const bs58_1 = __importDefault(require("bs58"));
|
|
33
|
-
const buffer_1 = require("buffer");
|
|
34
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
35
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
36
|
-
const solana_1 = require("@streamflow/common/solana");
|
|
37
|
-
const borsh = __importStar(require("borsh"));
|
|
38
|
-
const anchor_1 = require("@coral-xyz/anchor");
|
|
39
|
-
const common_1 = require("@streamflow/common");
|
|
40
|
-
const types_js_1 = require("./types.js");
|
|
41
|
-
const utils_js_1 = require("./lib/utils.js");
|
|
42
|
-
const constants_js_1 = require("./constants.js");
|
|
43
|
-
const instructions_js_1 = require("./instructions.js");
|
|
44
|
-
const types_js_2 = require("../common/types.js");
|
|
45
|
-
const BaseStreamClient_js_1 = require("../common/BaseStreamClient.js");
|
|
46
|
-
const utils_js_2 = require("../common/utils.js");
|
|
47
|
-
const constants_js_2 = require("../common/constants.js");
|
|
48
|
-
const streamflow_aligned_unlocks_json_1 = __importDefault(require("./descriptor/idl/streamflow_aligned_unlocks.json"));
|
|
49
|
-
const derive_accounts_js_1 = require("./lib/derive-accounts.js");
|
|
50
|
-
const contractUtils_js_1 = require("../common/contractUtils.js");
|
|
51
|
-
const METADATA_ACC_SIZE = 1104;
|
|
52
|
-
const ALIGNED_METADATA_ACC_SIZE = 320;
|
|
53
|
-
class SolanaStreamClient extends BaseStreamClient_js_1.BaseStreamClient {
|
|
54
|
-
/**
|
|
55
|
-
* Create Stream instance
|
|
56
|
-
*/
|
|
57
|
-
constructor(optionsOrClusterUrl, cluster = types_js_2.ICluster.Mainnet, commitment = "confirmed", programId = "", sendRate = 1, sendThrottler) {
|
|
58
|
-
super();
|
|
59
|
-
if (typeof optionsOrClusterUrl === "string") {
|
|
60
|
-
this.commitment = commitment;
|
|
61
|
-
this.connection = new web3_js_1.Connection(optionsOrClusterUrl, this.commitment);
|
|
62
|
-
this.programId = programId !== "" ? new web3_js_1.PublicKey(programId) : new web3_js_1.PublicKey(constants_js_1.PROGRAM_ID[cluster]);
|
|
63
|
-
this.schedulingParams = {
|
|
64
|
-
sendThrottler: sendThrottler ?? (0, solana_1.buildSendThrottler)(sendRate),
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
const { clusterUrl, cluster = types_js_2.ICluster.Mainnet, commitment = "confirmed", programId = "", sendScheduler, } = optionsOrClusterUrl;
|
|
69
|
-
this.commitment = commitment;
|
|
70
|
-
this.connection = new web3_js_1.Connection(clusterUrl, this.commitment);
|
|
71
|
-
this.programId = programId !== "" ? new web3_js_1.PublicKey(programId) : new web3_js_1.PublicKey(constants_js_1.PROGRAM_ID[cluster]);
|
|
72
|
-
const schedulingOptions = sendScheduler && "sendRate" in sendScheduler ? sendScheduler : undefined;
|
|
73
|
-
const sendThrottler = !sendScheduler
|
|
74
|
-
? (0, solana_1.buildSendThrottler)(1)
|
|
75
|
-
: "sendRate" in sendScheduler
|
|
76
|
-
? (0, solana_1.buildSendThrottler)(sendScheduler.sendRate ?? 1, sendScheduler.sendInterval)
|
|
77
|
-
: sendScheduler;
|
|
78
|
-
this.schedulingParams = {
|
|
79
|
-
...schedulingOptions,
|
|
80
|
-
sendThrottler,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
const alignedUnlocksProgram = {
|
|
84
|
-
...streamflow_aligned_unlocks_json_1.default,
|
|
85
|
-
address: streamflow_aligned_unlocks_json_1.default.address,
|
|
86
|
-
};
|
|
87
|
-
this.alignedProxyProgram = new anchor_1.Program(alignedUnlocksProgram, { connection: this.connection });
|
|
88
|
-
}
|
|
89
|
-
getConnection() {
|
|
90
|
-
return this.connection;
|
|
91
|
-
}
|
|
92
|
-
getCommitment() {
|
|
93
|
-
return typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
|
|
94
|
-
}
|
|
95
|
-
getProgramId() {
|
|
96
|
-
return this.programId.toBase58();
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Creates a new stream/vesting contract.
|
|
100
|
-
* All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
|
|
101
|
-
*/
|
|
102
|
-
async create(data, extParams) {
|
|
103
|
-
const { partner, amount, tokenProgramId } = data;
|
|
104
|
-
const { isNative, sender, customInstructions } = extParams;
|
|
105
|
-
const partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
|
|
106
|
-
const mintPublicKey = new web3_js_1.PublicKey(data.tokenId);
|
|
107
|
-
if (!sender.publicKey) {
|
|
108
|
-
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
109
|
-
}
|
|
110
|
-
const ixs = await this.getCreateATAInstructions([constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, partnerPublicKey], mintPublicKey, sender, true, tokenProgramId ? new web3_js_1.PublicKey(tokenProgramId) : undefined);
|
|
111
|
-
const { ixs: createIxs, metadata, metadataPubKey } = await this.prepareCreateInstructions(data, extParams);
|
|
112
|
-
ixs.push(...createIxs);
|
|
113
|
-
if (isNative) {
|
|
114
|
-
const totalFee = await this.getTotalFee({
|
|
115
|
-
address: partnerPublicKey.toString(),
|
|
116
|
-
});
|
|
117
|
-
const totalAmount = (0, utils_js_2.calculateTotalAmountToDeposit)(amount, totalFee);
|
|
118
|
-
ixs.push(...(await (0, solana_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalAmount)));
|
|
119
|
-
}
|
|
120
|
-
await this.applyCustomAfterInstructions(ixs, customInstructions, metadataPubKey);
|
|
121
|
-
const { tx, hash, context } = await (0, solana_1.prepareTransaction)(this.connection, ixs, sender.publicKey, undefined, metadata);
|
|
122
|
-
const signature = await (0, solana_1.signAndExecuteTransaction)(this.connection, extParams.sender, tx, {
|
|
123
|
-
hash,
|
|
124
|
-
context,
|
|
125
|
-
commitment: this.getCommitment(),
|
|
126
|
-
}, this.schedulingParams);
|
|
127
|
-
return { ixs, txId: signature, metadataId: metadataPubKey.toBase58() };
|
|
128
|
-
}
|
|
129
|
-
async prepareCreateInstructions(streamParams, extParams) {
|
|
130
|
-
const { ixs, metadata, metadataPubKey } = (0, contractUtils_js_1.isCreateAlignedStreamData)(streamParams)
|
|
131
|
-
? await this.prepareCreateAlignedUnlockInstructions(streamParams, extParams)
|
|
132
|
-
: await this.prepareCreateStreamInstructions(streamParams, extParams);
|
|
133
|
-
return { ixs, metadata, metadataPubKey };
|
|
134
|
-
}
|
|
135
|
-
async prepareCreateAlignedUnlockInstructions(streamParams, extParams) {
|
|
136
|
-
const { tokenId: mint, start, period, cliff, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, partner, recipient, cliffAmount, amountPerPeriod, amount: depositedAmount, name: streamName, minPrice, maxPercentage, minPercentage, maxPrice, skipInitial, tickSize, priceOracle, oracleType, tokenProgramId: streamTokenProgramId, } = streamParams;
|
|
137
|
-
const { isNative, sender, computeLimit, computePrice, metadataPubKeys } = extParams;
|
|
138
|
-
if (!sender.publicKey) {
|
|
139
|
-
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
140
|
-
}
|
|
141
|
-
if (!priceOracle && oracleType && oracleType !== "none") {
|
|
142
|
-
throw new Error("Price oracle is required for the specified oracle type");
|
|
143
|
-
}
|
|
144
|
-
const recipientPublicKey = new web3_js_1.PublicKey(recipient);
|
|
145
|
-
const mintPublicKey = isNative ? spl_token_1.NATIVE_MINT : new web3_js_1.PublicKey(mint);
|
|
146
|
-
const metadata = !metadataPubKeys ? web3_js_1.Keypair.generate() : undefined;
|
|
147
|
-
const metadataPubKey = metadata ? metadata.publicKey : metadataPubKeys[0];
|
|
148
|
-
let tokenProgramId = streamTokenProgramId ? new web3_js_1.PublicKey(streamTokenProgramId) : undefined;
|
|
149
|
-
if (!tokenProgramId) {
|
|
150
|
-
tokenProgramId = (await (0, solana_1.getMintAndProgram)(this.connection, mintPublicKey)).tokenProgramId;
|
|
151
|
-
}
|
|
152
|
-
const partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
|
|
153
|
-
const streamflowProgramPublicKey = new web3_js_1.PublicKey(this.programId);
|
|
154
|
-
const escrowPDA = (0, derive_accounts_js_1.deriveEscrowPDA)(streamflowProgramPublicKey, metadataPubKey);
|
|
155
|
-
const oracle = priceOracle ?? (0, derive_accounts_js_1.deriveTestOraclePDA)(this.alignedProxyProgram.programId, mintPublicKey, sender.publicKey);
|
|
156
|
-
const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
|
|
157
|
-
computePrice,
|
|
158
|
-
computeLimit: computeLimit ?? constants_js_1.ALIGNED_COMPUTE_LIMIT,
|
|
159
|
-
});
|
|
160
|
-
ixs.push(...(await this.getCreateATAInstructions([recipientPublicKey], mintPublicKey, sender, true, tokenProgramId)));
|
|
161
|
-
const encodedUIntArray = new TextEncoder().encode(streamName);
|
|
162
|
-
const streamNameArray = Array.from(encodedUIntArray);
|
|
163
|
-
const createIx = await this.alignedProxyProgram.methods
|
|
164
|
-
.create({
|
|
165
|
-
startTime: new bn_js_1.default(start),
|
|
166
|
-
netAmountDeposited: depositedAmount,
|
|
167
|
-
period: new bn_js_1.default(period),
|
|
168
|
-
amountPerPeriod: amountPerPeriod,
|
|
169
|
-
cliff: new bn_js_1.default(cliff),
|
|
170
|
-
cliffAmount: cliffAmount,
|
|
171
|
-
transferableBySender,
|
|
172
|
-
transferableByRecipient,
|
|
173
|
-
cancelableByRecipient,
|
|
174
|
-
cancelableBySender,
|
|
175
|
-
canTopup,
|
|
176
|
-
oracleType: (!!oracleType ? { [oracleType]: {} } : { none: {} }),
|
|
177
|
-
streamName: streamNameArray,
|
|
178
|
-
minPrice: minPrice instanceof bn_js_1.default ? minPrice : (0, common_1.getBN)(minPrice, constants_js_1.ALIGNED_PRECISION_FACTOR_POW),
|
|
179
|
-
maxPrice: maxPrice instanceof bn_js_1.default ? maxPrice : (0, common_1.getBN)(maxPrice, constants_js_1.ALIGNED_PRECISION_FACTOR_POW),
|
|
180
|
-
minPercentage: minPercentage instanceof bn_js_1.default ? minPercentage : (0, common_1.getBN)(minPercentage, constants_js_1.ALIGNED_PRECISION_FACTOR_POW),
|
|
181
|
-
maxPercentage: maxPercentage instanceof bn_js_1.default ? maxPercentage : (0, common_1.getBN)(maxPercentage, constants_js_1.ALIGNED_PRECISION_FACTOR_POW),
|
|
182
|
-
tickSize: new bn_js_1.default(tickSize || 1),
|
|
183
|
-
skipInitial: skipInitial ?? false,
|
|
184
|
-
})
|
|
185
|
-
.accountsPartial({
|
|
186
|
-
payer: sender.publicKey,
|
|
187
|
-
sender: sender.publicKey,
|
|
188
|
-
streamMetadata: metadataPubKey,
|
|
189
|
-
escrowTokens: escrowPDA,
|
|
190
|
-
mint: mintPublicKey,
|
|
191
|
-
partner: partnerPublicKey,
|
|
192
|
-
recipient: recipientPublicKey,
|
|
193
|
-
withdrawor: constants_js_1.WITHDRAWOR_PUBLIC_KEY,
|
|
194
|
-
feeOracle: constants_js_1.FEE_ORACLE_PUBLIC_KEY,
|
|
195
|
-
priceOracle: oracle,
|
|
196
|
-
tokenProgram: tokenProgramId,
|
|
197
|
-
streamflowProgram: this.programId,
|
|
198
|
-
})
|
|
199
|
-
.instruction();
|
|
200
|
-
ixs.push(createIx);
|
|
201
|
-
return { ixs, metadata, metadataPubKey };
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Creates a new stream/vesting contract.
|
|
205
|
-
* All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
|
|
206
|
-
*/
|
|
207
|
-
async prepareCreateStreamInstructions({ recipient, tokenId: mint, start, amount: depositedAmount, period, cliff, cliffAmount, amountPerPeriod, name, canPause, canUpdateRate, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal = false, withdrawalFrequency = 0, partner, tokenProgramId: streamTokenProgramId, }, { sender, metadataPubKeys, isNative = false, computePrice, computeLimit }) {
|
|
208
|
-
if (!sender.publicKey) {
|
|
209
|
-
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
210
|
-
}
|
|
211
|
-
const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
|
|
212
|
-
computePrice,
|
|
213
|
-
computeLimit,
|
|
214
|
-
});
|
|
215
|
-
const mintPublicKey = isNative ? spl_token_1.NATIVE_MINT : new web3_js_1.PublicKey(mint);
|
|
216
|
-
const recipientPublicKey = new web3_js_1.PublicKey(recipient);
|
|
217
|
-
const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);
|
|
218
|
-
const [escrowTokens] = web3_js_1.PublicKey.findProgramAddressSync([buffer_1.Buffer.from("strm"), metadataPubKey.toBuffer()], this.programId);
|
|
219
|
-
let tokenProgramId = streamTokenProgramId ? new web3_js_1.PublicKey(streamTokenProgramId) : undefined;
|
|
220
|
-
if (!tokenProgramId) {
|
|
221
|
-
tokenProgramId = (await (0, solana_1.getMintAndProgram)(this.connection, mintPublicKey)).tokenProgramId;
|
|
222
|
-
}
|
|
223
|
-
const senderTokens = await (0, solana_1.ata)(mintPublicKey, sender.publicKey, tokenProgramId);
|
|
224
|
-
const recipientTokens = await (0, solana_1.ata)(mintPublicKey, recipientPublicKey, tokenProgramId);
|
|
225
|
-
const streamflowTreasuryTokens = await (0, solana_1.ata)(mintPublicKey, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
|
|
226
|
-
const partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
|
|
227
|
-
const partnerTokens = await (0, solana_1.ata)(mintPublicKey, partnerPublicKey, tokenProgramId);
|
|
228
|
-
ixs.push(await (0, instructions_js_1.createStreamInstruction)({
|
|
229
|
-
start: new bn_js_1.default(start),
|
|
230
|
-
depositedAmount,
|
|
231
|
-
period: new bn_js_1.default(period),
|
|
232
|
-
amountPerPeriod,
|
|
233
|
-
cliff: new bn_js_1.default(cliff),
|
|
234
|
-
cliffAmount: new bn_js_1.default(cliffAmount),
|
|
235
|
-
cancelableBySender,
|
|
236
|
-
cancelableByRecipient,
|
|
237
|
-
automaticWithdrawal,
|
|
238
|
-
transferableBySender,
|
|
239
|
-
transferableByRecipient,
|
|
240
|
-
canTopup,
|
|
241
|
-
canUpdateRate: !!canUpdateRate,
|
|
242
|
-
canPause: !!canPause,
|
|
243
|
-
name,
|
|
244
|
-
withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
|
|
245
|
-
}, this.programId, {
|
|
246
|
-
sender: sender.publicKey,
|
|
247
|
-
senderTokens,
|
|
248
|
-
recipient: new web3_js_1.PublicKey(recipient),
|
|
249
|
-
metadata: metadataPubKey,
|
|
250
|
-
escrowTokens,
|
|
251
|
-
recipientTokens,
|
|
252
|
-
streamflowTreasury: constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
253
|
-
streamflowTreasuryTokens: streamflowTreasuryTokens,
|
|
254
|
-
partner: partnerPublicKey,
|
|
255
|
-
partnerTokens: partnerTokens,
|
|
256
|
-
mint: new web3_js_1.PublicKey(mint),
|
|
257
|
-
feeOracle: constants_js_1.FEE_ORACLE_PUBLIC_KEY,
|
|
258
|
-
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
259
|
-
timelockProgram: this.programId,
|
|
260
|
-
tokenProgram: tokenProgramId,
|
|
261
|
-
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
262
|
-
withdrawor: constants_js_1.WITHDRAWOR_PUBLIC_KEY,
|
|
263
|
-
systemProgram: web3_js_1.SystemProgram.programId,
|
|
264
|
-
}));
|
|
265
|
-
return { ixs, metadata, metadataPubKey };
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Creates a new stream/vesting contract using unchecked instruction.
|
|
269
|
-
*
|
|
270
|
-
* Unchecked instruction differs from the regular in:
|
|
271
|
-
* - does not check for initialized associated token account (wallets with no control over their ATA should not be used
|
|
272
|
-
* as sender/recipient/partner or there are risks of funds being locked in the contract)
|
|
273
|
-
* - initialized contract PDA off chain
|
|
274
|
-
*
|
|
275
|
-
* If you are not sure if you should use create or create_unchecked, go for create to be safer.
|
|
276
|
-
*/
|
|
277
|
-
async createUnchecked(data, extParams) {
|
|
278
|
-
const { ixs, metadata, metadataPubKey } = await this.prepareCreateUncheckedInstructions(data, extParams);
|
|
279
|
-
const { tx, hash, context } = await (0, solana_1.prepareTransaction)(this.connection, ixs, extParams.sender.publicKey, undefined, metadata);
|
|
280
|
-
const signature = await (0, solana_1.signAndExecuteTransaction)(this.connection, extParams.sender, tx, {
|
|
281
|
-
hash,
|
|
282
|
-
context,
|
|
283
|
-
commitment: this.getCommitment(),
|
|
284
|
-
}, this.schedulingParams);
|
|
285
|
-
return { ixs, txId: signature, metadataId: metadataPubKey.toBase58() };
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Create Transaction instructions for `createUnchecked`
|
|
289
|
-
*/
|
|
290
|
-
async prepareCreateUncheckedInstructions({ recipient, tokenId: mint, start, amount: depositedAmount, period, cliff, cliffAmount, amountPerPeriod, name, canTopup, canUpdateRate, canPause, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal = false, withdrawalFrequency = 0, partner, }, { sender, metadataPubKeys, isNative = false, computePrice, computeLimit }) {
|
|
291
|
-
if (!sender.publicKey) {
|
|
292
|
-
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
293
|
-
}
|
|
294
|
-
const mintPublicKey = new web3_js_1.PublicKey(mint);
|
|
295
|
-
const recipientPublicKey = new web3_js_1.PublicKey(recipient);
|
|
296
|
-
const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);
|
|
297
|
-
const rentToExempt = await this.connection.getMinimumBalanceForRentExemption(METADATA_ACC_SIZE);
|
|
298
|
-
const createMetadataInstruction = web3_js_1.SystemProgram.createAccount({
|
|
299
|
-
programId: this.programId,
|
|
300
|
-
space: METADATA_ACC_SIZE,
|
|
301
|
-
lamports: rentToExempt,
|
|
302
|
-
fromPubkey: sender?.publicKey,
|
|
303
|
-
newAccountPubkey: metadataPubKey,
|
|
304
|
-
});
|
|
305
|
-
const [escrowTokens] = web3_js_1.PublicKey.findProgramAddressSync([buffer_1.Buffer.from("strm"), metadataPubKey.toBuffer()], this.programId);
|
|
306
|
-
const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mintPublicKey);
|
|
307
|
-
const senderTokens = await (0, solana_1.ata)(mintPublicKey, sender.publicKey, tokenProgramId);
|
|
308
|
-
const partnerPublicKey = partner ? new web3_js_1.PublicKey(partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
|
|
309
|
-
const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
|
|
310
|
-
computePrice,
|
|
311
|
-
computeLimit,
|
|
312
|
-
});
|
|
313
|
-
if (isNative) {
|
|
314
|
-
const totalFee = await this.getTotalFee({ address: partnerPublicKey.toString() });
|
|
315
|
-
const totalAmount = (0, utils_js_2.calculateTotalAmountToDeposit)(depositedAmount, totalFee);
|
|
316
|
-
ixs.push(...(await (0, solana_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalAmount)));
|
|
317
|
-
}
|
|
318
|
-
const createInstruction = await (0, instructions_js_1.createUncheckedStreamInstruction)({
|
|
319
|
-
start: new bn_js_1.default(start),
|
|
320
|
-
depositedAmount,
|
|
321
|
-
period: new bn_js_1.default(period),
|
|
322
|
-
amountPerPeriod,
|
|
323
|
-
cliff: new bn_js_1.default(cliff),
|
|
324
|
-
cliffAmount: new bn_js_1.default(cliffAmount),
|
|
325
|
-
cancelableBySender,
|
|
326
|
-
cancelableByRecipient,
|
|
327
|
-
automaticWithdrawal,
|
|
328
|
-
transferableBySender,
|
|
329
|
-
transferableByRecipient,
|
|
330
|
-
canTopup,
|
|
331
|
-
canUpdateRate: !!canUpdateRate,
|
|
332
|
-
canPause: !!canPause,
|
|
333
|
-
name,
|
|
334
|
-
withdrawFrequency: new bn_js_1.default(automaticWithdrawal ? withdrawalFrequency : period),
|
|
335
|
-
recipient: recipientPublicKey,
|
|
336
|
-
partner: partnerPublicKey,
|
|
337
|
-
}, this.programId, {
|
|
338
|
-
sender: sender.publicKey,
|
|
339
|
-
senderTokens,
|
|
340
|
-
metadata: metadataPubKey,
|
|
341
|
-
escrowTokens,
|
|
342
|
-
mint: new web3_js_1.PublicKey(mint),
|
|
343
|
-
feeOracle: constants_js_1.FEE_ORACLE_PUBLIC_KEY,
|
|
344
|
-
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
345
|
-
timelockProgram: this.programId,
|
|
346
|
-
tokenProgram: tokenProgramId,
|
|
347
|
-
withdrawor: constants_js_1.WITHDRAWOR_PUBLIC_KEY,
|
|
348
|
-
systemProgram: web3_js_1.SystemProgram.programId,
|
|
349
|
-
});
|
|
350
|
-
ixs.push(createMetadataInstruction, createInstruction);
|
|
351
|
-
return { ixs, metadata, metadataPubKey };
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Creates multiple stream/vesting contracts.
|
|
355
|
-
* All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
|
|
356
|
-
*/
|
|
357
|
-
async createMultiple(data, extParams) {
|
|
358
|
-
const { recipients, ...streamParams } = data;
|
|
359
|
-
const { sender, metadataPubKeys: metadataPubKeysExt, isNative, computePrice, computeLimit, customInstructions, } = extParams;
|
|
360
|
-
const metadatas = [];
|
|
361
|
-
const metadataToRecipient = {};
|
|
362
|
-
const errors = [];
|
|
363
|
-
const signatures = [];
|
|
364
|
-
const batch = [];
|
|
365
|
-
const instructionsBatch = [];
|
|
366
|
-
const metadataPubKeys = metadataPubKeysExt || [];
|
|
367
|
-
const partnerPublicKey = data.partner ? new web3_js_1.PublicKey(data.partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
|
|
368
|
-
const mintPublicKey = new web3_js_1.PublicKey(data.tokenId);
|
|
369
|
-
if (recipients.length === 0) {
|
|
370
|
-
throw new Error("Recipients array is empty!");
|
|
371
|
-
}
|
|
372
|
-
if (!sender.publicKey) {
|
|
373
|
-
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
374
|
-
}
|
|
375
|
-
for (let i = 0; i < recipients.length; i++) {
|
|
376
|
-
const recipientData = recipients[i];
|
|
377
|
-
const createStreamData = { ...streamParams, ...recipientData };
|
|
378
|
-
const createStreamExtParams = {
|
|
379
|
-
sender,
|
|
380
|
-
metadataPubKeys: metadataPubKeys[i] ? [metadataPubKeys[i]] : undefined,
|
|
381
|
-
computePrice,
|
|
382
|
-
computeLimit,
|
|
383
|
-
customInstructions,
|
|
384
|
-
};
|
|
385
|
-
const { ixs, metadata, metadataPubKey } = await this.prepareCreateInstructions(createStreamData, createStreamExtParams);
|
|
386
|
-
await this.applyCustomAfterInstructions(ixs, customInstructions, metadataPubKey);
|
|
387
|
-
metadataToRecipient[metadataPubKey.toBase58()] = recipientData;
|
|
388
|
-
metadatas.push(metadataPubKey.toBase58());
|
|
389
|
-
instructionsBatch.push({
|
|
390
|
-
ixs,
|
|
391
|
-
metadata,
|
|
392
|
-
recipient: recipientData.recipient,
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
const { value: hash, context } = await this.connection.getLatestBlockhashAndContext();
|
|
396
|
-
for (const { ixs, metadata, recipient } of instructionsBatch) {
|
|
397
|
-
const messageV0 = new web3_js_1.TransactionMessage({
|
|
398
|
-
payerKey: sender.publicKey,
|
|
399
|
-
recentBlockhash: hash.blockhash,
|
|
400
|
-
instructions: ixs,
|
|
401
|
-
}).compileToV0Message();
|
|
402
|
-
const tx = new web3_js_1.VersionedTransaction(messageV0);
|
|
403
|
-
if (metadata) {
|
|
404
|
-
tx.sign([metadata]);
|
|
405
|
-
}
|
|
406
|
-
batch.push({ tx, recipient });
|
|
407
|
-
}
|
|
408
|
-
const prepareInstructions = await this.getCreateATAInstructions([constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, partnerPublicKey], mintPublicKey, sender, true);
|
|
409
|
-
if (isNative) {
|
|
410
|
-
const totalDepositedAmount = recipients.reduce((acc, recipient) => recipient.amount.add(acc), new bn_js_1.default(0));
|
|
411
|
-
const nativeInstructions = await (0, solana_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalDepositedAmount);
|
|
412
|
-
prepareInstructions.push(...nativeInstructions);
|
|
413
|
-
}
|
|
414
|
-
if (prepareInstructions.length > 0) {
|
|
415
|
-
const messageV0 = new web3_js_1.TransactionMessage({
|
|
416
|
-
payerKey: sender.publicKey,
|
|
417
|
-
recentBlockhash: hash.blockhash,
|
|
418
|
-
instructions: prepareInstructions,
|
|
419
|
-
}).compileToV0Message();
|
|
420
|
-
const tx = new web3_js_1.VersionedTransaction(messageV0);
|
|
421
|
-
batch.push({
|
|
422
|
-
tx,
|
|
423
|
-
recipient: sender.publicKey.toBase58(),
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
const signedBatch = await (0, utils_js_1.signAllTransactionWithRecipients)(sender, batch);
|
|
427
|
-
if (prepareInstructions.length > 0) {
|
|
428
|
-
const prepareTx = signedBatch.pop();
|
|
429
|
-
await (0, utils_js_1.sendAndConfirmStreamRawTransaction)(this.connection, prepareTx, { hash, context }, this.schedulingParams);
|
|
430
|
-
}
|
|
431
|
-
const responses = [];
|
|
432
|
-
if (metadataPubKeys.length > 0) {
|
|
433
|
-
//if metadata pub keys were passed we should execute transaction sequentially
|
|
434
|
-
//ephemeral signer need to be used first before proceeding with the next
|
|
435
|
-
for (const batchTx of signedBatch) {
|
|
436
|
-
responses.push(...(await Promise.allSettled([
|
|
437
|
-
(0, solana_1.executeTransaction)(this.connection, batchTx.tx, { hash, context }, this.schedulingParams),
|
|
438
|
-
])));
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
else {
|
|
442
|
-
//send all transactions in parallel and wait for them to settle.
|
|
443
|
-
//it allows to speed up the process of sending transactions
|
|
444
|
-
//we then filter all promise responses and handle failed transactions
|
|
445
|
-
responses.push(...(await (0, solana_1.executeMultipleTransactions)(this.connection, signedBatch.map((item) => item.tx), { hash, context }, this.schedulingParams)));
|
|
446
|
-
}
|
|
447
|
-
responses.forEach((item, index) => {
|
|
448
|
-
if (item.status === "fulfilled") {
|
|
449
|
-
signatures.push(item.value);
|
|
450
|
-
}
|
|
451
|
-
else {
|
|
452
|
-
errors.push({
|
|
453
|
-
recipient: signedBatch[index].recipient,
|
|
454
|
-
error: item.reason,
|
|
455
|
-
contractErrorCode: this.extractErrorCode(item.reason) || undefined,
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
return { txs: signatures, metadatas, metadataToRecipient, errors };
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* Creates multiple stream/vesting contracts, and send all transactions sequentially.
|
|
463
|
-
* All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
|
|
464
|
-
* In most cases, createMultiple should be used instead.
|
|
465
|
-
*/
|
|
466
|
-
async createMultipleSequential(data, extParams) {
|
|
467
|
-
const { recipients, ...streamParams } = data;
|
|
468
|
-
const { sender, metadataPubKeys: metadataPubKeysExt, isNative, computePrice, computeLimit, customInstructions, } = extParams;
|
|
469
|
-
const metadatas = [];
|
|
470
|
-
const metadataToRecipient = {};
|
|
471
|
-
const errors = [];
|
|
472
|
-
const signatures = [];
|
|
473
|
-
const batch = [];
|
|
474
|
-
const instructionsBatch = [];
|
|
475
|
-
const metadataPubKeys = metadataPubKeysExt || [];
|
|
476
|
-
const partnerPublicKey = data.partner ? new web3_js_1.PublicKey(data.partner) : constants_js_1.WITHDRAWOR_PUBLIC_KEY;
|
|
477
|
-
const mintPublicKey = new web3_js_1.PublicKey(data.tokenId);
|
|
478
|
-
if (recipients.length === 0) {
|
|
479
|
-
throw new Error("Recipients array is empty!");
|
|
480
|
-
}
|
|
481
|
-
if (!sender.publicKey) {
|
|
482
|
-
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
483
|
-
}
|
|
484
|
-
for (let i = 0; i < recipients.length; i++) {
|
|
485
|
-
const recipientData = recipients[i];
|
|
486
|
-
const createStreamData = { ...streamParams, ...recipientData };
|
|
487
|
-
const createStreamExtParams = {
|
|
488
|
-
sender,
|
|
489
|
-
metadataPubKeys: metadataPubKeys[i] ? [metadataPubKeys[i]] : undefined,
|
|
490
|
-
computePrice,
|
|
491
|
-
computeLimit,
|
|
492
|
-
customInstructions,
|
|
493
|
-
};
|
|
494
|
-
const { ixs, metadata, metadataPubKey } = await this.prepareCreateInstructions(createStreamData, createStreamExtParams);
|
|
495
|
-
await this.applyCustomAfterInstructions(ixs, customInstructions, metadataPubKey);
|
|
496
|
-
metadataToRecipient[metadataPubKey.toBase58()] = recipientData;
|
|
497
|
-
metadatas.push(metadataPubKey.toBase58());
|
|
498
|
-
instructionsBatch.push({
|
|
499
|
-
ixs,
|
|
500
|
-
metadata,
|
|
501
|
-
recipient: recipientData.recipient,
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
const { value: hash, context } = await this.connection.getLatestBlockhashAndContext();
|
|
505
|
-
for (const { ixs, metadata, recipient } of instructionsBatch) {
|
|
506
|
-
const messageV0 = new web3_js_1.TransactionMessage({
|
|
507
|
-
payerKey: sender.publicKey,
|
|
508
|
-
recentBlockhash: hash.blockhash,
|
|
509
|
-
instructions: ixs,
|
|
510
|
-
}).compileToV0Message();
|
|
511
|
-
const tx = new web3_js_1.VersionedTransaction(messageV0);
|
|
512
|
-
if (metadata) {
|
|
513
|
-
tx.sign([metadata]);
|
|
514
|
-
}
|
|
515
|
-
batch.push({ tx, recipient });
|
|
516
|
-
}
|
|
517
|
-
const prepareInstructions = await this.getCreateATAInstructions([constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, partnerPublicKey], mintPublicKey, sender, true);
|
|
518
|
-
if (isNative) {
|
|
519
|
-
const totalDepositedAmount = recipients.reduce((acc, recipient) => recipient.amount.add(acc), new bn_js_1.default(0));
|
|
520
|
-
const nativeInstructions = await (0, solana_1.prepareWrappedAccount)(this.connection, sender.publicKey, totalDepositedAmount);
|
|
521
|
-
prepareInstructions.push(...nativeInstructions);
|
|
522
|
-
}
|
|
523
|
-
if (prepareInstructions.length > 0) {
|
|
524
|
-
const messageV0 = new web3_js_1.TransactionMessage({
|
|
525
|
-
payerKey: sender.publicKey,
|
|
526
|
-
recentBlockhash: hash.blockhash,
|
|
527
|
-
instructions: prepareInstructions,
|
|
528
|
-
}).compileToV0Message();
|
|
529
|
-
const tx = new web3_js_1.VersionedTransaction(messageV0);
|
|
530
|
-
batch.push({
|
|
531
|
-
tx,
|
|
532
|
-
recipient: sender.publicKey.toBase58(),
|
|
533
|
-
});
|
|
534
|
-
}
|
|
535
|
-
const signedBatch = await (0, utils_js_1.signAllTransactionWithRecipients)(sender, batch);
|
|
536
|
-
if (prepareInstructions.length > 0) {
|
|
537
|
-
const prepareTx = signedBatch.shift();
|
|
538
|
-
await (0, utils_js_1.sendAndConfirmStreamRawTransaction)(this.connection, prepareTx, { hash, context }, this.schedulingParams);
|
|
539
|
-
}
|
|
540
|
-
const responses = [];
|
|
541
|
-
for (const batchTx of signedBatch) {
|
|
542
|
-
responses.push(...(await Promise.allSettled([
|
|
543
|
-
(0, solana_1.executeTransaction)(this.connection, batchTx.tx, { hash, context }, this.schedulingParams),
|
|
544
|
-
])));
|
|
545
|
-
}
|
|
546
|
-
responses.forEach((item, index) => {
|
|
547
|
-
if (item.status === "fulfilled") {
|
|
548
|
-
signatures.push(item.value);
|
|
549
|
-
}
|
|
550
|
-
else {
|
|
551
|
-
errors.push({
|
|
552
|
-
recipient: signedBatch[index].recipient,
|
|
553
|
-
error: item.reason,
|
|
554
|
-
contractErrorCode: this.extractErrorCode(item.reason) || undefined,
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
});
|
|
558
|
-
return { txs: signatures, metadatas, metadataToRecipient, errors };
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
* Attempts withdrawing from the specified stream.
|
|
562
|
-
*/
|
|
563
|
-
async withdraw({ id, amount = constants_js_2.WITHDRAW_AVAILABLE_AMOUNT }, extParams) {
|
|
564
|
-
const { invoker, customInstructions } = extParams;
|
|
565
|
-
if (!invoker.publicKey) {
|
|
566
|
-
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
567
|
-
}
|
|
568
|
-
const ixs = await this.prepareWithdrawInstructions({ id, amount }, extParams);
|
|
569
|
-
const metadata = new web3_js_1.PublicKey(id);
|
|
570
|
-
await this.applyCustomAfterInstructions(ixs, customInstructions, metadata);
|
|
571
|
-
const { tx, hash, context } = await (0, solana_1.prepareTransaction)(this.connection, ixs, invoker.publicKey);
|
|
572
|
-
const signature = await (0, solana_1.signAndExecuteTransaction)(this.connection, extParams.invoker, tx, {
|
|
573
|
-
hash,
|
|
574
|
-
context,
|
|
575
|
-
commitment: this.getCommitment(),
|
|
576
|
-
}, this.schedulingParams);
|
|
577
|
-
return { ixs, txId: signature };
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* Creates Transaction Instructions for withdrawal
|
|
581
|
-
*/
|
|
582
|
-
async prepareWithdrawInstructions({ id, amount = constants_js_2.WITHDRAW_AVAILABLE_AMOUNT }, { invoker, checkTokenAccounts, computePrice, computeLimit }) {
|
|
583
|
-
if (!invoker.publicKey) {
|
|
584
|
-
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
585
|
-
}
|
|
586
|
-
const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
|
|
587
|
-
computePrice,
|
|
588
|
-
computeLimit,
|
|
589
|
-
});
|
|
590
|
-
const streamPublicKey = new web3_js_1.PublicKey(id);
|
|
591
|
-
const escrow = await this.connection.getAccountInfo(streamPublicKey);
|
|
592
|
-
if (!escrow?.data) {
|
|
593
|
-
throw new Error("Couldn't get account info");
|
|
594
|
-
}
|
|
595
|
-
const data = (0, utils_js_1.decodeStream)(escrow.data);
|
|
596
|
-
const { sender, recipient, mint, streamflowTreasury, partner, recipientTokens, escrowTokens } = data;
|
|
597
|
-
const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mint);
|
|
598
|
-
const streamflowTreasuryTokens = await (0, solana_1.ata)(mint, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
|
|
599
|
-
const partnerTokens = await (0, solana_1.ata)(mint, partner, tokenProgramId);
|
|
600
|
-
const ataIx = await this.getCreateATAInstructions([sender, recipient, streamflowTreasury, partner], mint, invoker, checkTokenAccounts, tokenProgramId);
|
|
601
|
-
ixs.push(...ataIx, await (0, instructions_js_1.withdrawStreamInstruction)(amount, this.programId, {
|
|
602
|
-
partner,
|
|
603
|
-
partnerTokens,
|
|
604
|
-
mint,
|
|
605
|
-
streamflowTreasuryTokens,
|
|
606
|
-
recipientTokens,
|
|
607
|
-
escrowTokens,
|
|
608
|
-
authority: invoker.publicKey,
|
|
609
|
-
recipient: invoker.publicKey,
|
|
610
|
-
metadata: streamPublicKey,
|
|
611
|
-
streamflowTreasury: constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
612
|
-
tokenProgram: tokenProgramId,
|
|
613
|
-
}));
|
|
614
|
-
return ixs;
|
|
615
|
-
}
|
|
616
|
-
/**
|
|
617
|
-
* Attempts canceling the specified stream.
|
|
618
|
-
*/
|
|
619
|
-
async cancel(cancelData, extParams) {
|
|
620
|
-
const ixs = await this.prepareCancelInstructions(cancelData, extParams);
|
|
621
|
-
const { tx, hash, context } = await (0, solana_1.prepareTransaction)(this.connection, ixs, extParams.invoker.publicKey);
|
|
622
|
-
const signature = await (0, solana_1.signAndExecuteTransaction)(this.connection, extParams.invoker, tx, {
|
|
623
|
-
hash,
|
|
624
|
-
context,
|
|
625
|
-
commitment: this.getCommitment(),
|
|
626
|
-
}, this.schedulingParams);
|
|
627
|
-
return { ixs, txId: signature };
|
|
628
|
-
}
|
|
629
|
-
async prepareCancelInstructions(cancelData, extParams) {
|
|
630
|
-
const streamPublicKey = new web3_js_1.PublicKey(cancelData.id);
|
|
631
|
-
const account = await this.connection.getAccountInfo(streamPublicKey);
|
|
632
|
-
if (!account) {
|
|
633
|
-
throw new Error("Impossible to cancel a stream contract that does not exist");
|
|
634
|
-
}
|
|
635
|
-
const { sender: senderPublicKey } = (0, utils_js_1.decodeStream)(account.data);
|
|
636
|
-
const isAlignedUnlock = this.isAlignedUnlock(streamPublicKey, senderPublicKey);
|
|
637
|
-
const ixs = isAlignedUnlock
|
|
638
|
-
? await this.prepareCancelAlignedUnlockInstructions(cancelData, extParams)
|
|
639
|
-
: await this.prepareCancelStreamInstructions(cancelData, extParams);
|
|
640
|
-
return ixs;
|
|
641
|
-
}
|
|
642
|
-
async prepareCancelAlignedUnlockInstructions({ id }, { invoker, checkTokenAccounts, computePrice, computeLimit }) {
|
|
643
|
-
if (!invoker.publicKey) {
|
|
644
|
-
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
645
|
-
}
|
|
646
|
-
const streamPublicKey = new web3_js_1.PublicKey(id);
|
|
647
|
-
const escrowAcc = await this.connection.getAccountInfo(streamPublicKey);
|
|
648
|
-
if (!escrowAcc?.data) {
|
|
649
|
-
throw new Error("Couldn't get account info");
|
|
650
|
-
}
|
|
651
|
-
const streamData = (0, utils_js_1.decodeStream)(escrowAcc.data);
|
|
652
|
-
const { sender, recipient, mint, streamflowTreasury, partner, escrowTokens } = streamData;
|
|
653
|
-
const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mint);
|
|
654
|
-
const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
|
|
655
|
-
computePrice,
|
|
656
|
-
computeLimit: computeLimit ?? constants_js_1.ALIGNED_COMPUTE_LIMIT,
|
|
657
|
-
});
|
|
658
|
-
const ataIx = await this.getCreateATAInstructions([sender, recipient, partner, streamflowTreasury], mint, invoker, checkTokenAccounts, tokenProgramId);
|
|
659
|
-
const cancelIx = await this.alignedProxyProgram.methods
|
|
660
|
-
.cancel()
|
|
661
|
-
.accountsPartial({
|
|
662
|
-
mint,
|
|
663
|
-
partner,
|
|
664
|
-
recipient,
|
|
665
|
-
escrowTokens,
|
|
666
|
-
sender: invoker.publicKey,
|
|
667
|
-
streamMetadata: streamPublicKey,
|
|
668
|
-
streamflowTreasury: constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
669
|
-
tokenProgram: tokenProgramId,
|
|
670
|
-
streamflowProgram: this.programId,
|
|
671
|
-
})
|
|
672
|
-
.instruction();
|
|
673
|
-
ixs.push(...ataIx, cancelIx);
|
|
674
|
-
return ixs;
|
|
675
|
-
}
|
|
676
|
-
/**
|
|
677
|
-
* Creates Transaction Instructions for cancel
|
|
678
|
-
*/
|
|
679
|
-
async prepareCancelStreamInstructions({ id }, { invoker, checkTokenAccounts, computePrice, computeLimit }) {
|
|
680
|
-
if (!invoker.publicKey) {
|
|
681
|
-
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
682
|
-
}
|
|
683
|
-
const streamPublicKey = new web3_js_1.PublicKey(id);
|
|
684
|
-
const escrowAcc = await this.connection.getAccountInfo(streamPublicKey);
|
|
685
|
-
if (!escrowAcc?.data) {
|
|
686
|
-
throw new Error("Couldn't get account info");
|
|
687
|
-
}
|
|
688
|
-
const data = (0, utils_js_1.decodeStream)(escrowAcc.data);
|
|
689
|
-
const { sender, recipient, partner, streamflowTreasury, mint, senderTokens, recipientTokens, escrowTokens } = data;
|
|
690
|
-
const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mint);
|
|
691
|
-
const streamflowTreasuryTokens = await (0, solana_1.ata)(mint, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
|
|
692
|
-
const partnerTokens = await (0, solana_1.ata)(mint, partner, tokenProgramId);
|
|
693
|
-
const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
|
|
694
|
-
computePrice,
|
|
695
|
-
computeLimit,
|
|
696
|
-
});
|
|
697
|
-
const ixsAta = await this.getCreateATAInstructions([sender, recipient, partner, streamflowTreasury], mint, invoker, checkTokenAccounts, tokenProgramId);
|
|
698
|
-
ixs.push(...ixsAta, await (0, instructions_js_1.cancelStreamInstruction)(this.programId, {
|
|
699
|
-
sender,
|
|
700
|
-
senderTokens,
|
|
701
|
-
recipient,
|
|
702
|
-
recipientTokens,
|
|
703
|
-
streamflowTreasuryTokens,
|
|
704
|
-
partner,
|
|
705
|
-
partnerTokens,
|
|
706
|
-
mint,
|
|
707
|
-
escrowTokens,
|
|
708
|
-
authority: invoker.publicKey,
|
|
709
|
-
metadata: streamPublicKey,
|
|
710
|
-
streamflowTreasury: constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
711
|
-
tokenProgram: tokenProgramId,
|
|
712
|
-
}));
|
|
713
|
-
return ixs;
|
|
714
|
-
}
|
|
715
|
-
/**
|
|
716
|
-
* Attempts changing the stream/vesting contract's recipient (effectively transferring the stream/vesting contract).
|
|
717
|
-
* Potential associated token account rent fee (to make it rent-exempt) is paid by the transaction initiator.
|
|
718
|
-
*/
|
|
719
|
-
async transfer({ id, newRecipient }, extParams) {
|
|
720
|
-
const ixs = await this.prepareTransferInstructions({ id, newRecipient }, extParams);
|
|
721
|
-
const { tx, hash, context } = await (0, solana_1.prepareTransaction)(this.connection, ixs, extParams.invoker.publicKey);
|
|
722
|
-
const signature = await (0, solana_1.signAndExecuteTransaction)(this.connection, extParams.invoker, tx, {
|
|
723
|
-
hash,
|
|
724
|
-
context,
|
|
725
|
-
commitment: this.getCommitment(),
|
|
726
|
-
}, this.schedulingParams);
|
|
727
|
-
return { ixs, txId: signature };
|
|
728
|
-
}
|
|
729
|
-
/**
|
|
730
|
-
* Attempts changing the stream/vesting contract's recipient (effectively transferring the stream/vesting contract).
|
|
731
|
-
* Potential associated token account rent fee (to make it rent-exempt) is paid by the transaction initiator.
|
|
732
|
-
*/
|
|
733
|
-
async prepareTransferInstructions({ id, newRecipient }, { invoker, computePrice, computeLimit = 100001 }) {
|
|
734
|
-
if (!invoker.publicKey) {
|
|
735
|
-
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
736
|
-
}
|
|
737
|
-
const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
|
|
738
|
-
computePrice,
|
|
739
|
-
computeLimit,
|
|
740
|
-
});
|
|
741
|
-
const stream = new web3_js_1.PublicKey(id);
|
|
742
|
-
const newRecipientPublicKey = new web3_js_1.PublicKey(newRecipient);
|
|
743
|
-
const escrow = await this.connection.getAccountInfo(stream);
|
|
744
|
-
if (!escrow?.data) {
|
|
745
|
-
throw new Error("Couldn't get account info");
|
|
746
|
-
}
|
|
747
|
-
const { mint } = (0, utils_js_1.decodeStream)(escrow?.data);
|
|
748
|
-
const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mint);
|
|
749
|
-
const newRecipientTokens = await (0, solana_1.ata)(mint, newRecipientPublicKey, tokenProgramId);
|
|
750
|
-
ixs.push(await (0, instructions_js_1.transferStreamInstruction)(this.programId, {
|
|
751
|
-
authority: invoker.publicKey,
|
|
752
|
-
newRecipient: newRecipientPublicKey,
|
|
753
|
-
newRecipientTokens,
|
|
754
|
-
metadata: stream,
|
|
755
|
-
mint,
|
|
756
|
-
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
757
|
-
tokenProgram: tokenProgramId,
|
|
758
|
-
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
759
|
-
systemProgram: web3_js_1.SystemProgram.programId,
|
|
760
|
-
}));
|
|
761
|
-
return ixs;
|
|
762
|
-
}
|
|
763
|
-
/**
|
|
764
|
-
* Tops up stream account with specified amount.
|
|
765
|
-
*/
|
|
766
|
-
async topup({ id, amount }, extParams) {
|
|
767
|
-
const ixs = await this.prepareTopupInstructions({ id, amount }, extParams);
|
|
768
|
-
const { tx, hash, context } = await (0, solana_1.prepareTransaction)(this.connection, ixs, extParams.invoker.publicKey);
|
|
769
|
-
const signature = await (0, solana_1.signAndExecuteTransaction)(this.connection, extParams.invoker, tx, {
|
|
770
|
-
hash,
|
|
771
|
-
context,
|
|
772
|
-
commitment: this.getCommitment(),
|
|
773
|
-
}, this.schedulingParams);
|
|
774
|
-
return { ixs, txId: signature };
|
|
775
|
-
}
|
|
776
|
-
/**
|
|
777
|
-
* Create Transaction instructions for topup
|
|
778
|
-
*/
|
|
779
|
-
async prepareTopupInstructions({ id, amount }, { invoker, isNative, computePrice, computeLimit }) {
|
|
780
|
-
if (!invoker.publicKey) {
|
|
781
|
-
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
782
|
-
}
|
|
783
|
-
const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
|
|
784
|
-
computePrice,
|
|
785
|
-
computeLimit,
|
|
786
|
-
});
|
|
787
|
-
const streamPublicKey = new web3_js_1.PublicKey(id);
|
|
788
|
-
const escrow = await this.connection.getAccountInfo(streamPublicKey);
|
|
789
|
-
if (!escrow?.data) {
|
|
790
|
-
throw new Error("Couldn't get account info");
|
|
791
|
-
}
|
|
792
|
-
const { mint, partner, senderTokens, escrowTokens } = (0, utils_js_1.decodeStream)(escrow?.data);
|
|
793
|
-
const { tokenProgramId } = await (0, solana_1.getMintAndProgram)(this.connection, mint);
|
|
794
|
-
const streamflowTreasuryTokens = await (0, solana_1.ata)(mint, constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
|
|
795
|
-
const partnerTokens = await (0, solana_1.ata)(mint, partner, tokenProgramId);
|
|
796
|
-
if (isNative) {
|
|
797
|
-
ixs.push(...(await (0, solana_1.prepareWrappedAccount)(this.connection, invoker.publicKey, amount)));
|
|
798
|
-
}
|
|
799
|
-
ixs.push(await (0, instructions_js_1.topupStreamInstruction)(amount, this.programId, {
|
|
800
|
-
sender: invoker.publicKey,
|
|
801
|
-
senderTokens,
|
|
802
|
-
metadata: streamPublicKey,
|
|
803
|
-
escrowTokens,
|
|
804
|
-
streamflowTreasury: constants_js_1.STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
805
|
-
streamflowTreasuryTokens: streamflowTreasuryTokens,
|
|
806
|
-
partner: partner,
|
|
807
|
-
partnerTokens: partnerTokens,
|
|
808
|
-
mint,
|
|
809
|
-
tokenProgram: tokenProgramId,
|
|
810
|
-
withdrawor: constants_js_1.WITHDRAWOR_PUBLIC_KEY,
|
|
811
|
-
systemProgram: web3_js_1.SystemProgram.programId,
|
|
812
|
-
}));
|
|
813
|
-
return ixs;
|
|
814
|
-
}
|
|
815
|
-
/**
|
|
816
|
-
* Fetch stream data by its id (address).
|
|
817
|
-
*/
|
|
818
|
-
async getOne({ id }) {
|
|
819
|
-
const streamPublicKey = new web3_js_1.PublicKey(id);
|
|
820
|
-
const escrow = await this.connection.getAccountInfo(streamPublicKey, constants_js_1.TX_FINALITY_CONFIRMED);
|
|
821
|
-
if (!escrow?.data) {
|
|
822
|
-
throw new Error("Couldn't get account info.");
|
|
823
|
-
}
|
|
824
|
-
const stream = (0, utils_js_1.decodeStream)(escrow.data);
|
|
825
|
-
if (this.isAlignedUnlock(streamPublicKey, stream.sender)) {
|
|
826
|
-
const alignedProxy = await this.alignedProxyProgram.account.contract.fetch((0, derive_accounts_js_1.deriveContractPDA)(this.alignedProxyProgram.programId, streamPublicKey));
|
|
827
|
-
if (!alignedProxy) {
|
|
828
|
-
throw new Error("Couldn't get proxy account info.");
|
|
829
|
-
}
|
|
830
|
-
return new types_js_1.AlignedContract(stream, alignedProxy);
|
|
831
|
-
}
|
|
832
|
-
return new types_js_1.Contract(stream);
|
|
833
|
-
}
|
|
834
|
-
/**
|
|
835
|
-
* Fetch all aligned outgoing streams/contracts by the provided public key.
|
|
836
|
-
*/
|
|
837
|
-
async getOutgoingAlignedStreams(sender) {
|
|
838
|
-
const streams = {};
|
|
839
|
-
const alignedOutgoingProgramAccounts = await this.alignedProxyProgram.account.contract.all([
|
|
840
|
-
{
|
|
841
|
-
memcmp: {
|
|
842
|
-
offset: constants_js_1.ORIGINAL_CONTRACT_SENDER_OFFSET,
|
|
843
|
-
bytes: sender,
|
|
844
|
-
},
|
|
845
|
-
},
|
|
846
|
-
{
|
|
847
|
-
dataSize: ALIGNED_METADATA_ACC_SIZE,
|
|
848
|
-
},
|
|
849
|
-
]);
|
|
850
|
-
const streamPubKeys = alignedOutgoingProgramAccounts.map((account) => account.account.stream);
|
|
851
|
-
const streamAccounts = await (0, solana_1.getMultipleAccountsInfoBatched)(this.connection, streamPubKeys, constants_js_1.TX_FINALITY_CONFIRMED);
|
|
852
|
-
streamAccounts.forEach((account, index) => {
|
|
853
|
-
if (account) {
|
|
854
|
-
const alignedData = alignedOutgoingProgramAccounts[index].account;
|
|
855
|
-
streams[streamPubKeys[index].toBase58()] = new types_js_1.AlignedContract((0, utils_js_1.decodeStream)(account.data), alignedData);
|
|
856
|
-
}
|
|
857
|
-
});
|
|
858
|
-
return streams;
|
|
859
|
-
}
|
|
860
|
-
async getIncomingAlignedStreams(streamRecord) {
|
|
861
|
-
const streams = {};
|
|
862
|
-
const alignedStreamsPubKeys = Object.keys(streamRecord);
|
|
863
|
-
const alignedProxyPDAs = alignedStreamsPubKeys.map((streamPubKey) => (0, derive_accounts_js_1.deriveContractPDA)(this.alignedProxyProgram.programId, new web3_js_1.PublicKey(streamPubKey)));
|
|
864
|
-
const alignedProxyAccounts = await (0, solana_1.getMultipleAccountsInfoBatched)(this.connection, alignedProxyPDAs);
|
|
865
|
-
alignedProxyAccounts.forEach((account, index) => {
|
|
866
|
-
if (account && account.data.length === ALIGNED_METADATA_ACC_SIZE) {
|
|
867
|
-
const alignedData = streamRecord[alignedStreamsPubKeys[index]];
|
|
868
|
-
streams[alignedStreamsPubKeys[index]] = new types_js_1.AlignedContract(alignedData, this.alignedProxyProgram.account.contract.coder.accounts.decode("contract", account.data));
|
|
869
|
-
}
|
|
870
|
-
});
|
|
871
|
-
return streams;
|
|
872
|
-
}
|
|
873
|
-
/**
|
|
874
|
-
* Fetch streams/contracts by providing direction.
|
|
875
|
-
* Streams are sorted by start time in ascending order.
|
|
876
|
-
*/
|
|
877
|
-
async get({ address, type = types_js_2.StreamType.All, direction = types_js_2.StreamDirection.All, }) {
|
|
878
|
-
const publicKey = new web3_js_1.PublicKey(address);
|
|
879
|
-
let streams = {};
|
|
880
|
-
// don't do unnecessary rpc calls if we are not querying for vesting streams
|
|
881
|
-
const shouldFetchAligned = type === types_js_2.StreamType.All || type === types_js_2.StreamType.Vesting;
|
|
882
|
-
if (direction !== types_js_2.StreamDirection.Incoming) {
|
|
883
|
-
const outgoingStreamAccounts = await (0, solana_1.getProgramAccounts)(this.connection, publicKey, constants_js_1.STREAM_STRUCT_OFFSET_SENDER, this.programId);
|
|
884
|
-
outgoingStreamAccounts.forEach((account) => {
|
|
885
|
-
streams[account.pubkey.toBase58()] = new types_js_1.Contract((0, utils_js_1.decodeStream)(account.account.data));
|
|
886
|
-
});
|
|
887
|
-
if (shouldFetchAligned) {
|
|
888
|
-
const alignedStreams = await this.getOutgoingAlignedStreams(address);
|
|
889
|
-
streams = { ...streams, ...alignedStreams };
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
if (direction !== types_js_2.StreamDirection.Outgoing) {
|
|
893
|
-
const allIncomingAccounts = await (0, solana_1.getProgramAccounts)(this.connection, publicKey, constants_js_1.STREAM_STRUCT_OFFSET_RECIPIENT, this.programId);
|
|
894
|
-
const allIncomingStreams = allIncomingAccounts.map((account) => (0, utils_js_1.decodeStream)(account.account.data));
|
|
895
|
-
const alignedDecoded = {};
|
|
896
|
-
// filter out aligned unlocks and store them in a separate object
|
|
897
|
-
allIncomingAccounts.forEach((account, index) => {
|
|
898
|
-
if (this.isAlignedUnlock(account.pubkey, allIncomingStreams[index].sender)) {
|
|
899
|
-
alignedDecoded[account.pubkey.toBase58()] = allIncomingStreams[index];
|
|
900
|
-
}
|
|
901
|
-
else {
|
|
902
|
-
streams[account.pubkey.toBase58()] = new types_js_1.Contract(allIncomingStreams[index]);
|
|
903
|
-
}
|
|
904
|
-
});
|
|
905
|
-
if (shouldFetchAligned) {
|
|
906
|
-
const incomingAlignedStreams = await this.getIncomingAlignedStreams(alignedDecoded);
|
|
907
|
-
streams = { ...streams, ...incomingAlignedStreams };
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
const sortedStreams = Object.entries(streams).sort(([, stream1], [, stream2]) => stream2.start - stream1.start);
|
|
911
|
-
if (type === types_js_2.StreamType.All)
|
|
912
|
-
return sortedStreams;
|
|
913
|
-
return sortedStreams.filter((stream) => stream[1].type === type);
|
|
914
|
-
}
|
|
915
|
-
async searchStreams(data) {
|
|
916
|
-
const filters = Object.entries(data)
|
|
917
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
918
|
-
.filter(([_, value]) => value !== undefined) // Only keep entries where the value is truthy
|
|
919
|
-
.map(([key, value]) => ({
|
|
920
|
-
memcmp: {
|
|
921
|
-
offset: constants_js_1.STREAM_STRUCT_OFFSETS[key],
|
|
922
|
-
bytes: typeof value === "boolean" ? bs58_1.default.encode([Number(value)]) : value.toString(),
|
|
923
|
-
},
|
|
924
|
-
}));
|
|
925
|
-
filters.push({ dataSize: METADATA_ACC_SIZE });
|
|
926
|
-
const accounts = await this.connection.getProgramAccounts(this.programId, {
|
|
927
|
-
filters,
|
|
928
|
-
});
|
|
929
|
-
return accounts.map(({ pubkey, account }) => ({
|
|
930
|
-
publicKey: pubkey,
|
|
931
|
-
account: new types_js_1.Contract((0, utils_js_1.decodeStream)(account.data)),
|
|
932
|
-
}));
|
|
933
|
-
}
|
|
934
|
-
/**
|
|
935
|
-
* Attempts updating the stream auto withdrawal params and amount per period
|
|
936
|
-
*/
|
|
937
|
-
async update(data, extParams) {
|
|
938
|
-
const ixs = await this.prepareUpdateInstructions(data, extParams);
|
|
939
|
-
const { tx, hash, context } = await (0, solana_1.prepareTransaction)(this.connection, ixs, extParams.invoker.publicKey);
|
|
940
|
-
const signature = await (0, solana_1.signAndExecuteTransaction)(this.connection, extParams.invoker, tx, {
|
|
941
|
-
hash,
|
|
942
|
-
context,
|
|
943
|
-
commitment: this.getCommitment(),
|
|
944
|
-
}, this.schedulingParams);
|
|
945
|
-
return {
|
|
946
|
-
ixs,
|
|
947
|
-
txId: signature,
|
|
948
|
-
};
|
|
949
|
-
}
|
|
950
|
-
/**
|
|
951
|
-
* Create Transaction instructions for update
|
|
952
|
-
*/
|
|
953
|
-
async prepareUpdateInstructions(data, { invoker, computePrice, computeLimit }) {
|
|
954
|
-
if (!invoker.publicKey) {
|
|
955
|
-
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
956
|
-
}
|
|
957
|
-
const streamPublicKey = new web3_js_1.PublicKey(data.id);
|
|
958
|
-
const escrow = await this.connection.getAccountInfo(streamPublicKey);
|
|
959
|
-
if (!escrow) {
|
|
960
|
-
throw new Error("Couldn't get account info");
|
|
961
|
-
}
|
|
962
|
-
const ixs = (0, solana_1.prepareBaseInstructions)(this.connection, {
|
|
963
|
-
computePrice,
|
|
964
|
-
computeLimit,
|
|
965
|
-
});
|
|
966
|
-
ixs.push(await (0, instructions_js_1.updateStreamInstruction)(data, this.programId, {
|
|
967
|
-
authority: invoker.publicKey,
|
|
968
|
-
metadata: streamPublicKey,
|
|
969
|
-
withdrawor: constants_js_1.WITHDRAWOR_PUBLIC_KEY,
|
|
970
|
-
systemProgram: web3_js_1.SystemProgram.programId,
|
|
971
|
-
}));
|
|
972
|
-
return ixs;
|
|
973
|
-
}
|
|
974
|
-
async getFees({ address }) {
|
|
975
|
-
const [metadataPubKey] = web3_js_1.PublicKey.findProgramAddressSync([buffer_1.Buffer.from(constants_js_1.FEES_METADATA_SEED)], new web3_js_1.PublicKey(constants_js_1.PARTNER_ORACLE_PROGRAM_ID));
|
|
976
|
-
const data = await this.connection.getAccountInfo(metadataPubKey);
|
|
977
|
-
if (!data) {
|
|
978
|
-
return null;
|
|
979
|
-
}
|
|
980
|
-
const partners = borsh.deserialize(constants_js_1.PARTNERS_SCHEMA, data.data);
|
|
981
|
-
const filteredPartners = partners.filter((item) => new web3_js_1.PublicKey(item.pubkey).toString() === address);
|
|
982
|
-
if (filteredPartners.length === 0) {
|
|
983
|
-
return null;
|
|
984
|
-
}
|
|
985
|
-
return {
|
|
986
|
-
streamflowFee: Number(filteredPartners[0].strm_fee.toFixed(4)),
|
|
987
|
-
partnerFee: Number(filteredPartners[0].partner_fee.toFixed(4)),
|
|
988
|
-
};
|
|
989
|
-
}
|
|
990
|
-
async getDefaultStreamflowFee() {
|
|
991
|
-
return constants_js_1.DEFAULT_STREAMFLOW_FEE;
|
|
992
|
-
}
|
|
993
|
-
extractErrorCode(err) {
|
|
994
|
-
const logs = "logs" in err && Array.isArray(err.logs) ? err.logs : undefined;
|
|
995
|
-
return (0, utils_js_1.extractSolanaErrorCode)(err.toString() ?? "Unknown error!", logs);
|
|
996
|
-
}
|
|
997
|
-
/**
|
|
998
|
-
* Utility function to generate metadata for a Contract or return existing Pubkey
|
|
999
|
-
*/
|
|
1000
|
-
getOrCreateStreamMetadata(metadataPubKeys) {
|
|
1001
|
-
let metadata;
|
|
1002
|
-
let metadataPubKey;
|
|
1003
|
-
if (!metadataPubKeys) {
|
|
1004
|
-
metadata = web3_js_1.Keypair.generate();
|
|
1005
|
-
metadataPubKey = metadata.publicKey;
|
|
1006
|
-
}
|
|
1007
|
-
else {
|
|
1008
|
-
metadataPubKey = metadataPubKeys[0];
|
|
1009
|
-
}
|
|
1010
|
-
return { metadata, metadataPubKey };
|
|
1011
|
-
}
|
|
1012
|
-
/**
|
|
1013
|
-
* Utility function that checks whether the associated stream address is an aligned unlock contract, indicated by whether the sender/creator is a PDA
|
|
1014
|
-
*/
|
|
1015
|
-
isAlignedUnlock(streamPublicKey, senderPublicKey) {
|
|
1016
|
-
const pda = (0, derive_accounts_js_1.deriveContractPDA)(this.alignedProxyProgram.programId, streamPublicKey);
|
|
1017
|
-
return senderPublicKey.equals(pda);
|
|
1018
|
-
}
|
|
1019
|
-
/**
|
|
1020
|
-
* Returns insrtuctions for creating associated token accounts for the provided owners
|
|
1021
|
-
*/
|
|
1022
|
-
async getCreateATAInstructions(owners, mint, invoker, checkTokenAccounts, programId) {
|
|
1023
|
-
if (!checkTokenAccounts) {
|
|
1024
|
-
return [];
|
|
1025
|
-
}
|
|
1026
|
-
// filter out duplicate PublicKeys, otherwise transaction will fail
|
|
1027
|
-
const uniqueOwners = Array.from(new Set(owners.map((owner) => owner.toBase58()))).map((pkString) => new web3_js_1.PublicKey(pkString));
|
|
1028
|
-
return (0, solana_1.checkOrCreateAtaBatch)(this.connection, uniqueOwners, mint, invoker, programId);
|
|
1029
|
-
}
|
|
1030
|
-
async applyCustomAfterInstructions(ixs, customInstructions, metadata) {
|
|
1031
|
-
if (customInstructions?.after) {
|
|
1032
|
-
const afterInstructions = typeof customInstructions.after === "function"
|
|
1033
|
-
? await customInstructions.after({
|
|
1034
|
-
instructions: ixs,
|
|
1035
|
-
metadata,
|
|
1036
|
-
})
|
|
1037
|
-
: customInstructions.after;
|
|
1038
|
-
ixs.push(...afterInstructions);
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
exports.SolanaStreamClient = SolanaStreamClient;
|