Merge pull request #130 from breez/savage-rn-try-blocks

Wrap RN promises with try blocks
This commit is contained in:
Ross Savage
2024-02-06 21:21:44 +01:00
committed by GitHub
17 changed files with 275 additions and 145 deletions

View File

@@ -5,8 +5,12 @@ import {
const exampleBuyBtc = async () => { const exampleBuyBtc = async () => {
// ANCHOR: buy-btc // ANCHOR: buy-btc
try {
const buyBitcoinResponse = await buyBitcoin({ const buyBitcoinResponse = await buyBitcoin({
provider: BuyBitcoinProvider.MOONPAY provider: BuyBitcoinProvider.MOONPAY
}) })
} catch (err) {
console.error(err)
}
// ANCHOR_END: buy-btc // ANCHOR_END: buy-btc
} }

View File

@@ -5,15 +5,22 @@ import {
const examplePrepareRedeemOnchainFunds = async (feeRate: number) => { const examplePrepareRedeemOnchainFunds = async (feeRate: number) => {
// ANCHOR: prepare-redeem-onchain-funds // ANCHOR: prepare-redeem-onchain-funds
try {
const toAddress = 'bc1..' const toAddress = 'bc1..'
const satPerVbyte = feeRate const satPerVbyte = feeRate
const prepareRedeemOnchainFundsResp = await prepareRedeemOnchainFunds({ toAddress, satPerVbyte }) const prepareRedeemOnchainFundsResp = await prepareRedeemOnchainFunds({ toAddress, satPerVbyte })
} catch (err) {
console.error(err)
}
// ANCHOR_END: prepare-redeem-onchain-funds // ANCHOR_END: prepare-redeem-onchain-funds
} }
const exampleRedeemOnchainFunds = async (satPerVbyte: number, toAddress: string) => { const exampleRedeemOnchainFunds = async (satPerVbyte: number, toAddress: string) => {
// ANCHOR: redeem-onchain-funds // ANCHOR: redeem-onchain-funds
try {
const redeemOnchainFundsResp = await redeemOnchainFunds({ toAddress, satPerVbyte }) const redeemOnchainFundsResp = await redeemOnchainFunds({ toAddress, satPerVbyte })
} catch (err) {
console.error(err)
}
// ANCHOR_END: redeem-onchain-funds // ANCHOR_END: redeem-onchain-funds
} }

View File

@@ -2,20 +2,32 @@ import { connectLsp, listLsps, lspId, lspInfo } from '@breeztech/react-native-br
const exampleAutoConnect = async () => { const exampleAutoConnect = async () => {
// ANCHOR: get-lsp-info // ANCHOR: get-lsp-info
try {
const id = await lspId() const id = await lspId()
const info = await lspInfo() const info = await lspInfo()
} catch (err) {
console.error(err)
}
// ANCHOR_END: get-lsp-info // ANCHOR_END: get-lsp-info
} }
const exampleListLsps = async () => { const exampleListLsps = async () => {
// ANCHOR: list-lsps // ANCHOR: list-lsps
try {
const availableLsps = await listLsps() const availableLsps = await listLsps()
} catch (err) {
console.error(err)
}
// ANCHOR_END: list-lsps // ANCHOR_END: list-lsps
} }
const exampleManualConnect = async () => { const exampleManualConnect = async () => {
// ANCHOR: connect-lsp // ANCHOR: connect-lsp
try {
const id = 'your selected lsp id' const id = 'your selected lsp id'
await connectLsp(id) await connectLsp(id)
} catch (err) {
console.error(err)
}
// ANCHOR_END: connect-lsp // ANCHOR_END: connect-lsp
} }

View File

@@ -5,12 +5,20 @@ import {
const exampleListCurrencies = async () => { const exampleListCurrencies = async () => {
// ANCHOR: list-fiat-currencies // ANCHOR: list-fiat-currencies
try {
const fiatCurrencies = await listFiatCurrencies() const fiatCurrencies = await listFiatCurrencies()
} catch (err) {
console.error(err)
}
// ANCHOR_END: list-fiat-currencies // ANCHOR_END: list-fiat-currencies
} }
const exampleFetchRates = async () => { const exampleFetchRates = async () => {
// ANCHOR: fetch-fiat-rates // ANCHOR: fetch-fiat-rates
try {
const fiatRates = await fetchFiatRates() const fiatRates = await fetchFiatRates()
} catch (err) {
console.error(err)
}
// ANCHOR_END: fetch-fiat-rates // ANCHOR_END: fetch-fiat-rates
} }

View File

@@ -16,6 +16,7 @@ const exampleGettingStarted = async () => {
console.log(`Received event ${e.type}`) console.log(`Received event ${e.type}`)
} }
try {
// Create the default config // Create the default config
const seed = await mnemonicToSeed('<mnemonics words>') const seed = await mnemonicToSeed('<mnemonics words>')
const inviteCode = '<invite code>' const inviteCode = '<invite code>'
@@ -35,13 +36,20 @@ const exampleGettingStarted = async () => {
// Connect to the Breez SDK make it ready for use // Connect to the Breez SDK make it ready for use
await connect(config, seed, onBreezEvent) await connect(config, seed, onBreezEvent)
} catch (err) {
console.error(err)
}
// ANCHOR_END: init-sdk // ANCHOR_END: init-sdk
} }
const exampleFetchNodeInfo = async () => { const exampleFetchNodeInfo = async () => {
// ANCHOR: fetch-balance // ANCHOR: fetch-balance
try {
const nodeState = await nodeInfo() const nodeState = await nodeInfo()
const balanceLn = nodeState.channelsBalanceMsat const balanceLn = nodeState.channelsBalanceMsat
const balanceOnchain = nodeState.onchainBalanceMsat const balanceOnchain = nodeState.onchainBalanceMsat
} catch (err) {
console.error(err)
}
// ANCHOR_END: fetch-balance // ANCHOR_END: fetch-balance
} }

View File

@@ -5,16 +5,24 @@ import {
const exampleListPayments = async () => { const exampleListPayments = async () => {
// ANCHOR: list-payments // ANCHOR: list-payments
const payments = listPayments({}) try {
const payments = await listPayments({})
} catch (err) {
console.error(err)
}
// ANCHOR_END: list-payments // ANCHOR_END: list-payments
} }
const exampleListPaymentsFiltered = async () => { const exampleListPaymentsFiltered = async () => {
// ANCHOR: list-payments-filtered // ANCHOR: list-payments-filtered
const payments = listPayments({ try {
const payments = await listPayments({
filters: [PaymentTypeFilter.SENT], filters: [PaymentTypeFilter.SENT],
fromTimestamp: 1696880000, fromTimestamp: 1696880000,
includeFailures: true includeFailures: true
}) })
} catch (err) {
console.error(err)
}
// ANCHOR_END: list-payments-filtered // ANCHOR_END: list-payments-filtered
} }

View File

@@ -9,6 +9,7 @@ const exampleLnurlAuth = async () => {
// ANCHOR: lnurl-auth // ANCHOR: lnurl-auth
// Endpoint can also be of the form: // Endpoint can also be of the form:
// keyauth://domain.com/auth?key=val // keyauth://domain.com/auth?key=val
try {
const lnurlAuthUrl = const lnurlAuthUrl =
'lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu' 'lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu'
@@ -21,5 +22,8 @@ const exampleLnurlAuth = async () => {
console.log('Failed to authenticate') console.log('Failed to authenticate')
} }
} }
} catch (err) {
console.error(err)
}
// ANCHOR_END: lnurl-auth // ANCHOR_END: lnurl-auth
} }

