Skip to content

Commit

Permalink
STREAM-1172: optimise solana batching (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yolley authored Feb 19, 2024
1 parent 1cf6009 commit d410590
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"packages": [
"packages/*"
],
"version": "5.10.1",
"version": "5.10.2",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
2 changes: 1 addition & 1 deletion packages/stream/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/stream",
"version": "5.10.1",
"version": "5.10.2",
"description": "JavaScript SDK to interact with Streamflow protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/index.js",
Expand Down
53 changes: 32 additions & 21 deletions packages/stream/solana/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,19 +371,44 @@ export default class SolanaStreamClient extends BaseStreamClient {
const errors: ICreateMultiError[] = [];
const signatures: string[] = [];
const batch: BatchItem[] = [];
const instructionsBatch: {
ixs: TransactionInstruction[];
metadata: Keypair | undefined;
recipient: string;
}[] = [];
metadataPubKeys = metadataPubKeys || [];

for (let i = 0; i < recipients.length; i++) {
const recipientData = recipients[i];
const { tx, metadataPubKey } = await this.prepareStreamTransaction(recipientData, data, {
sender,
metadataPubKeys: metadataPubKeys[i] ? [metadataPubKeys[i]] : undefined,
});
const { ixs, metadata, metadataPubKey } = await this.prepareStreamInstructions(
recipientData,
data,
{
sender,
metadataPubKeys: metadataPubKeys[i] ? [metadataPubKeys[i]] : undefined,
}
);

metadataToRecipient[metadataPubKey.toBase58()] = recipientData;

metadatas.push(metadataPubKey.toBase58());
batch.push({ tx, recipient: recipientData.recipient });
instructionsBatch.push({ ixs, metadata, recipient: recipientData.recipient });
}

const commitment =
typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
const hash = await this.connection.getLatestBlockhash(commitment);

for (const { ixs, metadata, recipient } of instructionsBatch) {
const tx = new Transaction({
feePayer: sender.publicKey,
blockhash: hash.blockhash,
lastValidBlockHeight: hash.lastValidBlockHeight,
}).add(...ixs);
if (metadata) {
tx.partialSign(metadata);
}
batch.push({ tx, recipient });
}

if (isNative) {
Expand All @@ -397,9 +422,6 @@ export default class SolanaStreamClient extends BaseStreamClient {
totalDepositedAmount
);

const commitment =
typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
const hash = await this.connection.getLatestBlockhash(commitment);
const prepareTransaction = new Transaction({
feePayer: sender.publicKey,
blockhash: hash.blockhash,
Expand Down Expand Up @@ -814,7 +836,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
/**
* Forms instructions from params, creates a raw transaction and fetch recent blockhash.
*/
private async prepareStreamTransaction(
private async prepareStreamInstructions(
recipient: IRecipient,
streamParams: IStreamConfig,
solanaExtendedConfig: ICreateStreamSolanaExt
Expand All @@ -841,8 +863,6 @@ export default class SolanaStreamClient extends BaseStreamClient {
}

const ixs: TransactionInstruction[] = [];
const commitment =
typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
const recipientPublicKey = new PublicKey(recipient.recipient);
const mintPublicKey = new PublicKey(mint);
const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);
Expand Down Expand Up @@ -900,16 +920,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
}
)
);
const hash = await this.connection.getLatestBlockhash(commitment);
const tx = new Transaction({
feePayer: sender.publicKey,
blockhash: hash.blockhash,
lastValidBlockHeight: hash.lastValidBlockHeight,
}).add(...ixs);
if (metadata) {
tx.partialSign(metadata);
}
return { tx, metadataPubKey };
return { ixs, metadata, metadataPubKey };
}

/**
Expand Down

0 comments on commit d410590

Please sign in to comment.