Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 98e9d9e

Browse files
add stream ether transfer tutorial
1 parent 9d6247c commit 98e9d9e

File tree

25 files changed

+426
-259
lines changed

25 files changed

+426
-259
lines changed

content/guides/ethereum/_index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ title: Ethereum
1414

1515
#### Tutorials
1616

17-
- [Track transaction in real-time] ({{< ref "/guides/ethereum/tutorials/track_trx" >}})
17+
- [Stream the latest ether transfers]({{< ref "/guides/ethereum/tutorials/stream" >}})
1818
- [Search with query language] ({{< ref "/guides/ethereum/tutorials/search" >}})
19+
- [Track transaction in real-time] ({{< ref "/guides/ethereum/tutorials/lifecycle" >}})
1920

2021
<--->
2122

content/guides/ethereum/tutorials/_index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ protocol: ETH
77

88
In this section, you will find the list of tutorials currently written, allowing you to easily map your use case to one or more dfuse concepts under a concrete code repository.
99

10-
- [Track transaction in real-time]({{< ref "/guides/ethereum/tutorials/track_trx" >}}) (<small>Keywords: dfuse Lifecycle, JavaScript, React, Browser</small>)
10+
- [Stream the latest ether transfers]({{< ref "/guides/ethereum/tutorials/stream" >}}) (<small>Keywords: dfuse Stream, JavaScript, React, Browser</small>)
1111
- [Search with query language]({{< ref "/guides/ethereum/tutorials/search" >}}) (<small>Keywords: dfuse Search, JavaScript, React, Browser</small>)
12+
- [Track transaction in real-time]({{< ref "/guides/ethereum/tutorials/lifecycle" >}}) (<small>Keywords: dfuse Lifecycle, JavaScript, React, Browser</small>)
1213

1314
<!--
1415
List of potential other tutorials we had:

content/guides/ethereum/tutorials/track_trx.md renamed to content/guides/ethereum/tutorials/lifecycle.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
---
2-
weight: 2
3-
menuTitle: Track transaction in real-time
4-
title: Track transaction in Real-time
2+
weight: 3
3+
menuTitle: Track transaction lifecycle in real-time
4+
title: Track transaction lifecycle real-time
55
release: beta
66
---
77

88
In this guide we will create a simple React application that will use dfuse's Transaction State Tracker API to keep track of the state of an Ethereum transaction in real time. To do that, we will be using {{< external-link href="https://reactjs.org/docs/hooks-intro.html" title="react hooks">}}.
99

10+
![Ethereum Lifecycle Demo](/img/eth-lifecycle.gif)
11+
1012
## 0. Completed Example
1113

1214
If you prefer to skip forward and run the completed project, run:
@@ -32,8 +34,8 @@ Use the {{< external-link href="https://github.com/facebook/create-react-app">}}
3234
{{< tabs "create-react-app">}}
3335
{{< tab title="Shell" lang="shell" >}}
3436
# get create-react-app: https://github.com/facebook/create-react-app
35-
npx create-react-app track-trx
36-
cd track-trx
37+
npx create-react-app lifecycle
38+
cd lifecycle
3739
npm start
3840
{{< /tab >}}
3941
{{< /tabs >}}

content/guides/ethereum/tutorials/search.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
---
2-
weight: 3
2+
weight: 2
33
menuTitle: Search with query language
44
title: Search with query language
55
release: beta
66
---
77

88
In this guide we will create a simple React application that will use dfuse's Search API and query language to find specific Ethereum transactions. To do that, we will be using {{< external-link href="https://reactjs.org/docs/hooks-intro.html" title="react hooks">}}.
99

10+
![Ethereum Search Demo](/img/eth-search.gif)
11+
1012
## 0. Completed Example
1113

