Добавить учёт публикации блоков и обновить загрузку UI

This commit is contained in:
AidarKC
2026-12-01 20:39:18 +03:00
parent 3fa1e681d9
commit a320c12bdd
30 changed files with 714 additions and 60 deletions
@@ -188,6 +188,39 @@ export async function uploadFileWithTurbo({
};
}
/** Загружает готовый подписанный DataItem, сохраняя исходные bytes и ID. */
export async function uploadSignedBlockDataItem({ login, storagePwd, bytes, expectedId }) {
if (!(bytes instanceof Uint8Array) || bytes.length === 0) throw new Error('Empty signed DataItem');
const alreadyUploaded = async () => {
try {
const response = await fetch(`https://turbo.ardrive.io/tx/${encodeURIComponent(expectedId)}/status`);
if (!response.ok) return false;
const status = String((await response.json())?.status || '').toUpperCase();
return ['PENDING', 'CONFIRMED', 'BUNDLED', 'FINALIZED'].includes(status);
} catch { return false; }
};
if (await alreadyUploaded()) return { id: expectedId, alreadyUploaded: true };
const { turbo } = await getTurboContextForStoredSolanaKey({ login, storagePwd });
let result;
try {
result = await turbo.uploadSignedDataItem({
dataItemStreamFactory: () => new ReadableStream({
start(controller) { controller.enqueue(bytes); controller.close(); },
}),
dataItemSizeFactory: () => bytes.length,
});
} catch (error) {
// Потерянный ответ Turbo или повтор того же DataItem не создаёт новый блок.
if (await alreadyUploaded()) return { id: expectedId, alreadyUploaded: true };
if (/data item exists|already exists/i.test(String(error?.message || error))) {
return { id: expectedId, alreadyUploaded: true };
}
throw error;
}
if (String(result?.id || '') !== expectedId) throw new Error('Turbo returned another DataItem ID');
return result;
}
export async function topUpTurboWithSolana({
login,
storagePwd,