-
Notifications
You must be signed in to change notification settings - Fork 689
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: realtime balance fetcher for recent transactions (#1717)
* feat: realtime balance * chore: tweak changeset * chore: add <ConnectButton /> to changeset * chore: add import statement on changeset * chore: tweak changeset * chore: add 'useRefetchBalance' import from '@rainbow-me/rainbowkit' * chore: update docs, tweak changeset and tweak example dapp * chore: make useRealtimeBalance compatible with TransactionStoreContext * chore: tweak docs and changesets + do small update chore: remove console.log chore: remove console.log * chore: format * chore: refetch instead of context store for balance refreshment * chore: tweak changeset chore: revert index.tx change * chore: tweak balanceData naming to balance * chore: adopted includeBalance naming in useProfile * chore: changeset --------- Co-authored-by: Magomed Khamidov <[email protected]> Co-authored-by: Daniel Sinclair <[email protected]>
- Loading branch information
1 parent
a6b8dd4
commit 8841891
Showing
7 changed files
with
122 additions
and
30 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,26 @@ | ||
--- | ||
"@rainbow-me/rainbowkit": patch | ||
--- | ||
|
||
Added real-time balance fetching based on the [Recent Transaction](https://www.rainbowkit.com/docs/recent-transactions) API. As a transaction is confirmed on-chain, the user's gas balance will be updated to reflect the transaction. | ||
|
||
```tsx | ||
import { useAddRecentTransaction } from '@rainbow-me/rainbowkit'; | ||
|
||
export default () => { | ||
const addRecentTransaction = useAddRecentTransaction(); | ||
|
||
return ( | ||
<button | ||
onClick={() => { | ||
addRecentTransaction({ | ||
hash: '0x...', | ||
description: '...', | ||
}); | ||
}} | ||
> | ||
Add recent transaction | ||
</button> | ||
); | ||
}; | ||
``` |
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
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,19 @@ | ||
import { Address } from 'viem'; | ||
import { useBalance } from 'wagmi'; | ||
import { useMainnetEnsAvatar } from './useMainnetEnsAvatar'; | ||
import { useMainnetEnsName } from './useMainnetEnsName'; | ||
|
||
interface UseProfileParameters { | ||
address?: Address; | ||
includeBalance?: boolean; | ||
} | ||
|
||
export function useProfile({ address, includeBalance }: UseProfileParameters) { | ||
const ensName = useMainnetEnsName(address); | ||
const ensAvatar = useMainnetEnsAvatar(ensName); | ||
const { data: balance } = useBalance({ | ||
address: includeBalance ? address : undefined, | ||
}); | ||
|
||
return { ensName, ensAvatar, balance }; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import type { Address, PublicClient } from 'viem'; | ||
import type { Address, PublicClient, TransactionReceipt } from 'viem'; | ||
|
||
const storageKey = 'rk-transactions'; | ||
|
||
|
@@ -67,6 +67,9 @@ export function createTransactionStore({ | |
|
||
let provider = initialProvider; | ||
const listeners: Set<() => void> = new Set(); | ||
const transactionListeners: Set< | ||
(txStatus: TransactionReceipt['status']) => void | ||
> = new Set(); | ||
const transactionRequestCache: Map<string, Promise<void>> = new Map(); | ||
|
||
function setProvider(newProvider: PublicClient): void { | ||
|
@@ -153,6 +156,8 @@ export function createTransactionStore({ | |
// @ts-ignore - types changed with [email protected] | ||
status === 0 || status === 'reverted' ? 'failed' : 'confirmed', | ||
); | ||
|
||
notifyTransactionListeners(status); | ||
}) | ||
.catch(() => { | ||
// If a transaction is not found or cancelled | ||
|
@@ -207,6 +212,14 @@ export function createTransactionStore({ | |
} | ||
} | ||
|
||
function notifyTransactionListeners( | ||
txStatus: TransactionReceipt['status'], | ||
): void { | ||
for (const transactionListener of transactionListeners) { | ||
transactionListener(txStatus); | ||
} | ||
} | ||
|
||
function onChange(fn: () => void): () => void { | ||
listeners.add(fn); | ||
|
||
|
@@ -215,10 +228,21 @@ export function createTransactionStore({ | |
}; | ||
} | ||
|
||
function onTransactionStatus( | ||
fn: (txStatus: TransactionReceipt['status']) => void, | ||
): () => void { | ||
transactionListeners.add(fn); | ||
|
||
return () => { | ||
transactionListeners.delete(fn); | ||
}; | ||
} | ||
|
||
return { | ||
addTransaction, | ||
clearTransactions, | ||
getTransactions, | ||
onTransactionStatus, | ||
onChange, | ||
setProvider, | ||
waitForPendingTransactions, | ||
|