Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(1367): tidy up for tutorials #1401

Merged
merged 63 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
b5d6bbe
docs(1367): tidy up for tutorials
m4sterbunny Jul 10, 2024
2fad3a8
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 22, 2024
bb5c241
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 22, 2024
246192e
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 22, 2024
f41907b
Merge branch 'main' into 1367-tabs-for-options
m4sterbunny Jul 22, 2024
9c1bdd6
completes the separation of paths for yarn etc
m4sterbunny Jul 22, 2024
3293fc1
reverts tabs in favor of dropping support for npm/npx
m4sterbunny Jul 23, 2024
a190ff0
makes step 2 optional and extends it to point that all clicks make sense
m4sterbunny Jul 24, 2024
14c2aa0
improves numbering, rmvs 6 (not a step), aligns 5 with new flow
m4sterbunny Jul 24, 2024
5212057
attempts to allow both tutorials to function as hello world && to dir…
m4sterbunny Jul 25, 2024
258b8a2
final proofs
m4sterbunny Jul 25, 2024
6ac31ca
final proofs
m4sterbunny Jul 25, 2024
d0adc06
final proofs?
m4sterbunny Jul 25, 2024
40c535a
final proofs?
m4sterbunny Jul 25, 2024
3d90ca5
Merge branch 'main' into 1367-tabs-for-options
alexandratran Jul 26, 2024
2157f89
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
65aadfa
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
8b9a9cb
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
8f0ad03
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
ac27064
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
a0e21ec
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
b81e00d
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
0b010b5
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
b04aa0b
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
ffa431d
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
384ab2a
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
23b1a75
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
a1d3513
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 26, 2024
a735092
aligns both per proofs
m4sterbunny Jul 26, 2024
5272ab9
proofs and toc4
m4sterbunny Jul 26, 2024
a303055
proofs
m4sterbunny Jul 26, 2024
8bf9f6a
proofs
m4sterbunny Jul 26, 2024
c81ae2e
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
66f7113
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
3215d4c
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
bfe03c0
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
34d44f8
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
10388f6
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
27f8131
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
3c057b6
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
b6b6ba8
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
40a0a90
Update snaps/learn/tutorials/gas-estimation.md
m4sterbunny Jul 31, 2024
1e7e335
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
cf642ca
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
dc15ffa
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
2e12017
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
94e9fdf
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
5af492f
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
f5fb2ed
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
cdcf0f0
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
090e3fc
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
f70ebb5
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
eea2e77
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Jul 31, 2024
5e901a3
proposes solution for fragements
m4sterbunny Jul 31, 2024
c0937ef
docs: Reverts tabbed full code and applies proofs
m4sterbunny Aug 1, 2024
81ed3e0
fixes code sample and alt method to disable button
m4sterbunny Aug 1, 2024
171981b
Merge branch 'main' into 1367-tabs-for-options
m4sterbunny Aug 1, 2024
de3fbd5
Add closing tag
joaniekube Aug 1, 2024
13ac6f4
Merge branch 'main' into 1367-tabs-for-options
alexandratran Aug 2, 2024
5a025c4
edits
alexandratran Aug 2, 2024
132d735
minor fix
alexandratran Aug 2, 2024
5946246
Update snaps/learn/tutorials/transaction-insights.md
m4sterbunny Aug 2, 2024
2a57a21
Apply suggestions from code review
alexandratran Aug 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 110 additions & 72 deletions snaps/learn/tutorials/gas-estimation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,52 +31,58 @@ starter kit by running:
yarn create @metamask/snap gas-estimation-snap
```

or

```bash
npx @metamask/create-snap gas-estimation-snap
```

or

```bash
npm create @metamask/snap gas-estimation-snap
```

Next, `cd` into the `gas-estimation-snap` project directory and run:

```bash
yarn install
```

This initializes your development environment with the required dependencies.
You may get a warning similar to the following:
This initializes your development environment with the required dependencies.

<details>
<summary>Did you get a warning?</summary>
<div>
You may get a warning such as:

```bash
@lavamoat/allow-scripts has detected dependencies without configuration. explicit configuration required.
run "allow-scripts auto" to automatically populate the configuration.
```

You can resolve this error by running the following command:

You can resolve this error by running:

```bash
yarn run allow-scripts auto
```
</div>
</details>


### 2. (Optional) Customize your Snap's UX

This Snap is generated from a TypeScript template Snap. We recommend customizing your
Snap to improve its UX, but this is optional for testing. If you don't wish to customize your Snap,
skip to [Step 3](#3-enable-network-access).

### 2. Add a custom icon
#### 2.1. Provide an icon

Your Snap must [display an icon](../best-practices/design-guidelines.md#optimize-your-metadata) in MetaMask.
To add an icon, create a new folder `images` in the Snap package `packages/snap/`:
[Optimize your metadata](../best-practices/design-guidelines.md#optimize-your-metadata) and display an icon for your Snap in MetaMask.

Create a new folder `images` in the Snap package `packages/snap/`:

```bash
mkdir packages/snap/images
```

Download
[this `gas.svg` icon file](https://raw.githubusercontent.com/Montoya/gas-fee-snap/main/packages/snap/images/gas.svg)
into the `ìmages` folder you just created.
This is a free icon, "Gas" by Mello from
[Noun Project](https://thenounproject.com/browse/icons/term/gas/).
into that `ìmages` folder.

<details>
<summary>Icon attribution</summary>
<div>
This is a free icon, "Gas" by Mello from the [Noun Project](https://thenounproject.com/browse/icons/term/gas/).
</div>
</details>

Your file structure should look like this:

Expand All @@ -102,10 +108,9 @@ gas-estimation-snap/
├─ ... (other stuff)
```

