Add payment examples to the RN app (#337)

* Add payment examples to the RN app

* Apply suggestions from code review

Co-authored-by: Erdem Yerebasmaz <erdem@yerebasmaz.com>

---------

Co-authored-by: Erdem Yerebasmaz <erdem@yerebasmaz.com>
This commit is contained in:
Ross Savage
2024-06-27 14:02:15 +02:00
committed by GitHub
parent a4af73352a
commit a98ee61242
3 changed files with 56 additions and 7 deletions

View File

@@ -8,7 +8,19 @@
import React, { useState } from "react"
import { SafeAreaView, ScrollView, StatusBar, Text, TouchableOpacity, View } from "react-native"
import { addEventListener, connect, defaultConfig, getInfo, Network, removeEventListener } from "@breeztech/react-native-breez-liquid-sdk"
import {
addEventListener,
connect,
defaultConfig,
getInfo,
listPayments,
Network,
removeEventListener,
prepareReceivePayment,
prepareSendPayment,
receivePayment,
sendPayment
} from "@breeztech/react-native-breez-liquid-sdk"
import { generateMnemonic } from "@dreson4/react-native-quick-bip39"
import { getSecureItem, setSecureItem } from "./utils/storage"
@@ -39,9 +51,11 @@ const App = () => {
React.useEffect(() => {
let listenerId = null
let bolt11Invoice = null
const asyncFn = async () => {
try {
// Get the mnemonic
let mnemonic = await getSecureItem(MNEMONIC_STORE)
if (mnemonic == null) {
@@ -49,17 +63,50 @@ const App = () => {
setSecureItem(MNEMONIC_STORE, mnemonic)
}
const config = await defaultConfig(Network.TESTNET)
// Connect using the config
const config = await defaultConfig(Network.MAINNET)
addLine("defaultConfig", JSON.stringify(config))
await connect({ config, mnemonic })
addLine("connect", null)
// Get wallet info
let getInfoRes = await getInfo()
addLine("getInfo", JSON.stringify(getInfoRes))
// Historical payments list
let payments = listPayments()
// Register for events
listenerId = await addEventListener(eventHandler)
addLine("addEventListener", listenerId)
let walletInfo = await getInfo()
addLine("getInfo", JSON.stringify(walletInfo))
/* Receive lightning payment */
let prepareReceiveRes = await prepareReceivePayment({ payerAmountSat: 1000 })
addLine("prepareReceivePayment", JSON.stringify(prepareReceiveRes))
// Get the fees required for this payment
addLine("Payment fees", `${prepareReceiveRes.feesSat}`)
let receivePaymentRes = await receivePayment(prepareReceiveRes)
addLine("receivePayment", JSON.stringify(receivePaymentRes))
// Wait for payer to pay.... once successfully paid an event of `paymentSucceeded` will be emitted.
addLine("Bolt11 invoice", `${receivePaymentRes.invoice}`)
/* Send lightning payment */
// Set the `bolt11Invoice` to enable sending in the example app
if (bolt11Invoice) {
let prepareSendRes = await prepareSendPayment({ invoice: bolt11Invoice })
addLine("prepareSendPayment", JSON.stringify(prepareSendRes))
// Get the fees required for this payment
addLine("Payment fees", `${prepareSendRes.feesSat}`)
let sendPaymentRes = await sendPayment(prepareSendRes)
addLine("sendPayment", JSON.stringify(sendPaymentRes))
// Once successfully paid an event of `paymentSucceeded` will be emitted.
addLine("Payment", `${sendPaymentRes.payment}`)
}
} catch (e) {
addLine("error", e.toString())
console.log(`Error: ${JSON.stringify(e)}`)

View File

@@ -13,7 +13,7 @@
"rebuild": "rm -rf node_modules && yarn && yarn pods"
},
"dependencies": {
"@breeztech/react-native-breez-liquid-sdk": "0.0.1",
"@breeztech/react-native-breez-liquid-sdk": "0.1.0-rc1",
"@dreson4/react-native-quick-bip39": "^0.0.5",
"react": "18.1.0",
"react-native": "0.70.15",

View File

@@ -964,8 +964,10 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
"@breeztech/react-native-breez-liquid-sdk@../":
version "0.0.1"
"@breeztech/react-native-breez-liquid-sdk@0.1.0-rc1":
version "0.1.0-rc1"
resolved "https://registry.yarnpkg.com/@breeztech/react-native-breez-liquid-sdk/-/react-native-breez-liquid-sdk-0.1.0-rc1.tgz#7eb522f045acf3a448fb30f79f1bc93851ec6ed9"
integrity sha512-uAmQSUtIaCIywDmZ6FXfuHtpn+gYj8FmYOBPIwvAkBaIOJKl3+gz9Vs/RWbFu/dVF9uL98JdKqhNngKqI3mrJQ==
"@cnakazawa/watch@^1.0.3":
version "1.0.4"