diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index dfa265d2519..c226cc68278 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -15,39 +15,40 @@ - [SDK](./sdks/index.md) - [Rust SDK](./sdks/rust-sdk.md) - [TypeScript SDK](./sdks/typescript-sdk.md) -- [Basics](./basics/index.md) - - [Overview](./basics/overview.md) +- [Program Types](./sway-program-types/index.md) + - [Contracts](./sway-program-types/smart_contracts.md) + - [Libraries](./sway-program-types/libraries.md) + - [Scripts](./sway-program-types/scripts.md) + - [Predicates](./sway-program-types/predicates.md) +- [Sway Language Basics](./basics/index.md) + - [Variables](./basics/variables.md) - [Built-in Types](./basics/built_in_types.md) - - [Custom Types](./basics/custom_types.md) + - [Blockchain Types](./basics/blockchain_types.md) - [Functions](./basics/functions.md) - -- [Control Flow](./control-flow/index.md) - - [Loops](./control-flow/loops.md) + - [Structs and Tuples](./basics/structs_and_tuples.md) + - [Methods and Associated Functions](./basics/methods_and_associated_functions.md) + - [Comments and Logging](./basics/comments_and_logging.md) + - [Control Flow](./basics/control_flow.md) + - [Reference Types](./basics/reference_types.md) + +- [Blockchain Development with Sway](./blockchain-development/index.md) + - [Contract Storage](./blockchain-development/storage.md) + - [Function Purity](./blockchain-development/purity.md) + - [Identifiers](./blockchain-development/identifiers.md) + - [Native Assets](./blockchain-development/native_assets.md) - [Testing](./testing/index.md) + - [Testing with Rust](./testing/testing-with-rust.md) - [Testing with TypeScript](./testing/testing-with-typescript.md) -- [Sway on the Chain](./sway-on-chain/index.md) - - [Program Types](./sway-on-chain/program_types.md) - - [Contracts](./sway-on-chain/smart_contracts.md) - - [Libraries](./sway-on-chain/libraries.md) - - [Scripts](./sway-on-chain/scripts.md) - - [Predicates](./sway-on-chain/predicates.md) -- [Smart Contract Development](./smart-contract-development/index.md) - - [Contract Storage](./smart-contract-development/storage.md) - - [Function Purity](./smart-contract-development/purity.md) + - [Advanced Concepts](./advanced/index.md) - [Generic Types](./advanced/generic_types.md) - [Traits](./advanced/traits.md) - [Trait Constraints](./advanced/trait_constraints.md) - [Assembly](./advanced/assembly.md) -- [Blockchain Concepts](./blockchain-concepts/index.md) - - [Blockchain Types](./blockchain-concepts/blockchain_types.md) - - [Identifiers](./blockchain-concepts/identifiers.md) - - [Native Assets](./blockchain-concepts/native_assets.md) -- [Conventions](./style/index.md) - - [Capitalization](./style/capitalization.md) - [Reference](./reference/index.md) + - [Style Guide](./reference/style_guide.md) - [Temporary Workarounds](./reference/temporary_workarounds.md) - [Differences From Solidity](./reference/solidity_differences.md) - [Differences From Rust](./reference/rust_differences.md) diff --git a/docs/src/blockchain-concepts/blockchain_types.md b/docs/src/basics/blockchain_types.md similarity index 82% rename from docs/src/blockchain-concepts/blockchain_types.md rename to docs/src/basics/blockchain_types.md index ff7aa447965..6fee206ca6f 100644 --- a/docs/src/blockchain-concepts/blockchain_types.md +++ b/docs/src/basics/blockchain_types.md @@ -1,6 +1,8 @@ # Blockchain Types -Sway has a selection of types provided via the standard library ([`lib-std`](https://github.com/FuelLabs/sway-lib-std)) which both add a degree of type-safety, as well as make the intention of the developer more clear. +Sway is fundamentally a blockchain language, and it offers a selection of types tailored for the blockchain use case. + +These are provided via the standard library ([`lib-std`](https://github.com/FuelLabs/sway-lib-std)) which both add a degree of type-safety, as well as make the intention of the developer more clear. ## `Address` Type diff --git a/docs/src/basics/comments_and_logging.md b/docs/src/basics/comments_and_logging.md new file mode 100644 index 00000000000..512a36ee0ae --- /dev/null +++ b/docs/src/basics/comments_and_logging.md @@ -0,0 +1,37 @@ +# Comments and Logging + +## Comments + +Comments in Sway start with two slashes and continue until the end of the line. For comments that extend beyond a single line, you'll need to include `//` on each line. + +```sway +// hello world +``` + +```sway +// let's make a couple of lines +// commented. +``` + +You can also place comments at the ends of lines containing code. + +```sway +fn main() { + let baz = 8; // Eight is a lucky number +} +``` + +## Logging + +To log integers, you can use the `log_u64`, `log_u32`, `log_u16`, or `log_u8` functions from the `lib-std` library. + +```sway +use std::chain::log_u64; + +fn main() { + let baz = 8; + log_u64(baz); +} +``` + +Note that you cannot log arbitrary structs yet because we do not yet support serialization. diff --git a/docs/src/basics/control_flow.md b/docs/src/basics/control_flow.md new file mode 100644 index 00000000000..8398558ffca --- /dev/null +++ b/docs/src/basics/control_flow.md @@ -0,0 +1,85 @@ +# Control Flow + +## `if` expressions + +Sway supports _if_, _else_, and _else if_ expressions that allow you to branch your code depending on conditions. + +For example: + +```sway +fn main() { + let number = 6; + + if number % 4 == 0 { + // do something + } else if number % 3 == 0 { + // do something else + } else { + // do something else + }; // <------------ note this semicolon +} +``` + +In Sway, note that a _statement_ is a _declaration **or** expression with a semicolon after it_. This means that you need to add a semicolon after an `if` to turn it into a statement, if it is being used for control flow. + +This need for a semicolon after if expressions to turn them into statements will be removed eventually, but it hasn't been removed yet. + +### Using `if` in a `let` statement + +Like Rust, `if`s are expressions in Sway. What this means is you can use `if` expressions on the right side of a `let` statement to assign the outcome to a variable. + +```sway +let my_data = if some_bool < 10 { foo() } else { bar() }; +``` + +Note that all branches of the `if` expression must return a value of the same type or the code will panic. + +## Loops + +### `while` + +Loops in Sway are currently limited to `while` loops. This is what they look like: + +```sway +while counter < 10 { + counter = counter + 1; +} +``` + +You need the `while` keyword, some condition (`value < 10` in this case) which will be evaluated each iteration, and a block of code inside the curly braces (`{...}`) to execute each iteration. + +### `break` and `continue` + +There are no `break` or `continue` keywords yet, but [they're coming](https://github.com/FuelLabs/sway/issues/587). + +For now, the way to break out of a `while` loop early is to manually invalidate the condition. In this case, that just means setting `counter` to be >= 10. + +Building on the previous example, here's what that might look like: + +```sway +let mut counter = 0; +let mut break_early = false; +while counter < 10 { + if break_early == true { + // here we ensure the condition will evaluate to false, breaking the loop + counter = 10 + } else { + // calling some other function to set the bool value + break_early = get_bool_value(); + counter = counter + 1; + }; +} +``` + +### Nested loops + +You can also use nested `while` loops if needed: + +```sway +while condition_1 == true { + // do stuff... + while condition_2 == true { + // do more stuff... + } +} +``` diff --git a/docs/src/basics/functions.md b/docs/src/basics/functions.md index b4bbb525d42..24fd338afef 100644 --- a/docs/src/basics/functions.md +++ b/docs/src/basics/functions.md @@ -8,7 +8,9 @@ fn equals(first_param: u64, second_param: u64) -> bool { } ``` -We have just declared a function named `equals` which takes two parameters: `first_param` and `second_param`. This function also returns a `bool` value, i.e. either `true` or `false`. This function returns `true` if the two given parameters are equal, and `false` if they are not. If we want to use this function, we can do so like this: +We have just declared a function named `equals` which takes two parameters: `first_param` and `second_param`. The parameters must both be 64-bit unsigned integers. + +This function also returns a `bool` value, i.e. either `true` or `false`. This function returns `true` if the two given parameters are equal, and `false` if they are not. If we want to use this function, we can do so like this: ```sway fn main() { diff --git a/docs/src/basics/index.md b/docs/src/basics/index.md index 3a2346cb6c0..f478e5b2454 100644 --- a/docs/src/basics/index.md +++ b/docs/src/basics/index.md @@ -1,11 +1,15 @@ -# Basics +# Sway Language basics + +Sway is a programming language designed for the FuelVM. It is a statically typed, compiled language with type inference and traits. Sway aims to make smart contract development safer and more performant through the use of strong static analysis and compiler feedback. Sway basics. -- [Overview](./overview.md) - [Variables](./variables.md) - [Built-in Types](./built_in_types.md) -- [Custom Types](./custom_types.md) - [Blockchain Types](./blockchain_types.md) - [Functions](./functions.md) - +- [Structs and Tuples](./structs_and_tuples.md) +- [Methods and Associated Functions](./structs_and_tuples.md) +- [Control Flow](./control-flow.md) +- [Comments and Logging](./comments_and_logging.md) +- [Reference Types](./reference_types.md) diff --git a/docs/src/basics/methods_and_associated_functions.md b/docs/src/basics/methods_and_associated_functions.md new file mode 100644 index 00000000000..1046cd7476a --- /dev/null +++ b/docs/src/basics/methods_and_associated_functions.md @@ -0,0 +1,65 @@ +# Methods and Associated Functions + +Methods are similar to functions in that we declare them with the `fn` keyword and they have parameters and return a value. However, unlike functions, _Methods_ are defined within the context of a struct (or enum), and either refers to that type or mutates it. The first parameter of a method is always `self`, which represents the instance of the struct the method is being called on. + +_Associated functions_ are very similar to _methods_, in that they are also defined in the context of a struct or enum, but they do not actually use any of the data in the struct and as a result do not take _self_ as a parameter. Associated functions could be standalone functions, but they are included in a specific type for organizational or semantic reasons. + +To declare methods and associated functions for a struct or enum, use an _impl block_. Here, `impl` stands for implementation. + +```sway +script; + +struct Foo { + bar: u64, + baz: bool, +} + +impl Foo { + // this is a _method_, as it takes `self` as a parameter. + fn is_baz_true(self) -> bool { + self.baz + } + + // this is an _associated function_, since it does not take `self` as a parameter. + fn new_foo(number: u64, boolean: bool) -> Foo { + Foo { + bar: number, + baz: boolean, + } + } +} + +fn main() { + let foo = ~Foo::new_foo(42, true); + assert(foo.is_baz_true()); +} +``` + +Note the syntax of the associated function call: `~Foo::new_foo(42, true);`. This bit of syntax is unique to Sway: when referring to a type directly, you preface the type with a tilde (`~`). To call an associated function, refer to the type and then the function name. +To call a method, simply use dot syntax: `foo.iz_baz_true()`. + +## Syntax Examples + +```sway +enum Color { + Blue : (), + Green : (), + Red : (), + Silver : (), + Grey : (), + // etc... +} + +enum Make { + Ford : (), + Toyota : (), + Mazda : (), + Chevrolet : (), + BMW : (), + // etc... +} +struct Car { + make: CarMake, + color: Color, +} +``` diff --git a/docs/src/basics/overview.md b/docs/src/basics/overview.md deleted file mode 100644 index d6b8cf00546..00000000000 --- a/docs/src/basics/overview.md +++ /dev/null @@ -1,5 +0,0 @@ -# Overview - -Sway is a programming language designed for the FuelVM. It is a statically typed, compiled language with type inference -and traits. Sway aims to make smart contract development safer and more performant through the use of strong static -analysis and compiler feedback. \ No newline at end of file diff --git a/docs/src/basics/custom_types.md b/docs/src/basics/structs_and_tuples.md similarity index 69% rename from docs/src/basics/custom_types.md rename to docs/src/basics/structs_and_tuples.md index 8df8013df25..70de80f1c1e 100644 --- a/docs/src/basics/custom_types.md +++ b/docs/src/basics/structs_and_tuples.md @@ -1,4 +1,4 @@ -# Custom Types +# Structs and Tuples ## Structs @@ -120,68 +120,3 @@ let event = InventoryEvent::ItemLoss(Claim { _This information is not vital if you are new to the language, or programming in general._ Enums do have some memory overhead. To know which variant is being represented, Sway stores a one-word (8-byte) tag for the enum variant. The space reserved after the tag is equivalent to the size of the _largest_ enum variant. So, to calculate the size of an enum in memory, add 8 bytes to the size of the largest variant. For example, in the case of `Color` above, where the variants are all `()`, the size would be 8 bytes since the size of the largest variant is 0 bytes. - -## Methods and Associated Functions - -_Methods_ are functions that are associated with a specific type and either refer to that type or mutate it. _Associated functions_ are very similar, but they do not use any of the data in the type. Associated functions could be standalone functions, but they -are included in a specific type for organizational or semantic reasons. - -To declare methods and associated functions for a struct or enum, use an _impl block_. Here, `impl` stands for implementation. - -```sway -script; - -struct Foo { - bar: u64, - baz: bool, -} - -impl Foo { - // this is a _method_, as it takes `self` as a parameter. - fn is_baz_true(self) -> bool { - self.baz - } - - // this is an _associated function_, since it does not take `self` as a parameter. - fn new_foo(number: u64, boolean: bool) -> Foo { - Foo { - bar: number, - baz: boolean, - } - } -} - -fn main() { - let foo = ~Foo::new_foo(42, true); - assert(foo.is_baz_true()); -} -``` - -Note the syntax of the associated function call: `~Foo::new_foo(42, true);`. This bit of syntax is unique to Sway: when referring to a type directly, you preface the type with a tilde (`~`). To call an associated function, refer to the type and then the function name. -To call a method, simply use dot syntax: `foo.iz_baz_true()`. - -## Syntax Examples - -```sway -enum Color { - Blue : (), - Green : (), - Red : (), - Silver : (), - Grey : (), - // etc... -} - -enum Make { - Ford : (), - Toyota : (), - Mazda : (), - Chevrolet : (), - BMW : (), - // etc... -} -struct Car { - make: CarMake, - color: Color, -} -``` diff --git a/docs/src/blockchain-concepts/index.md b/docs/src/blockchain-concepts/index.md deleted file mode 100644 index 2716e74c6d4..00000000000 --- a/docs/src/blockchain-concepts/index.md +++ /dev/null @@ -1,7 +0,0 @@ -# Blockchain Concepts - -These are some concepts related to the FuelVM and Fuel ecosystem that you may utilize when writing Sway. - -- [Blockchain Types](./blockchain_types.md) -- [Identifiers](./identifiers.md) -- [Native Assets](./native_assets.md) diff --git a/docs/src/blockchain-concepts/identifiers.md b/docs/src/blockchain-development/identifiers.md similarity index 100% rename from docs/src/blockchain-concepts/identifiers.md rename to docs/src/blockchain-development/identifiers.md diff --git a/docs/src/blockchain-development/index.md b/docs/src/blockchain-development/index.md new file mode 100644 index 00000000000..08e59e02d51 --- /dev/null +++ b/docs/src/blockchain-development/index.md @@ -0,0 +1,10 @@ +# Blockchain Development with Sway + +Sway is fundamentally a blockchain language. Because of this, it has some features and requirements that you may not have seen in general-purpose programming languages. + +These are also some concepts related to the FuelVM and Fuel ecosystem that you may utilize when writing Sway. + +- [Contract Storage](./storage.md) +- [Function Purity](./purity.md) +- [Identifiers](./identifiers.md) +- [Native Assets](./native_assets.md) diff --git a/docs/src/blockchain-concepts/native_assets.md b/docs/src/blockchain-development/native_assets.md similarity index 100% rename from docs/src/blockchain-concepts/native_assets.md rename to docs/src/blockchain-development/native_assets.md diff --git a/docs/src/smart-contract-development/purity.md b/docs/src/blockchain-development/purity.md similarity index 100% rename from docs/src/smart-contract-development/purity.md rename to docs/src/blockchain-development/purity.md diff --git a/docs/src/smart-contract-development/storage.md b/docs/src/blockchain-development/storage.md similarity index 99% rename from docs/src/smart-contract-development/storage.md rename to docs/src/blockchain-development/storage.md index 65295f39b5b..469dbf4dc46 100644 --- a/docs/src/smart-contract-development/storage.md +++ b/docs/src/blockchain-development/storage.md @@ -6,7 +6,7 @@ Put in conventional programming terms, contract storage is like saving data to a Some basic use cases of storage include declaring an owner address for a contract and saving balances in a wallet. -## Syntax + diff --git a/docs/src/control-flow/index.md b/docs/src/control-flow/index.md deleted file mode 100644 index c5e31ef34ba..00000000000 --- a/docs/src/control-flow/index.md +++ /dev/null @@ -1,5 +0,0 @@ -# Control Flow - -Sway control flow. - -- [Loops](./loops.md) diff --git a/docs/src/control-flow/loops.md b/docs/src/control-flow/loops.md deleted file mode 100644 index abbb2ae0d3d..00000000000 --- a/docs/src/control-flow/loops.md +++ /dev/null @@ -1,48 +0,0 @@ -# Loops - -## While - -Loops in Sway are currently limited to `while` loops. This is what they look like: - -```sway -while counter < 10 { - counter = counter + 1; -} -``` - -You need the `while` keyword, some condition (`value < 10` in this case) which will be evaluated each iteration, and a block of code inside the curly braces (`{...}`) to execute each iteration. - -### Break & Continue - -There are no `break` or `continue` keywords yet, but they're coming. - -For now, the way to break out of a `while` loop early is to manually invalidate the condition. In this case, that just means setting `counter` to be >= 10. - -Building on the previous example, here's what that might look like: - -```sway -let mut break_early = false; -while counter < 10 { - if break_early == true { - // here we ensure the condition will evaluate to false, breaking the loop - counter = 10 - } else { - // calling some other function to set the bool value - break_early = get_bool_value(); - counter = counter + 1 - } -} -``` - -### Nested loops - -You can also use nested `while` loops if needed: - -```sway -while condition_1 == true { - // do stuff... - while condition_2 == true { - // do more stuff... - } -} -``` diff --git a/docs/src/reference/rust_differences.md b/docs/src/reference/rust_differences.md index a37d9f33b95..036521b5426 100644 --- a/docs/src/reference/rust_differences.md +++ b/docs/src/reference/rust_differences.md @@ -12,7 +12,7 @@ In Rust, enums generally take one of three forms: _unit_ variants, which have no enum Foo { UnitVariant, TupleVariant(u32, u64, bool), - StructVariant { + StructVariant { field_one: bool, field_two: bool } @@ -26,7 +26,7 @@ In Sway, enums are simplified. Enums variants must all specify exactly one type. enum Foo { UnitVariant : (), TupleVariant : (u32, u64, bool), - StructVariant : MyStruct + StructVariant : MyStruct } struct MyStruct { @@ -37,22 +37,20 @@ struct MyStruct { ## If Expressions -Like Rust, ifs are expressions in Sway. What this means is you can do stuff like this: - -```sway -let my_data = if some_bool < 10 { foo() } else { bar() }; -``` - In Sway, a _statement_ is a _declaration **or** expression with a semicolon after it_. This means that you need to add a semicolon after an `if` to turn it into a statement, if it is being used for control flow: ```sway fn main() { - if something_is_true { - do_this(); + let number = 6; + + if number % 4 == 0 { + // do something + } else if number % 3 == 0 { + // do something else } else { - do_that(); - }; // <------------ note this semicolon -} + // do something else + }; // <------------ note this semicolon + ``` This need for a semicolon after if expressions to turn them into statements will be removed eventually, but it hasn't been removed yet. diff --git a/docs/src/style/capitalization.md b/docs/src/reference/style_guide.md similarity index 86% rename from docs/src/style/capitalization.md rename to docs/src/reference/style_guide.md index b8308d825d5..801d3547f6b 100644 --- a/docs/src/style/capitalization.md +++ b/docs/src/reference/style_guide.md @@ -1,3 +1,5 @@ -# Capitalization +# Style Guide + +## Capitalization In Sway, structs, traits, and enums are `CapitalCase`. Modules, variables, and functions are `snake_case`, constants are `SCREAMING_SNAKE_CASE`. The compiler will warn you if your capitalization is ever unidiomatic. diff --git a/docs/src/smart-contract-development/index.md b/docs/src/smart-contract-development/index.md deleted file mode 100644 index 1e412ca9aae..00000000000 --- a/docs/src/smart-contract-development/index.md +++ /dev/null @@ -1,4 +0,0 @@ -# Smart Contract Development - -- [Contract Storage](./storage.md) -- [Function Purity](./purity.md) diff --git a/docs/src/style/index.md b/docs/src/style/index.md deleted file mode 100644 index e95fbdce236..00000000000 --- a/docs/src/style/index.md +++ /dev/null @@ -1,3 +0,0 @@ -# Conventions - -- [Capitalization](./capitalization.md) diff --git a/docs/src/sway-on-chain/index.md b/docs/src/sway-on-chain/index.md deleted file mode 100644 index 03f4f780266..00000000000 --- a/docs/src/sway-on-chain/index.md +++ /dev/null @@ -1,7 +0,0 @@ -# Sway on the Chain - -- [Program Types](./program_types.md) -- [Contracts](./smart_contracts.md) -- [Libraries](./libraries.md) -- [Scripts](./scripts.md) -- [Predicates](./predicates.md) diff --git a/docs/src/sway-program-types/index.md b/docs/src/sway-program-types/index.md new file mode 100644 index 00000000000..5155303f9aa --- /dev/null +++ b/docs/src/sway-program-types/index.md @@ -0,0 +1,16 @@ +# Sway Program Types + +A Sway program itself has a type: it is either a _contract_, a _predicate_, a _script_, or a _library_. The first three of these things are all deployable to the blockchain. A _library_ is simply a project designed for code reuse and is never directly deployed to the chain. + +Every Sway file _must_ begin with a declaration of what type of program it is. A project can have many libraries within it, but only one contract, script, or predicate. Scripts and predicates require `main` functions to serve as entry points, while contracts instead publish an ABI. This chapter will go into detail about all of these various types of programs and what purposes they serve. + +Contracts are used primarily for protocols or systems that operate within a fixed set of rules. A good example would be a staking contract or a decentralized exchange. + +Scripts are used for complex on-chain interactions that won't persist. An example of this may be using a DEX and Lender to create a leveraged position (borrow, swap, re-collateralize, borrow) which is a complex transaction that would usually take multiple steps. + +Libraries are for code that is reusable and useful for handling common situations. A good example of this would be a library to handle fixed-point math or big number math. + +- [Contracts](./smart_contracts.md) +- [Libraries](./libraries.md) +- [Scripts](./scripts.md) +- [Predicates](./predicates.md) diff --git a/docs/src/sway-on-chain/libraries.md b/docs/src/sway-program-types/libraries.md similarity index 92% rename from docs/src/sway-on-chain/libraries.md rename to docs/src/sway-program-types/libraries.md index 66bbb4e2332..a0272fb4ca2 100644 --- a/docs/src/sway-on-chain/libraries.md +++ b/docs/src/sway-program-types/libraries.md @@ -86,3 +86,9 @@ use std::storage::*; ``` Wildcard imports using `*` are supported, but it is always recommended to use explicit imports where possible. + +You will also need to link the library in the `Forc.toml` of the `forc` repo that you're calling from. You can do this by opening up the `Forc.toml` file and adding the following line to the bottom: + +```toml +wallet_lib = { path = "../wallet_lib" } +``` \ No newline at end of file diff --git a/docs/src/sway-on-chain/predicates.md b/docs/src/sway-program-types/predicates.md similarity index 100% rename from docs/src/sway-on-chain/predicates.md rename to docs/src/sway-program-types/predicates.md diff --git a/docs/src/sway-on-chain/program_types.md b/docs/src/sway-program-types/program_types.md similarity index 100% rename from docs/src/sway-on-chain/program_types.md rename to docs/src/sway-program-types/program_types.md diff --git a/docs/src/sway-on-chain/scripts.md b/docs/src/sway-program-types/scripts.md similarity index 100% rename from docs/src/sway-on-chain/scripts.md rename to docs/src/sway-program-types/scripts.md diff --git a/docs/src/sway-on-chain/smart_contracts.md b/docs/src/sway-program-types/smart_contracts.md similarity index 94% rename from docs/src/sway-on-chain/smart_contracts.md rename to docs/src/sway-program-types/smart_contracts.md index 7fdc6619bd2..74e83c339d3 100644 --- a/docs/src/sway-on-chain/smart_contracts.md +++ b/docs/src/sway-program-types/smart_contracts.md @@ -45,7 +45,7 @@ we are declaring an ABI interface surface method called `receive funds` which, w In the third line, ```sway - fn send_funds(amount_to_send: u64, recipient_address: b256); + fn send_funds(amount_to_send: u64, recipient_address: Address); ``` we are declaring another ABI method, this time called `send_funds`. It takes two parameters: the amount to send, and the address to send the funds to. @@ -54,7 +54,7 @@ we are declaring another ABI method, this time called `send_funds`. It takes two Now that we've discussed how to define the interface, let's discuss how to use it. We will start by implementing the above ABI for a specific contract. -Implementing an ABI for a contract is accomplished with _impl ABI_ syntax: +Implementing an ABI for a contract is accomplished with `impl for Contract` syntax. The `for Contract` syntax can only be used to implement an ABI for a contract; implementing methods for a struct should use `impl Foo` syntax. ```sway impl Wallet for Contract { diff --git a/examples/fizzbuzz/src/main.sw b/examples/fizzbuzz/src/main.sw index 09a42f81faa..506e9001ab3 100644 --- a/examples/fizzbuzz/src/main.sw +++ b/examples/fizzbuzz/src/main.sw @@ -1,5 +1,7 @@ contract; +use std::contract_id::ContractId; + enum FizzBuzzResult { Fizz: (), Buzz: (), @@ -8,11 +10,11 @@ enum FizzBuzzResult { } abi FizzBuzz { - fn fizzbuzz(gas: u64, coins: u64, asset_id: b256, input: u64) -> FizzBuzzResult; + fn fizzbuzz(gas: u64, coins: u64, asset_id: ContractId, input: u64) -> FizzBuzzResult; } impl FizzBuzz for Contract { - fn fizzbuzz(gas: u64, coins: u64, asset_id: b256, input: u64) -> FizzBuzzResult { + fn fizzbuzz(gas: u64, coins: u64, asset_id: ContractId, input: u64) -> FizzBuzzResult { if input % 15 == 0 { FizzBuzzResult::FizzBuzz } else if input % 3 == 0 { diff --git a/examples/subcurrency/src/main.sw b/examples/subcurrency/src/main.sw index 409f7376912..16bb677f1a4 100644 --- a/examples/subcurrency/src/main.sw +++ b/examples/subcurrency/src/main.sw @@ -4,6 +4,7 @@ contract; use std::chain::*; use std::hash::*; use std::storage::*; +use std::address::Address; //////////////////////////////////////// // Event declarations @@ -17,8 +18,8 @@ use std::storage::*; /// Emitted when a token is sent. struct Sent { - from: b256, - to: b256, + from: Address, + to: Address, amount: u64, } @@ -30,11 +31,11 @@ struct Sent { abi Token { // Mint new tokens and send to an address. // Can only be called by the contract creator. - fn mint(receiver: b256, amount: u64); + fn mint(receiver: Address, amount: u64); // Sends an amount of an existing token. // Can be called from any address. - fn send(sender: b256, receiver: b256, amount: u64); + fn send(sender: Address, receiver: Address, amount: u64); } //////////////////////////////////////// @@ -59,10 +60,10 @@ const STORAGE_BALANCES: b256 = 0x00000000000000000000000000000000000000000000000 /// Contract implements the `Token` ABI. impl Token for Contract { - fn mint(receiver: b256, amount: u64) { + fn mint(receiver: Address, amount: u64) { // Note: authentication is not yet implemented, for now just trust params // See https://github.com/FuelLabs/sway/issues/195 - if receiver == MINTER { + if receiver.into() == MINTER { let storage_slot = hash_pair(STORAGE_BALANCES, MINTER, HashMethod::Sha256); let mut receiver_amount = get::(storage_slot); @@ -74,14 +75,14 @@ impl Token for Contract { } } - fn send(sender: b256, receiver: b256, amount: u64) { - let sender_storage_slot = hash_pair(STORAGE_BALANCES, sender, HashMethod::Sha256); + fn send(sender: Address, receiver: Address, amount: u64) { + let sender_storage_slot = hash_pair(STORAGE_BALANCES, sender.into(), HashMethod::Sha256); let mut sender_amount = get::(sender_storage_slot); sender_amount = sender_amount - amount; store(sender_storage_slot, sender_amount); - let receiver_storage_slot = hash_pair(STORAGE_BALANCES, receiver, HashMethod::Sha256); + let receiver_storage_slot = hash_pair(STORAGE_BALANCES, receiver.into(), HashMethod::Sha256); let mut receiver_amount = get::(receiver_storage_slot); receiver_amount = receiver_amount + amount; diff --git a/examples/wallet_smart_contract/src/main.sw b/examples/wallet_smart_contract/src/main.sw index 9d4bdff23ba..af236f44685 100644 --- a/examples/wallet_smart_contract/src/main.sw +++ b/examples/wallet_smart_contract/src/main.sw @@ -5,6 +5,7 @@ contract; use std::*; +use std::address::Address; use std::chain::assert; const OWNER_ADDRESS: b256 = 0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861; @@ -16,19 +17,19 @@ const ETH_ID: b256 = 0x000000000000000000000000000000000000000000000000000000000 abi Wallet { fn receive_funds(); - fn send_funds(amount_to_send: u64, recipient_address: b256); + fn send_funds(amount_to_send: u64, recipient_address: Address); } impl Wallet for Contract { fn receive_funds() { - // if asset_id == ETH_ID { + // if asset_id.into() == ETH_ID { // let balance = storage.balance.write(); // deref balance = balance + coins_to_forward; // }; } - fn send_funds(amount_to_send: u64, recipient_address: b256) { - // assert(sender() == OWNER_ADDRESS); + fn send_funds(amount_to_send: u64, recipient_address: Address) { + // assert(sender().into() == OWNER_ADDRESS); // assert(storage.balance > req.amount_to_send); // storage.balance = storage.balance - req.amount_to_send; // transfer_coins(asset_id, req.recipient_address, req.amount_to_send);