@@ -73,38 +73,38 @@ forge v <CONTRACT_ADDRESS> \
7373After IPCM is deployed you can update the CID mapping using the ` updateMapping ` function and the library of your choice. Here is an example with [ Viem] ( https://viem.sh/docs/contract/writeContract#writecontract )
7474
7575``` typescript
76- import { account , publicClient , walletClient } from ' ./config'
77- import { abi } from ' ./ipcm_abi'
76+ import { account , publicClient , walletClient } from " ./config" ;
77+ import { abi } from " ./ipcm_abi" ;
7878
7979const { request } = await publicClient .simulateContract ({
8080 account ,
81- address: ' 0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2' ,
81+ address: " 0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2" ,
8282 abi: abi ,
83- functionName: ' updateMapping' , // Function to Update
84- args: [" ipfs://bafkreigfakpjywuxaq57zhnnma6ntvs6u5p6eurgdl5kfkjvuhg6sztmda" ] // IPFS CID args
85- })
86- await walletClient .writeContract (request )
83+ functionName: " updateMapping" , // Function to Update
84+ args: [" ipfs://bafkreigfakpjywuxaq57zhnnma6ntvs6u5p6eurgdl5kfkjvuhg6sztmda" ], // IPFS CID args
85+ });
86+ await walletClient .writeContract (request );
8787```
8888
8989Once a CID has been written to the contract, it can be fetched using the ` getMapping ` function.
9090
9191``` typescript
92- import { publicClient } from ' ./client'
93- import { abi } from ' ./ipcm_abi'
92+ import { publicClient } from " ./client" ;
93+ import { abi } from " ./ipcm_abi" ;
9494
9595const data = await publicClient .readContract ({
96- address: ' 0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2' ,
96+ address: " 0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2" ,
9797 abi: abi ,
98- functionName: ' getMapping' ,
99- })
98+ functionName: " getMapping" ,
99+ });
100100// ipfs://bafkreigfakpjywuxaq57zhnnma6ntvs6u5p6eurgdl5kfkjvuhg6sztmda
101101```
102102
103103With the CID reference you can render it into content using a tool like the [ IPFS SDK] ( https://docs.pinata.cloud/web3/sdk )
104104
105105``` typescript
106- import { publicClient } from ' ./client'
107- import { abi } from ' ./ipcm_abi'
106+ import { publicClient } from " ./client" ;
107+ import { abi } from " ./ipcm_abi" ;
108108import { PinataSDK } from " pinata-web3" ;
109109
110110const pinata = new PinataSDK ({
@@ -113,12 +113,12 @@ const pinata = new PinataSDK({
113113});
114114
115115const cidMapping = await publicClient .readContract ({
116- address: ' 0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2' ,
116+ address: " 0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2" ,
117117 abi: abi ,
118- functionName: ' getMapping' ,
119- })
118+ functionName: " getMapping" ,
119+ });
120120
121- const { data, contentType } = await pinata .gateways .get (cidMapping )
121+ const { data, contentType } = await pinata .gateways .get (cidMapping );
122122```
123123
124124### Example Demo
@@ -132,52 +132,52 @@ import { abi } from "../utils/contract";
132132import { pinata } from " ../utils/pinata" ;
133133
134134export default {
135- async fetch(
136- request : Request ,
137- env : Env ,
138- ctx : ExecutionContext ,
139- ): Promise <Response > {
140- try {
141- const publicClient = createPublicClient ({
142- chain: baseSepolia ,
143- transport: http (),
144- });
145-
146- const cid = await publicClient .readContract ({
147- address: " 0x81FBD1886121f8575734808959DF808D19De170D" ,
148- abi: abi ,
149- functionName: " getMapping" ,
150- });
151-
152- if (! cid ) {
153- throw new Error (` Failed to fetch latest state: ${cid } ` );
154- }
155-
156- const url = await pinata .gateways .convert (cid as string );
157-
158- const response = await fetch (url );
159-
160- if (! response .ok ) {
161- throw new Error (` Failed to fetch from CDN: ${response .statusText } ` );
162- }
163-
164- const contentType = response .headers .get (" content-type" );
165-
166- return new Response (response .body , {
167- status: 200 ,
168- headers: {
169- " Content-Type" : contentType || " text/html" ,
170- " Cache-Control" : " public, max-age=3600" ,
171- },
172- });
173- } catch (error ) {
174- console .error (" Error:" , error );
175- return new Response (` Error: ${error } ` , {
176- status: 500 ,
177- headers: { " Content-Type" : " text/plain" },
178- });
179- }
180- },
135+ async fetch(
136+ request : Request ,
137+ env : Env ,
138+ ctx : ExecutionContext ,
139+ ): Promise <Response > {
140+ try {
141+ const publicClient = createPublicClient ({
142+ chain: baseSepolia ,
143+ transport: http (),
144+ });
145+
146+ const cid = await publicClient .readContract ({
147+ address: " 0x81FBD1886121f8575734808959DF808D19De170D" ,
148+ abi: abi ,
149+ functionName: " getMapping" ,
150+ });
151+
152+ if (! cid ) {
153+ throw new Error (` Failed to fetch latest state: ${cid } ` );
154+ }
155+
156+ const url = await pinata .gateways .convert (cid as string );
157+
158+ const response = await fetch (url );
159+
160+ if (! response .ok ) {
161+ throw new Error (` Failed to fetch from CDN: ${response .statusText } ` );
162+ }
163+
164+ const contentType = response .headers .get (" content-type" );
165+
166+ return new Response (response .body , {
167+ status: 200 ,
168+ headers: {
169+ " Content-Type" : contentType || " text/html" ,
170+ " Cache-Control" : " public, max-age=3600" ,
171+ },
172+ });
173+ } catch (error ) {
174+ console .error (" Error:" , error );
175+ return new Response (` Error: ${error } ` , {
176+ status: 500 ,
177+ headers: { " Content-Type" : " text/plain" },
178+ });
179+ }
180+ },
181181} satisfies ExportedHandler <Env >;
182182```
183183
0 commit comments