Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { useBreez } from "@app/hooks"
import { useApolloClient } from "@apollo/client"
import { useI18nContext } from "@app/i18n/i18n-react"
import { useNavigation } from "@react-navigation/native"
import { useSetDefaultAccountModalQuery } from "@app/graphql/generated"
import {
useAccountUpdateDefaultWalletIdMutation,
useSetDefaultAccountModalQuery,
} from "@app/graphql/generated"
import { usePersistentStateContext } from "@app/store/persistent-state"

// utils
Expand All @@ -34,17 +37,23 @@ export const SetDefaultAccountModal = ({ isVisible, toggleModal }: Props) => {
const { LL } = useI18nContext()
const { btcWallet } = useBreez()
const { updateState } = usePersistentStateContext()
const [updateDefaultWalletId] = useAccountUpdateDefaultWalletIdMutation()

const { data } = useSetDefaultAccountModalQuery({
fetchPolicy: "cache-only",
})
const usdWallet = getUsdWallet(data?.me?.defaultAccount?.wallets)

const onPressHandler = (currency: string) => {
const onPressHandler = async (currency: string) => {
let defaultWallet = usdWallet
if (currency === "BTC") {
defaultWallet = btcWallet
}
if (defaultWallet?.id) {
await updateDefaultWalletId({
variables: { input: { walletId: defaultWallet.id } },
})
}
updateState((state: any) => {
if (state)
return {
Expand Down
85 changes: 76 additions & 9 deletions app/contexts/BreezContext.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import React, { createContext, useEffect, useRef, useState } from "react"
import { WalletCurrency } from "@app/graphql/generated"
import { useUpdateExternalWalletMutation, WalletCurrency } from "@app/graphql/generated"
import { usePersistentStateContext } from "@app/store/persistent-state"
import { Alert, Platform } from "react-native"
import { v4 as uuidv4 } from "uuid"
import { initializeBreezSDK, getInfo, handleSparkMigration } from "@app/utils/breez-sdk"
import {
initializeBreezSDK,
getInfo,
handleSparkMigration,
registerLightningAddress,
getLightningAddress,
checkLightningAddressAvailable,
} from "@app/utils/breez-sdk"
import { useAppConfig } from "@app/hooks/use-app-config"
import { useAddressScreenQuery } from "@app/graphql/generated"
import { useIsAuthed } from "@app/graphql/is-authed-context"
import SparkMigrationModal from "@app/components/spark-migration-modal"

type BtcWallet = {
id: string
walletCurrency: WalletCurrency
balance: number
isExternal: boolean
}

interface BreezInterface {
Expand All @@ -25,6 +36,7 @@ export const BreezContext = createContext<BreezInterface>({
id: "",
walletCurrency: "BTC",
balance: 0,
isExternal: true,
},
})

Expand All @@ -34,11 +46,20 @@ type Props = {

export const BreezProvider = ({ children }: Props) => {
const { persistentState, updateState } = usePersistentStateContext()
const { appConfig } = useAppConfig()
const isAuthed = useIsAuthed()
const { data: meData } = useAddressScreenQuery({
fetchPolicy: "cache-first",
skip: !isAuthed,
})
const [updateExternalWallet] = useUpdateExternalWalletMutation()

const [loading, setLoading] = useState(false)
const [btcWallet, setBtcWallet] = useState<BtcWallet>({
id: "",
walletCurrency: "BTC",
balance: persistentState.breezBalance || 0,
isExternal: true,
})
const initializingRef = useRef(false)
const updatingBalanceRef = useRef(false)
Expand Down Expand Up @@ -85,6 +106,7 @@ export const BreezProvider = ({ children }: Props) => {
id: "",
walletCurrency: "BTC",
balance: 0,
isExternal: true,
})
}
}
Expand All @@ -94,17 +116,16 @@ export const BreezProvider = ({ children }: Props) => {
if (updatingBalanceRef.current) return
updatingBalanceRef.current = true
try {
const balanceSats = await getInfo()
setBtcWallet({
id: uuidv4(),
walletCurrency: WalletCurrency.Btc,
balance: balanceSats,
})
const walletInfo = await getInfo()
setBtcWallet((prev) => ({
...prev,
balance: Number(walletInfo.balanceSats),
}))
updateState((state: any) => {
if (state)
return {
...state,
breezBalance: balanceSats,
breezBalance: Number(walletInfo.balanceSats),
}
return undefined
})
Expand All @@ -113,6 +134,49 @@ export const BreezProvider = ({ children }: Props) => {
}
}

const updateExternalWalletLnurlp = async (lnurlp: string) => {
const externalWalletRes = await updateExternalWallet({
variables: { input: { lnurlp } },
})
console.log("Update External Wallet Response: ", externalWalletRes)
const walletId = externalWalletRes.data?.updateExternalWallet.walletId
if (walletId) {
setBtcWallet((prev) => ({
...prev,
id: walletId,
}))
}
}

const ensureLightningAddress = async () => {
const username = meData?.me?.username
if (!username) return

try {
const existing = await getLightningAddress()
console.log("BREEZ LIGHTNING ADDRESS: ", existing)

if (existing) {
updateExternalWalletLnurlp(existing.lnurl.bech32)
return
}

// Register with username as the Lightning address
const lightningAddress = username + uuidv4()
const res = await registerLightningAddress(
lightningAddress,
`Pay to ${username}@${appConfig.galoyInstance.lnAddressHostname}`,
)
console.log("BREEZ LIGHTNING ADDRESS RES: ", res)

if (res) {
updateExternalWalletLnurlp(res.lnurl.bech32)
}
} catch (err) {
console.warn("Failed to register Lightning address:", err)
}
}

const getBreezInfo = async () => {
if (initializingRef.current) return
initializingRef.current = true
Expand All @@ -122,6 +186,9 @@ export const BreezProvider = ({ children }: Props) => {
await updateBalance()
setLoading(false)

// Register Lightning address
await ensureLightningAddress()

// Trigger migration after Spark SDK is ready
if (!persistentState.sparkMigrationCompleted) {
await onMigrate()
Expand Down
1 change: 1 addition & 0 deletions app/graphql/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gql`
id
balance
walletCurrency
isExternal
}
}

Expand Down
10 changes: 10 additions & 0 deletions app/graphql/front-end-mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,14 @@ gql`
uploadUrl
}
}

mutation UpdateExternalWallet($input: UpdateExternalWalletInput!) {
updateExternalWallet(input: $input) {
errors {
code
message
}
walletId
}
}
`
9 changes: 9 additions & 0 deletions app/graphql/front-end-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ gql`
id
balance
walletCurrency
isExternal
}
}
}
Expand Down Expand Up @@ -83,6 +84,7 @@ gql`
id
balance
walletCurrency
isExternal
}
}
}
Expand All @@ -97,6 +99,7 @@ gql`
id
balance
walletCurrency
isExternal
}
}
}
Expand All @@ -111,6 +114,7 @@ gql`
id
balance
walletCurrency
isExternal
}
}
}
Expand All @@ -135,6 +139,7 @@ gql`
id
balance
walletCurrency
isExternal
}
}
}
Expand All @@ -150,6 +155,7 @@ gql`
id
wallets {
id
isExternal
}
}
contacts {
Expand Down Expand Up @@ -178,6 +184,7 @@ gql`
id
walletCurrency
balance
isExternal
}
}
}
Expand Down Expand Up @@ -224,6 +231,7 @@ gql`
id
balance
walletCurrency
isExternal
}
}
}
Expand All @@ -245,6 +253,7 @@ gql`
id
wallets {
id
isExternal
}
}
contacts {
Expand Down
Loading