62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import { PLATFORM_BITCOIN_ADDRESS } from './platformConfig'
|
|
import type { TransactionVerificationResult } from './mempoolSpaceTypes'
|
|
import { getTransaction } from './mempoolSpaceApi'
|
|
import { verifySponsoringTransaction } from './mempoolSpaceVerification'
|
|
|
|
export function scheduleNextCheck(checkConfirmation: () => void, interval: number) {
|
|
setTimeout(() => {
|
|
void checkConfirmation()
|
|
}, interval)
|
|
}
|
|
|
|
export async function checkTransactionStatus(
|
|
txid: string,
|
|
startTime: number,
|
|
timeout: number,
|
|
interval: number,
|
|
resolve: (value: TransactionVerificationResult | null) => void,
|
|
checkConfirmation: () => void
|
|
): Promise<void> {
|
|
if (Date.now() - startTime > timeout) {
|
|
resolve(null)
|
|
return
|
|
}
|
|
|
|
const transaction = await getTransaction(txid)
|
|
if (!transaction) {
|
|
scheduleNextCheck(checkConfirmation, interval)
|
|
return
|
|
}
|
|
|
|
const authorOutput = transaction.vout.find(
|
|
(output) => output.scriptpubkey_address !== PLATFORM_BITCOIN_ADDRESS
|
|
)
|
|
|
|
if (!authorOutput) {
|
|
scheduleNextCheck(checkConfirmation, interval)
|
|
return
|
|
}
|
|
|
|
const result = await verifySponsoringTransaction(txid, authorOutput.scriptpubkey_address)
|
|
if (result.confirmed && result.valid) {
|
|
resolve(result)
|
|
} else {
|
|
scheduleNextCheck(checkConfirmation, interval)
|
|
}
|
|
}
|
|
|
|
export async function waitForConfirmation(
|
|
txid: string,
|
|
timeout: number = 600000, // 10 minutes
|
|
interval: number = 10000 // 10 seconds
|
|
): Promise<TransactionVerificationResult | null> {
|
|
const startTime = Date.now()
|
|
|
|
return new Promise((resolve) => {
|
|
const checkConfirmation = async () => {
|
|
await checkTransactionStatus(txid, startTime, timeout, interval, resolve, checkConfirmation)
|
|
}
|
|
void checkConfirmation()
|
|
})
|
|
}
|