View File

@@ -9,6 +9,7 @@ const exampleLnurlPay = async () => {
// Endpoint can also be of the // Endpoint can also be of the
// lnurlp://domain.com/lnurl-pay?key=val // lnurlp://domain.com/lnurl-pay?key=val
// lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf // lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf
try {
const lnurlPayUrl = 'lightning@address.com' const lnurlPayUrl = 'lightning@address.com'
const input = await parseInput(lnurlPayUrl) const input = await parseInput(lnurlPayUrl)
@@ -20,5 +21,8 @@ const exampleLnurlPay = async () => {
comment: 'comment' comment: 'comment'
}) })
} }
} catch (err) {
console.error(err)
}
// ANCHOR_END: lnurl-pay // ANCHOR_END: lnurl-pay
} }

View File

@@ -8,6 +8,7 @@ const exampleLnurlWithdraw = async () => {
// ANCHOR: lnurl-withdraw // ANCHOR: lnurl-withdraw
// Endpoint can also be of the form: // Endpoint can also be of the form:
// lnurlw://domain.com/lnurl-withdraw?key=val // lnurlw://domain.com/lnurl-withdraw?key=val
try {
const lnurlWithdrawUrl = const lnurlWithdrawUrl =
'lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk' 'lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk'
@@ -20,5 +21,8 @@ const exampleLnurlWithdraw = async () => {
description: 'comment' description: 'comment'
}) })
} }
} catch (err) {
console.error(err)
}
// ANCHOR_END: lnurl-withdraw // ANCHOR_END: lnurl-withdraw
} }

