nostros/frontend/Contexts/WalletContext.tsx

164 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-03-19 14:20:25 +00:00
import { getUnixTime } from 'date-fns'
import React, { useEffect, useState } from 'react'
import SInfo from 'react-native-sensitive-info'
2023-03-22 14:37:13 +00:00
import type WalletAction from '../lib/Lightning'
import LnBits, { type LnBitsConfig } from '../lib/Lightning/LnBits'
import LndHub, { type LndHubConfig } from '../lib/Lightning/LndHub'
2023-03-19 14:20:25 +00:00
export interface WalletContextProps {
2023-03-22 14:37:13 +00:00
type?: string
setType: (type: string) => void
updatedAt?: string
lndHub?: LndHubConfig | LnBitsConfig
2023-03-19 14:20:25 +00:00
balance?: number
transactions: WalletAction[]
invoices: WalletAction[]
2023-03-22 14:37:13 +00:00
updateWallet: () => Promise<void>
refreshWallet: (params?: object, type?: string) => void
2023-03-19 14:20:25 +00:00
payInvoice: (invoice: string) => Promise<boolean>
logoutWallet: () => void
}
export interface WalletContextProviderProps {
children: React.ReactNode
}
export const initialWalletContext: WalletContextProps = {
transactions: [],
invoices: [],
2023-03-22 14:37:13 +00:00
setType: () => {},
updateWallet: async () => {},
refreshWallet: () => {},
2023-03-19 14:20:25 +00:00
payInvoice: async () => false,
logoutWallet: () => {},
}
export const WalletContextProvider = ({ children }: WalletContextProviderProps): JSX.Element => {
2023-03-22 14:37:13 +00:00
const [type, setType] = React.useState<string>()
const [config, setConfig] = useState<LndHubConfig | LnBitsConfig>()
2023-03-19 14:20:25 +00:00
const [balance, setBalance] = useState<number>()
const [updatedAt, setUpdatedAt] = useState<string>()
const [transactions, setTransactions] = useState<WalletAction[]>(
initialWalletContext.transactions,
)
const [invoices, setInvoices] = useState<WalletAction[]>(initialWalletContext.invoices)
useEffect(() => {
SInfo.getItem('lndHub', {}).then((value) => {
if (value) {
2023-03-22 14:37:13 +00:00
setConfig(JSON.parse(value))
setType('lndHub')
}
})
SInfo.getItem('lnBits', {}).then((value) => {
if (value) {
setConfig(JSON.parse(value))
setType('lnBits')
2023-03-19 14:20:25 +00:00
}
})
}, [])
2023-04-03 16:50:21 +00:00
useEffect(() => {
if (config && type) updateWallet()
}, [config, type])
2023-03-22 14:37:13 +00:00
const getClient: (params?: any, clientType?: string) => LndHub | LnBits | undefined = (
params,
clientType,
2023-03-19 14:20:25 +00:00
) => {
2023-03-22 14:37:13 +00:00
const kind = clientType ?? type
let client
if (kind === 'lndHub') {
client = new LndHub(params ?? config)
} else if (kind === 'lnBits') {
client = new LnBits(params ?? config)
2023-03-19 14:20:25 +00:00
}
2023-03-22 14:37:13 +00:00
return client
}
2023-03-19 14:20:25 +00:00
2023-03-22 14:37:13 +00:00
const refreshWallet: (params?: any, clientType?: string) => void = (params, clientType) => {
setConfig(undefined)
if (clientType) {
setType(clientType)
const client = getClient(params, clientType)
if (client) {
client.refresh(params).then((response) => {
if (response?.status === 200) setConfig(response.config)
})
}
2023-03-19 14:20:25 +00:00
}
2023-03-22 14:37:13 +00:00
}
const updateWallet: () => Promise<void> = async () => {
if (!config || !type) return
2023-03-19 14:20:25 +00:00
2023-03-22 14:37:13 +00:00
const client = getClient()
if (client) {
client.getBalance().then((response) => {
if (response?.status === 200) {
2023-03-19 14:20:25 +00:00
setUpdatedAt(`${getUnixTime(new Date())}-balance`)
2023-03-22 14:37:13 +00:00
setBalance(response.balance)
SInfo.setItem(type, JSON.stringify(client.config), {})
} else if (response?.status === 401) {
refreshWallet()
2023-03-19 14:20:25 +00:00
}
2023-03-22 14:37:13 +00:00
})
client.getMovements(setTransactions, setInvoices, setUpdatedAt)
}
2023-03-19 14:20:25 +00:00
}
2023-03-22 14:37:13 +00:00
useEffect(() => {
if (config) updateWallet()
}, [config])
2023-03-19 14:20:25 +00:00
const payInvoice: (invoice: string) => Promise<boolean> = async (invoice) => {
2023-03-22 14:37:13 +00:00
if (type && config) {
const client = new LndHub(config as LndHubConfig)
const response = await client.payInvoice(invoice)
if (response?.status === 200) {
updateWallet()
return true
} else if (response?.status === 401) {
refreshWallet()
return true
2023-03-19 14:20:25 +00:00
}
}
return false
}
const logoutWallet: () => void = () => {
SInfo.deleteItem('lndHub', {})
2023-04-03 16:56:22 +00:00
SInfo.deleteItem('lnBits', {})
2023-03-22 14:37:13 +00:00
setType(undefined)
setConfig(undefined)
2023-03-19 14:20:25 +00:00
setBalance(undefined)
setUpdatedAt(undefined)
setTransactions([])
setInvoices([])
}
return (
<WalletContext.Provider
value={{
2023-03-22 14:37:13 +00:00
type,
setType,
2023-03-19 14:20:25 +00:00
updatedAt,
balance,
transactions,
invoices,
2023-03-22 14:37:13 +00:00
refreshWallet,
updateWallet,
2023-03-19 14:20:25 +00:00
payInvoice,
logoutWallet,
}}
>
{children}
</WalletContext.Provider>
)
}
export const WalletContext = React.createContext(initialWalletContext)