Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions api/resolvers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ const resolvers = {

export default resolvers

export async function createWithdrawal (parent, { invoice, maxFee }, { me, models, lnd, headers, protocol, logger }) {
export async function createWithdrawal (parent, { invoice, maxFee, amount }, { me, models, lnd, headers, protocol, logger }) {
assertApiKeyNotPermitted({ me })
await validateSchema(withdrawlSchema, { invoice, maxFee })
await validateSchema(withdrawlSchema, { invoice, maxFee, amount })
await assertGofacYourself({ models, headers })

// remove 'lightning:' prefix if present
Expand Down Expand Up @@ -95,7 +95,10 @@ export async function createWithdrawal (parent, { invoice, maxFee }, { me, model
}

if (!decoded.mtokens || BigInt(decoded.mtokens) <= 0) {
throw new GqlInputError('invoice must specify an amount')
if (!amount) {
throw new GqlInputError('invoice must specify an amount')
}
decoded.mtokens = BigInt(amount) * 1000n
}

if (decoded.mtokens > Number.MAX_SAFE_INTEGER) {
Expand Down
2 changes: 1 addition & 1 deletion api/typeDefs/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const typeDefs = gql`
}

extend type Mutation {
createWithdrawl(invoice: String!, maxFee: Int!): PayIn!
createWithdrawl(invoice: String!, maxFee: Int!, amount: Int): PayIn!
sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): PayIn!
dropBolt11(hash: String!): Boolean
buyCredits(credits: Int!): PayIn!
Expand Down
4 changes: 2 additions & 2 deletions fragments/withdrawal.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { gql } from '@apollo/client'

export const CREATE_WITHDRAWL = gql`
mutation createWithdrawl($invoice: String!, $maxFee: Int!) {
createWithdrawl(invoice: $invoice, maxFee: $maxFee) {
mutation createWithdrawl($invoice: String!, $maxFee: Int!, $amount: Int) {
createWithdrawl(invoice: $invoice, maxFee: $maxFee, amount: $amount) {
id
}
}`
Expand Down
3 changes: 2 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@ export const lastAuthRemovalSchema = object({

export const withdrawlSchema = object({
invoice: string().required('required').trim(),
maxFee: intValidator.required('required').min(0, 'must be at least 0')
maxFee: intValidator.required('required').min(0, 'must be at least 0'),
amount: intValidator.optional().min(1, 'must be at least 1 sat')
})

export const bioSchema = object({
Expand Down
53 changes: 42 additions & 11 deletions pages/withdraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import styles from '@/styles/nav.module.css'
import { useMutation } from '@apollo/client'
import { CREATE_WITHDRAWL, SEND_TO_LNADDR } from '@/fragments/withdrawal'
import { requestProvider } from 'webln'
import { useEffect, useState } from 'react'
import { useEffect, useState, useCallback } from 'react'
import { useMe } from '@/components/me'
import { Checkbox, Form, Input, InputUserSuggest, SubmitButton } from '@/components/form'
import { lnAddrSchema, withdrawlSchema } from '@/lib/validate'
Expand Down Expand Up @@ -82,6 +82,15 @@ export function InvWithdrawal () {
const [createWithdrawl] = useMutation(CREATE_WITHDRAWL)

const maxFeeDefault = me?.privates?.withdrawMaxFeeDefault
const [zeroAmount, setZeroAmount] = useState(false)
const checkInvoice = useCallback((invoice) => {
try {
const decoded = decode(invoice)
setZeroAmount(!decoded.mtokens || BigInt(decoded.mtokens) <= 0)
} catch {
setZeroAmount(false)
}
}, [])

useEffect(() => {
async function effect () {
Expand All @@ -106,11 +115,14 @@ export function InvWithdrawal () {
autoComplete='off'
initial={{
invoice: '',
maxFee: maxFeeDefault
maxFee: maxFeeDefault,
amount: ''
}}
schema={withdrawlSchema}
onSubmit={async ({ invoice, maxFee }) => {
const { data } = await createWithdrawl({ variables: { invoice, maxFee: Number(maxFee) } })
onSubmit={async ({ invoice, maxFee, amount }) => {
const variables = { invoice, maxFee: Number(maxFee) }
if (zeroAmount) variables.amount = Number(amount)
const { data } = await createWithdrawl({ variables })
router.push(`/transactions/${data.createWithdrawl.id}`)
}}
>
Expand All @@ -120,8 +132,18 @@ export function InvWithdrawal () {
required
autoFocus
clear
append={<InvoiceScanner fieldName='invoice' />}
append={<InvoiceScanner fieldName='invoice' onInvoiceChange={checkInvoice} />}
onChange={(_, e) => { checkInvoice(e.target.value) }}
/>
{zeroAmount && (
<Input
label='amount (sats)'
name='amount'
type='number'
required
min={1}
/>
)}
<Input
label='max fee'
name='maxFee'
Expand All @@ -136,7 +158,7 @@ export function InvWithdrawal () {
)
}

function InvoiceScanner ({ fieldName }) {
function InvoiceScanner ({ fieldName, onInvoiceChange }) {
const showModal = useShowModal()
const [,, helpers] = useField(fieldName)
const toaster = useToast()
Expand All @@ -157,13 +179,22 @@ function InvoiceScanner ({ fieldName }) {
formats={['qr_code']}
onScan={([{ rawValue: result }]) => {
result = result.toLowerCase()
if (result.split('lightning=')[1]) {
helpers.setValue(result.split('lightning=')[1].split(/[&?]/)[0])
} else if (decode(result.replace(/^lightning:/, ''))) {
helpers.setValue(result.replace(/^lightning:/, ''))
let invoice
if (result.includes('lightning=')) {
invoice = result.split('lightning=')[1].split(/[&?]/)[0]
helpers.setValue(invoice)
} else {
throw new Error('Not a proper lightning payment request')
try {
invoice = result.replace(/^lightning:/, '')
decode(invoice)
helpers.setValue(invoice)
} catch {
toaster.danger('Not a proper lightning payment request')
onClose()
return
}
}
if (onInvoiceChange) onInvoiceChange(invoice)
onClose()
}}
styles={{
Expand Down