Skip to content

Commit

Permalink
chore: improve code by always filtering previously found txs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir committed Feb 19, 2024
1 parent 025b80e commit d957a87
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 31 deletions.
28 changes: 2 additions & 26 deletions packages/core/src/controllers/TransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,14 @@ export const TransactionsController = {
transactions: Transaction[] = []
) {
const grouped = transactionsMap
console.log('GROUPING TXS', transactions, transactionsMap)
transactions.forEach(transaction => {
const year = new Date(transaction.metadata.minedAt).getFullYear()
const month = new Date(transaction.metadata.minedAt).getMonth()
const yearTransactions = grouped[year] ?? {}
const monthTransactions = yearTransactions[month] ?? []

const repeated = monthTransactions.find(
tx => tx.id === transaction.id && tx.metadata.status === transaction.metadata.status
)
if (repeated) {
return
}

let newMonthTransactions = [...monthTransactions]

const coinbaseTxToUpdate = monthTransactions.find(
tx =>
tx.id === transaction.id &&
tx.metadata.status === 'ONRAMP_TRANSACTION_STATUS_IN_PROGRESS' &&
(transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_SUCCESS' ||
transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_FAILED')
)

console.log('COINBASE TX TO UPDATE', coinbaseTxToUpdate, transaction)

if (coinbaseTxToUpdate) {
newMonthTransactions = monthTransactions.filter(
tx => tx.metadata.minedAt !== transaction.metadata.minedAt
)
}
// If there's a transaction with the same id, remove the old one
const newMonthTransactions = monthTransactions.filter(tx => tx.id !== transaction.id)

grouped[year] = {
...yearTransactions,
Expand All @@ -133,7 +110,6 @@ export const TransactionsController = {
)
}
})
console.log('GROUPED', grouped)

return grouped
},
Expand Down
5 changes: 0 additions & 5 deletions packages/scaffold/src/views/w3m-onramp-activity-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export class W3mOnRampActivityView extends LitElement {
clearTimeout(this.refetchTimeout)
},
TransactionsController.subscribe(val => {
console.log('Coinbase Transactions updated', val.coinbaseTransactions)
this.coinbaseTransactions = { ...val.coinbaseTransactions }
})
]
Expand Down Expand Up @@ -160,13 +159,10 @@ export class W3mOnRampActivityView extends LitElement {
const today = new Date()
const currentMonthTxs = this.coinbaseTransactions[today.getFullYear()]?.[today.getMonth()] || []

console.log('Activity - Current month transactions', currentMonthTxs)
const loadingTransactions = currentMonthTxs.filter(
transaction => transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_IN_PROGRESS'
)

console.log('Activity - Loading Transactions', loadingTransactions)

if (loadingTransactions.length === 0) {
clearTimeout(this.refetchTimeout)

Expand All @@ -176,7 +172,6 @@ export class W3mOnRampActivityView extends LitElement {
// Wait 2 seconds before refetching
this.refetchTimeout = setTimeout(async () => {
const address = AccountController.state.address
console.log('Refetching transactions')
await TransactionsController.fetchTransactions(address, 'coinbase')
this.refetchLoadingTransactions()
}, 3000)
Expand Down

0 comments on commit d957a87

Please sign in to comment.