Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const fs = require('fs')
let axios = require('axios')
const https = require('https')
const macaroon = fs.readFileSync('./admin.macaroon').toString('hex')
const myNode = axios.create({
baseURL: '', // Your endpoint here
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
headers: { 'Grpc-Metadata-macaroon': macaroon }
})
const getInfo = async () => {
try {
let res = await myNode.post('/v1/getinfo')
return res
} catch(err) {
console.error(err.repsonse)
}
}
const payInvoice = async (invoice) => {
try {
let res = await myNode.post('/v2/router/send', {
payment_request: invoice,
timeout_seconds: 60,
fee_limit_sat: 100
})
return res
} catch (err) {
console.error(err.response)
}
}
const createInvoice = async (amountSatoshis) => {
try {
let res = await myNode.post('/v1/invoices', {
value: amountSatoshis
})
return res
} catch (err) {
console.error(err.response)
}
}
const isSettled = async (r_hash) => {
try {
const buffer = Buffer.from(r_hash, 'base64')
let res = await myNode.get(`/v1/invoice/${buffer.toString('hex')}`)
return res.data.settled
} catch (err) {
console.log(err.response)
}
}
const run = async () => {
/* Get information of the lightning node */
const res = getInfo()
/* Pay an invoice generated elsewhere */
// const invoice = process.argv[2]
// const res = await payInvoice(invoice)
/* Create an invoice */
// const res = await createInvoice(5000)
/* Check that an invoice has been paid */
// const invoice = process.argv[2]
// const res = await isSettled(r_hash)
console.log(res)
}
run()
@ApisMellow
Copy link

ApisMellow commented Aug 11, 2022

I believe you have 2 misspellings here, line 18 '/v1/getisfo' and line 21, "error.repsonse". Also, should this be "err.response" at line 21 as it is at lines 34 and 45?

@timothyylim
Copy link
Author

timothyylim commented Aug 11, 2022

Well spotted, @ApisMellow! I've updated it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment