@filoz/synapse-react 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/LICENSE.md +228 -0
  2. package/README.md +32 -0
  3. package/dist/src/calibration.d.ts +9 -0
  4. package/dist/src/calibration.d.ts.map +1 -0
  5. package/dist/src/calibration.js +45 -0
  6. package/dist/src/calibration.js.map +1 -0
  7. package/dist/src/erc20.d.ts +17 -0
  8. package/dist/src/erc20.d.ts.map +1 -0
  9. package/dist/src/erc20.js +57 -0
  10. package/dist/src/erc20.js.map +1 -0
  11. package/dist/src/filsnap.d.ts +5 -0
  12. package/dist/src/filsnap.d.ts.map +1 -0
  13. package/dist/src/filsnap.js +14 -0
  14. package/dist/src/filsnap.js.map +1 -0
  15. package/dist/src/index.d.ts +7 -0
  16. package/dist/src/index.d.ts.map +1 -0
  17. package/dist/src/index.js +7 -0
  18. package/dist/src/index.js.map +1 -0
  19. package/dist/src/payments/index.d.ts +42 -0
  20. package/dist/src/payments/index.d.ts.map +1 -0
  21. package/dist/src/payments/index.js +192 -0
  22. package/dist/src/payments/index.js.map +1 -0
  23. package/dist/src/payments/use-deposit-and-approve.d.ts +11 -0
  24. package/dist/src/payments/use-deposit-and-approve.d.ts.map +1 -0
  25. package/dist/src/payments/use-deposit-and-approve.js +42 -0
  26. package/dist/src/payments/use-deposit-and-approve.js.map +1 -0
  27. package/dist/src/usdfc.d.ts +6 -0
  28. package/dist/src/usdfc.d.ts.map +1 -0
  29. package/dist/src/usdfc.js +15 -0
  30. package/dist/src/usdfc.js.map +1 -0
  31. package/dist/src/warm-storage/index.d.ts +6 -0
  32. package/dist/src/warm-storage/index.d.ts.map +1 -0
  33. package/dist/src/warm-storage/index.js +6 -0
  34. package/dist/src/warm-storage/index.js.map +1 -0
  35. package/dist/src/warm-storage/use-create-data-set.d.ts +14 -0
  36. package/dist/src/warm-storage/use-create-data-set.d.ts.map +1 -0
  37. package/dist/src/warm-storage/use-create-data-set.js +35 -0
  38. package/dist/src/warm-storage/use-create-data-set.js.map +1 -0
  39. package/dist/src/warm-storage/use-data-sets.d.ts +42 -0
  40. package/dist/src/warm-storage/use-data-sets.d.ts.map +1 -0
  41. package/dist/src/warm-storage/use-data-sets.js +51 -0
  42. package/dist/src/warm-storage/use-data-sets.js.map +1 -0
  43. package/dist/src/warm-storage/use-providers.d.ts +8 -0
  44. package/dist/src/warm-storage/use-providers.d.ts.map +1 -0
  45. package/dist/src/warm-storage/use-providers.js +14 -0
  46. package/dist/src/warm-storage/use-providers.js.map +1 -0
  47. package/dist/src/warm-storage/use-service-price.d.ts +8 -0
  48. package/dist/src/warm-storage/use-service-price.d.ts.map +1 -0
  49. package/dist/src/warm-storage/use-service-price.js +15 -0
  50. package/dist/src/warm-storage/use-service-price.js.map +1 -0
  51. package/dist/src/warm-storage/use-upload.d.ts +14 -0
  52. package/dist/src/warm-storage/use-upload.d.ts.map +1 -0
  53. package/dist/src/warm-storage/use-upload.js +39 -0
  54. package/dist/src/warm-storage/use-upload.js.map +1 -0
  55. package/package.json +91 -0
  56. package/src/calibration.ts +76 -0
  57. package/src/erc20.ts +103 -0
  58. package/src/filsnap.ts +16 -0
  59. package/src/index.ts +17 -0
  60. package/src/payments/index.ts +357 -0
  61. package/src/payments/use-deposit-and-approve.ts +71 -0
  62. package/src/usdfc.ts +26 -0
  63. package/src/warm-storage/index.ts +5 -0
  64. package/src/warm-storage/use-create-data-set.ts +62 -0
  65. package/src/warm-storage/use-data-sets.ts +74 -0
  66. package/src/warm-storage/use-providers.ts +21 -0
  67. package/src/warm-storage/use-service-price.ts +34 -0
  68. package/src/warm-storage/use-upload.ts +58 -0
