-
Notifications
You must be signed in to change notification settings - Fork 15
/
send.ts
46 lines (41 loc) · 1.01 KB
/
send.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { LNMarketsRest } from '@ln-markets/api'
interface SendArgs {
type: 'bolt11' | 'on-chain'
address: string
amountSats: number
lnmarkets: LNMarketsRest
}
export async function send({ type, address, amountSats, lnmarkets }: SendArgs) {
if (type === 'bolt11') {
await lnmarkets.withdraw({
amount: amountSats,
invoice: address,
})
return
}
if (type === 'on-chain') {
// Withdraw on-chain using a deezy swap.
const url = `https://api${
lnmarkets.network === 'testnet' ? '-testnet' : ''
}.deezy.io/v1/swap`
const result = await fetch(url, {
method: 'post',
body: JSON.stringify({
amount_sats: amountSats,
on_chain_address: address,
on_chain_sats_per_vbyte: 1, // TODO: realtime estimate
}),
})
.then(response => response.json() as Promise<{ bolt11_invoice: string }>)
.catch(err => {
console.log(err)
return null
})
if (result == null) {
return
}
const { bolt11_invoice } = result
await lnmarkets.withdraw({ invoice: bolt11_invoice })
return
}
}