100 lines
2.8 KiB
TypeScript
100 lines
2.8 KiB
TypeScript
import { calculateSponsoringSplit } from './platformCommissions'
|
|
import { mempoolSpaceService } from './mempoolSpace'
|
|
import { sponsoringTrackingService } from './sponsoringTracking'
|
|
|
|
export async function verifyTransactionBeforeTracking(
|
|
transactionId: string,
|
|
authorMainnetAddress: string
|
|
): Promise<{ valid: boolean; confirmed: boolean; confirmations: number; error?: string } | null> {
|
|
const verification = await mempoolSpaceService.verifySponsoringTransaction(
|
|
transactionId,
|
|
authorMainnetAddress
|
|
)
|
|
|
|
if (!verification.valid) {
|
|
console.error('Cannot track invalid sponsoring payment', {
|
|
transactionId,
|
|
error: verification.error,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
return null
|
|
}
|
|
|
|
return {
|
|
valid: verification.valid,
|
|
confirmed: verification.confirmed,
|
|
confirmations: verification.confirmations,
|
|
}
|
|
}
|
|
|
|
export function buildTrackingData(
|
|
transactionId: string,
|
|
authorPubkey: string,
|
|
authorMainnetAddress: string,
|
|
split: { total: number; authorSats: number; platformSats: number },
|
|
verification: { confirmed: boolean; confirmations: number }
|
|
) {
|
|
return {
|
|
transactionId,
|
|
authorPubkey,
|
|
authorMainnetAddress,
|
|
amount: split.total,
|
|
authorAmount: split.authorSats,
|
|
platformCommission: split.platformSats,
|
|
timestamp: Math.floor(Date.now() / 1000),
|
|
confirmed: verification.confirmed,
|
|
confirmations: verification.confirmations,
|
|
}
|
|
}
|
|
|
|
export function logTrackingSuccess(
|
|
transactionId: string,
|
|
authorPubkey: string,
|
|
split: { authorSats: number; platformSats: number },
|
|
verification: { confirmed: boolean; confirmations: number }
|
|
): void {
|
|
console.log('Sponsoring payment tracked', {
|
|
transactionId,
|
|
authorPubkey,
|
|
authorAmount: split.authorSats,
|
|
platformCommission: split.platformSats,
|
|
confirmed: verification.confirmed,
|
|
confirmations: verification.confirmations,
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
}
|
|
|
|
export async function trackSponsoringPayment(
|
|
transactionId: string,
|
|
authorPubkey: string,
|
|
authorMainnetAddress: string,
|
|
authorPrivateKey: string
|
|
): Promise<void> {
|
|
try {
|
|
const split = calculateSponsoringSplit()
|
|
const verification = await verifyTransactionBeforeTracking(transactionId, authorMainnetAddress)
|
|
|
|
if (!verification) {
|
|
return
|
|
}
|
|
|
|
const trackingData = buildTrackingData(
|
|
transactionId,
|
|
authorPubkey,
|
|
authorMainnetAddress,
|
|
split,
|
|
verification
|
|
)
|
|
|
|
await sponsoringTrackingService.trackSponsoringPayment(trackingData, authorPrivateKey)
|
|
logTrackingSuccess(transactionId, authorPubkey, split, verification)
|
|
} catch (error) {
|
|
console.error('Error tracking sponsoring payment', {
|
|
transactionId,
|
|
authorPubkey,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
}
|
|
}
|