Skip to content

Commit

Permalink
remove types
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Nov 4, 2024
1 parent 811f770 commit 36c8cfb
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 191 deletions.
99 changes: 1 addition & 98 deletions api/paidAction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,7 @@ export const paidActions = {
DONATE,
TRANSFER
}
/**
* @typedef {Object} ActionData
* @property {number} actionId
* @property {string} actionType
* @property {boolean} optimistic - Indicates if the action is optimistic.
* @property {*} args
*/

/**
* @typedef {import('@prisma/client').Invoice} InvoiceEntry
* @typedef {import('@prisma/client').InvoiceForward} InvoiceForward
* @typedef {import('@prisma/client').User} User
* @typedef {import('@prisma/client').PrismaClient} PrismaClient
* @typedef {Omit<PrismaClient, "$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends">} PrismaTransaction
* @typedef {import('@/wallets/server').InvoiceData} InvoiceData
*
* @typedef {Object} PaidActionContext
* @property {PrismaClient} [models]
* @property {PrismaClient | PrismaTransaction} [tx]
* @property {User} me
* @property {bigint} cost
* @property {string} description
* @property {boolean} [optimistic]
* @property {boolean} [forceFeeCredits]
* @property {boolean} [fallbackToSN]
* @property {boolean} [disableFeeCredit]
* @property {Object} lnd
* @property {*} actionId
* @property {InvoiceEntry} [retryForInvoice] - the id of the invoice to retry
*/

/**
* Check if the user is allowed to perform a paid action
* @param {number} userId
* @param {PaidActionContext} context
* @param {bigint} [minSats]
*/

