-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathactivity.helpers.test.ts
More file actions
192 lines (166 loc) · 6.04 KB
/
Copy pathactivity.helpers.test.ts
File metadata and controls
192 lines (166 loc) · 6.04 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { YBOLD_STAKING_ADDRESS, YBOLD_VAULT_ADDRESS } from '@pages/vaults/domain/normalizeVault'
import { BOLD_ADDRESS } from '@pages/vaults/utils/yBold'
import type { TNotification } from '@shared/types/notifications'
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
doesActivityEntryMatchSearch,
doesLocalActivityMatchFilters,
isRecentLocalActivityEntry,
toLocalActivityEntry
} from './activity.helpers'
const USER_ADDRESS = '0x1111111111111111111111111111111111111111'
const ASSET_ADDRESS = '0x2222222222222222222222222222222222222222'
const VAULT_ADDRESS = '0x3333333333333333333333333333333333333333'
const TX_HASH = '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
const FINISHED_AT = 1776902400
function createNotification(overrides: Partial<TNotification> = {}): TNotification {
return {
type: 'deposit',
address: USER_ADDRESS,
chainId: 8453,
amount: '10',
fromAddress: ASSET_ADDRESS,
fromTokenName: 'USDC',
fromAmount: '10',
toAddress: VAULT_ADDRESS,
toTokenName: 'yvUSDC',
toAmount: '9.5',
txHash: TX_HASH,
timeFinished: FINISHED_AT,
status: 'success',
...overrides
}
}
describe('portfolio activity helpers', () => {
afterEach(() => {
vi.restoreAllMocks()
vi.useRealTimers()
})
it('uses the expected vault share amount for local deposit rows', () => {
const entry = toLocalActivityEntry(createNotification())
expect(entry?.assetSymbol).toBe('USDC')
expect(entry?.assetAmount).toBe('10')
expect(entry?.shareAmount).toBe('9.5')
expect(entry?.shareAmountFormatted).toBe(9.5)
})
it('does not reuse the underlying amount as local deposit shares when shares are unknown', () => {
const entry = toLocalActivityEntry(createNotification({ toAmount: undefined }))
expect(entry?.assetAmount).toBe('10')
expect(entry?.shareAmount).toBe('')
expect(entry?.shareAmountFormatted).toBeNull()
})
it('uses the input amount for legacy local BOLD to ysyBOLD zap rows without a share amount', () => {
const entry = toLocalActivityEntry(
createNotification({
fromAddress: BOLD_ADDRESS,
fromTokenName: 'BOLD',
fromAmount: '1.886214',
toAddress: YBOLD_STAKING_ADDRESS,
toTokenName: 'ysyBOLD',
toAmount: undefined
})
)
expect(entry?.assetSymbol).toBe('BOLD')
expect(entry?.shareAmount).toBe('1.886214')
expect(entry?.shareAmountFormatted).toBe(1.886214)
})
it('uses the input amount for legacy local BOLD to yBOLD deposit rows without a share amount', () => {
const entry = toLocalActivityEntry(
createNotification({
fromAddress: BOLD_ADDRESS,
fromTokenName: 'BOLD',
fromAmount: '1.257',
toAddress: YBOLD_VAULT_ADDRESS,
toTokenName: 'yBOLD',
toAmount: undefined
})
)
expect(entry?.assetSymbol).toBe('BOLD')
expect(entry?.shareAmount).toBe('1.257')
expect(entry?.shareAmountFormatted).toBe(1.257)
})
it('uses the burned vault share amount and received underlying amount for local withdrawals', () => {
const entry = toLocalActivityEntry(
createNotification({
type: 'withdraw',
amount: '13.04',
fromAddress: VAULT_ADDRESS,
fromTokenName: 'yvUSDC-H',
toAddress: ASSET_ADDRESS,
toTokenName: 'USDC',
toAmount: '12.22'
})
)
expect(entry?.action).toBe('withdraw')
expect(entry?.displayType).toBeNull()
expect(entry?.assetSymbol).toBe('USDC')
expect(entry?.assetAmount).toBe('12.22')
expect(entry?.shareAmount).toBe('13.04')
})
it('matches local activity entries with the same search path as indexed rows', () => {
const entry = toLocalActivityEntry(createNotification())
expect(entry).not.toBeNull()
expect(doesActivityEntryMatchSearch(entry!, 'deposit', {})).toBe(true)
expect(doesActivityEntryMatchSearch(entry!, 'USDC', {})).toBe(true)
expect(doesActivityEntryMatchSearch(entry!, TX_HASH.slice(0, 12), {})).toBe(true)
expect(doesActivityEntryMatchSearch(entry!, 'not-present', {})).toBe(false)
})
it('can map an unresolved local notification with a fallback timestamp', () => {
const fallbackTimestamp = FINISHED_AT + 30
const entry = toLocalActivityEntry(
createNotification({
status: 'submitted',
timeFinished: undefined
}),
{ fallbackTimestamp }
)
expect(entry?.timestamp).toBe(fallbackTimestamp)
expect(entry?.action).toBe('deposit')
})
it('marks failed local notifications as failed activity', () => {
const entry = toLocalActivityEntry(createNotification({ status: 'error' }))
expect(entry?.transactionStatus).toBe('failed')
})
it('does not map local notifications without any timestamp', () => {
const entry = toLocalActivityEntry(
createNotification({
status: 'pending',
timeFinished: undefined
})
)
expect(entry).toBeNull()
})
it('applies type, chain, and date filters to local notifications before mapping rows', () => {
const notification = createNotification()
const filters = {
types: ['deposit' as const],
startDate: '',
endDate: ''
}
expect(
doesLocalActivityMatchFilters({
chainId: 8453,
endTimestamp: FINISHED_AT + 1,
filters,
notification,
startTimestamp: FINISHED_AT - 1
})
).toBe(true)
expect(
doesLocalActivityMatchFilters({
chainId: 1,
endTimestamp: FINISHED_AT + 1,
filters,
notification,
startTimestamp: FINISHED_AT - 1
})
).toBe(false)
})
it('keeps only recent successful local transactions that are not indexed yet', () => {
vi.spyOn(Date, 'now').mockReturnValue((FINISHED_AT + 60) * 1000)
const notification = createNotification()
expect(isRecentLocalActivityEntry(notification, new Set())).toBe(true)
expect(isRecentLocalActivityEntry(notification, new Set([TX_HASH.toLowerCase()]))).toBe(false)
expect(isRecentLocalActivityEntry({ ...notification, status: 'pending' }, new Set())).toBe(false)
})
})