|
1 | | -# Substrate Front End Template |
| 1 | +# substrate_node_front_end |
| 2 | +A Polkadot.js API + React Front End to the Grassland Substrate Node |
2 | 3 |
|
3 | | -This template allows you to create a front-end application that connects to a |
4 | | -[Substrate](https://github.com/paritytech/substrate) node back-end with minimal |
5 | | -configuration. To learn about Substrate itself, visit the |
6 | | -[Substrate Documentation](https://docs.substrate.io). |
7 | | - |
8 | | -The template is built with [Create React App](https://github.com/facebook/create-react-app) |
9 | | -and [Polkadot js API](https://polkadot.js.org/docs/api/). Familiarity with these tools |
10 | | -will be helpful, but the template strives to be self-explanatory. |
11 | | - |
12 | | -## Using The Template |
13 | | - |
14 | | -### Installation |
15 | | - |
16 | | -The codebase is installed using [git](https://git-scm.com/) and [yarn](https://yarnpkg.com/). This tutorial assumes you have installed yarn globally prior to installing it within the subdirectories. For the most recent version and how to install yarn, please refer to [Yarn](https://yarnpkg.com/) documentation and installation guides. |
17 | | - |
18 | | -```bash |
19 | | -# Clone the repository |
20 | | -git clone https://github.com/substrate-developer-hub/substrate-front-end-template.git |
21 | | -cd substrate-front-end-template |
22 | | -yarn install |
23 | | -``` |
24 | | - |
25 | | -### Usage |
26 | | - |
27 | | -You can start the template in development mode to connect to a locally running node |
28 | | - |
29 | | -```bash |
30 | | -yarn start |
31 | | -``` |
32 | | - |
33 | | -You can also build the app in production mode, |
34 | | - |
35 | | -```bash |
36 | | -yarn build |
37 | | -``` |
38 | | - |
39 | | -and open `build/index.html` in your favorite browser. |
40 | | - |
41 | | -### Try the Hosted Version |
42 | | - |
43 | | -Connecting to Polkadot:<br/> |
44 | | -https://substrate-developer-hub.github.io/substrate-front-end-template?rpc=wss://rpc.polkadot.io |
45 | | - |
46 | | -Connecting to your local Substrate node (Chrome and Firefox only):<br/> |
47 | | -https://substrate-developer-hub.github.io/substrate-front-end-template?rpc=ws://localhost:9944 |
48 | | - |
49 | | -Connecting to the development Substrate node `wss://dev-node.substrate.dev`:<br/> |
50 | | -https://substrate-developer-hub.github.io/substrate-front-end-template |
51 | | - |
52 | | - |
53 | | -## Configuration |
54 | | - |
55 | | -The template's configuration is stored in the `src/config` directory, with |
56 | | -`common.json` being loaded first, then the environment-specific json file, |
57 | | -and finally environment variables, with precedence. |
58 | | - |
59 | | -- `development.json` affects the development environment |
60 | | -- `test.json` affects the test environment, triggered in `yarn test` command. |
61 | | -- `production.json` affects the production environment, triggered in |
62 | | - `yarn build` command. |
63 | | - |
64 | | -Some environment variables are read and integrated in the template `config` object, |
65 | | -including: |
66 | | - |
67 | | -- `REACT_APP_PROVIDER_SOCKET` overriding `config[PROVIDER_SOCKET]` |
68 | | - |
69 | | -More on [React environment variables](https://create-react-app.dev/docs/adding-custom-environment-variables). |
70 | | - |
71 | | -When writing and deploying your own front end, you should configure: |
72 | | - |
73 | | -- `PROVIDER_SOCKET` in `src/config/production.json` pointing to your own |
74 | | - deployed node. |
75 | | - |
76 | | -### Specifying Connecting WebSocket |
77 | | - |
78 | | -There are two ways to specify it: |
79 | | - |
80 | | -- With `PROVIDER_SOCKET` in `{common, development, production}.json`. |
81 | | -- With `rpc=<ws or wss connection>` query parameter after the URL. This overrides the above setting. |
82 | | - |
83 | | -## Reusable Components |
84 | | - |
85 | | -### useSubstrate Custom Hook |
86 | | - |
87 | | -The custom hook `useSubstrate()` provides access to the Polkadot js API and thus the |
88 | | -keyring and the blockchain itself. Specifically it exposes this API. |
89 | | - |
90 | | -```js |
91 | | -{ |
92 | | - setCurrentAccount: func(acct) {...} |
93 | | - state: { |
94 | | - socket, |
95 | | - keyring, |
96 | | - keyringState, |
97 | | - api, |
98 | | - apiState, |
99 | | - currentAccount |
100 | | - } |
101 | | -} |
102 | | -``` |
103 | | - |
104 | | -- `socket` - The remote provider socket it is connecting to. |
105 | | -- `keyring` - A keyring of accounts available to the user. |
106 | | -- `keyringState` - One of `"READY"` or `"ERROR"` states. `keyring` is valid |
107 | | - only when `keyringState === "READY"`. |
108 | | -- `api` - The remote api to the connected node. |
109 | | -- `apiState` - One of `"CONNECTING"`, `"READY"`, or `"ERROR"` states. `api` is valid |
110 | | - only when `apiState === "READY"`. |
111 | | -- `currentAccount` - The current selected account pair in the application context. |
112 | | -- `setCurrentAccount` - Function to update the `currentAccount` value in the application context. |
113 | | - |
114 | | -If you are only interested in reading the `state`, there is a shorthand `useSubstrateState()` just to retrieve the state. |
115 | | - |
116 | | -### TxButton Component |
117 | | - |
118 | | -The [TxButton](./src/substrate-lib/components/TxButton.js) handles basic [query](https://polkadot.js.org/docs/api/start/api.query) and [transaction](https://polkadot.js.org/docs/api/start/api.tx) requests to the connected node. |
119 | | -You can reuse this component for a wide variety of queries and transactions. See [src/Transfer.js](./src/Transfer.js) for a transaction example and [src/Balances.js](./src/ChainState.js) for a query example. |
120 | | - |
121 | | -### Account Selector |
122 | | - |
123 | | -The [Account Selector](./src/AccountSelector.js) provides the user with a unified way to |
124 | | -select their account from a keyring. If the Balances module is installed in the runtime, |
125 | | -it also displays the user's token balance. It is included in the template already. |
126 | | - |
127 | | -## Miscellaneous |
128 | | - |
129 | | -- Polkadot-js API and related crypto libraries depend on [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) that is only supported by modern browsers. To ensure that react-scripts properly transpile your webapp code, update the `package.json` file: |
130 | | - |
131 | | - ```json |
132 | | - { |
133 | | - "browserslist": { |
134 | | - "production": [ |
135 | | - ">0.2%", |
136 | | - "not ie <= 99", |
137 | | - "not android <= 4.4.4", |
138 | | - "not dead", |
139 | | - "not op_mini all" |
140 | | - ] |
141 | | - } |
142 | | - } |
143 | | - ``` |
144 | | - |
145 | | - Refer to [this doc page](https://github.com/vacp2p/docs.wakuconnect.dev/blob/develop/content/docs/guides/07_reactjs_relay.md). |
| 4 | +See "docs" directory for Substrate Front End template documentation |
0 commit comments