-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathTransactionsController.ts
More file actions
142 lines (115 loc) · 4.26 KB
/
TransactionsController.ts
File metadata and controls
142 lines (115 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import type { Transaction } from '@reown/appkit-common-react-native';
import { proxy, subscribe as sub } from 'valtio';
import { OptionsController } from './OptionsController';
import { EventsController } from './EventsController';
import { SnackController } from './SnackController';
import { NetworkController } from './NetworkController';
import { BlockchainApiController } from './BlockchainApiController';
import { AccountController } from './AccountController';
// -- Types --------------------------------------------- //
type TransactionByMonthMap = Record<string, Transaction[]>;
type TransactionByYearMap = Record<string, TransactionByMonthMap>;
export interface TransactionsControllerState {
transactions: Transaction[];
loading: boolean;
empty: boolean;
next: string | undefined;
}
// -- State --------------------------------------------- //
const state = proxy<TransactionsControllerState>({
transactions: [],
loading: false,
empty: false,
next: undefined
});
// -- Controller ---------------------------------------- //
export const TransactionsController = {
state,
subscribe(callback: (newState: TransactionsControllerState) => void) {
return sub(state, () => callback(state));
},
async fetchTransactions(accountAddress?: string, reset?: boolean) {
const { projectId } = OptionsController.state;
if (!projectId || !accountAddress) {
throw new Error("Transactions can't be fetched without a projectId and an accountAddress");
}
state.loading = true;
if (reset) {
state.next = undefined;
}
try {
const response = await BlockchainApiController.fetchTransactions({
account: accountAddress,
projectId,
cursor: state.next
});
const nonSpamTransactions = this.filterSpamTransactions(response?.data ?? []);
let filteredTransactions = [...state.transactions, ...nonSpamTransactions];
if (reset) {
filteredTransactions = nonSpamTransactions;
}
state.loading = false;
state.transactions = filteredTransactions;
state.empty = nonSpamTransactions.length === 0;
state.next = response?.next ? response.next : undefined;
} catch (error) {
EventsController.sendEvent({
type: 'track',
event: 'ERROR_FETCH_TRANSACTIONS',
properties: {
address: accountAddress,
projectId,
cursor: state.next,
isSmartAccount: AccountController.state.preferredAccountType === 'smartAccount'
}
});
SnackController.showError('Failed to fetch transactions');
state.loading = false;
state.empty = true;
state.next = undefined;
}
},
getTransactionsByYearAndMonth(transactions: Transaction[]) {
const grouped: TransactionByYearMap = {};
let filteredTransactions = this.filterByConnectedChain(transactions);
filteredTransactions.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 there's a transaction with the same id, remove the old one
const newMonthTransactions = monthTransactions.filter(tx => tx.id !== transaction.id);
grouped[year] = {
...yearTransactions,
[month]: [...newMonthTransactions, transaction].sort(
(a, b) => new Date(b.metadata.minedAt).getTime() - new Date(a.metadata.minedAt).getTime()
)
};
});
return grouped;
},
filterSpamTransactions(transactions: Transaction[]) {
return transactions.filter(transaction => {
const isAllSpam = transaction.transfers.every(
transfer => transfer.nft_info?.flags.is_spam === true
);
return !isAllSpam;
});
},
filterByConnectedChain(transactions: Transaction[]) {
const chainId = NetworkController.state.caipNetwork?.id;
const filteredTransactions = transactions.filter(
transaction => transaction.metadata.chain === chainId
);
return filteredTransactions;
},
clearCursor() {
state.next = undefined;
},
resetTransactions() {
state.transactions = [];
state.loading = false;
state.empty = false;
state.next = undefined;
}
};