View File

@@ -8,29 +8,42 @@ import {
const exampleReceiveOnchain = async () => { const exampleReceiveOnchain = async () => {
// ANCHOR: generate-receive-onchain-address // ANCHOR: generate-receive-onchain-address
try {
const swapInfo = await receiveOnchain({}) const swapInfo = await receiveOnchain({})
// Send your funds to the below bitcoin address // Send your funds to the below bitcoin address
const address = swapInfo.bitcoinAddress const address = swapInfo.bitcoinAddress
console.log(`Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}`) console.log(`Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}`)
console.log(`Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}`) console.log(`Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}`)
} catch (err) {
console.error(err)
}
// ANCHOR_END: generate-receive-onchain-address // ANCHOR_END: generate-receive-onchain-address
} }
const exampleInProgressSwap = async () => { const exampleInProgressSwap = async () => {
// ANCHOR: in-progress-swap // ANCHOR: in-progress-swap
try {
const swapInfo = await inProgressSwap() const swapInfo = await inProgressSwap()
} catch (err) {
console.error(err)
}
// ANCHOR_END: in-progress-swap // ANCHOR_END: in-progress-swap
} }
const exampleListRefundables = async () => { const exampleListRefundables = async () => {
// ANCHOR: list-refundables // ANCHOR: list-refundables
try {
const refundables = await listRefundables() const refundables = await listRefundables()
} catch (err) {
console.error(err)
}
// ANCHOR_END: list-refundables // ANCHOR_END: list-refundables
} }
const exampleRefund = async () => { const exampleRefund = async () => {
// ANCHOR: execute-refund // ANCHOR: execute-refund
try {
const refundables = await listRefundables() const refundables = await listRefundables()
const toAddress = '...' const toAddress = '...'
const satPerVbyte = 5 const satPerVbyte = 5
@@ -40,15 +53,21 @@ const exampleRefund = async () => {
toAddress, toAddress,
satPerVbyte satPerVbyte
}) })
} catch (err) {
console.error(err)
}
// ANCHOR_END: execute-refund // ANCHOR_END: execute-refund
} }
const exampleOpenChannelFee = async () => { const exampleOpenChannelFee = async () => {
// ANCHOR: get-channel-opening-fees // ANCHOR: get-channel-opening-fees
try {
const amountMsat = 10000 const amountMsat = 10000
const openChannelFeeResponse = await openChannelFee({ const openChannelFeeResponse = await openChannelFee({
amountMsat amountMsat
}) })
} catch (err) {
console.error(err)
}
// ANCHOR_END: get-channel-opening-fees // ANCHOR_END: get-channel-opening-fees
} }

View File

@@ -2,11 +2,15 @@ import { receivePayment } from '@breeztech/react-native-breez-sdk'
const exampleReceiveLightningPayment = async () => { const exampleReceiveLightningPayment = async () => {
// ANCHOR: receive-payment // ANCHOR: receive-payment
try {
const receivePaymentResponse = await receivePayment({ const receivePaymentResponse = await receivePayment({
amountMsat: 3_000_000, amountMsat: 3_000_000,
description: 'Invoice for 3000 sats' description: 'Invoice for 3000 sats'
}) })
const invoice = receivePaymentResponse.lnInvoice const invoice = receivePaymentResponse.lnInvoice
} catch (err) {
console.error(err)
}
// ANCHOR_END: receive-payment // ANCHOR_END: receive-payment
} }

View File

