Fix isNaN checks
This commit is contained in:
		
							parent
							
								
									59ff146cc9
								
							
						
					
					
						commit
						12f9c4761d
					
				| @ -444,3 +444,7 @@ export const lud18PayerDataSchema = (k1) => object({ | |||||||
|   email: string().email('bad email address'), |   email: string().email('bad email address'), | ||||||
|   identifier: string() |   identifier: string() | ||||||
| }) | }) | ||||||
|  | 
 | ||||||
|  | // check if something is _really_ a number.
 | ||||||
|  | // returns true for every number in this range: [-Infinity, ..., 0, ..., Infinity]
 | ||||||
|  | export const isNumber = x => typeof x === 'number' && !Number.isNaN(x) | ||||||
|  | |||||||
| @ -7,7 +7,7 @@ import { WalletButtonBar, WalletCard } from '../../../components/wallet-card' | |||||||
| import { useMutation } from '@apollo/client' | import { useMutation } from '@apollo/client' | ||||||
| import { REMOVE_AUTOWITHDRAW, SET_AUTOWITHDRAW } from '../../../fragments/users' | import { REMOVE_AUTOWITHDRAW, SET_AUTOWITHDRAW } from '../../../fragments/users' | ||||||
| import { useToast } from '../../../components/toast' | import { useToast } from '../../../components/toast' | ||||||
| import { lnAddrAutowithdrawSchema } from '../../../lib/validate' | import { lnAddrAutowithdrawSchema, isNumber } from '../../../lib/validate' | ||||||
| import { useRouter } from 'next/router' | import { useRouter } from 'next/router' | ||||||
| import { useEffect, useState } from 'react' | import { useEffect, useState } from 'react' | ||||||
| 
 | 
 | ||||||
| @ -15,7 +15,7 @@ export const getServerSideProps = getGetServerSideProps({ authRequired: true }) | |||||||
| 
 | 
 | ||||||
| function useAutoWithdrawEnabled () { | function useAutoWithdrawEnabled () { | ||||||
|   const me = useMe() |   const me = useMe() | ||||||
|   return me?.privates?.lnAddr && !isNaN(me?.privates?.autoWithdrawThreshold) && !isNaN(me?.privates?.autoWithdrawMaxFeePercent) |   return me?.privates?.lnAddr && isNumber(me?.privates?.autoWithdrawThreshold) && isNumber(me?.privates?.autoWithdrawMaxFeePercent) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export default function LightningAddress () { | export default function LightningAddress () { | ||||||
| @ -25,7 +25,7 @@ export default function LightningAddress () { | |||||||
|   const [setAutoWithdraw] = useMutation(SET_AUTOWITHDRAW) |   const [setAutoWithdraw] = useMutation(SET_AUTOWITHDRAW) | ||||||
|   const enabled = useAutoWithdrawEnabled() |   const enabled = useAutoWithdrawEnabled() | ||||||
|   const [removeAutoWithdraw] = useMutation(REMOVE_AUTOWITHDRAW) |   const [removeAutoWithdraw] = useMutation(REMOVE_AUTOWITHDRAW) | ||||||
|   const autoWithdrawThreshold = isNaN(me?.privates?.autoWithdrawThreshold) ? 10000 : me?.privates?.autoWithdrawThreshold |   const autoWithdrawThreshold = isNumber(me?.privates?.autoWithdrawThreshold) ? me?.privates?.autoWithdrawThreshold : 10000 | ||||||
|   const [sendThreshold, setSendThreshold] = useState(Math.max(Math.floor(autoWithdrawThreshold / 10), 1)) |   const [sendThreshold, setSendThreshold] = useState(Math.max(Math.floor(autoWithdrawThreshold / 10), 1)) | ||||||
| 
 | 
 | ||||||
|   useEffect(() => { |   useEffect(() => { | ||||||
| @ -39,8 +39,8 @@ export default function LightningAddress () { | |||||||
|       <Form |       <Form | ||||||
|         initial={{ |         initial={{ | ||||||
|           lnAddr: me?.privates?.lnAddr || '', |           lnAddr: me?.privates?.lnAddr || '', | ||||||
|           autoWithdrawThreshold: isNaN(me?.privates?.autoWithdrawThreshold) ? 10000 : me?.privates?.autoWithdrawThreshold, |           autoWithdrawThreshold, | ||||||
|           autoWithdrawMaxFeePercent: isNaN(me?.privates?.autoWithdrawMaxFeePercent) ? 1 : me?.privates?.autoWithdrawMaxFeePercent |           autoWithdrawMaxFeePercent: isNumber(me?.privates?.autoWithdrawMaxFeePercent) ? me?.privates?.autoWithdrawMaxFeePercent : 1 | ||||||
|         }} |         }} | ||||||
|         schema={lnAddrAutowithdrawSchema({ me })} |         schema={lnAddrAutowithdrawSchema({ me })} | ||||||
|         onSubmit={async ({ autoWithdrawThreshold, autoWithdrawMaxFeePercent, ...values }) => { |         onSubmit={async ({ autoWithdrawThreshold, autoWithdrawMaxFeePercent, ...values }) => { | ||||||
| @ -73,7 +73,7 @@ export default function LightningAddress () { | |||||||
|             const value = e.target.value |             const value = e.target.value | ||||||
|             setSendThreshold(Math.max(Math.floor(value / 10), 1)) |             setSendThreshold(Math.max(Math.floor(value / 10), 1)) | ||||||
|           }} |           }} | ||||||
|           hint={isNaN(sendThreshold) ? undefined : `note: will attempt withdrawal when desired balance is exceeded by ${sendThreshold} sats`} |           hint={isNumber(sendThreshold) ? `note: will attempt withdraw when threshold is exceeded by ${sendThreshold} sats` : undefined} | ||||||
|           append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>} |           append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>} | ||||||
|         /> |         /> | ||||||
|         <Input |         <Input | ||||||
|  | |||||||
| @ -9,6 +9,7 @@ import { INVOICE_RETENTION_DAYS } from '../lib/constants' | |||||||
| import { sleep } from '../lib/time.js' | import { sleep } from '../lib/time.js' | ||||||
| import { sendToLnAddr } from '../api/resolvers/wallet.js' | import { sendToLnAddr } from '../api/resolvers/wallet.js' | ||||||
| import retry from 'async-retry' | import retry from 'async-retry' | ||||||
|  | import { isNumber } from '../lib/validate.js' | ||||||
| 
 | 
 | ||||||
| export async function subscribeToWallet (args) { | export async function subscribeToWallet (args) { | ||||||
|   await subscribeToDeposits(args) |   await subscribeToDeposits(args) | ||||||
| @ -292,8 +293,8 @@ export async function autoWithdraw ({ data: { id }, models, lnd }) { | |||||||
|   const user = await models.user.findUnique({ where: { id } }) |   const user = await models.user.findUnique({ where: { id } }) | ||||||
|   if (!user || |   if (!user || | ||||||
|     !user.lnAddr || |     !user.lnAddr || | ||||||
|     isNaN(user.autoWithdrawThreshold) || |     !isNumber(user.autoWithdrawThreshold) || | ||||||
|     isNaN(user.autoWithdrawMaxFeePercent)) return |     !isNumber(user.autoWithdrawMaxFeePercent)) return | ||||||
| 
 | 
 | ||||||
|   const threshold = satsToMsats(user.autoWithdrawThreshold) |   const threshold = satsToMsats(user.autoWithdrawThreshold) | ||||||
|   const excess = Number(user.msats - threshold) |   const excess = Number(user.msats - threshold) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user