1214
If you prefer to skip forward and run the completed project, run:
@@ -32,8 +34,8 @@ Use the {{< external-link href="https://github.com/facebook/create-react-app">}}
3234
{{< tabs "create-react-app">}}
3335
{{< tab title="Shell" lang="shell" >}}
3436
# get create-react-app: https://github.com/facebook/create-react-app
35-
npx create-react-app track-trx
36-
cd track-trx
37+
npx create-react-app search
38+
cd search
3739
npm start
3840
{{< /tab >}}
3941
{{< /tabs >}}
+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
weight: 1
3+
menuTitle: Stream the latest ether transfers
4+
title: Stream the latest ether transfers
5+
release: beta
6+
---
7+
8+
In this guide we will create a simple React application that will use dfuse's javascript client library and the stream API to easily stream all transfers happening on the Ethereum Mainnet. To do that, we will be using {{< external-link href="https://reactjs.org/docs/hooks-intro.html" title="react hooks">}}.
9+
10+
![Ethereum Stream Demo](/img/eth-stream.gif)
11+
12+
## 0. Completed Example
13+
14+
If you prefer to skip forward and run the completed project, run:
15+
16+
{{< tabs "clone-completed-example">}}
17+
{{< tab title="Shell" lang="shell" >}}
18+
# clone and install the example project
19+
git clone github.com/dfuse-io/docs
20+
cd docs/tutorials/stream
21+
yarn install
22+
yarn start
23+
{{< /tab >}}
24+
{{< /tabs >}}
25+
26+
{{< alert type="note" >}}
27+
Installing the {{< external-link href="https://reactjs.org/tutorial/tutorial.html#developer-tools" title="React Dev Tools">}} plugin for your browser is optional, but is very useful for seeing what goes on in the application.
28+
{{< /alert >}}
29+
30+
## 1. Create React App
31+
32+
Use the {{< external-link href="https://github.com/facebook/create-react-app">}} to set up your development environment so that you can use the latest JavaScript features. You’ll need to have Node >= 8.10 and npm >= 5.6 on your machine. To create a project, run:
33+
34+
{{< tabs "create-react-app">}}
35+
{{< tab title="Shell" lang="shell" >}}
36+
# get create-react-app: https://github.com/facebook/create-react-app
37+
npx create-react-app stream
38+
cd stream
39+
npm start
40+
{{< /tab >}}
41+
{{< /tabs >}}
42+
43+
then open ({{< external-link href="http://localhost:3000/">}})
44+
45+
## 2. Get your API key
46+
47+
{{< dfuse-account-creation >}}
48+
49+
## 3. Add the dfuse Client Library
50+
51+
The simplest way to get started with dfuse and JavaScript/TypeScript development is to use the dfuse JS client library.
52+
53+
{{< tabs "adding-dfuse-client-lib">}}
54+
{{< tab title="NPM" lang="shell" >}}
55+
# https://www.npmjs.com/package/@dfuse/client
56+
npm install --save @dfuse/client
57+
{{< /tab >}}
58+
{{< /tabs >}}
59+
60+
## 4. Setup dfuse Client
61+
62+
Import the necessary functions from `dfuse/client` at the top of `src/App.js`.
63+
64+
{{< tabs "setting-up-dfuse-client1-import">}}
65+
{{< tab title="src/App.js" lang="javascript" >}}
66+
{{< code-section "tutorials_eth_stream_section1">}}
67+
{{< /tab >}}
68+
{{< /tabs >}}
69+
70+
Initialize the dfuse client using the API key you created in the second step. Let's create the `dfuseClient` right after the `function App()` declaration.
71+
72+
{{< tabs "setting-up-dfuse-client1-initialize">}}
73+
{{< tab title="src/App.js" lang="javascript" >}}
74+
{{< code-section "tutorials_eth_stream_section2">}}
75+
{{< /tab >}}
76+
{{< /tabs >}}
77+
78+
## 5. Craft the GraphQL query
79+
A GraphQL subscription will continuously stream responses from the API. We will use a GraphQL subscription to return the the latest transfers for up to 100 results.
80+
81+
{{< alert type="note" >}}
82+
With GraphQL, you can choose to request as little or as much data as needed. Therefore you can shrink down the query to only 6 lines and only request the `transactionHash` if you prefer.
83+
84+
See [Getting Started with GraphQL](/guides/core-concepts/graphql/) for more information.
85+
{{< /alert >}}
86+
87+
We use the following parameters for the GraphQL query:
88+
89+
- `query`: query string to tell the API what you want to search for
90+
- `indexName`: (CALLS | LOGS) type of data to search for
91+
- `limit`: limit of results to return
92+
- `sort`: (ASC | DESC) ascending or desending direction to search in
93+
- `cursor`: chain-wide pointer to an exact location that allows you to resume your search at
94+
95+
The query string `-value:0` indicates that the results must have non-zero ether values within the transactions.
96+
97+
{{< alert type="tip" >}}
98+
See [Query Langauge](/guides/core-concepts/search-query-language/) to learn more about what you can search.
99+
{{< /alert >}}
100+
101+
{{< tabs "stream-query">}}
102+
{{< tab title="src/App.js" lang="javascript" >}}
103+
{{< code-section "tutorials_eth_stream_section3">}}
104+
{{< /tab >}}
105+
{{< /tabs >}}
106+
107+
## 6. Setup our Hooks
108+
109+
Lets setup a few hooks that will help us keep track of our transaction states and render our component. We use {{< external-link title="react state hook" href="https://reactjs.org/docs/hooks-state.html">}} to accomplish this.
110+
111+
- `transfers`: array that stores all the received transactions
112+
- `state`: stores the current state of the GraphQL subscription
113+
- `error`: stores our errors
114+
- `stream`: object to listen for events on
115+
116+
{{< tabs "setup-hooks">}}
117+
{{< tab title="src/App.js" lang="javascript" >}}
118+
{{< code-section "tutorials_eth_stream_section4">}}
119+
{{< /tab >}}
120+
{{< /tabs >}}
121+
122+
## 7. Stream Transfers Function
123+
124+
Create an `async` function `streamTransfers` that will use the dfuse JS client to execute the GraphQL subscription we crafted above and initialize a few state variables.
125+
126+
The message returned can have types of `error`, `data`, and `complete`.
127+
We handle each case by setting the `errors`, `transfers`, or `state` hooks to tell our app what to display.
128+
129+
{{< tabs "fetch-transaction-init">}}
130+
{{< tab title="src/App.js" lang="javascript" >}}
131+
{{< code-section "tutorials_eth_stream_section5">}}
132+
{{< /tab >}}
133+
{{< /tabs >}}
134+
135+
## 8. Stopping Stream and Disconnects
136+
137+
Define functions to stop the stream, handle streaming client closing and error catching.
138+
139+
{{< tabs "fetch-transaction-func-setup">}}
140+
{{< tab title="src/App.js" lang="javascript" >}}
141+
{{< code-section "tutorials_eth_stream_section6">}}
142+
{{< /tab >}}
143+
{{< /tabs >}}
144+
145+
## 9. Render Function
146+
147+
Build the `render` method for this component. It includes a launch and stop button to trigger the functions we defined. It also renders the list of transfers stored in our react hook.
148+
149+
{{< tabs "fetch-transaction-render">}}
150+
{{< tab title="src/App.js" lang="javascript" >}}
151+
{{< code-section "tutorials_eth_stream_section7">}}
152+
{{< /tab >}}
153+
{{< /tabs >}}
154+
155+
## 10. Prettifying it with CSS
156+
157+
Add some CSS to style this HTML a bit. Replace the contents of `src/App.css` with the following:
158+
159+
{{< tabs "fetch-transaction-css">}}
160+
{{< tab title="src/App.js" lang="javascript" >}}
161+
{{< code-section "tutorials_eth_stream_css_section1">}}
162+
{{< /tab >}}
163+
{{< /tabs >}}
164+
165+
## 11. Full Working Example
166+
167+
The source code for this tutorial is available on {{< external-link href="https://github.com/dfuse-io/docs/tree/master/tutorials/eth/stream" title="GitHub" >}}. Below are the code files discussed on this page.
168+
169+
{{< tabs "fetch-transaction-full-app">}}
170+
{{< tab title="src/App.js" lang="javascript" >}}
171+
{{< code-section "tutorials_eth_stream_section8">}}
172+
{{< /tab >}}
173+
{{< tab title="src/App.js" lang="javascript" >}}
174+
{{< code-section "tutorials_eth_stream_css_section1">}}
175+
{{< /tab >}}
176+
{{< /tabs >}}

