forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup-tokens.test.ts
More file actions
68 lines (62 loc) · 3.07 KB
/
Copy pathgroup-tokens.test.ts
File metadata and controls
68 lines (62 loc) · 3.07 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
import { describe, expect, it } from 'vitest'
import type { AssetRow } from '@/lib/api'
import { compactBalance, groupTokens } from './group-tokens'
function row(chain: string, familyId: string | null, decimals: number, amount: string, value: number): AssetRow {
return {
assetId: `${chain}/erc20:contract`, chainId: chain,
asset: { familyId, symbol: 'USDC', name: 'USD Coin', decimals, verified: true, iconUrl: null },
amount, spendable: amount, value, share: value > 0 ? value / 10 : 0, change1d: 0, price: 1,
holdings: [{ walletId: 'wallet', positionType: 'wallet', amount, value, protocol: null, groupId: null }],
}
}
describe('grouped token balances', () => {
it('merges a token across networks using provider identity and exact decimal normalization', () => {
const rows = [row('eip155:1', 'zerion:usdc', 6, '1000001', 1), row('eip155:56', 'zerion:usdc', 18, '2000000000000000001', 2)]
const [group] = groupTokens(rows)
expect(group?.amount).toBe('3000001000000000001')
expect(group?.spendable).toBe(group?.amount)
expect(group?.asset.decimals).toBe(18)
expect(group?.value).toBe(3)
expect(group?.networks).toHaveLength(2)
expect(group?.networks.find((network) => network.chainId === 'eip155:1')?.amount).toBe('1000001000000000000')
expect(rows[0]?.amount).toBe('1000001')
})
it('never merges identical tickers with different or missing identities', () => {
const groups = groupTokens([
row('eip155:1', 'zerion:usdc', 6, '1', 1),
row('eip155:56', 'zerion:imposter', 6, '1', 1),
row('eip155:8453', null, 6, '1', 1),
row('eip155:10', null, 6, '1', 1),
])
expect(groups).toHaveLength(4)
})
it('keeps debt, spendable balances, and unpriced positions distinct', () => {
const wallet = row('eip155:1', 'zerion:usdc', 6, '5000000', 5)
const loan = row('eip155:8453', 'zerion:usdc', 6, '2000000', -2)
loan.spendable = '0'
loan.holdings[0]!.positionType = 'loan'
loan.asset.verified = false
const [group] = groupTokens([wallet, loan])
expect(group?.value).toBe(3)
expect(group?.spendable).toBe('5000000')
expect(group?.asset.verified).toBe(false)
expect(group?.networks.find((network) => network.chainId === loan.chainId)?.value).toBe(-2)
wallet.holdings[0]!.value = null
expect(groupTokens([wallet])[0]?.priced).toBe(false)
})
it('collapses multiple implementations on one network to one icon and balance', () => {
const a = row('eip155:1', 'zerion:usdc', 6, '1000000', 1)
const b = { ...a, assetId: 'eip155:1/erc20:other-contract' }
const [group] = groupTokens([a, b])
expect(group?.networks).toHaveLength(1)
expect(group?.networks[0]?.amount).toBe('2000000')
})
})
describe('compact balances', () => {
it('truncates large balances without converting base units to floats', () => {
expect(compactBalance('1234999999999999999999999', 18)).toBe('1.23M')
expect(compactBalance('999999999', 6)).toBe('')
expect(compactBalance('1999999999', 6)).toBe('1.99K')
expect(compactBalance('1000000000000000000000000000000000', 18)).toBe('>999T')
})
})