Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@solana-program/compute-budget": "^0.9.0",
"@solana-program/system": "^0.8.0",
"commander": "^13.0.0",
"p-limit": "^7.1.1",
"pako": "^2.1.0",
"picocolors": "^1.1.1",
"yaml": "^2.7.0"
Expand Down
17 changes: 0 additions & 17 deletions clients/js/pnpm-lock.yaml

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

72 changes: 52 additions & 20 deletions clients/js/src/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
TransactionPlanner,
TransactionSigner,
} from '@solana/kit';
import { limitFunction } from 'p-limit';
import { findMetadataPda, SeedArgs } from './generated';
import { getProgramAuthority } from './utils';

Expand Down Expand Up @@ -88,25 +87,22 @@ export function createDefaultTransactionPlannerAndExecutor(input: {
});

const executor = createTransactionPlanExecutor({
executeTransactionMessage: limitFunction(
async (message, config) => {
const { value: latestBlockhash } = await input.rpc
.getLatestBlockhash()
.send();
const transaction = await pipe(
setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, message),
async (m) => await estimateAndSetCULimit(m, config),
async (m) => await signTransactionMessageWithSigners(await m, config)
);
assertIsSendableTransaction(transaction);
await sendAndConfirmTransaction(transaction, {
...config,
commitment: 'confirmed',
});
return { transaction };
},
{ concurrency: input.concurrency ?? 5 }
),
executeTransactionMessage: limitFunction(async (message, config) => {
const { value: latestBlockhash } = await input.rpc
.getLatestBlockhash()
.send();
const transaction = await pipe(
setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, message),
async (m) => await estimateAndSetCULimit(m, config),
async (m) => await signTransactionMessageWithSigners(await m, config)
);
assertIsSendableTransaction(transaction);
await sendAndConfirmTransaction(transaction, {
...config,
commitment: 'confirmed',
});
return { transaction };
}, input.concurrency ?? 5),
});

return { planner, executor };
Expand All @@ -123,3 +119,39 @@ export async function isValidInstructionPlan(
return false;
}
}

function limitFunction<TArguments extends unknown[], TReturnType>(
fn: (...args: TArguments) => PromiseLike<TReturnType>,
concurrency: number
): (...args: TArguments) => Promise<TReturnType> {
let running = 0;
const queue: Array<{
args: TArguments;
resolve: (value: TReturnType) => void;
reject: (reason?: unknown) => void;
}> = [];

function process() {
// Do nothing if we're still running at max concurrency
// or if there's nothing left to process.
if (running >= concurrency || queue.length === 0) return;

running++;
const { args, resolve, reject } = queue.shift()!;

Promise.resolve(fn(...args))
.then(resolve)
.catch(reject)
.finally(() => {
running--;
process();
});
}

return function (...args) {
return new Promise((resolve, reject) => {
queue.push({ args, resolve, reject });
process();
});
};
}