@@ -0,0 +1,71 @@
1
+ import { getChain } from '@filoz/synapse-core/chains'
2
+ import type { DepositAndApproveOptions } from '@filoz/synapse-core/pay'
3
+ import * as payments from '@filoz/synapse-core/pay'
4
+ import { type MutateOptions, useMutation, useQueryClient } from '@tanstack/react-query'
5
+ import type { TransactionReceipt } from 'viem'
6
+ import { waitForTransactionReceipt } from 'viem/actions'
7
+ import { useAccount, useChainId, useConfig } from 'wagmi'
8
+ import { getConnectorClient } from 'wagmi/actions'
9
+
10
+ type UseDepositVariables = Pick<DepositAndApproveOptions, 'amount'>
11
+ interface UseDepositAndApproveProps extends Omit<DepositAndApproveOptions, 'amount'> {
12
+ /**
13
+ * The mutation options.
14
+ */
15
+ mutation?: Omit<MutateOptions<TransactionReceipt, Error, UseDepositVariables>, 'mutationFn'>
16
+ /**
17
+ * The callback to call when the hash is available.
18
+ */
19
+ onHash?: (hash: string) => void
20
+ }
21
+
22
+ /**
23
+ * Deposit ERC20 tokens into the payments contract.
24
+ *
25
+ * @param props - The props for the deposit.
26
+ * @param props.address - The address of the account to deposit from.
27
+ * @param props.token - The address of the ERC20 token to deposit.
28
+ * @param props.mutation - The mutation options.
29
+ * @param props.onHash - The callback to call when the hash is available.
30
+ * @returns The deposit mutation.
31
+ */
32
+ export function useDepositAndApprove(props?: UseDepositAndApproveProps) {
33
+ const config = useConfig()
34
+ const chainId = useChainId({ config })
35
+ const chain = getChain(chainId)
36
+ const account = useAccount({ config })
37
+ const queryClient = useQueryClient()
38
+ const token = props?.token ?? chain.contracts.usdfc.address
39
+ const from = props?.address ?? account.address
40
+
41
+ return useMutation({
42
+ mutationFn: async ({ amount }: UseDepositVariables) => {
43
+ const client = await getConnectorClient(config, {
44
+ account: account.address,
45
+ chainId,
46
+ })
47
+
48
+ const hash = await payments.depositAndApprove(client, {
49
+ amount,
50
+ })
51
+
52
+ props?.onHash?.(hash)
53
+ const transactionReceipt = await waitForTransactionReceipt(config.getClient(), {
54
+ hash: hash,
55
+ })
56
+
57
+ queryClient.invalidateQueries({
58
+ queryKey: ['synapse-payments-account-info', from, token],
59
+ })
60
+ queryClient.invalidateQueries({
61
+ queryKey: ['synapse-erc20-balance', from, token],
62
+ })
63
+ queryClient.invalidateQueries({
64
+ queryKey: ['synapse-payments-operator-approvals', from, token, chain.contracts.storage.address],
65
+ })
66
+
67
+ return transactionReceipt
68
+ },
69
+ ...props?.mutation,
70
+ })
71
+ }
package/src/usdfc.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { watchUsdfc } from '@filoz/synapse-core/usdfc'
2
+ import { type MutateOptions, useMutation } from '@tanstack/react-query'
3
+ import { useConfig } from 'wagmi'
4
+ import { getConnectorClient } from 'wagmi/actions'
5
+
6
+ export interface UseWatchUsdfcProps {
7
+ mutation?: Omit<MutateOptions<boolean, Error>, 'mutationFn'>
8
+ }
9
+
10
+ /**
11
+ * Add the USDFC token to the wallet.
12
+ *
13
+ * @param props - The props for the add USDFC.
14
+ * @param props.mutation - The mutation options.
15
+ */
16
+ export function useAddUsdfc(props?: UseWatchUsdfcProps) {
17
+ const config = useConfig()
18
+
19
+ return useMutation({
20
+ ...props?.mutation,
21
+ mutationFn: async () => {
22
+ const client = await getConnectorClient(config)
23
+ return await watchUsdfc(client)
24
+ },
25
+ })
26
+ }
@@ -0,0 +1,5 @@
1
+ export * from './use-create-data-set.ts'
2
+ export * from './use-data-sets.ts'
3
+ export * from './use-providers.ts'
4
+ export * from './use-service-price.ts'
5
+ export * from './use-upload.ts'
@@ -0,0 +1,62 @@
1
+ import type { DataSetCreatedResponse } from '@filoz/synapse-core/curio'
2
+ import * as Curio from '@filoz/synapse-core/curio'
3
+ import type { PDPProvider } from '@filoz/synapse-core/warm-storage'
4
+ import { createDataSet } from '@filoz/synapse-core/warm-storage'
5
+ import { type MutateOptions, useMutation, useQueryClient } from '@tanstack/react-query'
6
+ import { useAccount, useChainId, useConfig } from 'wagmi'
7
+ import { getConnectorClient } from 'wagmi/actions'
8
+
9
+ export interface UseCreateDataSetProps {
10
+ /**
11
+ * The callback to call when the hash is available.
12
+ */
13
+ onHash?: (hash: string) => void
14
+ mutation?: Omit<MutateOptions<DataSetCreatedResponse, Error, UseCreateDataSetVariables>, 'mutationFn'>
15
+ }
16
+
17
+ export interface UseCreateDataSetVariables {
18
+ /**
19
+ * PDP Provider
20
+ */
21
+ provider: PDPProvider
22
+ cdn: boolean
23
+ }
24
+
25
+ export type UseCreateDataSetResult = DataSetCreatedResponse
26
+
27
+ export function useCreateDataSet(props: UseCreateDataSetProps) {
28
+ const config = useConfig()
29
+ const chainId = useChainId({ config })
30
+ const account = useAccount({ config })
31
+ const queryClient = useQueryClient()
32
+ return useMutation({
33
+ ...props?.mutation,
34
+ mutationFn: async ({ provider, cdn }: UseCreateDataSetVariables) => {
35
+ const connectorClient = await getConnectorClient(config, {
36
+ account: account.address,
37
+ chainId,
38
+ })
39
+
40
+ const { hash, statusUrl } = await createDataSet(connectorClient, {
41
+ publicClient: config.getClient(),
42
+ provider,
43
+ cdn,
44
+ // metadata: {
45
+ // title: 'Test Data Set',
46
+ // description: 'Test Description',
47
+ // },
48
+ })
49
+ props?.onHash?.(hash)
50
+
51
+ const dataSet = await Curio.pollForDataSetCreationStatus({ statusUrl })
52
+
53
+ queryClient.invalidateQueries({
54
+ queryKey: ['synapse-warm-storage-data-sets', account.address],
55
+ })
56
+ queryClient.invalidateQueries({
57
+ queryKey: ['synapse-warm-storage-providers-with-data-sets', account.address],
58
+ })
59
+ return dataSet
60
+ },
61
+ })
62
+ }
@@ -0,0 +1,74 @@
1
+ import { type MetadataObject, metadataArrayToObject } from '@filoz/synapse-core'
2
+ import { getChain } from '@filoz/synapse-core/chains'
3
+ import type { CurioPieceWithUrl } from '@filoz/synapse-core/curio'
4
+ import * as PDP from '@filoz/synapse-core/curio'
5
+ import { type DataSet, getDataSets, readProviders } from '@filoz/synapse-core/warm-storage'
6
+ import { skipToken, type UseQueryOptions, useQuery } from '@tanstack/react-query'
7
+ import type { Simplify } from 'type-fest'
8
+ import type { Address } from 'viem'
9
+ import { readContract } from 'viem/actions'
10
+ import { useChainId, useConfig } from 'wagmi'
11
+ import { useProviders } from './use-providers.ts'
12
+
13
+ export type PieceWithMetadata = Simplify<CurioPieceWithUrl & { metadata: MetadataObject }>
14
+
15
+ export interface DataSetWithPieces extends DataSet {
16
+ pieces: PieceWithMetadata[]
17
+ }
18
+
19
+ export type UseDataSetsResult = DataSetWithPieces[]
20
+
21
+ export interface UseDataSetsProps {
22
+ address?: Address
23
+ query?: Omit<UseQueryOptions<UseDataSetsResult>, 'queryKey' | 'queryFn'>
24
+ }
25
+
26
+ export function useDataSets(props: UseDataSetsProps) {
27
+ const config = useConfig()
28
+ const chainId = useChainId()
29
+ const address = props.address
30
+ const { data: providersPrefected } = useProviders()
31
+ const chain = getChain(chainId)
32
+ return useQuery({
33
+ queryKey: ['synapse-warm-storage-data-sets', address],
34
+ queryFn: address
35
+ ? async () => {
36
+ const providers = providersPrefected ?? (await readProviders(config.getClient()))
37
+ const dataSets = await getDataSets(config.getClient(), { address })
38
+ const dataSetsWithPieces = await Promise.all(
39
+ dataSets.map(async (dataSet) => {
40
+ // TODO: Get the active pieces from the PDP contract instead of the Curio API
41
+ const pieces = await PDP.getPiecesForDataSet({
42
+ endpoint: providers.find((p) => p.providerId === dataSet.providerId)?.pdp.serviceURL || '',
43
+ dataSetId: dataSet.pdpDatasetId,
44
+ chainId,
45
+ address,
46
+ cdn: dataSet.cdn,
47
+ })
48
+
49
+ const piecesWithMetadata = await Promise.all(
50
+ pieces.map(async (piece) => {
51
+ const metadata = await readContract(config.getClient(), {
52
+ address: chain.contracts.storageView.address,
53
+ abi: chain.contracts.storageView.abi,
54
+ functionName: 'getAllPieceMetadata',
55
+ args: [dataSet.pdpDatasetId, BigInt(piece.pieceId)],
56
+ })
57
+ return {
58
+ ...piece,
59
+ metadata: metadataArrayToObject(metadata),
60
+ }
61
+ })
62
+ )
63
+
64
+ return {
65
+ ...dataSet,
66
+ pieces: piecesWithMetadata,
67
+ }
68
+ })
69
+ )
70
+ return dataSetsWithPieces
71
+ }
72
+ : skipToken,
73
+ })
74
+ }
@@ -0,0 +1,21 @@
1
+ import { type PDPProvider, readProviders } from '@filoz/synapse-core/warm-storage'
2
+ import { type UseQueryOptions, useQuery } from '@tanstack/react-query'
3
+ import { useConfig } from 'wagmi'
4
+
5
+ export interface UseProvidersProps {
6
+ query?: Omit<UseQueryOptions<UseProvidersResult>, 'queryKey' | 'queryFn'>
7
+ }
8
+
9
+ export type UseProvidersResult = PDPProvider[]
10
+
11
+ export function useProviders(props?: UseProvidersProps) {
12
+ const config = useConfig()
13
+
14
+ return useQuery({
15
+ ...props?.query,
16
+ queryKey: ['synapse-warm-storage-providers'],
17
+ queryFn: () => {
18
+ return readProviders(config.getClient())
19
+ },
20
+ })
21
+ }
@@ -0,0 +1,34 @@
1
+ import { type ServicePriceResult, servicePrice } from '@filoz/synapse-core/warm-storage'
2
+ import { type UseQueryOptions, useQuery } from '@tanstack/react-query'
3
+ import { useConfig } from 'wagmi'
4
+
5
+ /**
6
+ * The props for the useServicePrice hook.
7
+ */
8
+ export interface UseServicePriceProps {
9
+ query?: Omit<UseQueryOptions<ServicePriceResult>, 'queryKey' | 'queryFn'>
10
+ }
11
+
12
+ /**
13
+ * The result for the useServicePrice hook.
14
+ */
15
+ export type UseServicePriceResult = ServicePriceResult
16
+
17
+ /**
18
+ * Get the service price for the warm storage.
19
+ *
20
+ * @param props - The props to use.
21
+ * @returns The service price.
22
+ */
23
+ export function useServicePrice(props?: UseServicePriceProps) {
24
+ const config = useConfig()
25
+
26
+ return useQuery({
27
+ ...props?.query,
28
+ queryKey: ['synapse-warm-storage-get-service-price'],
29
+ queryFn: async () => {
30
+ const result = await servicePrice(config.getClient())
31
+ return result
32
+ },
33
+ })
34
+ }
@@ -0,0 +1,58 @@
1
+ import { getChain } from '@filoz/synapse-core/chains'
2
+ import type { AddPiecesSuccess } from '@filoz/synapse-core/curio'
3
+ import * as Curio from '@filoz/synapse-core/curio'
4
+ import type { SessionKey } from '@filoz/synapse-core/session-key'
5
+ import { upload } from '@filoz/synapse-core/warm-storage'
6
+ import { type MutateOptions, useMutation, useQueryClient } from '@tanstack/react-query'
7
+ import { useAccount, useChainId, useConfig } from 'wagmi'
8
+ import { getConnectorClient } from 'wagmi/actions'
9
+
10
+ export interface UseUploadProps {
11
+ /**
12
+ * The callback to call when the hash is available.
13
+ */
14
+ onHash?: (hash: string) => void
15
+ mutation?: Omit<MutateOptions<AddPiecesSuccess, Error, UseUploadVariables>, 'mutationFn'>
16
+ }
17
+
18
+ export interface UseUploadVariables {
19
+ files: File[]
20
+ dataSetId: bigint
21
+ sessionKey?: SessionKey
22
+ }
23
+ export function useUpload(props: UseUploadProps) {
24
+ const config = useConfig()
25
+ const chainId = useChainId({ config })
26
+ const chain = getChain(chainId)
27
+ const account = useAccount({ config })
28
+ const queryClient = useQueryClient()
29
+ const client = config.getClient()
30
+
31
+ return useMutation({
32
+ ...props?.mutation,
33
+ mutationFn: async ({ files, dataSetId, sessionKey }: UseUploadVariables) => {
34
+ let connectorClient = await getConnectorClient(config, {
35
+ account: account.address,
36
+ chainId,
37
+ })
38
+ if (sessionKey && (await sessionKey.isValid(connectorClient, 'AddPieces'))) {
39
+ connectorClient = sessionKey.client(chain, client.transport)
40
+ }
41
+
42
+ const pieces = await upload(connectorClient, {
43
+ dataSetId,
44
+ data: files,
45
+ })
46
+
47
+ props?.onHash?.(pieces.txHash)
48
+ const rsp = await Curio.pollForAddPiecesStatus({
49
+ statusUrl: pieces.statusUrl,
50
+ })
51
+
52
+ queryClient.invalidateQueries({
53
+ queryKey: ['synapse-warm-storage-data-sets', account.address],
54
+ })
55
+ return rsp
56
+ },
57
+ })
58
+ }