Use wallet.createInvoice for autowithdrawals

This commit is contained in:
ekzyis 2024-07-06 07:16:12 +02:00
parent b569c8faa0
commit 12bedae01a
2 changed files with 96 additions and 94 deletions

View File

@ -86,11 +86,10 @@ export const server = {
throw new Error(details) throw new Error(details)
} }
}, },
// TODO: use this instead of `autowithdrawLND` in worker/autowithdraw.js
createInvoice: async ( createInvoice: async (
amount, amount,
{ cert, macaroon, socket }, { cert, macaroon, socket },
{ me, models, addWalletLog, lnService: { authenticatedLndGrpc, createInvoice } } { me, lnService: { authenticatedLndGrpc, createInvoice } }
) => { ) => {
const { lnd } = await authenticatedLndGrpc({ const { lnd } = await authenticatedLndGrpc({
cert, cert,

View File

@ -1,9 +1,8 @@
import { authenticatedLndGrpc, createInvoice } from 'ln-service' import { authenticatedLndGrpc, createInvoice as lndCreateInvoice } from 'ln-service'
import { msatsToSats, satsToMsats } from '@/lib/format' import { msatsToSats, satsToMsats } from '@/lib/format'
import { datePivot } from '@/lib/time' // import { datePivot } from '@/lib/time'
import { createWithdrawal, sendToLnAddr, addWalletLog } from '@/api/resolvers/wallet' import { createWithdrawal, /* sendToLnAddr, */ addWalletLog, SERVER_WALLET_DEFS } from '@/api/resolvers/wallet'
import { createInvoice as createInvoiceCLN } from '@/lib/cln' // import { createInvoice as createInvoiceCLN } from '@/lib/cln'
import { Wallet } from '@/lib/constants'
export async function autoWithdraw ({ data: { id }, models, lnd }) { export async function autoWithdraw ({ data: { id }, models, lnd }) {
const user = await models.user.findUnique({ where: { id } }) const user = await models.user.findUnique({ where: { id } })
@ -51,20 +50,33 @@ export async function autoWithdraw ({ data: { id }, models, lnd }) {
for (const wallet of wallets) { for (const wallet of wallets) {
try { try {
if (wallet.type === Wallet.LND.type) { for (const w of SERVER_WALLET_DEFS) {
await autowithdrawLND( const { server: { walletType, walletField, createInvoice } } = w.default || w
{ amount, maxFee }, if (wallet.type === walletType) {
{ models, me: user, lnd }) await autowithdraw(
} else if (wallet.type === Wallet.CLN.type) { { walletType, walletField, createInvoice },
await autowithdrawCLN( { amount, maxFee },
{ amount, maxFee }, { me: user, models, lnd }
{ models, me: user, lnd }) )
} else if (wallet.type === Wallet.LnAddr.type) { }
await autowithdrawLNAddr(
{ amount, maxFee },
{ models, me: user, lnd })
} }
// TODO: implement CLN and LnAddr wallets
// ------
// if (wallet.type === Wallet.LND.type) {
// await autowithdrawLND(
// { amount, maxFee },
// { models, me: user, lnd })
// } else if (wallet.type === Wallet.CLN.type) {
// await autowithdrawCLN(
// { amount, maxFee },
// { models, me: user, lnd })
// } else if (wallet.type === Wallet.LnAddr.type) {
// await autowithdrawLNAddr(
// { amount, maxFee },
// { models, me: user, lnd })
// }
return return
} catch (error) { } catch (error) {
console.error(error) console.error(error)
@ -81,9 +93,10 @@ export async function autoWithdraw ({ data: { id }, models, lnd }) {
// none of the wallets worked // none of the wallets worked
} }
async function autowithdrawLNAddr ( async function autowithdraw (
{ walletType, walletField, createInvoice: walletCreateInvoice },
{ amount, maxFee }, { amount, maxFee },
{ me, models, lnd, headers, autoWithdraw = false }) { { me, models, lnd }) {
if (!me) { if (!me) {
throw new Error('me not specified') throw new Error('me not specified')
} }
@ -91,86 +104,76 @@ async function autowithdrawLNAddr (
const wallet = await models.wallet.findFirst({ const wallet = await models.wallet.findFirst({
where: { where: {
userId: me.id, userId: me.id,
type: Wallet.LnAddr.type type: walletType
}, },
include: { include: {
walletLightningAddress: true [walletField]: true
} }
}) })
if (!wallet || !wallet.walletLightningAddress) { if (!wallet || !wallet[walletField]) {
throw new Error('no lightning address wallet found') throw new Error(`no ${walletType} wallet found`)
} }
const { walletLightningAddress: { address } } = wallet const bolt11 = await walletCreateInvoice(amount, wallet[walletField], { me, lnService: { authenticatedLndGrpc, createInvoice: lndCreateInvoice } })
return await sendToLnAddr(null, { addr: address, amount, maxFee }, { me, models, lnd, walletId: wallet.id })
return await createWithdrawal(null, { invoice: bolt11, maxFee }, { me, models, lnd, walletId: wallet.id })
} }
async function autowithdrawLND ({ amount, maxFee }, { me, models, lnd }) { // async function autowithdrawLNAddr (
if (!me) { // { amount, maxFee },
throw new Error('me not specified') // { me, models, lnd, headers, autoWithdraw = false }) {
} // if (!me) {
// throw new Error('me not specified')
// }
//
// const wallet = await models.wallet.findFirst({
// where: {
// userId: me.id,
// type: Wallet.LnAddr.type
// },
// include: {
// walletLightningAddress: true
// }
// })
//
// if (!wallet || !wallet.walletLightningAddress) {
// throw new Error('no lightning address wallet found')
// }
//
// const { walletLightningAddress: { address } } = wallet
// return await sendToLnAddr(null, { addr: address, amount, maxFee }, { me, models, lnd, walletId: wallet.id })
// }
const wallet = await models.wallet.findFirst({ // async function autowithdrawCLN ({ amount, maxFee }, { me, models, lnd }) {
where: { // if (!me) {
userId: me.id, // throw new Error('me not specified')
type: Wallet.LND.type // }
}, //
include: { // const wallet = await models.wallet.findFirst({
walletLND: true // where: {
} // userId: me.id,
}) // type: Wallet.CLN.type
// },
if (!wallet || !wallet.walletLND) { // include: {
throw new Error('no lnd wallet found') // walletCLN: true
} // }
// })
const { walletLND: { cert, macaroon, socket } } = wallet //
const { lnd: lndOut } = await authenticatedLndGrpc({ // if (!wallet || !wallet.walletCLN) {
cert, // throw new Error('no cln wallet found')
macaroon, // }
socket //
}) // const { walletCLN: { cert, rune, socket } } = wallet
//
const invoice = await createInvoice({ // const inv = await createInvoiceCLN({
description: me.hideInvoiceDesc ? undefined : 'autowithdraw to LND from SN', // socket,
lnd: lndOut, // rune,
tokens: amount, // cert,
expires_at: datePivot(new Date(), { seconds: 360 }) // description: me.hideInvoiceDesc ? undefined : 'autowithdraw to CLN from SN',
}) // msats: amount + 'sat',
// expiry: 360
return await createWithdrawal(null, { invoice: invoice.request, maxFee }, { me, models, lnd, walletId: wallet.id }) // })
} //
// return await createWithdrawal(null, { invoice: inv.bolt11, maxFee }, { me, models, lnd, walletId: wallet.id })
async function autowithdrawCLN ({ amount, maxFee }, { me, models, lnd }) { // }
if (!me) {
throw new Error('me not specified')
}
const wallet = await models.wallet.findFirst({
where: {
userId: me.id,
type: Wallet.CLN.type
},
include: {
walletCLN: true
}
})
if (!wallet || !wallet.walletCLN) {
throw new Error('no cln wallet found')
}
const { walletCLN: { cert, rune, socket } } = wallet
const inv = await createInvoiceCLN({
socket,
rune,
cert,
description: me.hideInvoiceDesc ? undefined : 'autowithdraw to CLN from SN',
msats: amount + 'sat',
expiry: 360
})
return await createWithdrawal(null, { invoice: inv.bolt11, maxFee }, { me, models, lnd, walletId: wallet.id })
}