@@ -8,11 +8,15 @@ import {
const exampleFetchReverseSwapFees = async () => { const exampleFetchReverseSwapFees = async () => {
// ANCHOR: estimate-current-reverse-swap-total-fees // ANCHOR: estimate-current-reverse-swap-total-fees
try {
const currentFees = await fetchReverseSwapFees({ sendAmountSat: 50000 }) const currentFees = await fetchReverseSwapFees({ sendAmountSat: 50000 })
console.log( console.log(
`Total estimated fees for reverse swap: ${currentFees.totalEstimatedFees}` `Total estimated fees for reverse swap: ${currentFees.totalEstimatedFees}`
) )
} catch (err) {
console.error(err)
}
// ANCHOR_END: estimate-current-reverse-swap-total-fees // ANCHOR_END: estimate-current-reverse-swap-total-fees
} }
@@ -25,16 +29,21 @@ const exampleListCurrentFees = (currentFees: ReverseSwapPairInfo) => {
const maxAmount = async () => { const maxAmount = async () => {
// ANCHOR: max-reverse-swap-amount // ANCHOR: max-reverse-swap-amount
try {
const maxAmount = await maxReverseSwapAmount() const maxAmount = await maxReverseSwapAmount()
console.log( console.log(
`Max reverse swap amount: ${maxAmount.totalSat}` `Max reverse swap amount: ${maxAmount.totalSat}`
) )
} catch (err) {
console.error(err)
}
// ANCHOR_END: max-reverse-swap-amount // ANCHOR_END: max-reverse-swap-amount
} }
const exampleSendOnchain = async (currentFees: ReverseSwapPairInfo) => { const exampleSendOnchain = async (currentFees: ReverseSwapPairInfo) => {
// ANCHOR: start-reverse-swap // ANCHOR: start-reverse-swap
try {
const onchainRecipientAddress = 'bc1..' const onchainRecipientAddress = 'bc1..'
const amountSat = currentFees.min const amountSat = currentFees.min
const satPerVbyte = 5 const satPerVbyte = 5
@@ -45,16 +54,23 @@ const exampleSendOnchain = async (currentFees: ReverseSwapPairInfo) => {
pairHash: currentFees.feesHash, pairHash: currentFees.feesHash,
satPerVbyte satPerVbyte
}) })
} catch (err) {
console.error(err)
}
// ANCHOR_END: start-reverse-swap // ANCHOR_END: start-reverse-swap
} }
const exampleInProgressReverseSwaps = async () => { const exampleInProgressReverseSwaps = async () => {
// ANCHOR: check-reverse-swaps-status // ANCHOR: check-reverse-swaps-status
try {
const swaps = await inProgressReverseSwaps() const swaps = await inProgressReverseSwaps()
for (const swap of swaps) { for (const swap of swaps) {
console.log( console.log(
`Reverse swap ${swap.id} in progress, status is ${swap.status}` `Reverse swap ${swap.id} in progress, status is ${swap.status}`
) )
} }
} catch (err) {
console.error(err)
}
// ANCHOR_END: check-reverse-swaps-status // ANCHOR_END: check-reverse-swaps-status
} }

View File

@@ -2,10 +2,14 @@ import { sendPayment } from '@breeztech/react-native-breez-sdk'
const exampleSendLightningPayment = async () => { const exampleSendLightningPayment = async () => {
// ANCHOR: send-payment // ANCHOR: send-payment
try {
const bolt11 = 'bolt11 invoice' const bolt11 = 'bolt11 invoice'
// The `amountMsat` param is optional and should only passed if the bolt11 doesn't specify an amount. // The `amountMsat` param is optional and should only passed if the bolt11 doesn't specify an amount.
// The amountMsat is required in case an amount is not specified in the bolt11 invoice'. // The amountMsat is required in case an amount is not specified in the bolt11 invoice'.
const amountMsat = 3000000 const amountMsat = 3000000
const response = await sendPayment({ bolt11, amountMsat }) const response = await sendPayment({ bolt11, amountMsat })
} catch (err) {
console.error(err)
}
// ANCHOR_END: send-payment // ANCHOR_END: send-payment
} }

View File

