Use wallet.createInvoice for autowithdrawals
This commit is contained in:
parent
b569c8faa0
commit
12bedae01a
|
@ -86,11 +86,10 @@ export const server = {
|
|||
throw new Error(details)
|
||||
}
|
||||
},
|
||||
// TODO: use this instead of `autowithdrawLND` in worker/autowithdraw.js
|
||||
createInvoice: async (
|
||||
amount,
|
||||
{ cert, macaroon, socket },
|
||||
{ me, models, addWalletLog, lnService: { authenticatedLndGrpc, createInvoice } }
|
||||
{ me, lnService: { authenticatedLndGrpc, createInvoice } }
|
||||
) => {
|
||||
const { lnd } = await authenticatedLndGrpc({
|
||||
cert,
|
||||
|
|
|
@ -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 { datePivot } from '@/lib/time'
|
||||
import { createWithdrawal, sendToLnAddr, addWalletLog } from '@/api/resolvers/wallet'
|
||||
import { createInvoice as createInvoiceCLN } from '@/lib/cln'
|
||||
import { Wallet } from '@/lib/constants'
|
||||
// import { datePivot } from '@/lib/time'
|
||||
import { createWithdrawal, /* sendToLnAddr, */ addWalletLog, SERVER_WALLET_DEFS } from '@/api/resolvers/wallet'
|
||||
// import { createInvoice as createInvoiceCLN } from '@/lib/cln'
|
||||
|
||||
export async function autoWithdraw ({ data: { id }, models, lnd }) {
|
||||
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) {
|
||||
try {
|
||||
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 })
|
||||
for (const w of SERVER_WALLET_DEFS) {
|
||||
const { server: { walletType, walletField, createInvoice } } = w.default || w
|
||||
if (wallet.type === walletType) {
|
||||
await autowithdraw(
|
||||
{ walletType, walletField, createInvoice },
|
||||
{ amount, maxFee },
|
||||
{ me: user, models, 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
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
|
@ -81,9 +93,10 @@ export async function autoWithdraw ({ data: { id }, models, lnd }) {
|
|||
// none of the wallets worked
|
||||
}
|
||||
|
||||
async function autowithdrawLNAddr (
|
||||
async function autowithdraw (
|
||||
{ walletType, walletField, createInvoice: walletCreateInvoice },
|
||||
{ amount, maxFee },
|
||||
{ me, models, lnd, headers, autoWithdraw = false }) {
|
||||
{ me, models, lnd }) {
|
||||
if (!me) {
|
||||
throw new Error('me not specified')
|
||||
}
|
||||
|
@ -91,86 +104,76 @@ async function autowithdrawLNAddr (
|
|||
const wallet = await models.wallet.findFirst({
|
||||
where: {
|
||||
userId: me.id,
|
||||
type: Wallet.LnAddr.type
|
||||
type: walletType
|
||||
},
|
||||
include: {
|
||||
walletLightningAddress: true
|
||||
[walletField]: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!wallet || !wallet.walletLightningAddress) {
|
||||
throw new Error('no lightning address wallet found')
|
||||
if (!wallet || !wallet[walletField]) {
|
||||
throw new Error(`no ${walletType} wallet found`)
|
||||
}
|
||||
|
||||
const { walletLightningAddress: { address } } = wallet
|
||||
return await sendToLnAddr(null, { addr: address, amount, maxFee }, { me, models, lnd, walletId: wallet.id })
|
||||
const bolt11 = await walletCreateInvoice(amount, wallet[walletField], { me, lnService: { authenticatedLndGrpc, createInvoice: lndCreateInvoice } })
|
||||
|
||||
return await createWithdrawal(null, { invoice: bolt11, maxFee }, { me, models, lnd, walletId: wallet.id })
|
||||
}
|
||||
|
||||
async function autowithdrawLND ({ amount, maxFee }, { me, models, lnd }) {
|
||||
if (!me) {
|
||||
throw new Error('me not specified')
|
||||
}
|
||||
// async function autowithdrawLNAddr (
|
||||
// { amount, maxFee },
|
||||
// { 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({
|
||||
where: {
|
||||
userId: me.id,
|
||||
type: Wallet.LND.type
|
||||
},
|
||||
include: {
|
||||
walletLND: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!wallet || !wallet.walletLND) {
|
||||
throw new Error('no lnd wallet found')
|
||||
}
|
||||
|
||||
const { walletLND: { cert, macaroon, socket } } = wallet
|
||||
const { lnd: lndOut } = await authenticatedLndGrpc({
|
||||
cert,
|
||||
macaroon,
|
||||
socket
|
||||
})
|
||||
|
||||
const invoice = await createInvoice({
|
||||
description: me.hideInvoiceDesc ? undefined : 'autowithdraw to LND from SN',
|
||||
lnd: lndOut,
|
||||
tokens: amount,
|
||||
expires_at: datePivot(new Date(), { seconds: 360 })
|
||||
})
|
||||
|
||||
return await createWithdrawal(null, { invoice: invoice.request, 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 })
|
||||
}
|
||||
// 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 })
|
||||
// }
|
||||
|
|
Loading…
Reference in New Issue