@streamflow/stream 6.5.0 → 7.0.0-alpha.10
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 +17 -22
- package/dist/cjs/__tests__/aptos/streamClient.spec.js +111 -0
- package/dist/cjs/__tests__/common/GenericStreamClient.spec.js +45 -0
- package/dist/cjs/aptos/StreamClient.js +226 -0
- package/dist/cjs/aptos/constants.js +27 -0
- package/dist/{aptos → cjs/aptos}/index.js +6 -6
- package/dist/cjs/aptos/types.js +69 -0
- package/dist/{aptos → cjs/aptos}/utils.js +4 -4
- package/dist/cjs/aptos/wallet.js +34 -0
- package/dist/cjs/common/BaseStreamClient.js +23 -0
- package/dist/cjs/common/GenericStreamClient.js +102 -0
- package/dist/{common → cjs/common}/constants.js +2 -2
- package/dist/{common → cjs/common}/contractUtils.js +19 -20
- package/dist/cjs/common/utils.js +66 -0
- package/dist/cjs/evm/StreamClient.js +238 -0
- package/dist/cjs/evm/constants.js +38 -0
- package/dist/{evm → cjs/evm}/index.js +8 -8
- package/dist/{evm → cjs/evm}/types.js +26 -35
- package/dist/{evm → cjs/evm}/utils.js +2 -2
- package/dist/{index.js → cjs/index.js} +10 -10
- package/dist/cjs/solana/StreamClient.js +737 -0
- package/dist/{solana → cjs/solana}/constants.js +33 -33
- package/dist/cjs/solana/index.js +39 -0
- package/dist/{solana → cjs/solana}/instructions.js +83 -83
- package/dist/{solana → cjs/solana}/layout.js +8 -9
- package/dist/{solana → cjs/solana}/types.js +16 -26
- package/dist/cjs/solana/utils.js +134 -0
- package/dist/cjs/sui/StreamClient.js +402 -0
- package/dist/cjs/sui/constants.js +42 -0
- package/dist/{sui → cjs/sui}/index.js +5 -5
- package/dist/cjs/sui/types.js +68 -0
- package/dist/{sui → cjs/sui}/utils.js +7 -7
- package/dist/cjs/sui/wallet.js +40 -0
- package/dist/esm/__tests__/aptos/streamClient.spec.js +106 -0
- package/dist/esm/__tests__/common/GenericStreamClient.spec.js +40 -0
- package/dist/{aptos → esm/aptos}/StreamClient.d.ts +3 -3
- package/dist/esm/aptos/StreamClient.js +226 -0
- package/dist/{aptos → esm/aptos}/constants.d.ts +1 -1
- package/dist/esm/aptos/constants.js +24 -0
- package/dist/esm/aptos/index.d.ts +4 -0
- package/dist/esm/aptos/index.js +4 -0
- package/dist/{aptos → esm/aptos}/types.d.ts +13 -13
- package/dist/esm/aptos/types.js +105 -0
- package/dist/esm/aptos/utils.js +30 -0
- package/dist/esm/aptos/wallet.js +33 -0
- package/dist/{common → esm/common}/BaseStreamClient.d.ts +1 -1
- package/dist/esm/common/BaseStreamClient.js +19 -0
- package/dist/{common → esm/common}/GenericStreamClient.d.ts +6 -6
- package/dist/esm/common/GenericStreamClient.js +101 -0
- package/dist/esm/common/constants.d.ts +3 -0
- package/dist/esm/common/constants.js +3 -0
- package/dist/{common → esm/common}/contractUtils.d.ts +15 -15
- package/dist/esm/common/contractUtils.js +42 -0
- package/dist/{common → esm/common}/types.d.ts +19 -19
- package/dist/esm/common/types.js +106 -0
- package/dist/{common → esm/common}/utils.d.ts +11 -12
- package/dist/esm/common/utils.js +56 -0
- package/dist/{evm → esm/evm}/StreamClient.d.ts +4 -4
- package/dist/esm/evm/StreamClient.js +237 -0
- package/dist/esm/evm/abi.js +1255 -0
- package/dist/{evm → esm/evm}/constants.d.ts +1 -1
- package/dist/esm/evm/constants.js +35 -0
- package/dist/esm/evm/ercAbi.js +222 -0
- package/dist/esm/evm/index.d.ts +5 -0
- package/dist/esm/evm/index.js +5 -0
- package/dist/{evm → esm/evm}/types.d.ts +38 -38
- package/dist/esm/evm/types.js +102 -0
- package/dist/esm/evm/utils.js +8 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +9 -0
- package/dist/{solana → esm/solana}/StreamClient.d.ts +4 -4
- package/dist/esm/solana/StreamClient.js +711 -0
- package/dist/{solana → esm/solana}/constants.d.ts +3 -1
- package/dist/esm/solana/constants.js +62 -0
- package/dist/esm/solana/index.d.ts +6 -0
- package/dist/esm/solana/index.js +6 -0
- package/dist/esm/solana/instructionTypes.js +1 -0
- package/dist/{solana → esm/solana}/instructions.d.ts +18 -18
- package/dist/esm/solana/instructions.js +257 -0
- package/dist/{solana → esm/solana}/layout.d.ts +2 -1
- package/dist/esm/solana/layout.js +155 -0
- package/dist/esm/solana/timelockIDL.js +420 -0
- package/dist/{solana → esm/solana}/types.d.ts +38 -37
- package/dist/esm/solana/types.js +101 -0
- package/dist/{solana → esm/solana}/utils.d.ts +4 -1
- package/dist/esm/solana/utils.js +123 -0
- package/dist/{sui → esm/sui}/StreamClient.d.ts +3 -3
- package/dist/esm/sui/StreamClient.js +403 -0
- package/dist/{sui → esm/sui}/constants.d.ts +1 -1
- package/dist/esm/sui/constants.js +39 -0
- package/dist/esm/sui/index.d.ts +4 -0
- package/dist/esm/sui/index.js +4 -0
- package/dist/{sui → esm/sui}/types.d.ts +13 -13
- package/dist/esm/sui/types.js +104 -0
- package/dist/{sui → esm/sui}/utils.d.ts +1 -1
- package/dist/esm/sui/utils.js +23 -0
- package/dist/{sui → esm/sui}/wallet.d.ts +1 -1
- package/dist/esm/sui/wallet.js +38 -0
- package/package.json +37 -35
- package/dist/__tests__/aptos/streamClient.spec.js +0 -161
- package/dist/__tests__/common/GenericStreamClient.spec.js +0 -44
- package/dist/aptos/StreamClient.js +0 -431
- package/dist/aptos/constants.js +0 -28
- package/dist/aptos/index.d.ts +0 -4
- package/dist/aptos/types.js +0 -77
- package/dist/aptos/wallet.js +0 -89
- package/dist/common/BaseStreamClient.js +0 -71
- package/dist/common/GenericStreamClient.js +0 -128
- package/dist/common/constants.d.ts +0 -3
- package/dist/common/utils.js +0 -118
- package/dist/evm/StreamClient.js +0 -496
- package/dist/evm/constants.js +0 -39
- package/dist/evm/index.d.ts +0 -5
- package/dist/index.d.ts +0 -9
- package/dist/solana/StreamClient.js +0 -1253
- package/dist/solana/index.d.ts +0 -7
- package/dist/solana/index.js +0 -43
- package/dist/solana/utils.js +0 -211
- package/dist/sui/StreamClient.js +0 -648
- package/dist/sui/constants.js +0 -43
- package/dist/sui/index.d.ts +0 -4
- package/dist/sui/types.js +0 -102
- package/dist/sui/wallet.js +0 -90
- /package/dist/{common → cjs/common}/types.js +0 -0
- /package/dist/{evm → cjs/evm}/abi.js +0 -0
- /package/dist/{evm → cjs/evm}/ercAbi.js +0 -0
- /package/dist/{solana → cjs/solana}/instructionTypes.js +0 -0
- /package/dist/{solana → cjs/solana}/timelockIDL.js +0 -0
- /package/dist/{__tests__ → esm/__tests__}/aptos/streamClient.spec.d.ts +0 -0
- /package/dist/{__tests__ → esm/__tests__}/common/GenericStreamClient.spec.d.ts +0 -0
- /package/dist/{aptos → esm/aptos}/utils.d.ts +0 -0
- /package/dist/{aptos → esm/aptos}/wallet.d.ts +0 -0
- /package/dist/{evm → esm/evm}/abi.d.ts +0 -0
- /package/dist/{evm → esm/evm}/ercAbi.d.ts +0 -0
- /package/dist/{evm → esm/evm}/utils.d.ts +0 -0
- /package/dist/{solana → esm/solana}/instructionTypes.d.ts +0 -0
- /package/dist/{solana → esm/solana}/timelockIDL.d.ts +0 -0
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
// Latest version of the SDK that does not use Anchor. It supports raw instructions.
|
|
2
|
+
import BigNumber from "bignumber.js";
|
|
3
|
+
import { Buffer } from "buffer";
|
|
4
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, NATIVE_MINT } from "@solana/spl-token";
|
|
5
|
+
import { Connection, Keypair, PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY, TransactionMessage, VersionedTransaction, } from "@solana/web3.js";
|
|
6
|
+
import { ata, checkOrCreateAtaBatch, signAndExecuteTransaction, getProgramAccounts, prepareWrappedAccount, prepareTransaction, prepareBaseInstructions, getMintAndProgram, executeTransaction, executeMultipleTransactions, buildSendThrottler, } from "@streamflow/common/solana";
|
|
7
|
+
import * as borsh from "borsh";
|
|
8
|
+
import { Contract, } from "./types.js";
|
|
9
|
+
import { decodeStream, extractSolanaErrorCode, sendAndConfirmStreamRawTransaction, signAllTransactionWithRecipients, } from "./utils.js";
|
|
10
|
+
import { PROGRAM_ID, STREAMFLOW_TREASURY_PUBLIC_KEY, STREAM_STRUCT_OFFSET_RECIPIENT, STREAM_STRUCT_OFFSET_SENDER, TX_FINALITY_CONFIRMED, WITHDRAWOR_PUBLIC_KEY, FEE_ORACLE_PUBLIC_KEY, DEFAULT_STREAMFLOW_FEE, PARTNER_ORACLE_PROGRAM_ID, FEES_METADATA_SEED, PARTNERS_SCHEMA, STREAM_STRUCT_OFFSETS, } from "./constants.js";
|
|
11
|
+
import { withdrawStreamInstruction, cancelStreamInstruction, transferStreamInstruction, topupStreamInstruction, createStreamInstruction, createUncheckedStreamInstruction, updateStreamInstruction, } from "./instructions.js";
|
|
12
|
+
import { ICluster, StreamDirection, StreamType, } from "../common/types.js";
|
|
13
|
+
import { BaseStreamClient } from "../common/BaseStreamClient.js";
|
|
14
|
+
import { calculateTotalAmountToDeposit } from "../common/utils.js";
|
|
15
|
+
import { WITHDRAW_AVAILABLE_AMOUNT } from "../common/constants.js";
|
|
16
|
+
const METADATA_ACC_SIZE = 1104;
|
|
17
|
+
export class SolanaStreamClient extends BaseStreamClient {
|
|
18
|
+
connection;
|
|
19
|
+
programId;
|
|
20
|
+
commitment;
|
|
21
|
+
sendThrottler;
|
|
22
|
+
/**
|
|
23
|
+
* Create Stream instance
|
|
24
|
+
*/
|
|
25
|
+
constructor(clusterUrl, cluster = ICluster.Mainnet, commitment = "confirmed", programId = "", sendRate = 1, sendThrottler) {
|
|
26
|
+
super();
|
|
27
|
+
this.commitment = commitment;
|
|
28
|
+
this.connection = new Connection(clusterUrl, this.commitment);
|
|
29
|
+
this.programId = programId !== "" ? new PublicKey(programId) : new PublicKey(PROGRAM_ID[cluster]);
|
|
30
|
+
this.sendThrottler = sendThrottler ?? buildSendThrottler(sendRate);
|
|
31
|
+
}
|
|
32
|
+
getConnection() {
|
|
33
|
+
return this.connection;
|
|
34
|
+
}
|
|
35
|
+
getCommitment() {
|
|
36
|
+
return typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
|
|
37
|
+
}
|
|
38
|
+
getProgramId() {
|
|
39
|
+
return this.programId.toBase58();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Creates a new stream/vesting contract.
|
|
43
|
+
* All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
|
|
44
|
+
*/
|
|
45
|
+
async create(data, extParams) {
|
|
46
|
+
const { ixs, metadata, metadataPubKey } = await this.prepareCreateInstructions(data, extParams);
|
|
47
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.sender.publicKey, undefined, metadata);
|
|
48
|
+
const signature = await signAndExecuteTransaction(this.connection, extParams.sender, tx, {
|
|
49
|
+
hash,
|
|
50
|
+
context,
|
|
51
|
+
commitment: this.getCommitment(),
|
|
52
|
+
}, { sendThrottler: this.sendThrottler });
|
|
53
|
+
return { ixs, txId: signature, metadataId: metadataPubKey.toBase58() };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Creates a new stream/vesting contract.
|
|
57
|
+
* All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
|
|
58
|
+
*/
|
|
59
|
+
async prepareCreateInstructions({ recipient, tokenId: mint, start, amount: depositedAmount, period, cliff, cliffAmount, amountPerPeriod, name, canPause, canUpdateRate, canTopup, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal = false, withdrawalFrequency = 0, partner, }, { sender, metadataPubKeys, isNative = false, computePrice, computeLimit }) {
|
|
60
|
+
if (!sender.publicKey) {
|
|
61
|
+
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
62
|
+
}
|
|
63
|
+
const ixs = prepareBaseInstructions(this.connection, {
|
|
64
|
+
computePrice,
|
|
65
|
+
computeLimit,
|
|
66
|
+
});
|
|
67
|
+
const mintPublicKey = isNative ? NATIVE_MINT : new PublicKey(mint);
|
|
68
|
+
const recipientPublicKey = new PublicKey(recipient);
|
|
69
|
+
const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);
|
|
70
|
+
const [escrowTokens] = PublicKey.findProgramAddressSync([Buffer.from("strm"), metadataPubKey.toBuffer()], this.programId);
|
|
71
|
+
const { tokenProgramId } = await getMintAndProgram(this.connection, mintPublicKey);
|
|
72
|
+
const senderTokens = await ata(mintPublicKey, sender.publicKey, tokenProgramId);
|
|
73
|
+
const recipientTokens = await ata(mintPublicKey, recipientPublicKey, tokenProgramId);
|
|
74
|
+
const streamflowTreasuryTokens = await ata(mintPublicKey, STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
|
|
75
|
+
const partnerPublicKey = partner ? new PublicKey(partner) : WITHDRAWOR_PUBLIC_KEY;
|
|
76
|
+
const partnerTokens = await ata(mintPublicKey, partnerPublicKey, tokenProgramId);
|
|
77
|
+
if (isNative) {
|
|
78
|
+
const totalFee = await this.getTotalFee({
|
|
79
|
+
address: partner ?? sender.publicKey.toBase58(),
|
|
80
|
+
});
|
|
81
|
+
const totalAmount = calculateTotalAmountToDeposit(depositedAmount, totalFee);
|
|
82
|
+
ixs.push(...(await prepareWrappedAccount(this.connection, sender.publicKey, totalAmount)));
|
|
83
|
+
}
|
|
84
|
+
ixs.push(createStreamInstruction({
|
|
85
|
+
start: BigNumber(start),
|
|
86
|
+
depositedAmount,
|
|
87
|
+
period: BigNumber(period),
|
|
88
|
+
amountPerPeriod,
|
|
89
|
+
cliff: BigNumber(cliff),
|
|
90
|
+
cliffAmount: BigNumber(cliffAmount),
|
|
91
|
+
cancelableBySender,
|
|
92
|
+
cancelableByRecipient,
|
|
93
|
+
automaticWithdrawal,
|
|
94
|
+
transferableBySender,
|
|
95
|
+
transferableByRecipient,
|
|
96
|
+
canTopup,
|
|
97
|
+
canUpdateRate: !!canUpdateRate,
|
|
98
|
+
canPause: !!canPause,
|
|
99
|
+
name,
|
|
100
|
+
withdrawFrequency: BigNumber(automaticWithdrawal ? withdrawalFrequency : period),
|
|
101
|
+
}, this.programId, {
|
|
102
|
+
sender: sender.publicKey,
|
|
103
|
+
senderTokens,
|
|
104
|
+
recipient: new PublicKey(recipient),
|
|
105
|
+
metadata: metadataPubKey,
|
|
106
|
+
escrowTokens,
|
|
107
|
+
recipientTokens,
|
|
108
|
+
streamflowTreasury: STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
109
|
+
streamflowTreasuryTokens: streamflowTreasuryTokens,
|
|
110
|
+
partner: partnerPublicKey,
|
|
111
|
+
partnerTokens: partnerTokens,
|
|
112
|
+
mint: new PublicKey(mint),
|
|
113
|
+
feeOracle: FEE_ORACLE_PUBLIC_KEY,
|
|
114
|
+
rent: SYSVAR_RENT_PUBKEY,
|
|
115
|
+
timelockProgram: this.programId,
|
|
116
|
+
tokenProgram: tokenProgramId,
|
|
117
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
118
|
+
withdrawor: WITHDRAWOR_PUBLIC_KEY,
|
|
119
|
+
systemProgram: SystemProgram.programId,
|
|
120
|
+
}));
|
|
121
|
+
return { ixs, metadata, metadataPubKey };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Creates a new stream/vesting contract using unchecked instruction.
|
|
125
|
+
*
|
|
126
|
+
* Unchecked instruction differs from the regular in:
|
|
127
|
+
* - does not check for initialized associated token account (wallets with no control over their ATA should not be used
|
|
128
|
+
* as sender/recipient/partner or there are risks of funds being locked in the contract)
|
|
129
|
+
* - initialized contract PDA off chain
|
|
130
|
+
*
|
|
131
|
+
* If you are not sure if you should use create or create_unchecked, go for create to be safer.
|
|
132
|
+
*/
|
|
133
|
+
async createUnchecked(data, extParams) {
|
|
134
|
+
const { ixs, metadata, metadataPubKey } = await this.prepareCreateUncheckedInstructions(data, extParams);
|
|
135
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.sender.publicKey, undefined, metadata);
|
|
136
|
+
const signature = await signAndExecuteTransaction(this.connection, extParams.sender, tx, {
|
|
137
|
+
hash,
|
|
138
|
+
context,
|
|
139
|
+
commitment: this.getCommitment(),
|
|
140
|
+
}, { sendThrottler: this.sendThrottler });
|
|
141
|
+
return { ixs, txId: signature, metadataId: metadataPubKey.toBase58() };
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Create Transaction instructions for `createUnchecked`
|
|
145
|
+
*/
|
|
146
|
+
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 }) {
|
|
147
|
+
if (!sender.publicKey) {
|
|
148
|
+
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
149
|
+
}
|
|
150
|
+
const mintPublicKey = new PublicKey(mint);
|
|
151
|
+
const recipientPublicKey = new PublicKey(recipient);
|
|
152
|
+
const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);
|
|
153
|
+
const rentToExempt = await this.connection.getMinimumBalanceForRentExemption(METADATA_ACC_SIZE);
|
|
154
|
+
const createMetadataInstruction = SystemProgram.createAccount({
|
|
155
|
+
programId: this.programId,
|
|
156
|
+
space: METADATA_ACC_SIZE,
|
|
157
|
+
lamports: rentToExempt,
|
|
158
|
+
fromPubkey: sender?.publicKey,
|
|
159
|
+
newAccountPubkey: metadataPubKey,
|
|
160
|
+
});
|
|
161
|
+
const [escrowTokens] = PublicKey.findProgramAddressSync([Buffer.from("strm"), metadataPubKey.toBuffer()], this.programId);
|
|
162
|
+
const { tokenProgramId } = await getMintAndProgram(this.connection, mintPublicKey);
|
|
163
|
+
const senderTokens = await ata(mintPublicKey, sender.publicKey, tokenProgramId);
|
|
164
|
+
const partnerPublicKey = partner ? new PublicKey(partner) : WITHDRAWOR_PUBLIC_KEY;
|
|
165
|
+
const ixs = prepareBaseInstructions(this.connection, {
|
|
166
|
+
computePrice,
|
|
167
|
+
computeLimit,
|
|
168
|
+
});
|
|
169
|
+
if (isNative) {
|
|
170
|
+
const totalFee = await this.getTotalFee({ address: partner ?? sender.publicKey.toBase58() });
|
|
171
|
+
const totalAmount = calculateTotalAmountToDeposit(depositedAmount, totalFee);
|
|
172
|
+
ixs.push(...(await prepareWrappedAccount(this.connection, sender.publicKey, totalAmount)));
|
|
173
|
+
}
|
|
174
|
+
const createInstruction = createUncheckedStreamInstruction({
|
|
175
|
+
start: BigNumber(start),
|
|
176
|
+
depositedAmount,
|
|
177
|
+
period: BigNumber(period),
|
|
178
|
+
amountPerPeriod,
|
|
179
|
+
cliff: BigNumber(cliff),
|
|
180
|
+
cliffAmount: BigNumber(cliffAmount),
|
|
181
|
+
cancelableBySender,
|
|
182
|
+
cancelableByRecipient,
|
|
183
|
+
automaticWithdrawal,
|
|
184
|
+
transferableBySender,
|
|
185
|
+
transferableByRecipient,
|
|
186
|
+
canTopup,
|
|
187
|
+
canUpdateRate: !!canUpdateRate,
|
|
188
|
+
canPause: !!canPause,
|
|
189
|
+
name,
|
|
190
|
+
withdrawFrequency: BigNumber(automaticWithdrawal ? withdrawalFrequency : period),
|
|
191
|
+
recipient: recipientPublicKey,
|
|
192
|
+
partner: partnerPublicKey,
|
|
193
|
+
}, this.programId, {
|
|
194
|
+
sender: sender.publicKey,
|
|
195
|
+
senderTokens,
|
|
196
|
+
metadata: metadataPubKey,
|
|
197
|
+
escrowTokens,
|
|
198
|
+
mint: new PublicKey(mint),
|
|
199
|
+
feeOracle: FEE_ORACLE_PUBLIC_KEY,
|
|
200
|
+
rent: SYSVAR_RENT_PUBKEY,
|
|
201
|
+
timelockProgram: this.programId,
|
|
202
|
+
tokenProgram: tokenProgramId,
|
|
203
|
+
withdrawor: WITHDRAWOR_PUBLIC_KEY,
|
|
204
|
+
systemProgram: SystemProgram.programId,
|
|
205
|
+
});
|
|
206
|
+
ixs.push(createMetadataInstruction, createInstruction);
|
|
207
|
+
return { ixs, metadata, metadataPubKey };
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Creates multiple stream/vesting contracts.
|
|
211
|
+
* All fees are paid by sender (escrow metadata account rent, escrow token account rent, recipient's associated token account rent, Streamflow's service fee).
|
|
212
|
+
*/
|
|
213
|
+
async createMultiple(data, { sender, metadataPubKeys, isNative = false, computePrice, computeLimit }) {
|
|
214
|
+
const { recipients } = data;
|
|
215
|
+
if (!sender.publicKey) {
|
|
216
|
+
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
217
|
+
}
|
|
218
|
+
const metadatas = [];
|
|
219
|
+
const metadataToRecipient = {};
|
|
220
|
+
const errors = [];
|
|
221
|
+
const signatures = [];
|
|
222
|
+
const batch = [];
|
|
223
|
+
const instructionsBatch = [];
|
|
224
|
+
metadataPubKeys = metadataPubKeys || [];
|
|
225
|
+
for (let i = 0; i < recipients.length; i++) {
|
|
226
|
+
const recipientData = recipients[i];
|
|
227
|
+
const { ixs, metadata, metadataPubKey } = await this.prepareStreamInstructions(recipientData, data, {
|
|
228
|
+
sender,
|
|
229
|
+
metadataPubKeys: metadataPubKeys[i] ? [metadataPubKeys[i]] : undefined,
|
|
230
|
+
computePrice,
|
|
231
|
+
computeLimit,
|
|
232
|
+
});
|
|
233
|
+
metadataToRecipient[metadataPubKey.toBase58()] = recipientData;
|
|
234
|
+
metadatas.push(metadataPubKey.toBase58());
|
|
235
|
+
instructionsBatch.push({
|
|
236
|
+
ixs,
|
|
237
|
+
metadata,
|
|
238
|
+
recipient: recipientData.recipient,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
const { value: hash, context } = await this.connection.getLatestBlockhashAndContext();
|
|
242
|
+
for (const { ixs, metadata, recipient } of instructionsBatch) {
|
|
243
|
+
const messageV0 = new TransactionMessage({
|
|
244
|
+
payerKey: sender.publicKey,
|
|
245
|
+
recentBlockhash: hash.blockhash,
|
|
246
|
+
instructions: ixs,
|
|
247
|
+
}).compileToV0Message();
|
|
248
|
+
const tx = new VersionedTransaction(messageV0);
|
|
249
|
+
if (metadata) {
|
|
250
|
+
tx.sign([metadata]);
|
|
251
|
+
}
|
|
252
|
+
batch.push({ tx, recipient });
|
|
253
|
+
}
|
|
254
|
+
if (isNative) {
|
|
255
|
+
const totalDepositedAmount = recipients.reduce((acc, recipient) => recipient.amount.plus(acc), BigNumber(0));
|
|
256
|
+
const nativeInstructions = await prepareWrappedAccount(this.connection, sender.publicKey, totalDepositedAmount);
|
|
257
|
+
const messageV0 = new TransactionMessage({
|
|
258
|
+
payerKey: sender.publicKey,
|
|
259
|
+
recentBlockhash: hash.blockhash,
|
|
260
|
+
instructions: nativeInstructions,
|
|
261
|
+
}).compileToV0Message();
|
|
262
|
+
const tx = new VersionedTransaction(messageV0);
|
|
263
|
+
batch.push({
|
|
264
|
+
tx,
|
|
265
|
+
recipient: sender.publicKey.toBase58(),
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
const signedBatch = await signAllTransactionWithRecipients(sender, batch);
|
|
269
|
+
if (isNative) {
|
|
270
|
+
const prepareTx = signedBatch.pop();
|
|
271
|
+
await sendAndConfirmStreamRawTransaction(this.connection, prepareTx, { hash, context }, { sendThrottler: this.sendThrottler });
|
|
272
|
+
}
|
|
273
|
+
const responses = [];
|
|
274
|
+
if (metadataPubKeys.length > 0) {
|
|
275
|
+
//if metadata pub keys were passed we should execute transaction sequentially
|
|
276
|
+
//ephemeral signer need to be used first before proceeding with the next
|
|
277
|
+
for (const batchTx of signedBatch) {
|
|
278
|
+
responses.push(...(await Promise.allSettled([
|
|
279
|
+
executeTransaction(this.connection, batchTx.tx, { hash, context }, { sendThrottler: this.sendThrottler }),
|
|
280
|
+
])));
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
//send all transactions in parallel and wait for them to settle.
|
|
285
|
+
//it allows to speed up the process of sending transactions
|
|
286
|
+
//we then filter all promise responses and handle failed transactions
|
|
287
|
+
responses.push(...(await executeMultipleTransactions(this.connection, signedBatch.map((item) => item.tx), { hash, context }, { sendThrottler: this.sendThrottler })));
|
|
288
|
+
}
|
|
289
|
+
responses.forEach((item, index) => {
|
|
290
|
+
if (item.status === "fulfilled") {
|
|
291
|
+
signatures.push(item.value);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
errors.push({
|
|
295
|
+
recipient: signedBatch[index].recipient,
|
|
296
|
+
error: item.reason,
|
|
297
|
+
contractErrorCode: this.extractErrorCode(item.reason) || undefined,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
return { txs: signatures, metadatas, metadataToRecipient, errors };
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Attempts withdrawing from the specified stream.
|
|
305
|
+
*/
|
|
306
|
+
async withdraw({ id, amount = WITHDRAW_AVAILABLE_AMOUNT }, extParams) {
|
|
307
|
+
const ixs = await this.prepareWithdrawInstructions({ id, amount }, extParams);
|
|
308
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
|
|
309
|
+
const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, {
|
|
310
|
+
hash,
|
|
311
|
+
context,
|
|
312
|
+
commitment: this.getCommitment(),
|
|
313
|
+
}, { sendThrottler: this.sendThrottler });
|
|
314
|
+
return { ixs, txId: signature };
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Creates Transaction Instructions for withdrawal
|
|
318
|
+
*/
|
|
319
|
+
async prepareWithdrawInstructions({ id, amount = WITHDRAW_AVAILABLE_AMOUNT }, { invoker, checkTokenAccounts, computePrice, computeLimit }) {
|
|
320
|
+
if (!invoker.publicKey) {
|
|
321
|
+
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
322
|
+
}
|
|
323
|
+
const ixs = prepareBaseInstructions(this.connection, {
|
|
324
|
+
computePrice,
|
|
325
|
+
computeLimit,
|
|
326
|
+
});
|
|
327
|
+
const streamPublicKey = new PublicKey(id);
|
|
328
|
+
const escrow = await this.connection.getAccountInfo(streamPublicKey);
|
|
329
|
+
if (!escrow?.data) {
|
|
330
|
+
throw new Error("Couldn't get account info");
|
|
331
|
+
}
|
|
332
|
+
const data = decodeStream(escrow.data);
|
|
333
|
+
const { tokenProgramId } = await getMintAndProgram(this.connection, data.mint);
|
|
334
|
+
const streamflowTreasuryTokens = await ata(data.mint, STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
|
|
335
|
+
const partnerTokens = await ata(data.mint, data.partner, tokenProgramId);
|
|
336
|
+
await this.checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs, tokenProgramId);
|
|
337
|
+
ixs.push(withdrawStreamInstruction(amount, this.programId, {
|
|
338
|
+
authority: invoker.publicKey,
|
|
339
|
+
recipient: invoker.publicKey,
|
|
340
|
+
recipientTokens: data.recipientTokens,
|
|
341
|
+
metadata: streamPublicKey,
|
|
342
|
+
escrowTokens: data.escrowTokens,
|
|
343
|
+
streamflowTreasury: STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
344
|
+
streamflowTreasuryTokens,
|
|
345
|
+
partner: data.partner,
|
|
346
|
+
partnerTokens,
|
|
347
|
+
mint: data.mint,
|
|
348
|
+
tokenProgram: tokenProgramId,
|
|
349
|
+
}));
|
|
350
|
+
return ixs;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Attempts canceling the specified stream.
|
|
354
|
+
*/
|
|
355
|
+
async cancel({ id }, extParams) {
|
|
356
|
+
const ixs = await this.prepareCancelInstructions({ id }, extParams);
|
|
357
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
|
|
358
|
+
const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, {
|
|
359
|
+
hash,
|
|
360
|
+
context,
|
|
361
|
+
commitment: this.getCommitment(),
|
|
362
|
+
}, { sendThrottler: this.sendThrottler });
|
|
363
|
+
return { ixs, txId: signature };
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Creates Transaction Instructions for cancel
|
|
367
|
+
*/
|
|
368
|
+
async prepareCancelInstructions({ id }, { invoker, checkTokenAccounts, computePrice, computeLimit }) {
|
|
369
|
+
if (!invoker.publicKey) {
|
|
370
|
+
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
371
|
+
}
|
|
372
|
+
const streamPublicKey = new PublicKey(id);
|
|
373
|
+
const escrowAcc = await this.connection.getAccountInfo(streamPublicKey);
|
|
374
|
+
if (!escrowAcc?.data) {
|
|
375
|
+
throw new Error("Couldn't get account info");
|
|
376
|
+
}
|
|
377
|
+
const data = decodeStream(escrowAcc?.data);
|
|
378
|
+
const { tokenProgramId } = await getMintAndProgram(this.connection, data.mint);
|
|
379
|
+
const streamflowTreasuryTokens = await ata(data.mint, STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
|
|
380
|
+
const partnerTokens = await ata(data.mint, data.partner, tokenProgramId);
|
|
381
|
+
const ixs = prepareBaseInstructions(this.connection, {
|
|
382
|
+
computePrice,
|
|
383
|
+
computeLimit,
|
|
384
|
+
});
|
|
385
|
+
await this.checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs, tokenProgramId);
|
|
386
|
+
ixs.push(cancelStreamInstruction(this.programId, {
|
|
387
|
+
authority: invoker.publicKey,
|
|
388
|
+
sender: data.sender,
|
|
389
|
+
senderTokens: data.senderTokens,
|
|
390
|
+
recipient: data.recipient,
|
|
391
|
+
recipientTokens: data.recipientTokens,
|
|
392
|
+
metadata: streamPublicKey,
|
|
393
|
+
escrowTokens: data.escrowTokens,
|
|
394
|
+
streamflowTreasury: STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
395
|
+
streamflowTreasuryTokens: streamflowTreasuryTokens,
|
|
396
|
+
partner: data.partner,
|
|
397
|
+
partnerTokens,
|
|
398
|
+
mint: data.mint,
|
|
399
|
+
tokenProgram: tokenProgramId,
|
|
400
|
+
}));
|
|
401
|
+
return ixs;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Attempts changing the stream/vesting contract's recipient (effectively transferring the stream/vesting contract).
|
|
405
|
+
* Potential associated token account rent fee (to make it rent-exempt) is paid by the transaction initiator.
|
|
406
|
+
*/
|
|
407
|
+
async transfer({ id, newRecipient }, extParams) {
|
|
408
|
+
const ixs = await this.prepareTransferInstructions({ id, newRecipient }, extParams);
|
|
409
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
|
|
410
|
+
const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, {
|
|
411
|
+
hash,
|
|
412
|
+
context,
|
|
413
|
+
commitment: this.getCommitment(),
|
|
414
|
+
}, { sendThrottler: this.sendThrottler });
|
|
415
|
+
return { ixs, txId: signature };
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Attempts changing the stream/vesting contract's recipient (effectively transferring the stream/vesting contract).
|
|
419
|
+
* Potential associated token account rent fee (to make it rent-exempt) is paid by the transaction initiator.
|
|
420
|
+
*/
|
|
421
|
+
async prepareTransferInstructions({ id, newRecipient }, { invoker, computePrice, computeLimit = 100001 }) {
|
|
422
|
+
if (!invoker.publicKey) {
|
|
423
|
+
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
424
|
+
}
|
|
425
|
+
const ixs = prepareBaseInstructions(this.connection, {
|
|
426
|
+
computePrice,
|
|
427
|
+
computeLimit,
|
|
428
|
+
});
|
|
429
|
+
const stream = new PublicKey(id);
|
|
430
|
+
const newRecipientPublicKey = new PublicKey(newRecipient);
|
|
431
|
+
const escrow = await this.connection.getAccountInfo(stream);
|
|
432
|
+
if (!escrow?.data) {
|
|
433
|
+
throw new Error("Couldn't get account info");
|
|
434
|
+
}
|
|
435
|
+
const { mint } = decodeStream(escrow?.data);
|
|
436
|
+
const { tokenProgramId } = await getMintAndProgram(this.connection, mint);
|
|
437
|
+
const newRecipientTokens = await ata(mint, newRecipientPublicKey, tokenProgramId);
|
|
438
|
+
ixs.push(transferStreamInstruction(this.programId, {
|
|
439
|
+
authority: invoker.publicKey,
|
|
440
|
+
newRecipient: newRecipientPublicKey,
|
|
441
|
+
newRecipientTokens,
|
|
442
|
+
metadata: stream,
|
|
443
|
+
mint,
|
|
444
|
+
rent: SYSVAR_RENT_PUBKEY,
|
|
445
|
+
tokenProgram: tokenProgramId,
|
|
446
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
447
|
+
systemProgram: SystemProgram.programId,
|
|
448
|
+
}));
|
|
449
|
+
return ixs;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Tops up stream account with specified amount.
|
|
453
|
+
*/
|
|
454
|
+
async topup({ id, amount }, extParams) {
|
|
455
|
+
const ixs = await this.prepareTopupInstructions({ id, amount }, extParams);
|
|
456
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
|
|
457
|
+
const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, {
|
|
458
|
+
hash,
|
|
459
|
+
context,
|
|
460
|
+
commitment: this.getCommitment(),
|
|
461
|
+
}, { sendThrottler: this.sendThrottler });
|
|
462
|
+
return { ixs, txId: signature };
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Create Transaction instructions for topup
|
|
466
|
+
*/
|
|
467
|
+
async prepareTopupInstructions({ id, amount }, { invoker, isNative, computePrice, computeLimit }) {
|
|
468
|
+
if (!invoker.publicKey) {
|
|
469
|
+
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
470
|
+
}
|
|
471
|
+
const ixs = prepareBaseInstructions(this.connection, {
|
|
472
|
+
computePrice,
|
|
473
|
+
computeLimit,
|
|
474
|
+
});
|
|
475
|
+
const streamPublicKey = new PublicKey(id);
|
|
476
|
+
const escrow = await this.connection.getAccountInfo(streamPublicKey);
|
|
477
|
+
if (!escrow?.data) {
|
|
478
|
+
throw new Error("Couldn't get account info");
|
|
479
|
+
}
|
|
480
|
+
const { mint, partner, senderTokens, escrowTokens } = decodeStream(escrow?.data);
|
|
481
|
+
const { tokenProgramId } = await getMintAndProgram(this.connection, mint);
|
|
482
|
+
const streamflowTreasuryTokens = await ata(mint, STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
|
|
483
|
+
const partnerTokens = await ata(mint, partner, tokenProgramId);
|
|
484
|
+
if (isNative) {
|
|
485
|
+
ixs.push(...(await prepareWrappedAccount(this.connection, invoker.publicKey, amount)));
|
|
486
|
+
}
|
|
487
|
+
ixs.push(topupStreamInstruction(amount, this.programId, {
|
|
488
|
+
sender: invoker.publicKey,
|
|
489
|
+
senderTokens,
|
|
490
|
+
metadata: streamPublicKey,
|
|
491
|
+
escrowTokens,
|
|
492
|
+
streamflowTreasury: STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
493
|
+
streamflowTreasuryTokens: streamflowTreasuryTokens,
|
|
494
|
+
partner: partner,
|
|
495
|
+
partnerTokens: partnerTokens,
|
|
496
|
+
mint,
|
|
497
|
+
tokenProgram: tokenProgramId,
|
|
498
|
+
withdrawor: WITHDRAWOR_PUBLIC_KEY,
|
|
499
|
+
systemProgram: SystemProgram.programId,
|
|
500
|
+
}));
|
|
501
|
+
return ixs;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Fetch stream data by its id (address).
|
|
505
|
+
*/
|
|
506
|
+
async getOne({ id }) {
|
|
507
|
+
const escrow = await this.connection.getAccountInfo(new PublicKey(id), TX_FINALITY_CONFIRMED);
|
|
508
|
+
if (!escrow?.data) {
|
|
509
|
+
throw new Error("Couldn't get account info.");
|
|
510
|
+
}
|
|
511
|
+
return new Contract(decodeStream(escrow?.data));
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Fetch streams/contracts by providing direction.
|
|
515
|
+
* Streams are sorted by start time in ascending order.
|
|
516
|
+
*/
|
|
517
|
+
async get({ address, type = StreamType.All, direction = StreamDirection.All, }) {
|
|
518
|
+
const publicKey = new PublicKey(address);
|
|
519
|
+
let accounts = [];
|
|
520
|
+
//todo: we need to be smart with our layout so we minimize rpc calls to the chain
|
|
521
|
+
if (direction === "all") {
|
|
522
|
+
const outgoingAccounts = await getProgramAccounts(this.connection, publicKey, STREAM_STRUCT_OFFSET_SENDER, this.programId);
|
|
523
|
+
const incomingAccounts = await getProgramAccounts(this.connection, publicKey, STREAM_STRUCT_OFFSET_RECIPIENT, this.programId);
|
|
524
|
+
accounts = [...outgoingAccounts, ...incomingAccounts];
|
|
525
|
+
}
|
|
526
|
+
else {
|
|
527
|
+
const offset = direction === "outgoing" ? STREAM_STRUCT_OFFSET_SENDER : STREAM_STRUCT_OFFSET_RECIPIENT;
|
|
528
|
+
accounts = await getProgramAccounts(this.connection, publicKey, offset, this.programId);
|
|
529
|
+
}
|
|
530
|
+
let streams = {};
|
|
531
|
+
accounts.forEach((account) => {
|
|
532
|
+
const decoded = new Contract(decodeStream(account.account.data));
|
|
533
|
+
streams = { ...streams, [account.pubkey.toBase58()]: decoded };
|
|
534
|
+
});
|
|
535
|
+
const sortedStreams = Object.entries(streams).sort(([, stream1], [, stream2]) => stream2.start - stream1.start);
|
|
536
|
+
if (type === "all")
|
|
537
|
+
return sortedStreams;
|
|
538
|
+
return sortedStreams.filter((stream) => stream[1].type === type);
|
|
539
|
+
}
|
|
540
|
+
async searchStreams(data) {
|
|
541
|
+
const filters = Object.entries(data)
|
|
542
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
543
|
+
.filter(([_, value]) => value) // Only keep entries where the value is truthy
|
|
544
|
+
.map(([key, value]) => ({
|
|
545
|
+
memcmp: {
|
|
546
|
+
offset: STREAM_STRUCT_OFFSETS[key],
|
|
547
|
+
bytes: value,
|
|
548
|
+
},
|
|
549
|
+
}));
|
|
550
|
+
const accounts = await this.connection.getProgramAccounts(this.programId, { filters });
|
|
551
|
+
return accounts.map(({ pubkey, account }) => ({
|
|
552
|
+
publicKey: pubkey,
|
|
553
|
+
account: new Contract(decodeStream(account.data)),
|
|
554
|
+
}));
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Attempts updating the stream auto withdrawal params and amount per period
|
|
558
|
+
*/
|
|
559
|
+
async update(data, extParams) {
|
|
560
|
+
const ixs = await this.prepareUpdateInstructions(data, extParams);
|
|
561
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
|
|
562
|
+
const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, {
|
|
563
|
+
hash,
|
|
564
|
+
context,
|
|
565
|
+
commitment: this.getCommitment(),
|
|
566
|
+
}, { sendThrottler: this.sendThrottler });
|
|
567
|
+
return {
|
|
568
|
+
ixs,
|
|
569
|
+
txId: signature,
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Create Transaction instructions for update
|
|
574
|
+
*/
|
|
575
|
+
async prepareUpdateInstructions(data, { invoker, computePrice, computeLimit }) {
|
|
576
|
+
if (!invoker.publicKey) {
|
|
577
|
+
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
578
|
+
}
|
|
579
|
+
const streamPublicKey = new PublicKey(data.id);
|
|
580
|
+
const escrow = await this.connection.getAccountInfo(streamPublicKey);
|
|
581
|
+
if (!escrow) {
|
|
582
|
+
throw new Error("Couldn't get account info");
|
|
583
|
+
}
|
|
584
|
+
const ixs = prepareBaseInstructions(this.connection, {
|
|
585
|
+
computePrice,
|
|
586
|
+
computeLimit,
|
|
587
|
+
});
|
|
588
|
+
ixs.push(updateStreamInstruction(data, this.programId, {
|
|
589
|
+
authority: invoker.publicKey,
|
|
590
|
+
metadata: streamPublicKey,
|
|
591
|
+
withdrawor: WITHDRAWOR_PUBLIC_KEY,
|
|
592
|
+
systemProgram: SystemProgram.programId,
|
|
593
|
+
}));
|
|
594
|
+
return ixs;
|
|
595
|
+
}
|
|
596
|
+
async getFees({ address }) {
|
|
597
|
+
const [metadataPubKey] = PublicKey.findProgramAddressSync([Buffer.from(FEES_METADATA_SEED)], new PublicKey(PARTNER_ORACLE_PROGRAM_ID));
|
|
598
|
+
const data = await this.connection.getAccountInfo(metadataPubKey);
|
|
599
|
+
if (!data) {
|
|
600
|
+
return null;
|
|
601
|
+
}
|
|
602
|
+
const partners = borsh.deserialize(PARTNERS_SCHEMA, data.data);
|
|
603
|
+
const filteredPartners = partners.filter((item) => new PublicKey(item.pubkey).toString() === address);
|
|
604
|
+
if (filteredPartners.length === 0) {
|
|
605
|
+
return null;
|
|
606
|
+
}
|
|
607
|
+
return {
|
|
608
|
+
streamflowFee: Number(filteredPartners[0].strm_fee.toFixed(4)),
|
|
609
|
+
partnerFee: Number(filteredPartners[0].partner_fee.toFixed(4)),
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
async getDefaultStreamflowFee() {
|
|
613
|
+
return DEFAULT_STREAMFLOW_FEE;
|
|
614
|
+
}
|
|
615
|
+
extractErrorCode(err) {
|
|
616
|
+
const logs = "logs" in err && Array.isArray(err.logs) ? err.logs : undefined;
|
|
617
|
+
return extractSolanaErrorCode(err.toString() ?? "Unknown error!", logs);
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Forms instructions from params, creates a raw transaction and fetch recent blockhash.
|
|
621
|
+
*/
|
|
622
|
+
async prepareStreamInstructions(recipient, streamParams, extParams) {
|
|
623
|
+
const { tokenId: mint, start, period, cliff, canTopup, canUpdateRate, canPause, cancelableBySender, cancelableByRecipient, transferableBySender, transferableByRecipient, automaticWithdrawal = false, withdrawalFrequency = 0, partner, } = streamParams;
|
|
624
|
+
const { sender, metadataPubKeys, computeLimit, computePrice } = extParams;
|
|
625
|
+
if (!sender.publicKey) {
|
|
626
|
+
throw new Error("Sender's PublicKey is not available, check passed wallet adapter!");
|
|
627
|
+
}
|
|
628
|
+
const ixs = prepareBaseInstructions(this.connection, {
|
|
629
|
+
computePrice,
|
|
630
|
+
computeLimit,
|
|
631
|
+
});
|
|
632
|
+
const recipientPublicKey = new PublicKey(recipient.recipient);
|
|
633
|
+
const mintPublicKey = new PublicKey(mint);
|
|
634
|
+
const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);
|
|
635
|
+
const [escrowTokens] = PublicKey.findProgramAddressSync([Buffer.from("strm"), metadataPubKey.toBuffer()], this.programId);
|
|
636
|
+
const { tokenProgramId } = await getMintAndProgram(this.connection, mintPublicKey);
|
|
637
|
+
const senderTokens = await ata(mintPublicKey, sender.publicKey, tokenProgramId);
|
|
638
|
+
const recipientTokens = await ata(mintPublicKey, recipientPublicKey, tokenProgramId);
|
|
639
|
+
const streamflowTreasuryTokens = await ata(mintPublicKey, STREAMFLOW_TREASURY_PUBLIC_KEY, tokenProgramId);
|
|
640
|
+
const partnerPublicKey = partner ? new PublicKey(partner) : WITHDRAWOR_PUBLIC_KEY;
|
|
641
|
+
const partnerTokens = await ata(mintPublicKey, partnerPublicKey, tokenProgramId);
|
|
642
|
+
ixs.push(createStreamInstruction({
|
|
643
|
+
start: BigNumber(start),
|
|
644
|
+
depositedAmount: recipient.amount,
|
|
645
|
+
period: BigNumber(period),
|
|
646
|
+
amountPerPeriod: recipient.amountPerPeriod,
|
|
647
|
+
cliff: BigNumber(cliff),
|
|
648
|
+
cliffAmount: BigNumber(recipient.cliffAmount),
|
|
649
|
+
cancelableBySender,
|
|
650
|
+
cancelableByRecipient,
|
|
651
|
+
automaticWithdrawal,
|
|
652
|
+
transferableBySender,
|
|
653
|
+
transferableByRecipient,
|
|
654
|
+
canTopup,
|
|
655
|
+
canUpdateRate: !!canUpdateRate,
|
|
656
|
+
canPause: !!canPause,
|
|
657
|
+
name: recipient.name,
|
|
658
|
+
withdrawFrequency: BigNumber(automaticWithdrawal ? withdrawalFrequency : period),
|
|
659
|
+
}, this.programId, {
|
|
660
|
+
sender: sender.publicKey,
|
|
661
|
+
senderTokens,
|
|
662
|
+
recipient: new PublicKey(recipient.recipient),
|
|
663
|
+
metadata: metadataPubKey,
|
|
664
|
+
escrowTokens,
|
|
665
|
+
recipientTokens,
|
|
666
|
+
streamflowTreasury: STREAMFLOW_TREASURY_PUBLIC_KEY,
|
|
667
|
+
streamflowTreasuryTokens: streamflowTreasuryTokens,
|
|
668
|
+
partner: partnerPublicKey,
|
|
669
|
+
partnerTokens: partnerTokens,
|
|
670
|
+
mint: new PublicKey(mint),
|
|
671
|
+
feeOracle: FEE_ORACLE_PUBLIC_KEY,
|
|
672
|
+
rent: SYSVAR_RENT_PUBKEY,
|
|
673
|
+
timelockProgram: this.programId,
|
|
674
|
+
tokenProgram: tokenProgramId,
|
|
675
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
676
|
+
withdrawor: WITHDRAWOR_PUBLIC_KEY,
|
|
677
|
+
systemProgram: SystemProgram.programId,
|
|
678
|
+
}));
|
|
679
|
+
return { ixs, metadata, metadataPubKey };
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Utility function to generate metadata for a Contract or return existing Pubkey
|
|
683
|
+
*/
|
|
684
|
+
getOrCreateStreamMetadata(metadataPubKeys) {
|
|
685
|
+
let metadata;
|
|
686
|
+
let metadataPubKey;
|
|
687
|
+
if (!metadataPubKeys) {
|
|
688
|
+
metadata = Keypair.generate();
|
|
689
|
+
metadataPubKey = metadata.publicKey;
|
|
690
|
+
}
|
|
691
|
+
else {
|
|
692
|
+
metadataPubKey = metadataPubKeys[0];
|
|
693
|
+
}
|
|
694
|
+
return { metadata, metadataPubKey };
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Utility function that checks whether associated token accounts still exist and adds instructions to add them if not
|
|
698
|
+
*/
|
|
699
|
+
async checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs, programId) {
|
|
700
|
+
if (!checkTokenAccounts) {
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
const owners = Array.from(new Set([
|
|
704
|
+
data.sender.toBase58(),
|
|
705
|
+
data.recipient.toBase58(),
|
|
706
|
+
data.partner.toBase58(),
|
|
707
|
+
data.streamflowTreasury.toBase58(),
|
|
708
|
+
]), (address) => new PublicKey(address));
|
|
709
|
+
ixs.push(...(await checkOrCreateAtaBatch(this.connection, owners, data.mint, invoker, programId)));
|
|
710
|
+
}
|
|
711
|
+
}
|