improvements to upload fees code (#2382)
* Use $queryRaw instead of $queryRawUnsafe * Replace comment with destructuring * Return all upload fees as BigInt
This commit is contained in:
parent
1bcc864ef4
commit
067d9069cb
@ -17,7 +17,7 @@ export async function getCost ({ id, boost = 0, uploadIds, bio }, { me, models }
|
||||
// or more boost
|
||||
const old = await models.item.findUnique({ where: { id: parseInt(id) } })
|
||||
const { totalFeesMsats } = await uploadFees(uploadIds, { models, me })
|
||||
const cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost)
|
||||
const cost = totalFeesMsats + satsToMsats(boost - old.boost)
|
||||
|
||||
if (cost > 0 && old.invoiceActionState && old.invoiceActionState !== 'PAID') {
|
||||
throw new Error('creation invoice not paid')
|
||||
|
@ -21,7 +21,7 @@ export async function getCost ({ oldName, billingType, uploadIds }, { models, me
|
||||
|
||||
const { totalFees } = await uploadFees(uploadIds, { models, me })
|
||||
|
||||
const cost = proratedBillingCost(oldSub, billingType) + totalFees
|
||||
const cost = BigInt(proratedBillingCost(oldSub, billingType)) + totalFees
|
||||
return satsToMsats(cost)
|
||||
}
|
||||
|
||||
|
@ -60,13 +60,16 @@ export function uploadIdsFromText (text) {
|
||||
}
|
||||
|
||||
export async function uploadFees (s3Keys, { models, me }) {
|
||||
// returns info object in this format:
|
||||
// { bytes24h: int, bytesUnpaid: int, nUnpaid: int, uploadFeesMsats: BigInt }
|
||||
const [info] = await models.$queryRawUnsafe('SELECT * FROM upload_fees($1::INTEGER, $2::INTEGER[])', me ? me.id : USER_ID.anon, s3Keys)
|
||||
const uploadFees = msatsToSats(info.uploadFeesMsats)
|
||||
const totalFeesMsats = info.nUnpaid * Number(info.uploadFeesMsats)
|
||||
const [{
|
||||
bytes24h,
|
||||
bytesUnpaid,
|
||||
nUnpaid,
|
||||
uploadFeesMsats
|
||||
}] = await models.$queryRaw`SELECT * FROM upload_fees(${me?.id ?? USER_ID.anon}::INTEGER, ${s3Keys}::INTEGER[])`
|
||||
const uploadFees = msatsToSats(uploadFeesMsats)
|
||||
const totalFeesMsats = BigInt(nUnpaid) * uploadFeesMsats
|
||||
const totalFees = msatsToSats(totalFeesMsats)
|
||||
return { ...info, uploadFees, totalFees, totalFeesMsats }
|
||||
return { bytes24h, bytesUnpaid, nUnpaid, uploadFees, totalFees, totalFeesMsats }
|
||||
}
|
||||
|
||||
export async function throwOnExpiredUploads (uploadIds, { tx }) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user