Skip to content

Commit

Permalink
bump intro-to-sway guide
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahschwartz committed May 24, 2024
1 parent 64cb8d4 commit e3a9dd4
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 73 deletions.
10 changes: 0 additions & 10 deletions docs/guides/docs/contract-quickstart/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ Getting started with Fuel as a smart contract developer is as simple as:
commentType="{/*"
/>

### Testnet toolchain

To use the `testnet` toolchain, run the commands below:

```sh
fuelup self update
fuelup toolchain install testnet
fuelup default testnet
```

## Generating a counter contract

Run the command below to generate a counter contract in Sway:
Expand Down
2 changes: 2 additions & 0 deletions docs/guides/docs/counter-dapp/building-a-frontend.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ To build a frontend application for the counter contract, we'll do the following

## Install the Fuel Browser Wallet

{/*install_wallet:example:start*/}
Our frontend application will allow users to connect with a wallet, so you'll need to have a browser wallet installed.

The current Fuel Wallet release can only be used with the `beta-5` network. To use the new testnet, you must use the [Fuel Development Wallet](https://chromewebstore.google.com/detail/fuel-wallet-development/hcgmehahnlbhpilepakbdinkhhaackmc).

You can get test funds for the testnet by visiting the [Testnet Faucet](https://faucet-testnet.fuel.network/).
{/*install_wallet:example:end*/}

## Initialize a React project

Expand Down
2 changes: 2 additions & 0 deletions docs/guides/docs/counter-dapp/building-a-smart-contract.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ tree .
4 directories, 8 files
```

{/*rust_harness:example:start*/}
We have two new files!

- The `Cargo.toml` is the manifest for our new test harness and specifies the required dependencies including `fuels` (the Fuel Rust SDK).
Expand All @@ -393,6 +394,7 @@ Open your `Cargo.toml` file and check the version of `fuels` used under `dev-dep
fuels = { version = "0.62.0", features = ["fuel-core-lib"] }
tokio = { version = "1.12", features = ["rt", "macros"] }
```
{/*rust_harness:example:end*/}

Now that we have our default test harness, let's add a useful test to it.

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/docs/intro-to-sway/checkpoint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ To deploy, use the following command if you've already set up the forc-wallet an
forc deploy --testnet
```

After deploying, remember to save your contract ID. You'll need it for frontend integration.
After deploying, you'll be able to find your contract ID in the `contract/out/deployments` folder. You'll need this for frontend integration.
14 changes: 8 additions & 6 deletions docs/guides/docs/intro-to-sway/contract-functions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Next, we'll utilize the `require` statement to ensure the sent asset is the corr

The `require` statement accepts two arguments: a condition, and a value that's logged when the condition is false. Should the condition evaluate as false, the entire transaction is rolled back, leaving no changes.

In this case, the condition checks if the `asset_id` matches the `BASE_ASSET_ID`the default asset associated with the base blockchain, imported from the standard library. For example, if the base blockchain is Ethereum, the base asset would be ETH.
In this case, the condition checks if the `asset_id` matches the base asset ID — the default asset associated with the base blockchain - using `AssetId::base()`. For example, if the base blockchain is Ethereum, the base asset would be ETH.

If there's a mismatch in the asset, for instance, if someone attempts to purchase an item using a different coin, we'll trigger the custom error previously defined, passing along the `asset_id`.

Expand Down Expand Up @@ -207,14 +207,16 @@ To get the details for an item, we can create a read-only function that returns
lang="sway"
/>

To return a value in a function, you can use the `return` keyword, similar to JavaScript. Alternatively, you can omit the semicolon in the last line to return that value like in Rust. Although both methods are effective.
To return a value in a function, you can use the `return` keyword, similar to JavaScript. Alternatively, you can omit the semicolon in the last line to return that value like in Rust.

```sway
fn my_function(num: u64) -> u64{
// returning the num variable
fn my_function_1(num: u64) -> u64{
// returns the num variable
return num;
// this would also work:
}
fn my_function_2(num: u64) -> u64{
// returns the num variable
num
}
```
Expand Down
2 changes: 0 additions & 2 deletions docs/guides/docs/intro-to-sway/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ parent:

# Introduction to Sway Language for JavaScript Developers

> **Note:** This guide has not yet been updated for the new `testnet` network.
If you're familiar with JavaScript and have a basic understanding of blockchain fundamentals, you can swiftly grasp how to build full-stack decentralized applications on Fuel using Sway. Once you get a handle on Sway's essentials, you'll be able to begin building your own dapp.

Within this tutorial, we will be crafting a Sway contract for an online marketplace similar to Amazon, where:
Expand Down
18 changes: 17 additions & 1 deletion docs/guides/docs/intro-to-sway/prerequisites.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,23 @@ parent:
commentType="{/*"
/>

Make sure you have the latest version of the [Fuel Wallet](https://chrome.google.com/webstore/detail/fuel-wallet/dldjpboieedgcmpkchcjcbijingjcgok) installed.
### Testnet toolchain

To use the `testnet` toolchain, run the commands below:

```sh
fuelup self update
fuelup toolchain install testnet
fuelup default testnet
```

### Fuel Wallet

<TextImport
file="../counter-dapp/building-a-frontend.mdx"
comment="install_wallet"
commentType="{/*"
/>

Additionally for this guide, ensure you're using Node.js/npm version {props.nodeVersion}.
You can check your Node.js version with:
Expand Down
12 changes: 9 additions & 3 deletions docs/guides/docs/intro-to-sway/rust-sdk.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ action={{
/>

```bash
cargo generate --init fuellabs/sway templates/sway-test-rs --name sway-store --tag v0.49.3
cargo generate --init fuellabs/sway templates/sway-test-rs --name sway-store
```

<TextImport
file="../counter-dapp/building-a-smart-contract.mdx"
comment="rust_harness"
commentType="{/*"
/>

## Imports

We will be changing the existing `harness.rs` test file that has been generated. Firstly we need to change the imports. By importing the Fuel Rust SDK you will get majority of the functionalities housed within the prelude.
Expand Down Expand Up @@ -96,7 +102,7 @@ action={{
## Test Cases

Given the immutable nature of smart contracts, it's important to cover all potential edge cases in your tests.
Let's write the test cases at the bottom of our `harness.rs` file.
Let's delete the example `can_get_contract_id` test case and start writing some test cases at the bottom of our `harness.rs` file.

### Setting Owner

Expand Down Expand Up @@ -194,7 +200,7 @@ If you have followed the previous steps correctly your `harness.rs` test file sh

## Running the Tests

To run the test located in `tests/harness.rs`, use:
To run the test located in `tests/harness.rs`, run the command below inside your `contract` folder:

<TestAction
id="cargo-test"
Expand Down
89 changes: 41 additions & 48 deletions docs/guides/docs/intro-to-sway/typescript-sdk.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ action={{
/>

```sh
npm install fuels@0.82.0 @fuels/react@0.18.0 @fuels/connectors@0.2.2 @tanstack/react-query@5.28.9
npm install fuels @fuels/react @fuels/connectors @tanstack/react-query
```

## Generate contract types
Expand All @@ -60,7 +60,7 @@ action={{
/>

```sh
npx fuels@0.82.0 init --contracts ../contract/ --output ./src/contracts
npx fuels init --contracts ../contract/ --output ./src/contracts
```

Now that you have a `fuels.config.ts` file, you can use the `fuels build` command to rebuild your contract and generate types.
Expand Down Expand Up @@ -88,7 +88,7 @@ action={{
/>

```sh
npx fuels@0.82.0 build
npx fuels build
```

A successful process should print and output like the following:
Expand All @@ -100,70 +100,53 @@ Generating types..
🎉 Build completed successfully!
```

> If you're having any issues with this part, try adding `useBuiltinForc: false` to your `fuels.config.ts` config file to make sure it's using the same version of `forc` as your default toolchain.
Now you should be able to find a new folder `fuel-project/frontend/src/contracts`.

Open the `src/App.tsx` file, and replace the boilerplate code with the template below:
## Wallet Providers

In your `index.tsx` file, wrap your `App` component with the `FuelProvider` and `QueryClientProvider` components to enable Fuel's custom React hooks for wallet functionalities.

This is where you can pass in custom wallet connectors to customize which wallets your users can use to connect to your app.

<TestAction
id="fe-app-template"
id="fe-index-all"
action={{
name: 'writeToFile',
filepath: 'guides-testing/fuel-project/frontend/src/App.tsx'
filepath: 'guides-testing/fuel-project/frontend/src/index.tsx'
}}
/>

<CodeImport
file="../../examples/intro-to-sway/frontend/src/App.tsx"
comment="fe_app_all"
file="../../examples/intro-to-sway/frontend/src/index.tsx"
comment="fe_index_all"
commentType="//"
lang="tsx"
/>

At the top of the file, change the `CONTRACT_ID` to the contract ID that you deployed earlier and set as a constant.

<CodeImport
file="../../examples/intro-to-sway/frontend/src/App.tsx"
comment="fe_contract_id"
commentType="//"
lang="tsx"
/>
## Connecting to the contract

Copy and paste the CSS code below in your `App.css` file to add some simple styling.
Next, open the `src/App.tsx` file, and replace the boilerplate code with the template below:

<TestAction
id="fe-css-template"
id="fe-app-template"
action={{
name: 'writeToFile',
filepath: 'guides-testing/fuel-project/frontend/src/App.css'
filepath: 'guides-testing/fuel-project/frontend/src/App.tsx'
}}
/>

<CodeImport
file="../../examples/intro-to-sway/frontend/src/App.css"
comment="fe_css"
file="../../examples/intro-to-sway/frontend/src/App.tsx"
comment="fe_app_all"
commentType="//"
lang="tsx"
/>

### Connecting to the contract

Wrap your components with the `FuelProvider` and `QueryClientProvider` components to enable Fuel's custom React hooks for wallet functionalities in `index.tsx`.

This is where you can pass in custom wallet connectors to customize which wallets your users can use to connect to your app.

<TestAction
id="fe-index-all"
action={{
name: 'writeToFile',
filepath: 'guides-testing/fuel-project/frontend/src/index.tsx'
}}
/>
At the top of the file, change the `CONTRACT_ID` to the contract ID that you deployed earlier and set as a constant.

<CodeImport
file="../../examples/intro-to-sway/frontend/src/index.tsx"
comment="fe_index_all"
file="../../examples/intro-to-sway/frontend/src/App.tsx"
comment="fe_contract_id"
commentType="//"
lang="tsx"
/>
Expand Down Expand Up @@ -197,16 +180,26 @@ The `useMemo` hook is used to connect to our contract with the connected wallet.
lang="tsx"
/>

## Styling

Copy and paste the CSS code below in your `App.css` file to add some simple styling.

<TestAction
id="fe-css-template"
action={{
name: 'writeToFile',
filepath: 'guides-testing/fuel-project/frontend/src/App.css'
}}
/>

<CodeImport
file="../../examples/intro-to-sway/frontend/src/App.tsx"
comment="fe_fuel_obj"
file="../../examples/intro-to-sway/frontend/src/App.css"
comment="fe_css"
commentType="//"
lang="tsx"
/>

Now we have our contract connection ready. You can console log the contract here to make sure this is working correctly.

### UI
## UI

In our app we're going to have two tabs: one to see all of the items listed for sale, and one to list a new item for sale.

Expand All @@ -221,7 +214,7 @@ We use another state variable called `active` that we can use to toggle between

Next we can create our components to show and list items.

### Listing an Item
## Listing an Item

Create a new folder in the `src` folder called `components`.

Expand Down Expand Up @@ -358,7 +351,7 @@ action={{
Now, try listing an item to make sure this works.
You should see the message `Item successfully listed!`.

### Show All Items
## Show All Items

Next, let's create a new file called `AllItems.tsx` in the `components` folder.

Expand Down Expand Up @@ -447,7 +440,7 @@ action={{
lang="tsx"
/>

### Item Card
## Item Card

Now let's create the item card component.
Create a new file called `ItemCard.tsx` in the components folder.
Expand Down Expand Up @@ -539,7 +532,7 @@ action={{

Now you should be able to see and buy all of the items listed in your contract.

### Checkpoint
## Checkpoint

Ensure that all your files are correctly configured by examining the code below. If you require additional assistance, refer to the repository [here](https://github.com/FuelLabs/intro-to-sway/tree/main/frontend)

Expand Down Expand Up @@ -579,7 +572,7 @@ Ensure that all your files are correctly configured by examining the code below.
lang="tsx"
/>

### Run your project
## Run your project

Inside the `fuel-project/frontend` directory run:

Expand Down
2 changes: 1 addition & 1 deletion src/pages/[...slug].tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'path';
import type { GetStaticProps } from 'next';

import { existsSync, readFileSync, writeFileSync } from 'fs';
import { readFileSync } from 'fs';
import type { MdDoc } from '../../.contentlayer/generated';
import { allMdDocs } from '../../.contentlayer/generated';
import { DOCS_DIRECTORY } from '../config/constants';
Expand Down

0 comments on commit e3a9dd4

Please sign in to comment.