Skip to content

Commit f53c4f5

Browse files
feat(docs): add prepare web2json request guide
1 parent c34ea96 commit f53c4f5

File tree

7 files changed

+467
-3
lines changed

7 files changed

+467
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
sidebar_position: 1
3+
slug: tanstack-query-prepare-request
4+
title: Prepare request (Tanstack Query)
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+
24+
In this guide we will prepare an ABI encoded request that can be submitted to the FDC.
25+
This is a core step of the [FDC process](/fdc/overview).
26+
In order to be able to submit request data to the protocol, we need to encode it in a specific way.
27+
While this process can be done manually, it is recommended that the data is encoded through a special verifier server.
28+
Besides encoding the data, the server also checks its validity.
29+
Thus, it is a useful tool for catching user error.
30+
31+
We will make the request to the verifier server with the [Tanstack Query](https://tanstack.com/query/latest/docs/framework/react/overview) library.
32+
Because we want the request to be sent only when the user presses a button, we will not employ its `useQuery` hook.
33+
Instead, we will utilize [`useMutation`](https://tanstack.com/query/latest/docs/framework/react/guides/mutations).
34+
35+
<BrowserOnly>{() => <PrepareWeb2JsonRequestApp />}</BrowserOnly>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
sidebar_position: 1
3+
slug: prepare-request
4+
title: Prepare 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+
24+
In the following series of guides we will prepare different ABI encoded requests that can be submitted to the FDC.
25+
The guides will showcase code for making a request of each [attestation type](/fdc/attestation-types/).
26+
They will also include an interactive widget for testing the code.
27+
While we could pack all of these into a single component with selectable tabs, we believe it would make for a less understandable code.
28+
For that reason, each guide will cover only one attestation type.
29+
30+
Preparing the request is a core step of the [FDC process](/fdc/overview).
31+
In order to be able to submit request data to the protocol, we need to encode it in a specific way.
32+
While this process can be done manually, it is recommended that the data is encoded through a special verifier server.
33+
Besides encoding the data, the server also checks its validity.
34+
Thus, it is a useful tool for catching user error.
35+
36+
We will make the request to the verifier server with the [Tanstack Query](https://tanstack.com/query/latest/docs/framework/react/overview) library.
37+
Because we want the request to be sent only when the user presses a button, we will not employ its `useQuery` hook.
38+
Instead, we will utilize [`useMutation`](https://tanstack.com/query/latest/docs/framework/react/guides/mutations).
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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>

sidebars.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,18 @@ const sidebars: SidebarsConfig = {
289289
collapsed: true,
290290
link: { type: "doc", id: "frontend/overview" },
291291
items: [
292-
"frontend/tanstack-query-get-contract-addresses",
292+
{
293+
type: "category",
294+
label: "Tanstack Query",
295+
collapsed: true,
296+
link: {
297+
slug: "/frontend/tanstack-query",
298+
type: "generated-index",
299+
},
300+
items: [
301+
{ type: "autogenerated", dirName: "frontend/tanstack-query" },
302+
],
303+
},
293304
{
294305
type: "category",
295306
label: "Wagmi",

0 commit comments

Comments
 (0)