From dd1898b399957219e689d5293cd3febd7e7a4410 Mon Sep 17 00:00:00 2001 From: tomiir Date: Mon, 19 Feb 2024 14:50:01 -0600 Subject: [PATCH 1/5] chore: add logs and potential activity fix --- .../src/controllers/TransactionsController.ts | 33 +++++++++++++------ .../views/w3m-onramp-activity-view/index.ts | 5 +++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/core/src/controllers/TransactionsController.ts b/packages/core/src/controllers/TransactionsController.ts index 1a63e33424..bd68dfb21f 100644 --- a/packages/core/src/controllers/TransactionsController.ts +++ b/packages/core/src/controllers/TransactionsController.ts @@ -93,31 +93,44 @@ export const TransactionsController = { transactionsMap: TransactionByYearMap = {}, transactions: Transaction[] = [] ) { - const grouped: TransactionByYearMap = transactionsMap - + 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] ?? [] - if ( - monthTransactions.find( - t => - (t.metadata.hash && t.metadata.hash === transaction.metadata.hash) || - t.metadata.minedAt === transaction.metadata.minedAt - ) - ) { + const repeated = monthTransactions.find( + tx => tx.metadata.hash && tx.metadata.hash === transaction.metadata.hash + ) + if (repeated) { return } + let newMonthTransactions = [...monthTransactions] + + const coinbaseTxsToUpdate = monthTransactions.filter( + tx => + (tx.metadata.minedAt === transaction.metadata.minedAt && + tx.metadata.status === 'ONRAMP_TRANSACTION_STATUS_IN_PROGRESS' && + transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_SUCCESS') || + transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_FAILED' + ) + if (coinbaseTxsToUpdate.length) { + newMonthTransactions = monthTransactions.filter( + tx => tx.metadata.minedAt !== transaction.metadata.minedAt + ) + } + grouped[year] = { ...yearTransactions, - [month]: [...monthTransactions, transaction].sort( + [month]: [...newMonthTransactions, transaction].sort( (a, b) => new Date(b.metadata.minedAt).getTime() - new Date(a.metadata.minedAt).getTime() ) } }) + console.log('GROUPED', grouped) return grouped }, diff --git a/packages/scaffold/src/views/w3m-onramp-activity-view/index.ts b/packages/scaffold/src/views/w3m-onramp-activity-view/index.ts index da214f83d0..2b1200e336 100644 --- a/packages/scaffold/src/views/w3m-onramp-activity-view/index.ts +++ b/packages/scaffold/src/views/w3m-onramp-activity-view/index.ts @@ -45,6 +45,7 @@ export class W3mOnRampActivityView extends LitElement { clearTimeout(this.refetchTimeout) }, TransactionsController.subscribe(val => { + console.log('Coinbase Transactions updated', val.coinbaseTransactions) this.coinbaseTransactions = { ...val.coinbaseTransactions } }) ] @@ -159,10 +160,13 @@ 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) @@ -172,6 +176,7 @@ 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) From 7e0b35f29f49d3c88f2f2789a4fa682564392b34 Mon Sep 17 00:00:00 2001 From: tomiir Date: Mon, 19 Feb 2024 15:17:28 -0600 Subject: [PATCH 2/5] fix: parenthesis placement --- packages/core/src/controllers/TransactionsController.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/core/src/controllers/TransactionsController.ts b/packages/core/src/controllers/TransactionsController.ts index bd68dfb21f..d67a29aa23 100644 --- a/packages/core/src/controllers/TransactionsController.ts +++ b/packages/core/src/controllers/TransactionsController.ts @@ -112,10 +112,10 @@ export const TransactionsController = { const coinbaseTxsToUpdate = monthTransactions.filter( tx => - (tx.metadata.minedAt === transaction.metadata.minedAt && - tx.metadata.status === 'ONRAMP_TRANSACTION_STATUS_IN_PROGRESS' && - transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_SUCCESS') || - transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_FAILED' + tx.metadata.minedAt === transaction.metadata.minedAt && + tx.metadata.status === 'ONRAMP_TRANSACTION_STATUS_IN_PROGRESS' && + (transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_SUCCESS' || + transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_FAILED') ) if (coinbaseTxsToUpdate.length) { newMonthTransactions = monthTransactions.filter( From 644709c799c603a8f3a671aef96da573f608ee56 Mon Sep 17 00:00:00 2001 From: tomiir Date: Mon, 19 Feb 2024 15:43:48 -0600 Subject: [PATCH 3/5] chore: add coinnbase tx to update log --- packages/core/src/controllers/TransactionsController.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/core/src/controllers/TransactionsController.ts b/packages/core/src/controllers/TransactionsController.ts index d67a29aa23..604dfb6d43 100644 --- a/packages/core/src/controllers/TransactionsController.ts +++ b/packages/core/src/controllers/TransactionsController.ts @@ -110,14 +110,17 @@ export const TransactionsController = { let newMonthTransactions = [...monthTransactions] - const coinbaseTxsToUpdate = monthTransactions.filter( + const coinbaseTxToUpdate = monthTransactions.find( tx => tx.metadata.minedAt === transaction.metadata.minedAt && tx.metadata.status === 'ONRAMP_TRANSACTION_STATUS_IN_PROGRESS' && (transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_SUCCESS' || transaction.metadata.status === 'ONRAMP_TRANSACTION_STATUS_FAILED') ) - if (coinbaseTxsToUpdate.length) { + + console.log('COINBASE TX TO UPDATE', coinbaseTxToUpdate, transaction) + + if (coinbaseTxToUpdate) { newMonthTransactions = monthTransactions.filter( tx => tx.metadata.minedAt !== transaction.metadata.minedAt ) From 025b80e79661e8bdce46635de396b5b7d163e797 Mon Sep 17 00:00:00 2001 From: tomiir Date: Mon, 19 Feb 2024 15:51:37 -0600 Subject: [PATCH 4/5] chore: use tx ids --- packages/core/src/controllers/TransactionsController.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/controllers/TransactionsController.ts b/packages/core/src/controllers/TransactionsController.ts index 604dfb6d43..46304f3524 100644 --- a/packages/core/src/controllers/TransactionsController.ts +++ b/packages/core/src/controllers/TransactionsController.ts @@ -102,7 +102,7 @@ export const TransactionsController = { const monthTransactions = yearTransactions[month] ?? [] const repeated = monthTransactions.find( - tx => tx.metadata.hash && tx.metadata.hash === transaction.metadata.hash + tx => tx.id === transaction.id && tx.metadata.status === transaction.metadata.status ) if (repeated) { return @@ -112,7 +112,7 @@ export const TransactionsController = { const coinbaseTxToUpdate = monthTransactions.find( tx => - tx.metadata.minedAt === transaction.metadata.minedAt && + 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') From d957a8730dd49a9d5996fc311130a07e2c8f411d Mon Sep 17 00:00:00 2001 From: tomiir Date: Mon, 19 Feb 2024 16:31:09 -0600 Subject: [PATCH 5/5] chore: improve code by always filtering previously found txs --- .../src/controllers/TransactionsController.ts | 28 ++----------------- .../views/w3m-onramp-activity-view/index.ts | 5 ---- 2 files changed, 2 insertions(+), 31 deletions(-) diff --git a/packages/core/src/controllers/TransactionsController.ts b/packages/core/src/controllers/TransactionsController.ts index 46304f3524..808790ee37 100644 --- a/packages/core/src/controllers/TransactionsController.ts +++ b/packages/core/src/controllers/TransactionsController.ts @@ -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, @@ -133,7 +110,6 @@ export const TransactionsController = { ) } }) - console.log('GROUPED', grouped) return grouped }, diff --git a/packages/scaffold/src/views/w3m-onramp-activity-view/index.ts b/packages/scaffold/src/views/w3m-onramp-activity-view/index.ts index 2b1200e336..da214f83d0 100644 --- a/packages/scaffold/src/views/w3m-onramp-activity-view/index.ts +++ b/packages/scaffold/src/views/w3m-onramp-activity-view/index.ts @@ -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 } }) ] @@ -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) @@ -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)