Open `packages/snap/snap.manifest.json` in a text editor.
This file contains the main configuration details for your Snap.
Edit the `npm` object, within the `location` object,
and add `iconPath` with the value `"images/gas.svg"` to point to your new icon:
Open `packages/snap/snap.manifest.json` in a text editor. This file contains the main configuration
details for your Snap. Edit the `npm` object, within the `location` object, and add `iconPath` with
the value `"images/gas.svg"` to point to your new icon:

```json title="snap.manifest.json"
"location": {
Expand All @@ -118,8 +123,8 @@ and add `iconPath` with the value `"images/gas.svg"` to point to your new icon:
}
```

Open `packages/snap/package.json` in a text editor.
Edit the `files` array and add the `images/` folder:
Open `packages/snap/package.json` in a text editor. Edit the `files` array and reference the
`images/` folder:

```json title="package.json"
"files": [
Expand All @@ -129,6 +134,39 @@ Edit the `files` array and add the `images/` folder:
],
```

#### 2.2. Update your Snap's name

[Optimize your metadata](../best-practices/design-guidelines.md#optimize-your-metadata) and update
your Snap's name in MetaMask.
MetaMask uses the `proposedName` of the Snap, currently "TypeScript Example" in the template.

Open `packages/snap/snap.manifest.json` in a text editor.
Edit the `"proposedName"` property within the metadata to provide a functional name such as
"Gas Estimator":

```json title="snap.manifest.json"
{
"version": "0.1.0",
"description": "An example Snap written in TypeScript.",
"proposedName": "Gas Estimator",
...
}
```

#### 2.3. Update your Snap's button

Open `packages/site/src/components/Buttons.tsx` in a text editor.
Edit the `Button` property to provide functional label text such as "Estimate Gas":

```typescript title="Buttons.tsx"
export const SendHelloButton = (props: ComponentProps<typeof Button>) => {
return <Button {...props}>Estimate Gas</Button>;
};
````

These three updates are the minimum required to ensure that each user interaction with your Snap is well-informed.
However, your Snap will function without these tweaks.

### 3. Enable network access

To enable your Snap to [access the internet using the `fetch` API](../../features/network-access.md),
Expand All @@ -144,7 +182,6 @@ permission in `packages/snap/snap.manifest.json`:
},
"endowment:network-access": {}
},
"manifestVersion": "0.1"
```

### 4. Fetch gas fee estimates
Expand Down Expand Up @@ -187,7 +224,7 @@ Update the `hello` method with the following code:
case "hello":
const fees = await getFees();
return snap.request({
method: 'snap_dialog',
method: "snap_dialog",
params: {
type: "alert",
content: (
Expand All @@ -208,7 +245,7 @@ case "hello":
case "hello":
const fees = await getFees();
return snap.request({
method: 'snap_dialog',
method: "snap_dialog",
params: {
type: "alert",
content: panel([
Expand All @@ -223,72 +260,73 @@ case "hello":
</TabItem>
</Tabs>

### 5. Build and test the Snap
### 5. Build and test your Snap

To build and test your Snap:
Complete the following steps to build and test your Snap:

1. Open `package.json` in the root directory of the project, and increment the `"version"` (if the `"version"` is
`0.1.0`, increase it to `0.2.0`).
#### 5.1. Build your Snap

2. From the command line, run `yarn start`.
In the terminal, at the bottom of the message log, you see the browser URL:
From the command line, run `yarn start`.
The following displays:

```bash
You can now view site in the browser.

```bash
You can now view site in the browser.
http://localhost:8000/
```

http://localhost:8000/
```
Open [`localhost:8000`](http://localhost:8000/) in your browser (with MetaMask Flask installed).
A page like the following displays:

3. Open [`localhost:8000`](http://localhost:8000/) in your browser (with MetaMask Flask installed).
A page like the following displays:
<img src={require('../../assets/template-snap.png').default} alt="Test dapp with template Snap" style={{border: '1px solid #DCDCDC'}} />

<img src={require('../../assets/template-snap.png').default} alt="Test dapp with template Snap" style={{border: '1px solid #DCDCDC'}} />
This is a template test dapp for installing and testing your Snap.

This is a boilerplate test dapp for installing and testing your Snap.
#### 5.2. Test your Snap

4. Select **Connect** to connect Flask to the dapp.
After connecting, you're prompted to install the Snap with the following permissions:
Select **Connect** to connect Flask to the dapp.
After connecting, you're prompted to install the Snap with the following permissions:

- **Allow dapps to communicate directly with this Snap.**
- **Access the internet.**
- **Display dialog windows in MetaMask.**
- **Allow websites to communicate directly with this Snap.**
- **Access the internet.**
- **Display dialog windows in MetaMask.**

5. Select **Approve** > **Install**.
Next, select **Confirm** > **OK**.

6. After installing, the **Send message** button on the page is enabled. Select this button. A dialog prompt displays with the response from the gas fee API:
Select the **Send message** button (or **Estimate gas** button, if you followed Step 2). A dialog prompt displays with the response from the gas fee API:

<p align="center">
<img src={require('../../assets/gas-estimation.png').default} alt="Gas estimation dialog" width="400px" style={{border: '1px solid #DCDCDC'}} />
</p>

You have integrated a public API into MetaMask and displayed real-time gas fee estimates.

### 6. Next steps
Congratulations, you have integrated a public API into MetaMask and displayed real-time gas fee estimates.

Next, you can try:
### Next steps

You can improve your Snap's UX by:
- Completing [Step 2](#2-optional-customize-your-snaps-ux).
- Parsing the JSON response from the remote API.
- Displaying the fees in a nicely formatted way.
- Formatting the fees for better readability.

You can also update the fields in `snap.manifest.json` to match your custom Snap:
Before publishing a Snap, it's also important to customize the metadata and properties of your Snap:

- `proposedName` - The name of your Snap.
- `description` - The description of your Snap.
- `source` - The `shasum` is set automatically when you build from the command line.
If you decided to publish your Snap to `npm`, update the `location` to its published location.
- Update the `location` in `snap.manifest.json` to your Snap's published location.
- Update the `description` in `snap.manifest.json` to a description of your Snap.
- Update the `name`, `version`, `description`, and `repository` fields of
`/packages/snap/package.json` (even if you do not plan to publish your Snap to npm).
- Update the content of `/packages/site/src/pages/index.tsx` by changing the
name of the method for showing gas fee estimates. If you change the method name in
`/packages/site/src/pages/index.tsx`, ensure you change the method name in
`/packages/snap/src/index.ts` to match.

Similarly, you should update the `name`, `version`, `description`, and `repository` fields of
`/packages/snap/package.json` even if you do not plan to publish your Snap to npm.
:::note
When editing `source`, the `shasum` is set automatically when you build from the command line.
:::

:::caution important
The `version` and `repository` fields in `snap.manifest.json` inherit the values from
`package.json` and overwrite them in `snap.manifest.json`.
We recommend updating `version` and `repository` in `package.json` first, then building the Snap project.
We recommend updating `version` and `repository` in `package.json` first, then building your Snap project.
:::

You can update the content of `/packages/site/src/pages/index.tsx` by changing the
name of the method for showing gas fee estimates.
If you change the method name in `/packages/site/src/pages/index.tsx`, ensure you change the method name in `/packages/snap/src/index.ts` to match.

After you have made all necessary changes, you can
[publish your Snap to npm](../../how-to/publish-a-snap.md).
After you have made all necessary changes, you can [publish your Snap to npm](../../how-to/publish-a-snap.md).
Loading
Loading