2021-10-28 19:59:53 +00:00
// verify k1 exists
// send back
import models from '../../api/models'
import getSSRApolloClient from '../../api/ssrApollo'
import { CREATE _WITHDRAWL } from '../../fragments/wallet'
export default async ( { query } , res ) => {
2022-05-19 16:06:34 +00:00
if ( ! query . k1 ) {
return res . status ( 400 ) . json ( { status : 'ERROR' , reason : 'k1 not provided' } )
}
2021-10-28 19:59:53 +00:00
if ( query . pr ) {
return doWithdrawal ( query , res )
}
let reason
try {
2022-05-19 16:06:34 +00:00
const lnwith = await models . lnWith . findFirst ( {
where : {
k1 : query . k1 ,
createdAt : {
gt : new Date ( new Date ( ) . setHours ( new Date ( ) . getHours ( ) - 1 ) )
}
}
} )
2021-10-28 19:59:53 +00:00
if ( lnwith ) {
const user = await models . user . findUnique ( { where : { id : lnwith . userId } } )
if ( user ) {
return res . status ( 200 ) . json ( {
tag : 'withdrawRequest' , // type of LNURL
callback : process . env . LNWITH _URL , // The URL which LN SERVICE would accept a withdrawal Lightning invoice as query parameter
k1 : query . k1 , // Random or non-random string to identify the user's LN WALLET when using the callback URL
defaultDescription : ` Withdrawal for @ ${ user . name } on SN ` , // A default withdrawal invoice description
minWithdrawable : 1000 , // Min amount (in millisatoshis) the user can withdraw from LN SERVICE, or 0
2022-11-27 00:23:49 +00:00
maxWithdrawable : Number ( user . msats - 10000 n ) // Max amount (in millisatoshis) the user can withdraw from LN SERVICE, or equal to minWithdrawable if the user has no choice over the amounts
2021-10-28 19:59:53 +00:00
} )
} else {
reason = 'user not found'
}
} else {
reason = 'withdrawal not found'
}
} catch ( error ) {
console . log ( error )
reason = 'internal server error'
}
2022-05-19 16:06:34 +00:00
console . log ( reason )
2021-10-28 19:59:53 +00:00
return res . status ( 400 ) . json ( { status : 'ERROR' , reason } )
}
async function doWithdrawal ( query , res ) {
const lnwith = await models . lnWith . findUnique ( { where : { k1 : query . k1 } } )
if ( ! lnwith ) {
return res . status ( 400 ) . json ( { status : 'ERROR' , reason : 'invalid k1' } )
}
const me = await models . user . findUnique ( { where : { id : lnwith . userId } } )
if ( ! me ) {
return res . status ( 400 ) . json ( { status : 'ERROR' , reason : 'user not found' } )
}
// create withdrawal in gql
const client = await getSSRApolloClient ( null , me )
const { error , data } = await client . mutate ( {
mutation : CREATE _WITHDRAWL ,
variables : { invoice : query . pr , maxFee : 10 }
} )
if ( error || ! data ? . createWithdrawl ) {
return res . status ( 400 ) . json ( { status : 'ERROR' , reason : error ? . toString ( ) || 'could not generate withdrawl' } )
}
// store withdrawal id lnWith so client can show it
await models . lnWith . update ( { where : { k1 : query . k1 } , data : { withdrawalId : Number ( data . createWithdrawl . id ) } } )
return res . status ( 200 ) . json ( { status : 'OK' } )
}