Skip to content

Commit

Permalink
Remove reliance on get_balance
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Jan 1, 2024
1 parent 6d28b42 commit f1bf5dd
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 43 deletions.
3 changes: 1 addition & 2 deletions components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ function WalletSummary ({ me }) {
if (me.privates?.hideWalletBalance) {
return <HiddenWalletSummary abbreviate fixedWidth />
}
const sats = me.privates?.sats + me.weblnSats
return `${abbrNum(sats)}`
return `${abbrNum(me.privates?.sats)}`
}

function Back () {
Expand Down
4 changes: 1 addition & 3 deletions components/hidden-wallet-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ export default function HiddenWalletSummary ({ abbreviate, fixedWidth }) {
setWidth(ref.current?.offsetWidth)
}, [])

const sats = me.privates?.sats + me.weblnSats

return (
<span
ref={ref} style={{ width: fixedWidth ? width : undefined }}
className='d-inline-block text-monospace' align='right' onPointerEnter={() => setHover(true)} onPointerLeave={() => setHover(false)}
>
{hover ? (abbreviate ? abbrNum(sats) : numWithUnits(sats, { abbreviate: false, format: true })) : '******'}
{hover ? (abbreviate ? abbrNum(me.privates?.sats) : numWithUnits(me.privates?.sats, { abbreviate: false, format: true })) : '******'}
</span>
)
}
4 changes: 0 additions & 4 deletions components/me.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export const MeContext = React.createContext({
export function MeProvider ({ me, children }) {
const { data } = useQuery(ME, SSR ? {} : { pollInterval: 1000, nextFetchPolicy: 'cache-and-network' })

const futureMe = data?.me || me
// weblnSats is initially undefined on page load for some reason
if (futureMe.weblnSats === undefined) futureMe.weblnSats = 0

return (
<MeContext.Provider value={data?.me || me}>
{children}
Expand Down
25 changes: 2 additions & 23 deletions components/webln.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createContext, createRef, useContext, useEffect, useImperativeHandle, useState } from 'react'
import { createContext, useContext, useEffect, useState } from 'react'

const WebLNContext = createContext({})
export const WebLNContextRef = createRef()

const fetchWebLNProvider = async () => {
// sync provider from local storage
Expand All @@ -18,7 +17,6 @@ const fetchWebLNProvider = async () => {
export function WebLNProvider ({ children }) {
const [provider, setProvider] = useState(null)
const [info, setInfo] = useState(null)
const [balance, setBalance] = useState(0)

const initProvider = async (provider) => {
const WebLNProviders = await import('@getalby/bitcoin-connect').then((mod) => mod.WebLNProviders)
Expand All @@ -33,14 +31,11 @@ export function WebLNProvider ({ children }) {
setProvider(provider)
const info = await provider.getInfo()
setInfo(o => ({ ...o, ...info }))
const { balance } = await provider.getBalance()
setBalance(balance)
}

const clearProvider = () => {
setProvider(null)
setInfo(null)
setBalance(0)
}

useEffect(() => {
Expand Down Expand Up @@ -80,23 +75,7 @@ export function WebLNProvider ({ children }) {
}
}, [])

// poll balance
// TODO is there a better way?
useEffect(() => {
if (!provider) return
// TODO check rate limiting of services - is every 15 seconds too often? (it probably is)
const BALANCE_POLL = 15000 // 15 seconds
const interval = setInterval(() => {
provider?.getBalance().then(({ balance }) => setBalance(balance)).catch(console.error)
}, BALANCE_POLL)
return () => clearInterval(interval)
}, [provider])

const value = { provider, setProvider, info, balance }

// required to resolve fields marked with @client using values from WebLN context
useImperativeHandle(WebLNContextRef, () => value)

const value = { provider, setProvider, info }
return (
<WebLNContext.Provider value={value}>
{children}
Expand Down
1 change: 0 additions & 1 deletion fragments/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const ME = gql`
id
name
bioId
weblnSats @client
privates {
autoDropBolt11s
diagnostics
Expand Down
9 changes: 0 additions & 9 deletions lib/apollo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client'
import { decodeCursor, LIMIT } from './cursor'
import { SSR } from './constants'
import { WebLNContextRef } from '../components/webln'

function isFirstPage (cursor, existingThings, limit = LIMIT) {
if (cursor) {
Expand Down Expand Up @@ -189,14 +188,6 @@ function getClient (uri) {
}
}
}),
resolvers: {
User: {
weblnSats: (user, args, { cache }) => {
const balance = WebLNContextRef.current?.balance
return balance
}
}
},
assumeImmutableResults: true,
defaultOptions: {
watchQuery: {
Expand Down
2 changes: 1 addition & 1 deletion pages/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function YouHaveSats () {
<span className='text-monospace'>{me && (
me.privates?.hideWalletBalance
? <HiddenWalletSummary />
: numWithUnits(me.privates?.sats + me.weblnSats, { abbreviate: false, format: true })
: numWithUnits(me.privates?.sats, { abbreviate: false, format: true })
)}
</span>
</h2>
Expand Down

0 comments on commit f1bf5dd

Please sign in to comment.