Skip to content

Commit 067d906

Browse files
authored
improvements to upload fees code (#2382)
* Use $queryRaw instead of $queryRawUnsafe * Replace comment with destructuring * Return all upload fees as BigInt
1 parent 1bcc864 commit 067d906

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

api/paidAction/itemUpdate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function getCost ({ id, boost = 0, uploadIds, bio }, { me, models }
1717
// or more boost
1818
const old = await models.item.findUnique({ where: { id: parseInt(id) } })
1919
const { totalFeesMsats } = await uploadFees(uploadIds, { models, me })
20-
const cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost)
20+
const cost = totalFeesMsats + satsToMsats(boost - old.boost)
2121

2222
if (cost > 0 && old.invoiceActionState && old.invoiceActionState !== 'PAID') {
2323
throw new Error('creation invoice not paid')

api/paidAction/territoryUpdate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function getCost ({ oldName, billingType, uploadIds }, { models, me
2121

2222
const { totalFees } = await uploadFees(uploadIds, { models, me })
2323

24-
const cost = proratedBillingCost(oldSub, billingType) + totalFees
24+
const cost = BigInt(proratedBillingCost(oldSub, billingType)) + totalFees
2525
return satsToMsats(cost)
2626
}
2727

api/resolvers/upload.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@ export function uploadIdsFromText (text) {
6060
}
6161

6262
export async function uploadFees (s3Keys, { models, me }) {
63-
// returns info object in this format:
64-
// { bytes24h: int, bytesUnpaid: int, nUnpaid: int, uploadFeesMsats: BigInt }
65-
const [info] = await models.$queryRawUnsafe('SELECT * FROM upload_fees($1::INTEGER, $2::INTEGER[])', me ? me.id : USER_ID.anon, s3Keys)
66-
const uploadFees = msatsToSats(info.uploadFeesMsats)
67-
const totalFeesMsats = info.nUnpaid * Number(info.uploadFeesMsats)
63+
const [{
64+
bytes24h,
65+
bytesUnpaid,
66+
nUnpaid,
67+
uploadFeesMsats
68+
}] = await models.$queryRaw`SELECT * FROM upload_fees(${me?.id ?? USER_ID.anon}::INTEGER, ${s3Keys}::INTEGER[])`
69+
const uploadFees = msatsToSats(uploadFeesMsats)
70+
const totalFeesMsats = BigInt(nUnpaid) * uploadFeesMsats
6871
const totalFees = msatsToSats(totalFeesMsats)
69-
return { ...info, uploadFees, totalFees, totalFeesMsats }
72+
return { bytes24h, bytesUnpaid, nUnpaid, uploadFees, totalFees, totalFeesMsats }
7073
}
7174

7275
export async function throwOnExpiredUploads (uploadIds, { tx }) {

0 commit comments

Comments
 (0)