You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sdk/serverless-signing/fetch.md
+40-90Lines changed: 40 additions & 90 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,119 +3,69 @@ import FeedbackComponent from "@site/src/pages/feedback.md";
3
3
import Tabs from '@theme/Tabs';
4
4
import TabItem from '@theme/TabItem';
5
5
6
-
# Using Fetch
7
-
8
-
## Prerequisites
9
-
10
-
- Familiarity with JavaScript
11
-
- Basic understanding of [serverless signing](../serverless-signing/quick-start.md)
6
+
# Fetching Data from the Web within a Lit Action
12
7
13
8
## Overview
14
-
Unlike traditional smart contract ecosystems, Lit Actions can natively talk to the external world. This is useful for things like fetching data from the web, or sending API requests to other services.
15
9
16
-
The Lit Action below will get the current temperature from the [National Weather Service](https://www.weather.gov/) API, and ONLY sign a txn if the temperature is forecast to be **above 60 degrees F**. Since you can put this HTTP request and logic that uses the response directly in your Lit Action, you don't have to worry about using a 3rd party oracle to pull data in.
10
+
Unlike traditional smart contract ecosystems, Lit Actions can natively talk to the external world. This is useful for things like fetching data from the web, or sending API requests to other services.
17
11
18
-
### How it works
12
+
This Lit Action fetches the current temperature from the [National Weather Service](https://www.weather.gov/) API. It will only sign the given message if the temperature forecast exceeds 60°F (15.5°C). By incorporating the API request and response logic directly within the Lit Action, you eliminate the need for a third-party oracle.
19
13
20
-
The HTTP request will be sent out by all the Lit Nodes in parallel, and consensus is based on at least 2/3 of the nodes getting the same response. If less than 2/3 nodes get the same response, then the user can not collect the signature shares above the threshold and therefore cannot produce the final signature. Note that your HTTP request will be sent N times where N is the number of nodes in the Lit Network, because it's sent from every Lit Node in parallel.
14
+
## Prerequisites
21
15
22
-
Be careful about how many requests you're making and note that this may trigger rate limiting issues on some servers.
16
+
- Basic understanding of [PKPs](../../../user-wallets/pkps/overview)
17
+
- Basic understanding of [Lit Actions](../serverless-signing/quick-start)
23
18
24
-
## Example
19
+
## Complete Code Example
25
20
26
-
### Lit Action code
21
+
The complete code example is available in the [Lit Developer Guides Code Repository](https://github.com/LIT-Protocol/developer-guides-code/tree/master/lit-action-using-fetch). There you can find a Node.js and browser implementation of this example code.
27
22
28
-
:::note
29
-
`toSign` data is required to be in 32 byte format.
23
+
### Example Lit Action
30
24
31
-
The `ethers.utils.arrayify(ethers.utils.keccak256(...)` can be used to convert the `toSign` data to the correct format.
32
-
:::
25
+
The `signEcdsa` function returns a boolean value stored in the `sigShare` variable. It's `true` if the message is successfully signed, and `false` if an error occurs during the Lit Action. If the temperature is below 60°F (15.5°C), the Lit Action will instead return a response message to the user.
You can use fetch() inside a Lit Action to write data, but caution is necessary. The HTTP request will execute multiple times, once for each Lit Node in the network.
52
+
When writing data, it's crucial to use operations that produce the same result regardless of how often they're repeated. Consider these examples:
You can also use fetch() inside a Lit Action to write data, but you **must be careful** (because the HTTP request will be run N times where N is the number of Lit Nodes). On Datil networks, N is 10, so any fetch() request will be sent to the server 10 times.
56
+
- SQL Update: Running it multiple times only changes the row once.
57
+
- Result: Consistent outcome, regardless of repetition.
89
58
90
-
**This is safe**, however, if the place you're writing the data to is *idempotent*. Idempotent means that applying the same operation over and over will not change the result. So for example, a SQL Insert is not idempotent, because if you run it 10 times, it will create 10 rows. On the other hand, a SQL Update is idempotent, because if you run it 10 times, it will only update the row once. So if you're using fetch() to write data, make sure the server you're writing to is idempotent.
59
+
2. Non-repeatable operation (avoid):
91
60
92
-
### Lit Action code
61
+
- SQL Insert: Each execution creates a new row.
62
+
- Result: Unintended duplicates with multiple executions.
93
63
94
-
```jsx
95
-
construnLitAction=async () => {
96
-
if (day ==="") {
97
-
alert("Select a day first!");
98
-
return;
99
-
}
64
+
For Lit Actions using fetch() to write data, aim for repeatable operations. This approach prevents issues like duplicate entries or unintended changes if the request repeats due to network conditions or distributed execution across nodes.
This guide demonstrates how to fetch data from the web within a Lit Action.
118
68
119
-
```
69
+
If you'd like to learn more about Lit Actions, check out the [Lit Actions SDK](https://actions-docs.litprotocol.com/), or our [Advanced Topics](https://developer.litprotocol.com/category/advanced-topics-1) section on Lit Actions.
0 commit comments