-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Content is complete just need to revise grammar and formatting * Majority of the information is here now time to make grammar edits * Fix UI * Finished revisions * Adding intro to sway as submodule * adding code imports and better code detection * fixing github audit * Removing submodule and fixing audit * Ready for review * Ready for review * Revert "Ready for review" This reverts commit 439e392. * Revert "Ready for review" This reverts commit 04e73fa. * Revert "Removing submodule and fixing audit" This reverts commit 82be9e4. * Audit fix * Updated intro-to-sway submodule to latest commit * Ready for review * Moving intro to sway example * Revert "Moving intro to sway example" This reverts commit 368ede7. * Update docs/guides/docs/intro-to-sway/contract-functions.mdx Co-authored-by: Sarah Schwartz <[email protected]> * Update docs/guides/docs/intro-to-sway/contract-structs.mdx Co-authored-by: Sarah Schwartz <[email protected]> * Update docs/guides/docs/intro-to-sway/contract-structs.mdx Co-authored-by: Sarah Schwartz <[email protected]> * Update docs/guides/docs/intro-to-sway/contract-abi.mdx Co-authored-by: Sarah Schwartz <[email protected]> * Update docs/guides/docs/intro-to-sway/contract-errors.mdx Co-authored-by: Sarah Schwartz <[email protected]> * small fixes * Update docs/guides/docs/intro-to-sway/rust-sdk.mdx Co-authored-by: Sarah Schwartz <[email protected]> * More changes * Checkpoint before continuing to resolve more comments * Going to continue on the weekend * Testing code import changeback * testing quickstart * Finished * Small change * New code changes * Andres comments * Pausing for now * Fix linter * Fix linter * Fix * Lint * lint * spell * Fixing indentation * Fix * Imports are looking good now * Removing intro to sway git module * Removing intro to sway git module * Remove existing 'docs/guides/examples' from the index * Submodule added * Fixing frontend tests * Almost ready for review * fix lint * Brining back in tests * fix * fix * stupid * fix * fix * updating * Resetting checks * adding to it * fix * Adding this for later * Updating intro to sway side * Update docs/guides/docs/intro-to-sway/typescript-sdk.mdx Co-authored-by: Sarah Schwartz <[email protected]> * Update docs/guides/docs/intro-to-sway/typescript-sdk.mdx Co-authored-by: Sarah Schwartz <[email protected]> * Update docs/guides/docs/intro-to-sway/contract-abi.mdx Co-authored-by: Sarah Schwartz <[email protected]> * fix * testing singular * test only playwrite * Playwrite tests seperate * parallelization * more extraction fixes * Ready for review * small fixes * fix * Sarah changes * Reaedy * Ready * Fixing test * Missing label --------- Co-authored-by: Sarah Schwartz <[email protected]>
- Loading branch information
1 parent
6ed6042
commit 098cb54
Showing
36 changed files
with
2,064 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
title: Checkpoint | ||
category: Intro to Sway | ||
parent: | ||
label: All Guides | ||
link: /guides | ||
--- | ||
|
||
# Checkpoint | ||
|
||
## Sway Contract Checkpoint | ||
|
||
If you have followed the previous steps correctly your `main.sw` marketplace contract should look like this: | ||
|
||
<CodeImport | ||
file="../../examples/intro-to-sway/contract/src/main.sw" | ||
comment="all" | ||
commentType="//" | ||
lang="sway" | ||
/> | ||
|
||
## Building the contract | ||
|
||
Here's a polished version of your instructions: | ||
|
||
To format your contract, execute the command: | ||
|
||
<TestAction | ||
id="format-contract" | ||
action={{ | ||
name: 'runCommand', | ||
commandFolder: 'guides-testing/fuel-project/contract' | ||
}} | ||
/> | ||
|
||
```sh | ||
forc fmt | ||
``` | ||
|
||
To compile your contract, navigate to the contract folder and run: | ||
|
||
<TestAction | ||
id="build-contract" | ||
action={{ | ||
name: 'runCommand', | ||
commandFolder: 'guides-testing/fuel-project/contract' | ||
}} | ||
/> | ||
|
||
```sh | ||
forc build | ||
``` | ||
|
||
Congratulations! You've successfully written a full contract in Sway! | ||
|
||
Post-compilation, the system will automatically generate `abi.json`, `storage_slots.json`, and `contract.bin`. You can locate these files in the following directory: | ||
|
||
```sh | ||
contract/out/debug/* | ||
``` | ||
|
||
## Deploying the contract | ||
|
||
For detailed steps on deploying this contract, refer to the official Fuel developer quickstart guide: | ||
[Deploy the Contract](/guides/quickstart/building-a-smart-contract/#deploy-the-contract) | ||
|
||
To deploy, use the following command if you've already set up the forc-wallet and have testnet funds in your account. If not, follow the instructions above. | ||
|
||
```sh | ||
forc deploy --testnet | ||
``` | ||
|
||
After deploying, remember to save your contract ID. You'll need it for frontend integration. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: ABI | ||
category: Intro to Sway | ||
parent: | ||
label: All Guides | ||
link: /guides | ||
--- | ||
|
||
# Defining the ABI | ||
|
||
Next, we will define our ABI. ABI stands for Application Binary Interface. In a Sway contract, it serves as an outline of all the functions within the contract. For each function, you need to specify its name, input types, return types, level of storage access, and if it's payable. | ||
|
||
The ABI for our contract is structured as follows. Write the ABI provided below into your `main.sw` file: | ||
|
||
<TestAction | ||
id="sway-abi" | ||
action={{ | ||
name: 'modifyFile', | ||
filepath: 'guides-testing/fuel-project/contract/src/main.sw' | ||
}} | ||
/> | ||
|
||
<CodeImport | ||
file="../../examples/intro-to-sway/contract/src/main.sw" | ||
comment="abi" | ||
commentType="//" | ||
lang="sway" | ||
/> | ||
|
||
Don't be worried about understanding the specifics of each function at this moment. We will dive into detailed explanations in the "Functions" section. | ||
|
||
## Functions Structure | ||
|
||
A function is defined using the `fn` keyword. In Sway, snake case is the convention, so instead of naming a function `myFunction`, you would name it `my_function`. | ||
|
||
If the function returns a value, its return type must be defined using a skinny arrow. Additionally, if the function has parameters, their types must also be specified. Semicolons are *required* at the end of each statement. | ||
|
||
If a function either reads from or writes to storage, you need to specify the access level above the function using annotations like `#[storage(read)]` or `#[storage(read, write)]`. | ||
|
||
For functions that are expected to receive funds when called, such as the `buy_item` function, the `#[payable]` annotation is required. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: Errors | ||
category: Intro to Sway | ||
parent: | ||
label: All Guides | ||
link: /guides | ||
--- | ||
|
||
# Defining Error Handling | ||
|
||
Enumerations, commonly referred to as enums, are a type that can represent one of several possible variants. Within our contract, we can employ enums to craft custom error messages, facilitating more precise error handling within functions. | ||
|
||
Copy the custom error block into your `main.sw` file: | ||
|
||
<TestAction | ||
id="sway-errors" | ||
action={{ | ||
name: 'modifyFile', | ||
filepath: 'guides-testing/fuel-project/contract/src/main.sw' | ||
}} | ||
/> | ||
|
||
<CodeImport | ||
file="../../examples/intro-to-sway/contract/src/main.sw" | ||
comment="error_handling" | ||
commentType="//" | ||
lang="sway" | ||
/> | ||
|
||
Within our contract, we can anticipate various scenarios where we'd want to throw an error and halt the transaction: | ||
|
||
1. Someone might attempt to pay for an item using an incorrect currency. | ||
2. An individual could try to purchase an item without possessing sufficient coins. | ||
3. Someone other than the owner might attempt to withdraw funds from the contract. | ||
|
||
For each situation, we can define specific return types for the errors: | ||
|
||
- For the `IncorrectAssetId` error, we can return the submitted asset id, which is of type `AssetId`. | ||
- In the case of the `NotEnoughTokens` error, we can define the return type as `u64` to indicate the number of coins involved. | ||
- For the `OnlyOwner` error, we can utilize the `Identity` of the message sender as the return value. |
Oops, something went wrong.