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 all 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
2 changes: 1 addition & 1 deletion packages/common/src/utils/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export interface Transaction {
export interface TransactionMetadata {
operationType: string
hash: string
chain: `${string}:${string}`
minedAt: string
sentFrom: string
sentTo: string
status: TransactionStatus | CoinbaseTransactionStatus
nonce: number
chain?: string
}

export interface TransactionTransfer {
Expand Down
23 changes: 21 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'
import { AccountController } from './AccountController.js'
import { W3mFrameRpcConstants } from '@web3modal/wallet'
Expand All @@ -15,6 +17,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 @@ -25,6 +28,7 @@ const state = proxy<TransactionsControllerState>({
transactions: [],
coinbaseTransactions: {},
transactionsByYear: {},
lastNetworkInView: undefined,
loading: false,
empty: false,
next: undefined
Expand All @@ -38,6 +42,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 @@ -58,7 +66,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 @@ -71,7 +80,7 @@ export const TransactionsController = {
state.transactions = filteredTransactions
state.transactionsByYear = this.groupTransactionsByYearAndMonth(
state.transactionsByYear,
nonSpamTransactions
sameChainTransactions
)
}

Expand Down Expand Up @@ -133,13 +142,23 @@ 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.


clearCursor() {
state.next = undefined
},

resetTransactions() {
state.transactions = []
state.transactionsByYear = {}
state.lastNetworkInView = undefined
state.loading = false
state.empty = false
state.next = undefined
Expand Down
43 changes: 35 additions & 8 deletions packages/scaffold-ui/src/partials/w3m-activity-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AccountController,
ChainController,
EventsController,
NetworkController,
OptionsController,
RouterController,
TransactionsController
Expand Down Expand Up @@ -63,6 +64,9 @@ export class W3mActivityList extends LitElement {
}
}
}),
NetworkController.subscribeKey('caipNetwork', () => {
this.updateTransactionView()
}),
TransactionsController.subscribe(val => {
this.transactionsByYear = val.transactionsByYear
this.loading = val.loading
Expand All @@ -80,7 +84,8 @@ export class W3mActivityList extends LitElement {

return
}
TransactionsController.fetchTransactions(this.address)

this.updateTransactionView()
this.createPaginationObserver()
}

Expand All @@ -100,28 +105,50 @@ export class W3mActivityList 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()

return sortedYearKeys.map((year, index) => {
const isLastGroup = index === sortedYearKeys.length - 1
return sortedYearKeys.map(year => {
const yearInt = parseInt(year, 10)

const sortedMonthIndexes = new Array(12)
.fill(null)
.map((_, idx) => idx)
.map((_, idx) => {
const groupTitle = TransactionUtil.getTransactionGroupTitle(yearInt, idx)
const transactions = this.transactionsByYear[yearInt]?.[idx]

return {
groupTitle,
transactions
}
})
.filter(({ transactions }) => transactions)
.reverse()

return sortedMonthIndexes.map(month => {
const groupTitle = TransactionUtil.getTransactionGroupTitle(yearInt, month)
const transactions = this.transactionsByYear[yearInt]?.[month]
return sortedMonthIndexes.map(({ groupTitle, transactions }, index) => {
const isLastGroup = index === sortedMonthIndexes.length - 1

if (!transactions) {
return null
}

return html`
<wui-flex flexDirection="column">
<wui-flex
flexDirection="column"
class="group-container"
last-group="${isLastGroup ? 'true' : 'false'}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

why use strings?
if neded, we can do String(isLastGroup)

>
<wui-flex
alignItems="center"
flexDirection="row"
Expand Down
4 changes: 4 additions & 0 deletions packages/scaffold-ui/src/partials/w3m-activity-list/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default css`
min-height: 100%;
}
.group-container[last-group='true'] {
padding-bottom: var(--wui-spacing-m);
}
.contentContainer {
height: 280px;
}
Expand Down
Loading