-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathget-pdp-providers.ts
More file actions
275 lines (257 loc) · 8.18 KB
/
Copy pathget-pdp-providers.ts
File metadata and controls
275 lines (257 loc) · 8.18 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import type { Simplify } from 'type-fest'
import type {
Address,
Chain,
Client,
ContractFunctionParameters,
ContractFunctionReturnType,
MulticallErrorType,
ReadContractErrorType,
Transport,
} from 'viem'
import { multicall, readContract } from 'viem/actions'
import type { serviceProviderRegistry as serviceProviderRegistryAbi } from '../abis/index.ts'
import { asChain } from '../chains.ts'
import type { ActionCallChain } from '../types.ts'
import { getApprovedProviderIdsCall } from '../warm-storage/get-approved-provider-ids.ts'
import { getPDPProviderCall, parsePDPProvider } from './get-pdp-provider.ts'
import type { getProvidersByProductType } from './get-providers-by-product-type.ts'
import { type PDPProvider, PRODUCTS, type ProviderWithProduct } from './types.ts'
export namespace getPDPProviders {
export type OptionsType = Omit<getProvidersByProductType.OptionsType, 'productType'>
export type ContractOutputType = ContractFunctionReturnType<
typeof serviceProviderRegistryAbi,
'pure' | 'view',
'getProvidersByProductType'
>
/** The paginated providers result */
export type OutputType = { providers: PDPProvider[]; hasMore: boolean }
export type ErrorType = asChain.ErrorType | ReadContractErrorType
}
/**
* Get PDP providers with pagination
*
* @param client - The client to use to get the providers.
* @param options - {@link getPDPProviders.OptionsType}
* @returns The paginated providers result {@link getPDPProviders.OutputType}
* @throws Errors {@link getPDPProviders.ErrorType}
*
* @example
* ```ts
* import { getPDPProviders } from '@filoz/synapse-core/sp-registry'
* import { createPublicClient, http } from 'viem'
* import { calibration } from '@filoz/synapse-core/chains'
*
* const client = createPublicClient({
* chain: calibration,
* transport: http(),
* })
*
* const result = await getPDPProviders(client, {
* onlyActive: true,
* })
*
* console.log(result.providers)
* console.log(result.hasMore)
* ```
*/
export async function getPDPProviders(
client: Client<Transport, Chain>,
options: getPDPProviders.OptionsType = {}
): Promise<getPDPProviders.OutputType> {
const data = await readContract(
client,
getPDPProvidersCall({
chain: client.chain,
onlyActive: options.onlyActive,
offset: options.offset,
limit: options.limit,
contractAddress: options.contractAddress,
})
)
return parsePDPProviders(data)
}
export namespace getPDPProvidersCall {
export type OptionsType = Simplify<getPDPProviders.OptionsType & ActionCallChain>
export type ErrorType = asChain.ErrorType
export type OutputType = ContractFunctionParameters<
typeof serviceProviderRegistryAbi,
'pure' | 'view',
'getProvidersByProductType'
>
}
/**
* Create a call to the getPDPProviders function
*
* This function is used to create a call to the getPDPProviders function for use with the multicall or readContract function.
*
* @param options - {@link getPDPProvidersCall.OptionsType}
* @returns The call to the getPDPProviders function {@link getPDPProvidersCall.OutputType}
* @throws Errors {@link getPDPProvidersCall.ErrorType}
*
* @example
* ```ts
* import { getPDPProvidersCall } from '@filoz/synapse-core/sp-registry'
* import { createPublicClient, http } from 'viem'
* import { multicall } from 'viem/actions'
* import { calibration } from '@filoz/synapse-core/chains'
*
* const client = createPublicClient({
* chain: calibration,
* transport: http(),
* })
*
* const results = await multicall(client, {
* contracts: [
* getPDPProvidersCall({
* chain: calibration,
* onlyActive: true,
* }),
* ],
* })
*
* console.log(results[0])
* ```
*/
export function getPDPProvidersCall(options: getPDPProvidersCall.OptionsType) {
const chain = asChain(options.chain)
return {
abi: chain.contracts.serviceProviderRegistry.abi,
address: options.contractAddress ?? chain.contracts.serviceProviderRegistry.address,
functionName: 'getProvidersByProductType',
args: [PRODUCTS.PDP, options.onlyActive ?? true, options.offset ?? 0n, options.limit ?? 50n],
} satisfies getPDPProvidersCall.OutputType
}
/**
* Parse the contract output into a PDPProvider array
*
* @param data - The contract output from the getPDPProviders function {@link getPDPProviders.ContractOutputType}
* @returns The PDPProvider array {@link getPDPProviders.OutputType}
*/
export function parsePDPProviders(data: getPDPProviders.ContractOutputType): getPDPProviders.OutputType {
return {
providers: data.providers.map(parsePDPProvider),
hasMore: data.hasMore,
}
}
export namespace getApprovedPDPProviders {
export type OptionsType = Omit<getPDPProviders.OptionsType, 'onlyActive' | 'offset' | 'limit'>
export type OutputType = PDPProvider[]
export type ErrorType =
| asChain.ErrorType
| MulticallErrorType
| getApprovedProviderIdsCall.ErrorType
| getPDPProvidersCall.ErrorType
}
/**
* Get FilecoinWarmStorage approved PDP providers
*
* @param client - The client to use to get the providers.
* @param options - {@link getApprovedPDPProviders.OptionsType}
* @returns The approved PDP providers {@link getApprovedPDPProviders.OutputType}
* @throws Errors {@link getApprovedPDPProviders.ErrorType}
*
* @example
* ```ts
* import { getApprovedPDPProviders } from '@filoz/synapse-core/sp-registry'
* import { createPublicClient, http } from 'viem'
* import { calibration } from '@filoz/synapse-core/chains'
*
* const client = createPublicClient({
* chain: calibration,
* transport: http(),
* })
*
* const result = await getApprovedPDPProviders(client)
*
* console.log(result)
* ```
*/
export async function getApprovedPDPProviders(
client: Client<Transport, Chain>,
options: getApprovedPDPProviders.OptionsType = {}
): Promise<getApprovedPDPProviders.OutputType> {
const [pdpProviders, approvedProviders] = await multicall(client, {
allowFailure: false,
contracts: [
getPDPProvidersCall({
chain: client.chain,
onlyActive: true,
offset: 0n,
limit: 100n,
contractAddress: options.contractAddress,
}),
getApprovedProviderIdsCall({
chain: client.chain,
}),
],
})
const providers = [] as ProviderWithProduct[]
for (const provider of pdpProviders.providers) {
if (approvedProviders.includes(provider.providerId)) {
providers.push(provider)
}
}
return parsePDPProviders({
providers,
hasMore: false,
}).providers
}
export namespace getPDPProvidersByIds {
export type OptionsType = {
providerIds: bigint[]
/** The contract address to use. If not provided, the default is the ServiceProviderRegistry contract address for the chain. */
contractAddress?: Address
}
export type OutputType = PDPProvider[]
export type ErrorType =
| asChain.ErrorType
| MulticallErrorType
| getApprovedProviderIdsCall.ErrorType
| getPDPProvidersCall.ErrorType
}
/**
* Get PDP providers by IDs
*
* @param client - The client to use to get the providers.
* @param options - {@link getPDPProvidersByIds.OptionsType}
* @returns The approved PDP providers {@link getPDPProvidersByIds.OutputType}
* @throws Errors {@link getPDPProvidersByIds.ErrorType}
*
* @example
* ```ts
* import { getPDPProvidersByIds } from '@filoz/synapse-core/sp-registry'
* import { createPublicClient, http } from 'viem'
* import { calibration } from '@filoz/synapse-core/chains'
*
* const client = createPublicClient({
* chain: calibration,
* transport: http(),
* })
*
* const result = await getPDPProvidersByIds(client, {
* providerIds: [1n, 2n, 3n],
* })
*
* console.log(result)
* ```
*/
export async function getPDPProvidersByIds(
client: Client<Transport, Chain>,
options: getPDPProvidersByIds.OptionsType
): Promise<getPDPProvidersByIds.OutputType> {
const result = await multicall(client, {
allowFailure: true,
contracts: options.providerIds.map((providerId) =>
getPDPProviderCall({
chain: client.chain,
providerId,
contractAddress: options.contractAddress,
})
),
})
return parsePDPProviders({
providers: result.filter((result) => result.status === 'success').map((result) => result.result),
hasMore: false,
}).providers
}