async function checkUser (userId, { models, tx, me }, minSats) {
// count pending invoices and bail if we're over the limit
const pendingInvoices = await (tx ?? models).invoice.count({
Expand All @@ -91,13 +55,6 @@ async function checkUser (userId, { models, tx, me }, minSats) {
}
}

/**
*
* @param {string} actionType
* @param {Object} args
* @param {Object} context
* @returns
*/
export default async function performPaidAction (actionType, args, context) {
try {
// TODO : this part is just to keep the current SN behavior
Expand Down Expand Up @@ -167,14 +124,6 @@ export default async function performPaidAction (actionType, args, context) {
}
}

/**
*
* @param {InvoiceEntry} invoiceEntry
* @param {Object} paidAction
* @param {*} args
* @param {PaidActionContext} context
* @returns
*/
async function performAction (invoiceEntry, paidAction, args, context) {
const { retryForInvoice } = context
if (retryForInvoice && paidAction.retry) {
Expand All @@ -184,15 +133,6 @@ async function performAction (invoiceEntry, paidAction, args, context) {
}
}

/**
*
* @param {InvoiceData} invoiceData
* @param {string} actionType
* @param {Object} paidAction
* @param {*} args
* @param {PaidActionContext} context
* @returns
*/
async function performFeeCreditAction (invoiceData, actionType, paidAction, args, context) {
const { me, models, retryForInvoice } = context
const cost = invoiceData.msats
Expand Down Expand Up @@ -258,13 +198,6 @@ async function performFeeCreditAction (invoiceData, actionType, paidAction, args
return result
}

/**
* @param {InvoiceData} invoiceData*
* @param {string} actionType
* @param {Object} paidAction
* @param {Object} args
* @param {PaidActionContext} context
*/
async function performOptimisticAction (invoiceData, actionType, paidAction, args, context) {
const { models, actionId, me } = context
const performInvoicedAction = async tx => {
Expand All @@ -288,14 +221,6 @@ async function performOptimisticAction (invoiceData, actionType, paidAction, arg
return await models.$transaction(performInvoicedAction, { isolationLevel: Prisma.TransactionIsolationLevel.ReadCommitted })
}

/**
* @param {InvoiceData} invoiceData
*
* @param {string} actionType
* @param {Object} paidAction
* @param {Object} args
* @param {PaidActionContext} context
*/
async function performPessimisticAction (invoiceData, actionType, paidAction, args, context) {
const { models, actionId, me } = context
const performInvoicedAction = async tx => {
Expand All @@ -318,15 +243,6 @@ async function performPessimisticAction (invoiceData, actionType, paidAction, ar
return await models.$transaction(performInvoicedAction, { isolationLevel: Prisma.TransactionIsolationLevel.ReadCommitted })
}

/**
*
* @param {string} actionType
* @param {Object} args
* @param {InvoiceEntry} args.invoice
* @param {boolean} args.forceFeeCredits
* @param {PaidActionContext} context
* @returns
*/
export async function retryPaidAction (actionType, { invoice, forceFeeCredits }, context) {
const { models } = context
const failedInvoice = invoice
Expand Down Expand Up @@ -369,16 +285,6 @@ export async function retryPaidAction (actionType, { invoice, forceFeeCredits },
}, { isolationLevel: Prisma.TransactionIsolationLevel.ReadCommitted })
}

/**
* Track an invoice, optionally associate it to some action data
* @param {InvoiceData} invoiceData
* @param {ActionData} actionData
* @param {Object} context
* @param {Object} [context.me]
* @param {import('@/api/paidAction').PrismaClient} [context.models]
* @param {import('@/api/paidAction').PrismaTransaction} [context.tx]
* @returns
*/
export async function createDbInvoice (invoiceData, actionData, { me, models, tx }) {
const { invoice: servedBolt11, wallet, maxFee, preimage, innerInvoice: bolt11, isWrapped, isHodl, isFeeCredit } = invoiceData
const { actionId, actionType, optimistic: isOptimisticAction, args } = actionData
Expand Down Expand Up @@ -441,10 +347,8 @@ export async function createDbInvoice (invoiceData, actionData, { me, models, tx
}
}
})
// @ts-ignore
invoiceEntry = invoiceForward.invoice
} else {
// @ts-ignore
invoiceEntry = await db.invoice.create({ data: invoiceEntryData })
}

Expand All @@ -459,7 +363,6 @@ export async function createDbInvoice (invoiceData, actionData, { me, models, tx
// the HMAC is only returned during invoice creation
// this makes sure that only the person who created this invoice
// has access to the HMAC
// @ts-ignore
invoiceEntry.hmac = createHmac(invoiceEntry.hash)

return invoiceEntry
Expand Down
93 changes: 0 additions & 93 deletions wallets/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,6 @@ export default walletDefs

export const SN_WALLET = null

/**
* @typedef {Object} InvoiceData
* @property {string} invoice - The bolt11 invoice
* @property {string} [preimage] - The preimage (secret) of the invoice.
* @property {Object} wallet - The wallet that created the invoice (SN_WALLET (null) if from Stacker News central wallet).
* @property {boolean} [isWrapped] - Indicates if the invoice is wrapped.
* @property {boolean} [isHodl] - Indicates if the invoice is a hodl invoice.
* @property {string} [innerInvoice] - The bolt11 invoice that was wrapped if isWrapped is true otherwise undefined
* @property {bigint} [maxFee] - The maximum fee for the wrapped invoice.
* @property {boolean} [isFeeCredit] - Indicates if it is a fee credit invoice
* @property {bigint} [msats] - The cost of the fee credit invoice
* @property {string} [description] - The description of the invoice.
*/

/**
* @typedef {Object} ActionData
* @property {number} actionId
* @property {string} actionType
* @property {boolean} optimistic - Indicates if the action is optimistic.
* @property {*} args
*/

/**
* Create a fee credit invoice.
* Used only for internal transactions.
* @param {number | undefined} userId - the id of the user for whom the invoice is being created (or SN_WALLET if for stacker news).
* @param {Object} args
* @param {bigint} args.msats - The amount in millisatoshis.
* @param {string} args.description - The description of the invoice.
* @returns {Promise<InvoiceData>} A promise that resolves to an object containing the invoice details.
*/
export async function createFeeCreditInvoice (userId, { msats, description }) {
msats = toPositiveBigInt(msats)
// TODO generate better random ID
Expand All @@ -70,28 +39,6 @@ export async function createFeeCreditInvoice (userId, { msats, description }) {
}
}

/**
* Create an hodl invoice for the given user.
* Same as calling createInvoice(..., { hodl: true, ... })
* Unless specified otherwise:
* - If p2p is possible, a wrapped invoice will be returned.
* - If p2p is not possible, an invoice for the custodial SN wallet will be returned.
*
* @param {number | undefined} userId - the ID of the user for whom the invoice is being created (or SN_WALLET if the invoice is paid to stacker news).
* @param {Object} args
* @param {bigint} args.msats - The amount in millisatoshis.
* @param {string} args.description - The description of the invoice.
* @param {string} [args.descriptionHash] - The hash of the description.
* @param {number} [args.expiry=INVOICE_EXPIRE_SECS] - The expiry time of the invoice in seconds.
* @param {bigint} [args.sybilFeePercent] - The sybil fee to use when needed (default to ZAP_SYBIL_FEE_MULT if undefined).
* @param {boolean} [args.fallbackToSN=true] - fallback to use stacker news central wallet if no other user wallet is available.
* @param {boolean} [args.direct=false] - if true, it will return an unwrapped invoice from attached wallets (unless hodl=true).
* @param {Object} context
* @param {Object} context.models
* @param {Object} context.lnd
* @param {Object} [context.useWallet] - Specify which wallet to use, if undefined the best wallet will be used.
* @returns {Promise<InvoiceData>} A promise that resolves to an object containing the invoice details.
*/
export async function createHodlInvoice (
userId,
{
Expand All @@ -117,29 +64,6 @@ export async function createHodlInvoice (
}, { models, lnd, useWallet })
}

/**
* Create an invoice for the given user.
* Unless specified otherwise:
* - If p2p is possible, a wrapped invoice will be returned.
* - If p2p is not possible, an invoice for the custodial SN wallet will be returned.
*
* @param {number} receiverUserId - the ID of the user for whom the invoice is being created (or SN_WALLET if the invoice is paid to stacker news).
* @param {Object} args
* @param {bigint} args.msats - The amount in millisatoshis.
* @param {string} args.description - The description of the invoice.
* @param {string} [args.descriptionHash] - The hash of the description.
* @param {number} [args.expiry=INVOICE_EXPIRE_SECS] - The expiry time of the invoice in seconds.
* @param {bigint} [args.sybilFeePercent] - The sybil fee to use when needed (default to ZAP_SYBIL_FEE_MULT if undefined).
* @param {boolean} [args.fallbackToSN=true] - fallback to use stacker news central wallet if no other user wallet is available.
* @param {boolean} [args.hodl=false] - if true, the returned invoice will be always an hodl invoice.
* @param {boolean} [args.direct=false] - if true, it will return an unwrapped invoice from attached wallets (unless hodl=true).
* @param {Object} context
* @param {Object} context.models
* @param {Object} context.lnd
* @param {Object} [context.useWallet] - Specify which wallet to use, if undefined the best wallet will be used.
* @returns {Promise<InvoiceData>} A promise that resolves to an object containing the invoice details.
* @private
*/
export async function createInvoice (
receiverUserId,
{
Expand Down Expand Up @@ -309,13 +233,6 @@ export const addWalletLog = async ({ wallet, level, message }, { models }) => {
}
}

/**
* Check if an invoice is good
* @param {*} models
* @param {*} wallet
* @param {string} invoice
* @param {bigint} msats
*/
async function checkInvoice (models, wallet, invoice, msats) {
const bolt11 = await parsePaymentRequest({ request: invoice })
if (BigInt(bolt11.mtokens) !== msats) {
Expand Down Expand Up @@ -366,16 +283,6 @@ async function checkWallet (models, walletFull, walletField, walletType) {
}
}

/**
*
* @param {string} description
* @param {string | undefined} descriptionHash
* @param {bigint} cost
* @param {number} expiry
* @param {boolean} isHodl
* @param {*} param4
* @returns
*/
async function createSNInvoice (description, descriptionHash, cost, expiry, isHodl, { lnd }) {
// sanity check
cost = toPositiveBigInt(cost)
Expand Down

0 comments on commit 36c8cfb

Please sign in to comment.