-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hooks for spend/receive ecash (#103)
- Loading branch information
1 parent
a3b69ff
commit e98a1b8
Showing
5 changed files
with
185 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@fedimint/react': patch | ||
--- | ||
|
||
Added useSpendEcash and useReceiveEcash hooks |
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,45 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { useFedimintWallet, useOpenWallet } from '.' | ||
|
||
export const useReceiveEcash = () => { | ||
const wallet = useFedimintWallet() | ||
const { walletStatus } = useOpenWallet() | ||
|
||
const [operationId, setOperationId] = useState<string>() | ||
const [notes, setNotes] = useState<string>() | ||
const [state, setState] = useState<any>() | ||
|
||
useEffect(() => { | ||
if (!operationId) return | ||
|
||
const unsubscribe = wallet.mint.subscribeReissueExternalNotes( | ||
operationId, | ||
(_state) => (_state ? setState(_state) : setState(undefined)), | ||
(error) => { | ||
console.error('ECASH SPEND STATE ERROR', error) | ||
}, | ||
) | ||
|
||
return () => { | ||
unsubscribe() | ||
} | ||
}, [operationId]) | ||
|
||
const redeemEcash = useCallback( | ||
async (notes: string) => { | ||
if (walletStatus !== 'open') throw new Error('Wallet is not open') | ||
const response = await wallet.mint.redeemEcash(notes) | ||
console.error('REEDEEEM', response) | ||
// setOperationId(response.operation_id) | ||
// setNotes(response.notes) | ||
return response | ||
}, | ||
[wallet], | ||
) | ||
|
||
return { | ||
redeemEcash, | ||
notes, | ||
state, | ||
} | ||
} |
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,47 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { useFedimintWallet, useOpenWallet } from '.' | ||
|
||
export const useSpendEcash = () => { | ||
const wallet = useFedimintWallet() | ||
const { walletStatus } = useOpenWallet() | ||
|
||
const [operationId, setOperationId] = useState<string>() | ||
const [notes, setNotes] = useState<string>() | ||
const [state, setState] = useState<any>() | ||
|
||
useEffect(() => { | ||
if (!operationId) return | ||
|
||
const unsubscribe = wallet.mint.subscribeSpendNotes( | ||
operationId, | ||
(_state) => (_state ? setState(_state) : setState(undefined)), | ||
(error) => { | ||
console.error('ECASH SPEND STATE ERROR', error) | ||
}, | ||
) | ||
|
||
return () => { | ||
unsubscribe() | ||
} | ||
}, [operationId]) | ||
|
||
const spendEcash = useCallback( | ||
async (amountSats: number, reclaimAfter?: number) => { | ||
if (walletStatus !== 'open') throw new Error('Wallet is not open') | ||
const response = await wallet.mint.spendNotes( | ||
amountSats * 1000, | ||
reclaimAfter, | ||
) | ||
setOperationId(response.operation_id) | ||
setNotes(response.notes) | ||
return response.notes | ||
}, | ||
[wallet], | ||
) | ||
|
||
return { | ||
spendEcash, | ||
notes, | ||
state, | ||
} | ||
} |
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