2024-08-17 00:33:17 +00:00
|
|
|
import { msatsSatsFloor, msatsToSats, satsToMsats } from '@/lib/format'
|
2024-08-13 14:48:30 +00:00
|
|
|
import { createWithdrawal } from '@/api/resolvers/wallet'
|
|
|
|
import { createInvoice } from 'wallets/server'
|
2024-02-13 19:17:56 +00:00
|
|
|
|
|
|
|
export async function autoWithdraw ({ data: { id }, models, lnd }) {
|
|
|
|
const user = await models.user.findUnique({ where: { id } })
|
2024-10-18 16:15:53 +00:00
|
|
|
if (
|
|
|
|
user.autoWithdrawThreshold === null ||
|
|
|
|
user.autoWithdrawMaxFeePercent === null ||
|
2024-10-19 14:21:50 +00:00
|
|
|
user.autoWithdrawMaxFeeTotal === null) return
|
2024-02-13 19:17:56 +00:00
|
|
|
|
|
|
|
const threshold = satsToMsats(user.autoWithdrawThreshold)
|
|
|
|
const excess = Number(user.msats - threshold)
|
|
|
|
|
|
|
|
// excess must be greater than 10% of threshold
|
|
|
|
if (excess < Number(threshold) * 0.1) return
|
|
|
|
|
2024-08-17 00:33:17 +00:00
|
|
|
// floor fee to nearest sat but still denominated in msats
|
2024-10-18 16:15:53 +00:00
|
|
|
const maxFeeMsats = msatsSatsFloor(Math.max(
|
|
|
|
Math.ceil(excess * (user.autoWithdrawMaxFeePercent / 100.0)),
|
2024-10-19 14:21:50 +00:00
|
|
|
Number(satsToMsats(user.autoWithdrawMaxFeeTotal))
|
2024-10-18 16:15:53 +00:00
|
|
|
))
|
2024-08-17 00:33:17 +00:00
|
|
|
// msats will be floored by createInvoice if it needs to be
|
|
|
|
const msats = BigInt(excess) - maxFeeMsats
|
2024-02-13 19:17:56 +00:00
|
|
|
|
|
|
|
// must be >= 1 sat
|
2024-08-17 00:33:17 +00:00
|
|
|
if (msats < 1000n) return
|
2024-02-13 19:17:56 +00:00
|
|
|
|
|
|
|
// check that
|
|
|
|
// 1. the user doesn't have an autowithdraw pending
|
|
|
|
// 2. we have not already attempted to autowithdraw this fee recently
|
|
|
|
const [pendingOrFailed] = await models.$queryRaw`
|
|
|
|
SELECT EXISTS(
|
|
|
|
SELECT *
|
|
|
|
FROM "Withdrawl"
|
2024-08-18 19:06:17 +00:00
|
|
|
WHERE "userId" = ${id}
|
|
|
|
AND "autoWithdraw"
|
|
|
|
AND status IS DISTINCT FROM 'CONFIRMED'
|
|
|
|
AND now() < created_at + interval '1 hour'
|
|
|
|
AND "msatsFeePaying" >= ${maxFeeMsats}
|
2024-02-13 19:17:56 +00:00
|
|
|
)`
|
|
|
|
|
|
|
|
if (pendingOrFailed.exists) return
|
|
|
|
|
2024-08-13 14:48:30 +00:00
|
|
|
const { invoice, wallet } = await createInvoice(id, { msats, description: 'SN: autowithdrawal', expiry: 360 }, { models })
|
|
|
|
return await createWithdrawal(null,
|
2024-08-17 00:33:17 +00:00
|
|
|
{ invoice, maxFee: msatsToSats(maxFeeMsats) },
|
2024-08-13 14:48:30 +00:00
|
|
|
{ me: { id }, models, lnd, walletId: wallet.id })
|
2024-04-14 22:34:21 +00:00
|
|
|
}
|