Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added filter transactions by chain #1834

Merged
merged 18 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/common/src/utils/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Transaction {
export interface TransactionMetadata {
operationType: string
hash: string
chain: `${string}:${string}`
minedAt: string
sentFrom: string
sentTo: string
Expand Down
25 changes: 23 additions & 2 deletions packages/core/src/controllers/TransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { proxy, subscribe as sub } from 'valtio/vanilla'
import { OptionsController } from './OptionsController.js'
import { EventsController } from './EventsController.js'
import { SnackController } from './SnackController.js'
import { NetworkController } from './NetworkController.js'
import type { CaipNetworkId } from '../utils/TypeUtil.js'
import { BlockchainApiController } from './BlockchainApiController.js'

// -- Types --------------------------------------------- //
Expand All @@ -13,6 +15,7 @@ export interface TransactionsControllerState {
transactions: Transaction[]
coinbaseTransactions: TransactionByYearMap
transactionsByYear: TransactionByYearMap
lastNetworkInView: CaipNetworkId | undefined
tomiir marked this conversation as resolved.
Show resolved Hide resolved
loading: boolean
empty: boolean
next: string | undefined
Expand All @@ -23,6 +26,7 @@ const state = proxy<TransactionsControllerState>({
transactions: [],
coinbaseTransactions: {},
transactionsByYear: {},
lastNetworkInView: undefined,
loading: false,
empty: false,
next: undefined
Expand All @@ -36,6 +40,10 @@ export const TransactionsController = {
return sub(state, () => callback(state))
},

setLastNetworkInView(lastNetworkInView: TransactionsControllerState['lastNetworkInView']) {
state.lastNetworkInView = lastNetworkInView
},

async fetchTransactions(accountAddress?: string, onramp?: 'coinbase') {
const { projectId } = OptionsController.state

Expand All @@ -54,7 +62,8 @@ export const TransactionsController = {
})

const nonSpamTransactions = this.filterSpamTransactions(response.data)
const filteredTransactions = [...state.transactions, ...nonSpamTransactions]
const sameChainTransactions = this.filterByConnectedChain(nonSpamTransactions)
const filteredTransactions = [...state.transactions, ...sameChainTransactions]

state.loading = false

Expand All @@ -67,7 +76,7 @@ export const TransactionsController = {
state.transactions = filteredTransactions
state.transactionsByYear = this.groupTransactionsByYearAndMonth(
state.transactionsByYear,
nonSpamTransactions
sameChainTransactions
)
}

Expand All @@ -86,6 +95,8 @@ export const TransactionsController = {
SnackController.showError('Failed to fetch transactions')
state.loading = false
state.empty = true

throw error
}
},

Expand Down Expand Up @@ -124,9 +135,19 @@ export const TransactionsController = {
})
},

filterByConnectedChain(transactions: Transaction[]) {
const chainId = NetworkController.state.caipNetwork?.id
const filteredTransactions = transactions.filter(
transaction => transaction.metadata.chain === chainId
)

return filteredTransactions
},
Comment on lines +145 to +152
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we were requesting the filtered txs directly from blockchain api 🤔

wouldn't this mess up with pagination?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this mess up with pagination?

Mmm I'm not sure and I'm not able to test this 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@geekbrother any way we can do the filterin directly on blockchain API side? I think there's an edge case to this where if we filter txs and don't have enough to fill the ui, we will never request more txs even if you have them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can filter it on the blockchain-api side and even request the filtered data from the provider.
I created an issue reown-com/blockchain-api#540 to track it and ping you when it's ready.


resetTransactions() {
state.transactions = []
state.transactionsByYear = {}
state.lastNetworkInView = undefined
state.loading = false
state.empty = false
state.next = undefined
Expand Down
17 changes: 16 additions & 1 deletion packages/scaffold/src/views/w3m-transactions-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Transaction, TransactionImage } from '@web3modal/common'
import {
AccountController,
EventsController,
NetworkController,
OptionsController,
TransactionsController
} from '@web3modal/core'
Expand Down Expand Up @@ -50,6 +51,9 @@ export class W3mTransactionsView extends LitElement {
}
}
}),
NetworkController.subscribeKey('caipNetwork', () => {
this.updateTransactionView()
}),
TransactionsController.subscribe(val => {
this.transactionsByYear = val.transactionsByYear
this.loading = val.loading
Expand All @@ -61,7 +65,7 @@ export class W3mTransactionsView extends LitElement {
}

public override firstUpdated() {
TransactionsController.fetchTransactions(this.address)
this.updateTransactionView()
this.createPaginationObserver()
}

Expand All @@ -85,6 +89,17 @@ export class W3mTransactionsView extends LitElement {
}

// -- Private ------------------------------------------- //
private updateTransactionView() {
const currentNetwork = NetworkController.state.caipNetwork?.id
const lastNetworkInView = TransactionsController.state.lastNetworkInView

if (lastNetworkInView !== currentNetwork) {
TransactionsController.resetTransactions()
TransactionsController.fetchTransactions(this.address)
}
TransactionsController.setLastNetworkInView(currentNetwork)
}

private templateTransactionsByYear() {
const sortedYearKeys = Object.keys(this.transactionsByYear).sort().reverse()

Expand Down
Loading