-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetListVariables.ts
66 lines (50 loc) · 1.62 KB
/
getListVariables.ts
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
import { isAddress } from 'ethers'
import { validateArgs } from '../../utils'
import { StakeWiseSubgraphGraph } from '../../types/graphql/subgraph'
export type GetListVariablesInput = {
vaultAddress: string
orderDirection?: StakeWiseSubgraphGraph.OrderDirection
search?: string
limit?: number
skip?: number
addressIn?: string[]
}
const validateList = (addressIn: string[]) => {
const isValid = addressIn.every((address) => isAddress(address))
if (!isValid) {
throw new Error('The "addressIn" argument must be an array of valid addresses')
}
}
const getListVariables = <T>(input: GetListVariablesInput): T => {
const { vaultAddress, orderDirection, search, limit, skip, addressIn } = input
validateArgs.address({ vaultAddress })
if (typeof skip !== 'undefined') {
validateArgs.number({ skip })
}
if (typeof limit !== 'undefined') {
validateArgs.number({ limit })
}
if (typeof search !== 'undefined') {
validateArgs.string({ search })
}
if (typeof orderDirection !== 'undefined') {
if (![ 'asc', 'desc' ].includes(orderDirection)) {
throw new Error(`The "orderDirection" argument must be "asc" or "desc"`)
}
}
if (typeof addressIn !== 'undefined') {
validateArgs.array({ addressIn })
validateList(addressIn as string[])
}
const vault = vaultAddress.toLowerCase()
const where = search
? { vault, address_in: addressIn, address_contains: search.toLowerCase() }
: { vault, address_in: addressIn }
return {
where,
skip: skip || 0,
limit: limit || 100,
orderDirection: orderDirection || 'desc',
} as T
}
export default getListVariables