Skip to content

Commit

Permalink
execute multiple tx in seq for SquadsX (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yolley authored Dec 8, 2023
1 parent 71ae905 commit c28cc07
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 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.7.0",
"version": "5.7.1",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.7.0",
"version": "5.7.1",
"description": "JavaScript SDK to interact with Streamflow protocol.",
"main": "dist/index.js",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
Expand Down
73 changes: 40 additions & 33 deletions packages/stream/solana/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
const mintPublicKey = isNative ? NATIVE_MINT : new PublicKey(mint);
const recipientPublicKey = new PublicKey(recipient);

let metadata: Keypair | null = null;
let metadataPubKey: PublicKey;
if (!metadataPubKeys) {
metadata = Keypair.generate();
metadataPubKey = metadata.publicKey;
} else {
metadataPubKey = metadataPubKeys[0];
}
const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);
const [escrowTokens] = PublicKey.findProgramAddressSync(
[Buffer.from("strm"), metadataPubKey.toBuffer()],
this.programId
Expand Down Expand Up @@ -271,15 +264,7 @@ export default class SolanaStreamClient extends BaseStreamClient {

const mintPublicKey = new PublicKey(mint);
const recipientPublicKey = new PublicKey(recipient);

let metadata: Keypair | null = null;
let metadataPubKey: PublicKey;
if (!metadataPubKeys) {
metadata = Keypair.generate();
metadataPubKey = metadata.publicKey;
} else {
metadataPubKey = metadataPubKeys[0];
}
const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);

const rentToExempt = await this.connection.getMinimumBalanceForRentExemption(METADATA_ACC_SIZE);
const createMetadataInstruction = SystemProgram.createAccount({
Expand Down Expand Up @@ -423,14 +408,26 @@ export default class SolanaStreamClient extends BaseStreamClient {
await sendAndConfirmStreamRawTransaction(this.connection, prepareTx!);
}

//send all transactions in parallel and wait for them to settle.
//it allows to speed up the process of sending transactions
//we then filter all promise responses and handle failed transactions
const batchTransactionsCalls = signedBatch.map((el) =>
sendAndConfirmStreamRawTransaction(this.connection, el)
);

const responses = await Promise.allSettled(batchTransactionsCalls);
const responses: PromiseSettledResult<BatchItem>[] = [];
if (metadataPubKeys.length > 0) {
//if metadata pub keys were passed we should execute transaction sequentially
//ephemeral signer need to be used first before proceeding with the next
for (const batchTx of signedBatch) {
responses.push(
...(await Promise.allSettled([
sendAndConfirmStreamRawTransaction(this.connection, batchTx),
]))
);
}
} else {
//send all transactions in parallel and wait for them to settle.
//it allows to speed up the process of sending transactions
//we then filter all promise responses and handle failed transactions
const batchTransactionsCalls = signedBatch.map((el) =>
sendAndConfirmStreamRawTransaction(this.connection, el)
);
responses.push(...(await Promise.allSettled(batchTransactionsCalls)));
}

const successes = responses
.filter((el): el is PromiseFulfilledResult<BatchItemSuccess> => el.status === "fulfilled")
Expand Down Expand Up @@ -842,14 +839,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
const recipientPublicKey = new PublicKey(recipient.recipient);
const mintPublicKey = new PublicKey(mint);
let metadata: Keypair | null = null;
let metadataPubKey: PublicKey;
if (!metadataPubKeys) {
metadata = Keypair.generate();
metadataPubKey = metadata.publicKey;
} else {
metadataPubKey = metadataPubKeys[0];
}
const { metadata, metadataPubKey } = this.getOrCreateStreamMetadata(metadataPubKeys);
const [escrowTokens] = PublicKey.findProgramAddressSync(
[Buffer.from("strm"), metadataPubKey.toBuffer()],
this.programId
Expand Down Expand Up @@ -915,4 +905,21 @@ export default class SolanaStreamClient extends BaseStreamClient {
}
return { tx, metadataPubKey };
}

/**
* Utility function to generate metadata for a Contract or return existing Pubkey
*/
private getOrCreateStreamMetadata(metadataPubKeys?: PublicKey[]) {
let metadata;
let metadataPubKey;

if (!metadataPubKeys) {
metadata = Keypair.generate();
metadataPubKey = metadata.publicKey;
} else {
metadataPubKey = metadataPubKeys[0];
}

return { metadata, metadataPubKey };
}
}
2 changes: 1 addition & 1 deletion packages/stream/solana/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface Account {

export interface ICreateStreamSolanaExt {
sender: SignerWalletAdapter | Keypair;
// allow custom Metadata Account to be passed, i.e. an ephemeral signer, accepts array to be compatible in createMultiple
// allow custom Metadata Account to be passed, ephemeral signer is most cases, accepts array to be compatible in createMultiple
metadataPubKeys?: PublicKey[];
partner?: string | null;
isNative?: boolean;
Expand Down

0 comments on commit c28cc07

Please sign in to comment.