-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef7e159
commit aa5a12d
Showing
3 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { useFedimintWallet, useOpenWallet } from '.' | ||
import { | ||
type LnPayState, | ||
type OutgoingLightningPayment, | ||
} from '@fedimint/core-web' | ||
|
||
export const useSendLightning = () => { | ||
const wallet = useFedimintWallet() | ||
const { walletStatus } = useOpenWallet() | ||
const [payment, setPayment] = useState<OutgoingLightningPayment>() | ||
const [paymentState, setPaymentState] = useState<LnPayState>() | ||
const [error, setError] = useState<string>() | ||
|
||
const payInvoice = useCallback( | ||
async (bolt11: string) => { | ||
if (walletStatus !== 'open') throw new Error('Wallet is not open') | ||
const response = await wallet.lightning.payInvoice(bolt11) | ||
setPayment(response) | ||
return response | ||
}, | ||
[wallet, walletStatus], | ||
) | ||
|
||
useEffect(() => { | ||
if (walletStatus !== 'open' || !payment) return | ||
const unsubscribe = wallet.lightning.subscribeLnPay( | ||
// @ts-ignore | ||
payment.payment_type.lightning, | ||
(state) => { | ||
setPaymentState(state) | ||
}, | ||
(error) => { | ||
setError(error) | ||
}, | ||
) | ||
|
||
return () => { | ||
unsubscribe() | ||
} | ||
}, [walletStatus, payment]) | ||
|
||
return { | ||
payInvoice, | ||
payment, | ||
paymentStatus: paymentState, | ||
paymentError: error, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters