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 4 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
6 changes: 3 additions & 3 deletions apps/laboratory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@emotion/styled": "11.11.0",
"@sentry/browser": "7.92.0",
"@sentry/react": "7.92.0",
"@tanstack/react-query": "5.17.19",
"@tanstack/react-query": "5.18.1",
"@web3modal/ethers": "4.0.0-648b6755",
"@web3modal/ethers5": "4.0.0-648b6755",
"@web3modal/wagmi": "4.0.0-648b6755",
Expand All @@ -29,8 +29,8 @@
"react-icons": "4.12.0",
"siwe": "2.1.4",
"valtio": "1.11.2",
"viem": "2.5.0",
"wagmi": "2.5.0"
"viem": "2.7.1",
"wagmi": "2.5.5"
},
"devDependencies": {
"@mailsac/api": "1.0.5",
Expand Down
202 changes: 199 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/common/src/utils/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Transaction {
export interface TransactionMetadata {
operationType: string
hash: string
chain: `${string}:${string}`
minedAt: string
sentFrom: string
sentTo: string
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/controllers/NetworkController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { subscribeKey as subKey } from 'valtio/utils'
import { proxy, ref } from 'valtio/vanilla'
import { proxy, ref, subscribe as sub } from 'valtio/vanilla'
import type { CaipNetwork, CaipNetworkId } from '../utils/TypeUtil.js'
import { PublicStateController } from './PublicStateController.js'
import { EventsController } from './EventsController.js'
Expand Down Expand Up @@ -36,6 +36,10 @@ const state = proxy<NetworkControllerState>({
export const NetworkController = {
state,

subscribe(callback: (newState: NetworkControllerState) => void) {
return sub(state, () => callback(state))
},

subscribeKey<K extends StateKey>(key: K, callback: (value: NetworkControllerState[K]) => void) {
return subKey(state, key, callback)
},
Expand Down
22 changes: 20 additions & 2 deletions packages/core/src/controllers/TransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BlockchainApiController } from './BlockchainApiController.js'
import { OptionsController } from './OptionsController.js'
import { EventsController } from './EventsController.js'
import { SnackController } from './SnackController.js'
import { NetworkController } from './NetworkController.js'

// -- Types --------------------------------------------- //
type TransactionByMonthMap = Record<number, Transaction[]>
Expand All @@ -12,6 +13,7 @@ type TransactionByYearMap = Record<number, TransactionByMonthMap>
export interface TransactionsControllerState {
transactions: Transaction[]
transactionsByYear: TransactionByYearMap
lastNetworkInView: string | undefined
glitch-txs marked this conversation as resolved.
Show resolved Hide resolved
loading: boolean
empty: boolean
next: string | undefined
Expand All @@ -21,6 +23,7 @@ export interface TransactionsControllerState {
const state = proxy<TransactionsControllerState>({
transactions: [],
transactionsByYear: {},
lastNetworkInView: undefined,
loading: false,
empty: false,
next: undefined
Expand All @@ -34,6 +37,10 @@ export const TransactionsController = {
return sub(state, () => callback(state))
},

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

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

Expand All @@ -51,13 +58,14 @@ 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
state.transactions = filteredTransactions
state.transactionsByYear = this.groupTransactionsByYearAndMonth(
state.transactionsByYear,
nonSpamTransactions
sameChainTransactions
)
state.empty = filteredTransactions.length === 0
state.next = response.next ? response.next : undefined
Expand Down Expand Up @@ -108,9 +116,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
Loading
Loading