content/reference/ethereum/search-terms.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ The following fields go in the `query` string of those calls.
3131
### `method` _string_ or _4-bytes hexdata_
3232

3333
* description: either the signature in ascii form or the first 4 bytes (in hex format) of the keccak-256 of that signature (which can be found in the first 4 bytes of the `input`) of the **method called** (or the method that generated the **log**)
34-
* example: `method:transfer(address,uint256)` or `method:a9059cbb`
34+
* example: `method:'transfer(address,uint256)'` or `method:a9059cbb`
3535

3636

3737
## Supported terms for CALLS only

content/samples/_index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protocol: DFUSE
3333

3434
| Description | Ethereum Source | EOSIO Source |
3535
| :----------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------: | -------------------------------------------------------------------------------------------------------------------------------- |
36-
| Example of dfuse Client Library stream transfers with React | {{< external-link title="Github" href="https://github.com/dfuse-io/docs/tree/master/samples/javascript/eth/stream-transfers" >}} | {{< external-link title="Github" href="https://github.com/dfuse-io/docs/tree/master/samples/javascript/eos/stream-transfers" >}} |
36+
| Example of dfuse Client Library stream transfers with React | {{< external-link title="Github" href="https://github.com/dfuse-io/docs/tree/master/tutorials/eth/stream" >}} | {{< external-link title="Github" href="https://github.com/dfuse-io/docs/tree/master/samples/javascript/eos/stream-transfers" >}} |
3737
| Showcase on how to use the dfuse Client Library in a Node.js environment | {{< external-link title="Github" href="https://github.com/dfuse-io/docs/tree/master/samples/javascript/eth/node-server" >}} | {{< external-link title="Github" href="https://github.com/dfuse-io/docs/tree/master/samples/javascript/eos/node-server" >}} |
3838
| How to track transaction in real-time with dfuse Lifecycle | {{< external-link title="Github" href="https://github.com/dfuse-io/docs/tree/master/tutorials/eth/lifecycle" >}} | N/A |
3939
| How to search transactions with dfuse Search and render with React | {{< external-link title="Github" href="https://github.com/dfuse-io/docs/tree/master/tutorials/eth/search" >}} | N/A <!-- Migrate?--> |

0 commit comments

Comments
 (0)