@@ -5,12 +5,16 @@ import {
const exampleSendSpontaneousPayment = async () => { const exampleSendSpontaneousPayment = async () => {
// ANCHOR: send-spontaneous-payment // ANCHOR: send-spontaneous-payment
try {
const nodeId = '...' const nodeId = '...'
const sendPaymentResponse = await sendSpontaneousPayment({ const sendPaymentResponse = await sendSpontaneousPayment({
nodeId, nodeId,
amountMsat: 3000000 amountMsat: 3000000
}) })
} catch (err) {
console.error(err)
}
// ANCHOR_END: send-spontaneous-payment // ANCHOR_END: send-spontaneous-payment
} }
@@ -24,6 +28,7 @@ const stringToBytes = (str: string): number[] => {
const exampleSendSpontaneousPaymentWithTlvs = async () => { const exampleSendSpontaneousPaymentWithTlvs = async () => {
// ANCHOR: send-spontaneous-payment-with-tlvs // ANCHOR: send-spontaneous-payment-with-tlvs
try {
const nodeId = '...' const nodeId = '...'
const extraTlvs: TlvEntry[] = [ const extraTlvs: TlvEntry[] = [
{ fieldNumber: 34349334, value: stringToBytes('Hello world!') } { fieldNumber: 34349334, value: stringToBytes('Hello world!') }
@@ -34,5 +39,8 @@ const exampleSendSpontaneousPaymentWithTlvs = async () => {
amountMsat: 3000000, amountMsat: 3000000,
extraTlvs extraTlvs
}) })
} catch (err) {
console.error(err)
}
// ANCHOR_END: send-spontaneous-payment-with-tlvs // ANCHOR_END: send-spontaneous-payment-with-tlvs
} }

View File

@@ -2,17 +2,25 @@ import { serviceHealthCheck, reportIssue, ReportIssueRequestVariant } from '@bre
const healthCheckStatus = async () => { const healthCheckStatus = async () => {
// ANCHOR: health-check-status // ANCHOR: health-check-status
try {
const healthCheck = await serviceHealthCheck() const healthCheck = await serviceHealthCheck()
console.log(`Current service status is: ${healthCheck.status}`) console.log(`Current service status is: ${healthCheck.status}`)
} catch (err) {
console.error(err)
}
// ANCHOR_END: health-check-status // ANCHOR_END: health-check-status
} }
const reportPaymentFailure = async () => { const reportPaymentFailure = async () => {
// ANCHOR: report-payment-failure // ANCHOR: report-payment-failure
try {
const paymentHash = '...' const paymentHash = '...'
await reportIssue({ await reportIssue({
type: ReportIssueRequestVariant.PAYMENT_FAILURE, type: ReportIssueRequestVariant.PAYMENT_FAILURE,
data: { paymentHash } data: { paymentHash }
}) })
} catch (err) {
console.error(err)
}
// ANCHOR_END: report-payment-failure // ANCHOR_END: report-payment-failure
} }

View File

@@ -2,6 +2,10 @@ import { staticBackup } from '@breeztech/react-native-breez-sdk'
const exampleStaticBackup = async () => { const exampleStaticBackup = async () => {
// ANCHOR: static-channel-backup // ANCHOR: static-channel-backup
try {
const backupData = await staticBackup({ workingDir: '<working directory>' }) const backupData = await staticBackup({ workingDir: '<working directory>' })
} catch (err) {
console.error(err)
}
// ANCHOR_END: static-channel-backup // ANCHOR_END: static-channel-backup
} }

View File

@@ -2,12 +2,20 @@ import { registerWebhook } from '@breeztech/react-native-breez-sdk'
const webhook = async () => { const webhook = async () => {
// ANCHOR: register-webook // ANCHOR: register-webook
try {
await registerWebhook('https://yourapplication.com') await registerWebhook('https://yourapplication.com')
} catch (err) {
console.error(err)
}
// ANCHOR_END: register-webook // ANCHOR_END: register-webook
} }
const paymentWebhook = async () => { const paymentWebhook = async () => {
// ANCHOR: register-payment-webook // ANCHOR: register-payment-webook
try {
await registerWebhook('https://your-nds-service.com/notify?platform=ios&token=<PUSH_TOKEN>') await registerWebhook('https://your-nds-service.com/notify?platform=ios&token=<PUSH_TOKEN>')
} catch (err) {
console.error(err)
}
// ANCHOR_END: register-payment-webook // ANCHOR_END: register-payment-webook
} }