SHA256
42 lines
2.3 KiB
JavaScript
42 lines
2.3 KiB
JavaScript
/** Solana JSON-RPC transport. Network selection belongs to the connected SHiNE server. */
|
|
export const DEVNET_FAUCET_RPC = 'https://api.devnet.solana.com';
|
|
let cluster = '';
|
|
export function setSolanaCluster(value) {
|
|
const next = String(value || '').toLowerCase();
|
|
if (next !== 'devnet' && next !== 'mainnet-beta' && next !== 'testnet') throw new Error(`Unsupported server Solana cluster: ${value}`);
|
|
cluster = next;
|
|
}
|
|
export function getSolanaCluster() { return cluster; }
|
|
export function rpcEndpointFromShineHttp(httpBase) {
|
|
const base = String(httpBase || '').trim();
|
|
if (!/^https?:\/\//i.test(base)) throw new Error('SHiNE server HTTP address is unavailable');
|
|
return `${base.replace(/\/+$/, '')}/solana-rpc`;
|
|
}
|
|
export async function confirmSolanaTransactionHttp(connection, signature, {
|
|
lastValidBlockHeight = null, commitment = 'confirmed', timeoutMs = 90_000,
|
|
} = {}) {
|
|
const started = Date.now();
|
|
while (Date.now() - started < timeoutMs) {
|
|
const response = await connection.getSignatureStatuses([signature], { searchTransactionHistory: true });
|
|
const status = response?.value?.[0];
|
|
if (status?.err) throw new Error(`Solana transaction failed: ${JSON.stringify(status.err)}`);
|
|
if (status && (status.confirmationStatus === 'finalized' ||
|
|
(commitment !== 'finalized' && status.confirmationStatus === 'confirmed'))) return signature;
|
|
if (lastValidBlockHeight != null) {
|
|
const height = await connection.getBlockHeight('confirmed');
|
|
if (height > lastValidBlockHeight) throw new Error(`Solana transaction expired: ${signature}`);
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 1800));
|
|
}
|
|
throw new Error(`Solana transaction confirmation timed out: ${signature}`);
|
|
}
|
|
export async function sendAndConfirmSolanaHttp(connection, transaction, signers) {
|
|
const latest = await connection.getLatestBlockhash('confirmed');
|
|
transaction.recentBlockhash = latest.blockhash;
|
|
if (!transaction.feePayer && signers?.[0]) transaction.feePayer = signers[0].publicKey;
|
|
transaction.sign(...signers);
|
|
const signature = await connection.sendRawTransaction(transaction.serialize(), { skipPreflight: false });
|
|
await confirmSolanaTransactionHttp(connection, signature, { lastValidBlockHeight: latest.lastValidBlockHeight });
|
|
return signature;
|
|
}
|