Skip to content

Commit

Permalink
Autowithdrawal logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Apr 3, 2024
1 parent 5f392e3 commit 7a97702
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/resolvers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ export default {
}
}

const addWalletLog = async ({ wallet, level, message }, { me, models }) => {
export const addWalletLog = async ({ wallet, level, message }, { me, models }) => {
try {
await models.walletLog.create({ data: { userId: me.id, wallet, level, message } })
} catch (err) {
Expand Down
20 changes: 18 additions & 2 deletions worker/autowithdraw.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { authenticatedLndGrpc, createInvoice } from 'ln-service'
import { msatsToSats, satsToMsats } from '@/lib/format'
import { msatsToSats, numWithUnits, satsToMsats } from '@/lib/format'
import { datePivot } from '@/lib/time'
import { createWithdrawal, sendToLnAddr } from '@/api/resolvers/wallet'
import { createWithdrawal, sendToLnAddr, addWalletLog } from '@/api/resolvers/wallet'

export async function autoWithdraw ({ data: { id }, models, lnd }) {
const user = await models.user.findUnique({ where: { id } })
Expand Down Expand Up @@ -45,19 +45,35 @@ export async function autoWithdraw ({ data: { id }, models, lnd }) {

for (const wallet of wallets) {
try {
const message = `autowithdrawal of ${numWithUnits(amount, { abbreviate: false, unitSingular: 'sat', unitPlural: 'sats' })}`
if (wallet.type === 'LND') {
await autowithdrawLND(
{ amount, maxFee },
{ models, me: user, lnd })
await addWalletLog({
wallet: 'walletLND',
level: 'SUCCESS',
message
}, { me: user, models })
} else if (wallet.type === 'LIGHTNING_ADDRESS') {
await autowithdrawLNAddr(
{ amount, maxFee },
{ models, me: user, lnd })
await addWalletLog({
wallet: 'walletLightningAddress',
level: 'SUCCESS',
message
}, { me: user, models })
}

return
} catch (error) {
console.error(error)
await addWalletLog({
wallet: wallet.type === 'LND' ? 'walletLND' : 'walletLightningAddress',
level: 'ERROR',
message: 'autowithdrawal failed: ' + (error.message || error.toString?.())
})
}
}

Expand Down

0 comments on commit 7a97702

Please sign in to comment.