diff --git a/packages/txs-core/package.json b/packages/txs-core/package.json index 0f0e8f7..b8783a6 100644 --- a/packages/txs-core/package.json +++ b/packages/txs-core/package.json @@ -1,6 +1,6 @@ { "name": "@pcnv/txs-core", - "version": "0.0.3", + "version": "0.0.6", "repository": { "type": "git", "url": "https://github.com/ConcaveFi/txs" diff --git a/packages/txs-core/src/store.ts b/packages/txs-core/src/store.ts index b3181f9..68ff301 100644 --- a/packages/txs-core/src/store.ts +++ b/packages/txs-core/src/store.ts @@ -59,9 +59,17 @@ export type TransactionsStoreEvents = | { type: 'removed'; payload: StoredTransaction } | { type: 'cleared' } +type StoreContext = { + user: Address + chainId: number + provider: BaseProvider +} + export const createTransactionsStore = (_config?: Partial) => { const config = { ...defaultConfig, ..._config } + let ctx: StoreContext | undefined = undefined + const txsStorage = createStorage(config.localStorageKey) let transactions = txsStorage.get() @@ -84,9 +92,9 @@ export const createTransactionsStore = (_config?: Partial( newTx: NewTransaction, - user: Address, - chainId: number, - provider: BaseProvider, + user: Address = ctx?.user!, + chainId: number = ctx?.chainId!, + provider: BaseProvider = ctx?.provider!, ) { const tx = parseNewTransaction(newTx, provider._network.chainId, config) updateUserTransactions(user, chainId, (txs) => @@ -96,17 +104,24 @@ export const createTransactionsStore = (_config?: Partial( + user: Address = ctx?.user!, + chainId: number = ctx?.chainId!, + ) { + return (transactions[user]?.[chainId] as StoredTransaction[]) || stableNoTransactions } - function clearTransactions(user: Address, chainId: number) { + function clearTransactions(user: Address = ctx?.user!, chainId: number = ctx?.chainId!) { updateUserTransactions(user, chainId, () => []) listeners.emit('cleared') } - function removeTransaction(user: Address, chainId: number, hash: StoredTransaction['hash']) { - const tx = transactionsOf(user, chainId)?.find((tx) => tx.hash === hash) + function removeTransaction( + hash: StoredTransaction['hash'], + user: Address = ctx?.user!, + chainId: number = ctx?.chainId!, + ) { + const tx = getTransactions(user, chainId)?.find((tx) => tx.hash === hash) if (!tx) return updateUserTransactions(user, chainId, (txs) => txs.filter((tx) => tx.hash !== hash)) listeners.emit('removed', tx) @@ -152,6 +167,7 @@ export const createTransactionsStore = (_config?: Partial tx.status === 'pending') Promise.all(pendingTxs.map((tx) => waitForTransaction(provider, user, chainId, tx))) @@ -159,6 +175,7 @@ export const createTransactionsStore = (_config?: Partial[] }, ): Selected } + export const useRecentTransactions = < Meta extends StoredTransaction['meta'], Selector = StoredTransaction[], @@ -21,18 +21,10 @@ export const useRecentTransactions = < { initialTransactions = [] }: { initialTransactions?: StoredTransaction[] } = {}, ) => { const store = useTransactionsStore() - const { address } = useAccount() - const { chain } = useNetwork() const transactions = useSyncExternalStoreWithSelector( store.onTransactionsChange, - useCallback( - () => - address && chain?.id - ? (store.transactionsOf(address, chain.id) as StoredTransaction[]) - : [], - [address, chain?.id, store, selector], - ), + store.getTransactions, useCallback(() => initialTransactions, [initialTransactions]), selector, ) @@ -47,40 +39,17 @@ export const useAddRecentTransaction = < Meta extends NewTransaction['meta'] = DefaultToastTransactionMeta, >(): ((transaction: NewTransaction) => void) => { const store = useTransactionsStore() - const { address } = useAccount() - const { chain } = useNetwork() - const provider = useProvider() - - return useCallback( - (transaction) => { - if (address && chain) store.addTransaction(transaction, address, chain.id, provider) - }, - [address, chain, provider, store], - ) + return useCallback((tx: NewTransaction) => store.addTransaction(tx), [store]) } export const useClearRecentTransactions = () => { const store = useTransactionsStore() - const { address } = useAccount() - const { chain } = useNetwork() - const provider = useProvider() - - return useCallback(() => { - if (address && chain) store.clearTransactions(address, chain.id) - }, [address, chain, provider, store]) + return store.clearTransactions() } export const useRemoveRecentTransaction = () => { const store = useTransactionsStore() - const { address } = useAccount() - const { chain } = useNetwork() - - return useCallback( - (hash: StoredTransaction['hash']) => { - if (address && chain) store.removeTransaction(address, chain.id, hash) - }, - [address, chain, store], - ) + return useCallback((hash: StoredTransaction['hash']) => store.removeTransaction(hash), [store]) } export const useTransactionsStoreEvent = <