Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ import { privateKeyToAccount } from "viem/accounts"
import type { Hex } from "viem";
const synapse = Synapse.create({ account: privateKeyToAccount("0x..."), source: "my-app" })
const contexts = await synapse.storage.createContexts({
copies: 2,
copies: 2,
metadata: { source: "my-app" },
})
const [primary, secondary] = contexts
Expand Down Expand Up @@ -496,9 +496,9 @@ const [primary] = selectProviders({
// Secondary: pass empty set to allow any approved provider
const [secondary] = selectProviders({
...input,
endorsedIds: new Set(),
endorsedIds: [],
count: 1,
excludeProviderIds: new Set([primary.provider.id]),
excludeProviderIds: [primary.provider.id],
metadata: { source: "my-app" },
})
```
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/developer-guides/synapse-core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const publicClient = createPublicClient({ chain: calibration, transport: http()
import * as warmStorage from "@filoz/synapse-core/warm-storage"

// List approved providers
const providers = await warmStorage.getApprovedProviders(publicClient)
const providers = await warmStorage.getApprovedProviderIds(publicClient)
console.log(providers) // bigint[] — approved provider IDs
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { providerIdSetAbi } from '../abis/generated.ts'
import { asChain } from '../chains.ts'
import type { ActionCallChain } from '../types.ts'

export namespace getProviderIds {
export namespace getEndorsedProviderIds {
export type OptionsType = {
/** Endorsements contract address. If not provided, the default is the endorsements contract address for the chain. */
contractAddress?: Address
Expand All @@ -25,8 +25,8 @@ export namespace getProviderIds {
'getProviderIds'
>

/** Set of endorsed provider IDs */
export type OutputType = Set<bigint>
/** Array of endorsed provider IDs */
export type OutputType = bigint[]

export type ErrorType = asChain.ErrorType | ReadContractErrorType
}
Expand All @@ -35,13 +35,13 @@ export namespace getProviderIds {
* Get all endorsed provider IDs
*
* @param client - The client to use to get the endorsed providers.
* @param options - {@link getProviderIds.OptionsType}
* @returns Set of endorsed provider IDs {@link getProviderIds.OutputType}
* @throws Errors {@link getProviderIds.ErrorType}
* @param options - {@link getEndorsedProviderIds.OptionsType}
* @returns Array of endorsed provider IDs {@link getEndorsedProviderIds.OutputType}
* @throws Errors {@link getEndorsedProviderIds.ErrorType}
*
* @example
* ```ts
* import { getProviderIds } from '@filoz/synapse-core/endorsements'
* import { getEndorsedProviderIds } from '@filoz/synapse-core/endorsements'
* import { createPublicClient, http } from 'viem'
* import { calibration } from '@filoz/synapse-core/chains'
*
Expand All @@ -50,46 +50,46 @@ export namespace getProviderIds {
* transport: http(),
* })
*
* const providerIds = await getProviderIds(client)
* const providerIds = await getEndorsedProviderIds(client)
*
* console.log(providerIds)
* ```
*/
export async function getProviderIds(
export async function getEndorsedProviderIds(
client: Client<Transport, Chain>,
options: getProviderIds.OptionsType = {}
): Promise<getProviderIds.OutputType> {
options: getEndorsedProviderIds.OptionsType = {}
): Promise<getEndorsedProviderIds.OutputType> {
const data = await readContract(
client,
getProviderIdsCall({
getEndorsedProviderIdsCall({
chain: client.chain,
contractAddress: options.contractAddress,
})
)
return parseGetProviderIds(data)
return parseGetEndorsedProviderIds(data)
}

export namespace getProviderIdsCall {
export type OptionsType = Simplify<getProviderIds.OptionsType & ActionCallChain>
export namespace getEndorsedProviderIdsCall {
export type OptionsType = Simplify<getEndorsedProviderIds.OptionsType & ActionCallChain>

export type ErrorType = asChain.ErrorType
export type OutputType = ContractFunctionParameters<typeof providerIdSetAbi, 'pure' | 'view', 'getProviderIds'>
}

/**
* Create a call to the getProviderIds function
* Create a call to the getEndorsedProviderIds function
*
* This function is used to create a call to the getProviderIds function for use with the multicall or readContract function.
* This function is used to create a call to the getEndorsedProviderIds function for use with the multicall or readContract function.
*
* To get the same output type as the action, use {@link parseGetProviderIds} to transform the contract output.
* To get the same output type as the action, use {@link parseGetEndorsedProviderIds} to transform the contract output.
*
* @param options - {@link getProviderIdsCall.OptionsType}
* @returns The call to the getProviderIds function {@link getProviderIdsCall.OutputType}
* @throws Errors {@link getProviderIdsCall.ErrorType}
* @param options - {@link getEndorsedProviderIdsCall.OptionsType}
* @returns The call to the getEndorsedProviderIdsCall function {@link getEndorsedProviderIdsCall.OutputType}
* @throws Errors {@link getEndorsedProviderIdsCall.ErrorType}
*
* @example
* ```ts
* import { getProviderIdsCall } from '@filoz/synapse-core/endorsements'
* import { getEndorsedProviderIdsCall } from '@filoz/synapse-core/endorsements'
* import { createPublicClient, http } from 'viem'
* import { multicall } from 'viem/actions'
* import { calibration } from '@filoz/synapse-core/chains'
Expand All @@ -101,37 +101,40 @@ export namespace getProviderIdsCall {
*
* const results = await multicall(client, {
* contracts: [
* getProviderIdsCall({ chain: calibration }),
* getEndorsedProviderIdsCall({ chain: calibration }),
* ],
* })
*
* console.log(results[0])
* ```
*/
export function getProviderIdsCall(options: getProviderIdsCall.OptionsType) {
export function getEndorsedProviderIdsCall(options: getEndorsedProviderIdsCall.OptionsType) {
const chain = asChain(options.chain)
return {
abi: chain.contracts.endorsements.abi,
address: options.contractAddress ?? chain.contracts.endorsements.address,
functionName: 'getProviderIds',
args: [],
} satisfies getProviderIdsCall.OutputType
} satisfies getEndorsedProviderIdsCall.OutputType
}

/**
* Parse the result of the getProviderIds function
* Parse the result of the getEndorsedProviderIds function
*
* @param data - The result of the getProviderIds function {@link getProviderIds.ContractOutputType}
* @returns Set of endorsed provider IDs {@link getProviderIds.OutputType}
* @param data - The result of the getEndorsedProviderIds function {@link getEndorsedProviderIds.ContractOutputType}
* @returns Array of endorsed provider IDs {@link getEndorsedProviderIds.OutputType}
*
* @example
* ```ts
* import { parseGetProviderIds } from '@filoz/synapse-core/endorsements'
* import { parseGetEndorsedProviderIds } from '@filoz/synapse-core/endorsements'
*
* const providerIds = parseGetProviderIds([1n, 2n, 1n])
* console.log(providerIds) // Set { 1n, 2n }
* const providerIds = parseGetEndorsedProviderIds([1n, 2n, 1n])
* console.log(providerIds) // [1n, 2n]
* ```
*/
export function parseGetProviderIds(data: getProviderIds.ContractOutputType): getProviderIds.OutputType {
return new Set(data)
export function parseGetEndorsedProviderIds(
data: getEndorsedProviderIds.ContractOutputType
): getEndorsedProviderIds.OutputType {
// deduplicate provider IDs
return Array.from(new Set(data))
}
8 changes: 4 additions & 4 deletions packages/synapse-core/src/endorsements/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {
getProviderIds,
getProviderIdsCall,
parseGetProviderIds,
} from './get-provider-ids.ts'
getEndorsedProviderIds,
getEndorsedProviderIdsCall,
parseGetEndorsedProviderIds,
} from './get-endorsed-provider-ids.ts'
8 changes: 4 additions & 4 deletions packages/synapse-core/src/sp-registry/get-pdp-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 { getApprovedProvidersCall } from '../warm-storage/get-approved-providers.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'
Expand Down Expand Up @@ -151,7 +151,7 @@ export namespace getApprovedPDPProviders {
export type ErrorType =
| asChain.ErrorType
| MulticallErrorType
| getApprovedProvidersCall.ErrorType
| getApprovedProviderIdsCall.ErrorType
| getPDPProvidersCall.ErrorType
}

Expand Down Expand Up @@ -193,7 +193,7 @@ export async function getApprovedPDPProviders(
limit: 100n,
contractAddress: options.contractAddress,
}),
getApprovedProvidersCall({
getApprovedProviderIdsCall({
chain: client.chain,
}),
],
Expand Down Expand Up @@ -223,7 +223,7 @@ export namespace getPDPProvidersByIds {
export type ErrorType =
| asChain.ErrorType
| MulticallErrorType
| getApprovedProvidersCall.ErrorType
| getApprovedProviderIdsCall.ErrorType
| getPDPProvidersCall.ErrorType
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Address, Chain, Client, Transport } from 'viem'
import { getProviderIds } from '../endorsements/get-provider-ids.ts'
import { getEndorsedProviderIds } from '../endorsements/get-endorsed-provider-ids.ts'
import { getApprovedPDPProviders } from '../sp-registry/get-pdp-providers.ts'
import { getPdpDataSets } from './get-pdp-data-sets.ts'
import type { ProviderSelectionInput } from './location-types.ts'
Expand Down Expand Up @@ -34,7 +34,7 @@ export async function fetchProviderSelectionInput(
): Promise<ProviderSelectionInput> {
const [providers, endorsedIds, pdpDataSets] = await Promise.all([
getApprovedPDPProviders(client),
getProviderIds(client),
getEndorsedProviderIds(client),
getPdpDataSets(client, { address: options.address }),
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { fwssView as storageViewAbi } from '../abis/index.ts'
import { asChain } from '../chains.ts'
import type { ActionCallChain } from '../types.ts'

export namespace getApprovedProviders {
export namespace getApprovedProviderIds {
export type OptionsType = {
/** Starting index (0-based). Use 0 to start from beginning. Defaults to 0. */
offset?: bigint
Expand All @@ -36,19 +36,19 @@ export namespace getApprovedProviders {
}

/**
* Get approved provider IDs with optional pagination
* Get all approved provider IDs with optional pagination
*
* For large lists, use pagination to avoid gas limit issues. If limit=0,
* returns all remaining providers starting from offset.
*
* @param client - The client to use to get the approved providers.
* @param options - {@link getApprovedProviders.OptionsType}
* @returns Array of approved provider IDs {@link getApprovedProviders.OutputType}
* @throws Errors {@link getApprovedProviders.ErrorType}
* @param options - {@link getApprovedProviderIds.OptionsType}
* @returns Array of approved provider IDs {@link getApprovedProviderIds.OutputType}
* @throws Errors {@link getApprovedProviderIds.ErrorType}
*
* @example
* ```ts
* import { getApprovedProviders } from '@filoz/synapse-core/warm-storage'
* import { getApprovedProviderIds } from '@filoz/synapse-core/warm-storage'
* import { createPublicClient, http } from 'viem'
* import { calibration } from '@filoz/synapse-core/chains'
*
Expand All @@ -58,47 +58,47 @@ export namespace getApprovedProviders {
* })
*
* // Get first 100 providers
* const providerIds = await getApprovedProviders(client, {
* const providerIds = await getApprovedProviderIds(client, {
* offset: 0n,
* limit: 100n,
* })
*
* console.log(providerIds)
* ```
*/
export async function getApprovedProviders(
export async function getApprovedProviderIds(
client: Client<Transport, Chain>,
options: getApprovedProviders.OptionsType = {}
): Promise<getApprovedProviders.OutputType> {
options: getApprovedProviderIds.OptionsType = {}
): Promise<getApprovedProviderIds.OutputType> {
const data = await readContract(
client,

getApprovedProvidersCall({
getApprovedProviderIdsCall({
chain: client.chain,
offset: options.offset,
limit: options.limit,
contractAddress: options.contractAddress,
})
)
return data as getApprovedProviders.OutputType
return data as getApprovedProviderIds.OutputType
}

export namespace getApprovedProvidersCall {
export type OptionsType = Simplify<getApprovedProviders.OptionsType & ActionCallChain>
export namespace getApprovedProviderIdsCall {
export type OptionsType = Simplify<getApprovedProviderIds.OptionsType & ActionCallChain>
export type ErrorType = asChain.ErrorType
export type OutputType = ContractFunctionParameters<typeof storageViewAbi, 'pure' | 'view', 'getApprovedProviders'>
}

/**
* Create a call to the {@link getApprovedProviders} function for use with the Viem multicall, readContract, or simulateContract functions.
* Create a call to the {@link getApprovedProviderIds} function for use with the Viem multicall, readContract, or simulateContract functions.
*
* @param options - {@link getApprovedProvidersCall.OptionsType}
* @returns Call object {@link getApprovedProvidersCall.OutputType}
* @throws Errors {@link getApprovedProvidersCall.ErrorType}
* @param options - {@link getApprovedProviderIdsCall.OptionsType}
* @returns Call object {@link getApprovedProviderIdsCall.OutputType}
* @throws Errors {@link getApprovedProviderIdsCall.ErrorType}
*
* @example
* ```ts
* import { getApprovedProvidersCall } from '@filoz/synapse-core/warm-storage'
* import { getApprovedProviderIdsCall } from '@filoz/synapse-core/warm-storage'
* import { createPublicClient, http } from 'viem'
* import { multicall } from 'viem/actions'
* import { calibration } from '@filoz/synapse-core/chains'
Expand All @@ -111,20 +111,20 @@ export namespace getApprovedProvidersCall {
* // Paginate through providers in batches of 50
* const results = await multicall(client, {
* contracts: [
* getApprovedProvidersCall({ chain: calibration, offset: 0n, limit: 50n }),
* getApprovedProvidersCall({ chain: calibration, offset: 50n, limit: 50n }),
* getApprovedProviderIdsCall({ chain: calibration, offset: 0n, limit: 50n }),
* getApprovedProviderIdsCall({ chain: calibration, offset: 50n, limit: 50n }),
* ],
* })
*
* console.log(results)
* ```
*/
export function getApprovedProvidersCall(options: getApprovedProvidersCall.OptionsType) {
export function getApprovedProviderIdsCall(options: getApprovedProviderIdsCall.OptionsType) {
const chain = asChain(options.chain)
return {
abi: chain.contracts.fwssView.abi,
address: options.contractAddress ?? chain.contracts.fwssView.address,
functionName: 'getApprovedProviders',
args: [options.offset ?? 0n, options.limit ?? 0n],
} satisfies getApprovedProvidersCall.OutputType
} satisfies getApprovedProviderIdsCall.OutputType
}
2 changes: 1 addition & 1 deletion packages/synapse-core/src/warm-storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export * from './find-matching-data-sets.ts'
export * from './get-account-total-storage-size.ts'
export * from './get-all-data-set-metadata.ts'
export * from './get-all-piece-metadata.ts'
export * from './get-approved-providers.ts'
export * from './get-approved-provider-ids.ts'
export * from './get-client-data-set-ids.ts'
export * from './get-client-data-sets.ts'
export * from './get-client-data-sets-length.ts'
Expand Down
Loading