|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +slug: prepare-web2json-request |
| 4 | +title: Prepare Web2Json request |
| 5 | +authors: [nikerzetic] |
| 6 | +description: Prepare an FDC request through a Verifier server using Tanstack Query. |
| 7 | +tags: [quickstart, ethereum, flare-smart-accounts] |
| 8 | +keywords: |
| 9 | + [ |
| 10 | + flare-fdc, |
| 11 | + ethereum, |
| 12 | + flare-smart-accounts, |
| 13 | + evm, |
| 14 | + flare-network, |
| 15 | + account-abstraction, |
| 16 | + ] |
| 17 | +unlisted: false |
| 18 | +--- |
| 19 | + |
| 20 | +import { PrepareWeb2JsonRequestApp } from "@site/src/components/Frontend/PrepareWeb2JsonRequestCard"; |
| 21 | +import CodeBlock from "@theme/CodeBlock"; |
| 22 | +import BrowserOnly from "@docusaurus/BrowserOnly"; |
| 23 | +import PrepareWeb2JsonRequestCode from "!!raw-loader!/src/components/Frontend/PrepareWeb2JsonRequestCard"; |
| 24 | + |
| 25 | +In this guide, we will prepare a [Web2Json](/fdc/attestation-types/web2-json) attestation type request. |
| 26 | +Shown below is a card component that accepts user input and makes the request with those parameters. |
| 27 | + |
| 28 | +{/* TODO:(Nik) why did this stop working */} |
| 29 | + |
| 30 | +<BrowserOnly>{() => <PrepareWeb2JsonRequestApp />}</BrowserOnly> |
| 31 | + |
| 32 | +We will focus only on the part of the component that defines and calls the Tanstack Query mutation. |
| 33 | +The remainder of the code is mostly HTML boilerplate, though the full source code is available at the end of the guide. |
| 34 | + |
| 35 | +Let us take a look at how we invoke the `useMutation` hook. |
| 36 | +We provide two parameters, the `mutationFn` and `onSuccess` functions. |
| 37 | +In a production application, we should at least also define the `onError` side effect. |
| 38 | +You can see the full list of parameters in the [Tanstack Query documentation](https://tanstack.com/query/latest/docs/framework/react/guides/mutations). |
| 39 | + |
| 40 | +The `useMutation` hook is called within the main body of the component. |
| 41 | +We take the `mutate` return value and call it when the `Prepare ABI-encoded request` button is clicked. |
| 42 | +It is an async function that will run in the background. |
| 43 | +When it succeeds, the `onSuccess` side effect is triggered. |
| 44 | + |
| 45 | +```tsx |
| 46 | + const { mutate } = useMutation({ |
| 47 | + mutationFn: async () => { |
| 48 | + ... |
| 49 | + }, |
| 50 | + onSuccess: (result) => { |
| 51 | + ... |
| 52 | + }, |
| 53 | + }); |
| 54 | +``` |
| 55 | + |
| 56 | +The mutation function constructs a request body from the parameters input in the form. |
| 57 | +Their values are stored as React states (`useState` hook), and updated when the field values change. |
| 58 | + |
| 59 | +```tsx |
| 60 | +const requestBody = { |
| 61 | + url: url, |
| 62 | + httpMethod: httpMethod, |
| 63 | + headers: headers, |
| 64 | + queryParams: queryParams, |
| 65 | + body: body, |
| 66 | + postProcessJq: postProcessJq, |
| 67 | + abiSignature: abiSignature, |
| 68 | +}; |
| 69 | +``` |
| 70 | + |
| 71 | +Two additional values are necessary for the request: the attestation type and source ID. |
| 72 | +They are UTF8 hex encoded strings, padded to 32 bytes, of `Web2Json` and `PublicWeb2` respectively. |
| 73 | + |
| 74 | +```tsx |
| 75 | +const attestationType = toUtf8HexString(attestationTypeBase); |
| 76 | +const sourceId = toUtf8HexString(sourceIdBase); |
| 77 | +``` |
| 78 | + |
| 79 | +The encoding function is as follows. |
| 80 | + |
| 81 | +```tsx |
| 82 | +export function toHex(data: string) { |
| 83 | + let result = ""; |
| 84 | + for (let i = 0; i < data.length; i++) { |
| 85 | + result += data.charCodeAt(i).toString(16); |
| 86 | + } |
| 87 | + return result.padEnd(64, "0"); |
| 88 | +} |
| 89 | + |
| 90 | +export function toUtf8HexString(data: string) { |
| 91 | + return "0x" + toHex(data); |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +Out of these three values - the attestation type, source ID, and request body - the mutation function constructs the request. |
| 96 | + |
| 97 | +```tsx |
| 98 | +const request = { |
| 99 | + attestationType: attestationType, |
| 100 | + sourceId: sourceId, |
| 101 | + requestBody: requestBody, |
| 102 | +}; |
| 103 | +``` |
| 104 | + |
| 105 | +The request is sent to the `/Web2Json/prepareRequest` endpoint at the verifier URL `https://web2json-verifier-test.flare.rocks`. |
| 106 | +An API key needs to be provided as a header. |
| 107 | +Though custom keys are available, the public key `00000000-0000-0000-0000-000000000000` can provide a rate-limited access. |
| 108 | + |
| 109 | +```tsx |
| 110 | +const response = await fetch(`${verifierUrlBase}/Web2Json/prepareRequest`, { |
| 111 | + method: "POST", |
| 112 | + headers: { |
| 113 | + "X-API-KEY": apiKey, |
| 114 | + "Content-Type": "application/json", |
| 115 | + "Access-Control-Allow-Origin": "*", |
| 116 | + }, |
| 117 | + body: JSON.stringify(request), |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +Finally, the response is returned as a JSON. |
| 122 | +The entire `mutationFn` parameter is as follows. |
| 123 | + |
| 124 | +```tsx |
| 125 | + mutationFn: async () => { |
| 126 | + const requestBody = { |
| 127 | + url: url, |
| 128 | + httpMethod: httpMethod, |
| 129 | + headers: headers, |
| 130 | + queryParams: queryParams, |
| 131 | + body: body, |
| 132 | + postProcessJq: postProcessJq, |
| 133 | + abiSignature: abiSignature, |
| 134 | + }; |
| 135 | + const attestationType = toUtf8HexString(attestationTypeBase); |
| 136 | + const sourceId = toUtf8HexString(sourceIdBase); |
| 137 | + |
| 138 | + const request = { |
| 139 | + attestationType: attestationType, |
| 140 | + sourceId: sourceId, |
| 141 | + requestBody: requestBody, |
| 142 | + }; |
| 143 | + const response = await fetch( |
| 144 | + `${verifierUrlBase}/Web2Json/prepareRequest`, |
| 145 | + { |
| 146 | + method: "POST", |
| 147 | + headers: { |
| 148 | + "X-API-KEY": apiKey, |
| 149 | + "Content-Type": "application/json", |
| 150 | + "Access-Control-Allow-Origin": "*", |
| 151 | + }, |
| 152 | + body: JSON.stringify(request), |
| 153 | + } |
| 154 | + ); |
| 155 | + return response.json(); |
| 156 | + }, |
| 157 | +``` |
| 158 | + |
| 159 | +The `onSuccess` side effect is much simpler. |
| 160 | +It only sets the `abiEncodedRequest` React state to whatever the value of the `abiEncodedRequest` field of the result is. |
| 161 | + |
| 162 | +```tsx |
| 163 | + onSuccess: (result) => { |
| 164 | + setAbiEncodedRequest(result.abiEncodedRequest); |
| 165 | + }, |
| 166 | +``` |
| 167 | + |
| 168 | +The `abiEncodedRequest` state is then read and displayed within the component. |
| 169 | + |
| 170 | +:::warning |
| 171 | +In order for the `useMutation` hook to work, the app needs to be wrapped in a [`QueryClientProvider`](https://tanstack.com/query/latest/docs/framework/react/reference/QueryClientProvider) component. |
| 172 | +Ideally, that is done at the root level of the app. |
| 173 | +::: |
| 174 | + |
| 175 | +The full code for the component is included here. |
| 176 | + |
| 177 | +<CodeBlock |
| 178 | + language="tsx" |
| 179 | + title="Code for the PrepareWeb2JsonRequestApp component" |
| 180 | +> |
| 181 | + {PrepareWeb2JsonRequestCode} |
| 182 | +</CodeBlock> |
0 commit comments