Skip to content

Commit

Permalink
fix my bad codinglings
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-schrammel committed Feb 17, 2023
1 parent 410a892 commit cd7dba8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/txs-core/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
35 changes: 26 additions & 9 deletions packages/txs-core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TransactionsStoreConfig>) => {
const config = { ...defaultConfig, ..._config }

let ctx: StoreContext | undefined = undefined

const txsStorage = createStorage(config.localStorageKey)
let transactions = txsStorage.get()

Expand All @@ -84,9 +92,9 @@ export const createTransactionsStore = (_config?: Partial<TransactionsStoreConfi

function addTransaction<Meta extends NewTransaction['meta']>(
newTx: NewTransaction<Meta>,
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) =>
Expand All @@ -96,17 +104,24 @@ export const createTransactionsStore = (_config?: Partial<TransactionsStoreConfi
waitForTransaction(provider, user, chainId, tx)
}

function transactionsOf(user: Address, chainId: number) {
return transactions[user]?.[chainId] || stableNoTransactions
function getTransactions<Meta extends NewTransaction['meta']>(
user: Address = ctx?.user!,
chainId: number = ctx?.chainId!,
) {
return (transactions[user]?.[chainId] as StoredTransaction<Meta>[]) || 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)
Expand Down Expand Up @@ -152,13 +167,15 @@ export const createTransactionsStore = (_config?: Partial<TransactionsStoreConfi
}

function mount(provider: BaseProvider, user: Address, chainId: number) {
ctx = { user, chainId, provider }
if (!transactions?.[user]?.[chainId]) return
const pendingTxs = transactions[user][chainId].filter((tx) => tx.status === 'pending')
Promise.all(pendingTxs.map((tx) => waitForTransaction(provider, user, chainId, tx)))
listeners.emit('mounted', transactions[user][chainId])
}

function unmount() {
ctx = undefined
pendingTxsCache.clear()
listeners.clear()
}
Expand All @@ -178,7 +195,7 @@ export const createTransactionsStore = (_config?: Partial<TransactionsStoreConfi

return {
addTransaction,
transactionsOf,
getTransactions,
clearTransactions,
removeTransaction,
onTransactionsChange,
Expand Down
2 changes: 1 addition & 1 deletion packages/txs-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pcnv/txs-react",
"version": "0.0.5",
"version": "0.0.6",
"description": "Easy way to manage user transaction status on your dapp",
"repository": {
"type": "git",
Expand Down
41 changes: 5 additions & 36 deletions packages/txs-react/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useCallback, useEffect } from 'react'
import { useAccount, useNetwork, useProvider } from 'wagmi'
import { useTransactionsStore } from './Provider'
import type { NewTransaction, StoredTransaction, TransactionsStoreEvents } from '@pcnv/txs-core'
import { DefaultToastTransactionMeta } from './toasts/ToastsViewport'
Expand All @@ -13,6 +12,7 @@ export interface TypedUseRecentTransactions<Meta extends StoredTransaction['meta
options?: { initialTransactions?: StoredTransaction<Meta>[] },
): Selected
}

export const useRecentTransactions = <
Meta extends StoredTransaction['meta'],
Selector = StoredTransaction<Meta>[],
Expand All @@ -21,18 +21,10 @@ export const useRecentTransactions = <
{ initialTransactions = [] }: { initialTransactions?: StoredTransaction<Meta>[] } = {},
) => {
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<Meta>[])
: [],
[address, chain?.id, store, selector],
),
store.getTransactions,
useCallback(() => initialTransactions, [initialTransactions]),
selector,
)
Expand All @@ -47,40 +39,17 @@ export const useAddRecentTransaction = <
Meta extends NewTransaction['meta'] = DefaultToastTransactionMeta,
>(): ((transaction: NewTransaction<Meta>) => 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<Meta>) => 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 = <
Expand Down

0 comments on commit cd7dba8

Please sign in to comment.