Skip to content

Commit b81df4c

Browse files
authored
Merge pull request #130 from breez/savage-rn-try-blocks
Wrap RN promises with try blocks
2 parents 0379597 + aec1446 commit b81df4c

17 files changed

+275
-145
lines changed

snippets/react-native/buy_btc.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import {
55

66
const exampleBuyBtc = async () => {
77
// ANCHOR: buy-btc
8-
const buyBitcoinResponse = await buyBitcoin({
9-
provider: BuyBitcoinProvider.MOONPAY
10-
})
8+
try {
9+
const buyBitcoinResponse = await buyBitcoin({
10+
provider: BuyBitcoinProvider.MOONPAY
11+
})
12+
} catch (err) {
13+
console.error(err)
14+
}
1115
// ANCHOR_END: buy-btc
1216
}

snippets/react-native/closed_channel.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ import {
55

66
const examplePrepareRedeemOnchainFunds = async (feeRate: number) => {
77
// ANCHOR: prepare-redeem-onchain-funds
8-
const toAddress = 'bc1..'
9-
const satPerVbyte = feeRate
10-
11-
const prepareRedeemOnchainFundsResp = await prepareRedeemOnchainFunds({ toAddress, satPerVbyte })
8+
try {
9+
const toAddress = 'bc1..'
10+
const satPerVbyte = feeRate
11+
const prepareRedeemOnchainFundsResp = await prepareRedeemOnchainFunds({ toAddress, satPerVbyte })
12+
} catch (err) {
13+
console.error(err)
14+
}
1215
// ANCHOR_END: prepare-redeem-onchain-funds
1316
}
1417

1518
const exampleRedeemOnchainFunds = async (satPerVbyte: number, toAddress: string) => {
1619
// ANCHOR: redeem-onchain-funds
17-
const redeemOnchainFundsResp = await redeemOnchainFunds({ toAddress, satPerVbyte })
20+
try {
21+
const redeemOnchainFundsResp = await redeemOnchainFunds({ toAddress, satPerVbyte })
22+
} catch (err) {
23+
console.error(err)
24+
}
1825
// ANCHOR_END: redeem-onchain-funds
1926
}

snippets/react-native/connecting_lsp.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@ import { connectLsp, listLsps, lspId, lspInfo } from '@breeztech/react-native-br
22

33
const exampleAutoConnect = async () => {
44
// ANCHOR: get-lsp-info
5-
const id = await lspId()
6-
const info = await lspInfo()
5+
try {
6+
const id = await lspId()
7+
const info = await lspInfo()
8+
} catch (err) {
9+
console.error(err)
10+
}
711
// ANCHOR_END: get-lsp-info
812
}
913

1014
const exampleListLsps = async () => {
1115
// ANCHOR: list-lsps
12-
const availableLsps = await listLsps()
16+
try {
17+
const availableLsps = await listLsps()
18+
} catch (err) {
19+
console.error(err)
20+
}
1321
// ANCHOR_END: list-lsps
1422
}
1523

1624
const exampleManualConnect = async () => {
1725
// ANCHOR: connect-lsp
18-
const id = 'your selected lsp id'
19-
await connectLsp(id)
26+
try {
27+
const id = 'your selected lsp id'
28+
await connectLsp(id)
29+
} catch (err) {
30+
console.error(err)
31+
}
2032
// ANCHOR_END: connect-lsp
2133
}

snippets/react-native/fiat_currencies.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ import {
55

66
const exampleListCurrencies = async () => {
77
// ANCHOR: list-fiat-currencies
8-
const fiatCurrencies = await listFiatCurrencies()
8+
try {
9+
const fiatCurrencies = await listFiatCurrencies()
10+
} catch (err) {
11+
console.error(err)
12+
}
913
// ANCHOR_END: list-fiat-currencies
1014
}
1115

1216
const exampleFetchRates = async () => {
1317
// ANCHOR: fetch-fiat-rates
14-
const fiatRates = await fetchFiatRates()
18+
try {
19+
const fiatRates = await fetchFiatRates()
20+
} catch (err) {
21+
console.error(err)
22+
}
1523
// ANCHOR_END: fetch-fiat-rates
1624
}

snippets/react-native/getting_started.ts

+27-19
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,40 @@ const exampleGettingStarted = async () => {
1616
console.log(`Received event ${e.type}`)
1717
}
1818

19-
// Create the default config
20-
const seed = await mnemonicToSeed('<mnemonics words>')
21-
const inviteCode = '<invite code>'
22-
const apiKey = '<api key>'
23-
const nodeConfig: NodeConfig = {
24-
type: NodeConfigVariant.GREENLIGHT,
25-
config: {
26-
inviteCode
19+
try {
20+
// Create the default config
21+
const seed = await mnemonicToSeed('<mnemonics words>')
22+
const inviteCode = '<invite code>'
23+
const apiKey = '<api key>'
24+
const nodeConfig: NodeConfig = {
25+
type: NodeConfigVariant.GREENLIGHT,
26+
config: {
27+
inviteCode
28+
}
2729
}
28-
}
2930

30-
const config = await defaultConfig(
31-
EnvironmentType.PRODUCTION,
32-
apiKey,
33-
nodeConfig
34-
)
31+
const config = await defaultConfig(
32+
EnvironmentType.PRODUCTION,
33+
apiKey,
34+
nodeConfig
35+
)
3536

36-
// Connect to the Breez SDK make it ready for use
37-
await connect(config, seed, onBreezEvent)
37+
// Connect to the Breez SDK make it ready for use
38+
await connect(config, seed, onBreezEvent)
39+
} catch (err) {
40+
console.error(err)
41+
}
3842
// ANCHOR_END: init-sdk
3943
}
4044

4145
const exampleFetchNodeInfo = async () => {
4246
// ANCHOR: fetch-balance
43-
const nodeState = await nodeInfo()
44-
const balanceLn = nodeState.channelsBalanceMsat
45-
const balanceOnchain = nodeState.onchainBalanceMsat
47+
try {
48+
const nodeState = await nodeInfo()
49+
const balanceLn = nodeState.channelsBalanceMsat
50+
const balanceOnchain = nodeState.onchainBalanceMsat
51+
} catch (err) {
52+
console.error(err)
53+
}
4654
// ANCHOR_END: fetch-balance
4755
}

snippets/react-native/list_payments.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@ import {
55

66
const exampleListPayments = async () => {
77
// ANCHOR: list-payments
8-
const payments = listPayments({})
8+
try {
9+
const payments = await listPayments({})
10+
} catch (err) {
11+
console.error(err)
12+
}
913
// ANCHOR_END: list-payments
1014
}
1115

1216
const exampleListPaymentsFiltered = async () => {
1317
// ANCHOR: list-payments-filtered
14-
const payments = listPayments({
15-
filters: [PaymentTypeFilter.SENT],
16-
fromTimestamp: 1696880000,
17-
includeFailures: true
18-
})
18+
try {
19+
const payments = await listPayments({
20+
filters: [PaymentTypeFilter.SENT],
21+
fromTimestamp: 1696880000,
22+
includeFailures: true
23+
})
24+
} catch (err) {
25+
console.error(err)
26+
}
1927
// ANCHOR_END: list-payments-filtered
2028
}

snippets/react-native/lnurl_auth.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ const exampleLnurlAuth = async () => {
99
// ANCHOR: lnurl-auth
1010
// Endpoint can also be of the form:
1111
// keyauth://domain.com/auth?key=val
12-
const lnurlAuthUrl =
13-
'lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu'
12+
try {
13+
const lnurlAuthUrl =
14+
'lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu'
1415

15-
const input = await parseInput(lnurlAuthUrl)
16-
if (input.type === InputTypeVariant.LN_URL_AUTH) {
17-
const result = await lnurlAuth(input.data)
18-
if (result.type === LnUrlCallbackStatusVariant.OK) {
19-
console.log('Successfully authenticated')
20-
} else {
21-
console.log('Failed to authenticate')
16+
const input = await parseInput(lnurlAuthUrl)
17+
if (input.type === InputTypeVariant.LN_URL_AUTH) {
18+
const result = await lnurlAuth(input.data)
19+
if (result.type === LnUrlCallbackStatusVariant.OK) {
20+
console.log('Successfully authenticated')
21+
} else {
22+
console.log('Failed to authenticate')
23+
}
2224
}
25+
} catch (err) {
26+
console.error(err)
2327
}
2428
// ANCHOR_END: lnurl-auth
2529
}

snippets/react-native/lnurl_pay.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ const exampleLnurlPay = async () => {
99
// Endpoint can also be of the
1010
// lnurlp://domain.com/lnurl-pay?key=val
1111
// lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf
12-
const lnurlPayUrl = 'lightning@address.com'
12+
try {
13+
const lnurlPayUrl = 'lightning@address.com'
1314

14-
const input = await parseInput(lnurlPayUrl)
15-
if (input.type === InputTypeVariant.LN_URL_PAY) {
16-
const amountMsat = input.data.minSendable
17-
const lnUrlPayResult = await payLnurl({
18-
data: input.data,
19-
amountMsat,
20-
comment: 'comment'
21-
})
15+
const input = await parseInput(lnurlPayUrl)
16+
if (input.type === InputTypeVariant.LN_URL_PAY) {
17+
const amountMsat = input.data.minSendable
18+
const lnUrlPayResult = await payLnurl({
19+
data: input.data,
20+
amountMsat,
21+
comment: 'comment'
22+
})
23+
}
24+
} catch (err) {
25+
console.error(err)
2226
}
2327
// ANCHOR_END: lnurl-pay
2428
}

snippets/react-native/lnurl_withdraw.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ const exampleLnurlWithdraw = async () => {
88
// ANCHOR: lnurl-withdraw
99
// Endpoint can also be of the form:
1010
// lnurlw://domain.com/lnurl-withdraw?key=val
11-
const lnurlWithdrawUrl =
12-
'lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk'
11+
try {
12+
const lnurlWithdrawUrl =
13+
'lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk'
1314

14-
const input = await parseInput(lnurlWithdrawUrl)
15-
if (input.type === InputTypeVariant.LN_URL_WITHDRAW) {
16-
const amountMsat = input.data.minWithdrawable
17-
const lnUrlWithdrawResult = await withdrawLnurl({
18-
data: input.data,
19-
amountMsat,
20-
description: 'comment'
21-
})
15+
const input = await parseInput(lnurlWithdrawUrl)
16+
if (input.type === InputTypeVariant.LN_URL_WITHDRAW) {
17+
const amountMsat = input.data.minWithdrawable
18+
const lnUrlWithdrawResult = await withdrawLnurl({
19+
data: input.data,
20+
amountMsat,
21+
description: 'comment'
22+
})
23+
}
24+
} catch (err) {
25+
console.error(err)
2226
}
2327
// ANCHOR_END: lnurl-withdraw
2428
}

snippets/react-native/receive_onchain.ts

+39-20
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,66 @@ import {
88

99
const exampleReceiveOnchain = async () => {
1010
// ANCHOR: generate-receive-onchain-address
11-
const swapInfo = await receiveOnchain({})
11+
try {
12+
const swapInfo = await receiveOnchain({})
1213

13-
// Send your funds to the below bitcoin address
14-
const address = swapInfo.bitcoinAddress
15-
console.log(`Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}`)
16-
console.log(`Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}`)
14+
// Send your funds to the below bitcoin address
15+
const address = swapInfo.bitcoinAddress
16+
console.log(`Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}`)
17+
console.log(`Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}`)
18+
} catch (err) {
19+
console.error(err)
20+
}
1721
// ANCHOR_END: generate-receive-onchain-address
1822
}
1923

2024
const exampleInProgressSwap = async () => {
2125
// ANCHOR: in-progress-swap
22-
const swapInfo = await inProgressSwap()
26+
try {
27+
const swapInfo = await inProgressSwap()
28+
} catch (err) {
29+
console.error(err)
30+
}
2331
// ANCHOR_END: in-progress-swap
2432
}
2533

2634
const exampleListRefundables = async () => {
2735
// ANCHOR: list-refundables
28-
const refundables = await listRefundables()
36+
try {
37+
const refundables = await listRefundables()
38+
} catch (err) {
39+
console.error(err)
40+
}
2941
// ANCHOR_END: list-refundables
3042
}
3143

3244
const exampleRefund = async () => {
3345
// ANCHOR: execute-refund
34-
const refundables = await listRefundables()
35-
const toAddress = '...'
36-
const satPerVbyte = 5
46+
try {
47+
const refundables = await listRefundables()
48+
const toAddress = '...'
49+
const satPerVbyte = 5
3750

38-
const refundResponse = await refund({
39-
swapAddress: refundables[0].bitcoinAddress,
40-
toAddress,
41-
satPerVbyte
42-
})
51+
const refundResponse = await refund({
52+
swapAddress: refundables[0].bitcoinAddress,
53+
toAddress,
54+
satPerVbyte
55+
})
56+
} catch (err) {
57+
console.error(err)
58+
}
4359
// ANCHOR_END: execute-refund
4460
}
4561

4662
const exampleOpenChannelFee = async () => {
4763
// ANCHOR: get-channel-opening-fees
48-
const amountMsat = 10000
49-
50-
const openChannelFeeResponse = await openChannelFee({
51-
amountMsat
52-
})
64+
try {
65+
const amountMsat = 10000
66+
const openChannelFeeResponse = await openChannelFee({
67+
amountMsat
68+
})
69+
} catch (err) {
70+
console.error(err)
71+
}
5372
// ANCHOR_END: get-channel-opening-fees
5473
}

snippets/react-native/receive_payment.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { receivePayment } from '@breeztech/react-native-breez-sdk'
22

33
const exampleReceiveLightningPayment = async () => {
44
// ANCHOR: receive-payment
5-
const receivePaymentResponse = await receivePayment({
6-
amountMsat: 3_000_000,
7-
description: 'Invoice for 3000 sats'
8-
})
5+
try {
6+
const receivePaymentResponse = await receivePayment({
7+
amountMsat: 3_000_000,
8+
description: 'Invoice for 3000 sats'
9+
})
910

10-
const invoice = receivePaymentResponse.lnInvoice
11+
const invoice = receivePaymentResponse.lnInvoice
12+
} catch (err) {
13+
console.error(err)
14+
}
1115
// ANCHOR_END: receive-payment
1216
}

0 commit comments

Comments
 (0)