@@ -3,8 +3,8 @@ import { ethers } from "ethers";
3
3
import { useState , useCallback } from "react" ;
4
4
import { useAccount } from "wagmi" ;
5
5
6
- import { Synapse } from "@filoz/synapse-sdk" ;
7
- import { calculate as calculateCommP } from "@filoz/synapse-sdk/commp " ;
6
+ import { Synapse , CONTRACT_ADDRESSES , TOKENS } from "@filoz/synapse-sdk" ;
7
+ import { PandoraService } from "@filoz/synapse-sdk/pandora " ;
8
8
9
9
export function FileUploader ( ) {
10
10
const [ file , setFile ] = useState < File | null > ( null ) ;
@@ -17,7 +17,7 @@ export function FileUploader() {
17
17
fileName : string ;
18
18
fileSize : number ;
19
19
commp : string ;
20
- txHash : string ;
20
+ rootId ?: number ;
21
21
} | null > ( null ) ;
22
22
23
23
const handleDrag = useCallback ( ( e : React . DragEvent ) => {
@@ -62,44 +62,75 @@ export function FileUploader() {
62
62
63
63
// 2) Initialize ethers provider & signer
64
64
const provider = new ethers . BrowserProvider ( window . ethereum ) ;
65
- const signer = await provider . getSigner ( ) ;
66
- // (Note: Synapse.create({ provider }) will pick up the signer automatically)
67
65
68
66
// 3) Create Synapse instance
69
- const synapse = await Synapse . create ( { provider } ) ;
70
- const balance = await synapse . payments . walletBalance ( ) ;
71
- console . log ( "FIL balance:" , balance . toString ( ) ) ;
67
+ const pandoraAddress = CONTRACT_ADDRESSES . PANDORA_SERVICE . calibration ;
68
+ const synapse = await Synapse . create ( {
69
+ provider,
70
+ pandoraAddress,
71
+ } ) ;
72
+ const filBalance = await synapse . payments . walletBalance ( ) ;
73
+ const usdfcBalance = await synapse . payments . walletBalance ( TOKENS . USDFC ) ;
74
+ console . log ( "FIL balance:" , filBalance . toString ( ) ) ;
75
+ console . log ( "USDFC balance:" , usdfcBalance . toString ( ) ) ;
72
76
73
- // 4) Create (mock) StorageService
74
- // Because the real StorageService is “pending,” this will return MockStorageService
77
+ // 4) Create StorageService using Synapse SDK v0.5
78
+ setStatus ( "Creating storage service..." ) ;
75
79
const storage = await synapse . createStorage ( {
76
- storageProvider : "f01234" , // replace with a valid provider ID or leave as mock
80
+ withCDN : false ,
81
+ callbacks : {
82
+ onProviderSelected : ( provider ) => {
83
+ console . log ( "Selected storage provider:" , provider . owner ) ;
84
+ console . log ( "PDP URL:" , provider . pdpUrl ) ;
85
+ } ,
86
+ onProofSetResolved : ( info ) => {
87
+ if ( info . isExisting ) {
88
+ console . log ( "Using existing proof set:" , info . proofSetId ) ;
89
+ } else {
90
+ console . log ( "Created new proof set:" , info . proofSetId ) ;
91
+ }
92
+ } ,
93
+ onProofSetCreationStarted : ( txHash ) => {
94
+ console . log ( "Creating proof set, tx:" , txHash ) ;
95
+ } ,
96
+ } ,
77
97
} ) ;
78
98
79
- // 5) Kick off upload (→ MockStorageService.upload under the hood)
80
- setStatus ( "Uploading to mock storage service..." ) ;
81
- const uploadTask = storage . upload ( uint8ArrayBytes ) ;
82
-
83
- // 6) Wait for CommP calculation (mock)
84
- const commp = await uploadTask . commp ( ) ;
85
- console . log ( "CommP (mock):" , commp ) ;
99
+ // 5) Run a preflight check
100
+ setStatus ( "Running preflight check..." ) ;
101
+ const preflight = await storage . preflightUpload ( uint8ArrayBytes . length ) ;
86
102
87
- // 7) (Optional) If you want to display intermediate progress, you could do that here.
88
- // But MockStorageService usually just resolves immediately.
89
- setProgress ( 50 ) ;
90
- setStatus ( "Finalizing upload..." ) ;
103
+ if ( ! preflight . allowanceCheck . sufficient ) {
104
+ console . log ( "Insufficient allowances, preparing upload..." ) ;
105
+ const pandoraService = new PandoraService ( provider , pandoraAddress ) ;
106
+ const prep = await pandoraService . prepareStorageUpload (
107
+ { dataSize : uint8ArrayBytes . length } ,
108
+ synapse . payments
109
+ ) ;
110
+ for ( const action of prep . actions ) {
111
+ setStatus ( action . description + "..." ) ;
112
+ await action . execute ( ) ;
113
+ }
114
+ }
91
115
92
- // 8) Wait for “chain commit” (mock)
93
- const txHash = await uploadTask . done ( ) ;
94
- console . log ( "Mock txHash:" , txHash ) ;
116
+ // 6) Upload the file using the new API
117
+ setStatus ( "Uploading to storage provider..." ) ;
118
+ const result = await storage . upload ( uint8ArrayBytes , {
119
+ onUploadComplete : ( commp ) => {
120
+ console . log ( "CommP:" , commp ) ;
121
+ } ,
122
+ onRootAdded : ( ) => {
123
+ console . log ( "Root added to proof set" ) ;
124
+ }
125
+ } ) ;
95
126
96
127
setProgress ( 100 ) ;
97
- setStatus ( "✅ File uploaded successfully (mock) !" ) ;
128
+ setStatus ( "✅ File uploaded successfully!" ) ;
98
129
setUploadedInfo ( {
99
130
fileName : file . name ,
100
131
fileSize : file . size ,
101
- commp : commp . toLocaleString ( ) ,
102
- txHash : txHash ,
132
+ commp : result . commp ,
133
+ rootId : result . rootId ,
103
134
} ) ;
104
135
} catch ( err : any ) {
105
136
console . error ( err ) ;
@@ -122,7 +153,6 @@ export function FileUploader() {
122
153
return null ;
123
154
}
124
155
125
- console . log ( uploadedInfo ) ;
126
156
return (
127
157
< div className = "w-full max-w-md" >
128
158
< div
@@ -232,10 +262,12 @@ export function FileUploader() {
232
262
< span className = "font-medium" > CommP:</ span > { " " }
233
263
{ uploadedInfo . commp }
234
264
</ div >
235
- < div className = "break-all" >
236
- < span className = "font-medium" > Tx Hash:</ span > { " " }
237
- { uploadedInfo . txHash }
238
- </ div >
265
+ { uploadedInfo . rootId != null && (
266
+ < div className = "break-all" >
267
+ < span className = "font-medium" > Root ID:</ span > { " " }
268
+ { uploadedInfo . rootId }
269
+ </ div >
270
+ ) }
239
271
</ div >
240
272
</ div >
241
273
) }
0 commit comments