From d27aff9d697bd907f924499b78185c5a0ac5d6ea Mon Sep 17 00:00:00 2001 From: John Adler Date: Tue, 1 Feb 2022 14:23:31 -0500 Subject: [PATCH 1/2] Move subcurrency to examples directory. --- examples/subcurrency/Cargo.toml | 20 +++++ examples/subcurrency/Forc.toml | 9 ++ examples/subcurrency/src/main.sw | 113 ++++++++++++++++++++++++++ examples/subcurrency/tests/harness.rs | 5 ++ 4 files changed, 147 insertions(+) create mode 100644 examples/subcurrency/Cargo.toml create mode 100644 examples/subcurrency/Forc.toml create mode 100644 examples/subcurrency/src/main.sw create mode 100644 examples/subcurrency/tests/harness.rs diff --git a/examples/subcurrency/Cargo.toml b/examples/subcurrency/Cargo.toml new file mode 100644 index 00000000000..61bc8a65a71 --- /dev/null +++ b/examples/subcurrency/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "subcurrency" +version = "0.0.0" +authors = ["Fuel Labs "] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +fuels-abigen-macro = "0.2" +fuels-core = "0.2" +fuels-rs = "0.2" +fuel-gql-client = { version = "0.2", default-features = false } +fuel-tx = "0.3" +rand = "0.8" +tokio = { version = "1.12", features = ["rt", "macros"] } + +[[test]] +name = "integration_tests" +path = "tests/harness.rs" +harness = true diff --git a/examples/subcurrency/Forc.toml b/examples/subcurrency/Forc.toml new file mode 100644 index 00000000000..a105f20ca64 --- /dev/null +++ b/examples/subcurrency/Forc.toml @@ -0,0 +1,9 @@ +[project] +author = "Fuel Labs " +entry = "main.sw" +license = "Apache-2.0" +name = "subcurrency" + +[dependencies] +core = { git = "http://github.com/FuelLabs/sway-lib-core" } +std = { git = "http://github.com/FuelLabs/sway-lib-std" } diff --git a/examples/subcurrency/src/main.sw b/examples/subcurrency/src/main.sw new file mode 100644 index 00000000000..7216b2a161c --- /dev/null +++ b/examples/subcurrency/src/main.sw @@ -0,0 +1,113 @@ +// ANCHOR: body +contract; + +use std::chain::*; +use std::hash::*; +use std::storage::*; + +//////////////////////////////////////// +// Event declarations +//////////////////////////////////////// + +// Events allow clients to react to changes in the contract. +// Unlike Solidity, events are simply structs. +// Note: Serialization is not yet implemented, therefore logging +// of arbitrary structures will not work without manual +// serialization. + +/// Emitted when a token is sent. +struct Sent { + from: b256, + to: b256, + amount: u64, +} + +//////////////////////////////////////// +// ABI method parameter declarations +//////////////////////////////////////// + +/// Parameters for `mint` method. +struct ParamsMint { + receiver: b256, + amount: u64, +} + +/// Parameters for `send` method. +struct ParamsSend { + sender: b256, + receiver: b256, + amount: u64, +} + +//////////////////////////////////////// +// ABI declarations +//////////////////////////////////////// + +/// ABI definition for a subcurrency. +abi Token { + // Mint new tokens and send to an address. + // Can only be called by the contract creator. + fn mint(gas_: u64, coins_: u64, asset_id_: b256, args: ParamsMint); + + // Sends an amount of an existing token. + // Can be called from any address. + fn send(gas_: u64, coins_: u64, asset_id_: b256, args: ParamsSend); +} + +// Note: ABI methods for now must explicitly have as parameters: +// gas_ to forward: u64 +// coins_ to forward: u64 +// asset_id_ of coins: b256 + +//////////////////////////////////////// +// Constants +//////////////////////////////////////// + +/// Address of contract creator. +const MINTER: b256 = 0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b; + +//////////////////////////////////////// +// Contract storage +//////////////////////////////////////// + +// Contract storage persists across transactions. +// Note: Contract storage variables are not implemented yet. + +const STORAGE_BALANCES: b256 = 0x0000000000000000000000000000000000000000000000000000000000000000; + +//////////////////////////////////////// +// ABI definitions +//////////////////////////////////////// + +/// Contract implements the `Token` ABI. +impl Token for Contract { + fn mint(gas_: u64, coins_: u64, asset_id_: b256, args: ParamsMint) { + // Note: authentication is not yet implemented, for now just trust params + // See https://github.com/FuelLabs/sway/issues/195 + if args.receiver == MINTER { + let storage_slot = hash_pair(STORAGE_BALANCES, MINTER, HashMethod::Sha256); + + let mut amount = get::(storage_slot); + amount = amount + args.amount; + store(storage_slot, amount); + } else { + // Revert with error `69`, chosen arbitrarily + panic(69); + } + } + + fn send(gas_: u64, coins_: u64, asset_id_: b256, args: ParamsSend) { + let sender_storage_slot = hash_pair(STORAGE_BALANCES, args.sender, HashMethod::Sha256); + + let mut sender_amount = get::(sender_storage_slot); + sender_amount = sender_amount - args.amount; + store(sender_storage_slot, sender_amount); + + let receiver_storage_slot = hash_pair(STORAGE_BALANCES, args.receiver, HashMethod::Sha256); + + let mut receiver_amount = get::(receiver_storage_slot); + receiver_amount = receiver_amount + args.amount; + store(receiver_storage_slot, receiver_amount); + } +} +// ANCHOR_END: body diff --git a/examples/subcurrency/tests/harness.rs b/examples/subcurrency/tests/harness.rs new file mode 100644 index 00000000000..a6167187ee0 --- /dev/null +++ b/examples/subcurrency/tests/harness.rs @@ -0,0 +1,5 @@ + +#[tokio::test] +async fn harness() { + assert_eq!(true, true); +} From 7b03ffa8f78dd2fa5673231d0865e710e8ac671e Mon Sep 17 00:00:00 2001 From: John Adler Date: Tue, 1 Feb 2022 14:23:54 -0500 Subject: [PATCH 2/2] Include subcurrency code in book. --- docs/src/examples/subcurrency.md | 112 +------------------------------ 1 file changed, 1 insertion(+), 111 deletions(-) diff --git a/docs/src/examples/subcurrency.md b/docs/src/examples/subcurrency.md index 2a6094b3517..ac14d31dc6d 100644 --- a/docs/src/examples/subcurrency.md +++ b/docs/src/examples/subcurrency.md @@ -1,115 +1,5 @@ # Subcurrency ```sway -contract; - -use std::chain::*; -use std::hash::*; -use std::storage::*; - -//////////////////////////////////////// -// Event declarations -//////////////////////////////////////// - -// Events allow clients to react to changes in the contract. -// Unlike Solidity, events are simply structs. -// Note: Serialization is not yet implemented, therefore logging -// of arbitrary structures will not work without manual -// serialization. - -/// Emitted when a token is sent. -struct Sent { - from: b256, - to: b256, - amount: u64, -} - -//////////////////////////////////////// -// ABI method parameter declarations -//////////////////////////////////////// - -/// Parameters for `mint` method. -struct ParamsMint { - receiver: b256, - amount: u64, -} - -/// Parameters for `send` method. -struct ParamsSend { - sender: b256, - receiver: b256, - amount: u64, -} - -//////////////////////////////////////// -// ABI declarations -//////////////////////////////////////// - -/// ABI definition for a subcurrency. -abi Token { - // Mint new tokens and send to an address. - // Can only be called by the contract creator. - fn mint(gas_: u64, coins_: u64, asset_id_: b256, args: ParamsMint); - - // Sends an amount of an existing token. - // Can be called from any address. - fn send(gas_: u64, coins_: u64, asset_id_: b256, args: ParamsSend); -} - -// Note: ABI methods for now must explicitly have as parameters: -// gas_ to forward: u64 -// coins_ to forward: u64, -// asset_id_ of coins: b256 - -//////////////////////////////////////// -// Constants -//////////////////////////////////////// - -/// Address of contract creator. -const MINTER: b256 = 0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b; - -//////////////////////////////////////// -// Contract storage -//////////////////////////////////////// - -// Contract storage persists across transactions. -// Note: Contract storage variables are not implemented yet. - -const STORAGE_BALANCES: b256 = 0x0000000000000000000000000000000000000000000000000000000000000000; - -//////////////////////////////////////// -// ABI definitions -//////////////////////////////////////// - -/// Contract implements the `Token` ABI. -impl Token for Contract { - fn mint(gas_: u64, coins_: u64, asset_id_: b256, args: ParamsMint) { - // Note: authentication is not yet implemented, for now just trust params - // See https://github.com/FuelLabs/sway/issues/195 - if args.receiver == MINTER { - let storage_slot = hash_pair(STORAGE_BALANCES, MINTER, HashMethod::Sha256); - - let mut amount = get::(storage_slot); - amount = amount + args.amount; - store(storage_slot, amount); - } else { - // Revert with error `69`, chosen arbitrarily - panic(69); - } - } - - fn send(gas_: u64, coins_: u64, asset_id_: b256, args: ParamsSend) { - let sender_storage_slot = hash_pair(STORAGE_BALANCES, args.sender, HashMethod::Sha256); - - let mut sender_amount = get::(sender_storage_slot); - sender_amount = sender_amount - args.amount; - store(sender_storage_slot, sender_amount); - - let receiver_storage_slot = hash_pair(STORAGE_BALANCES, args.receiver, HashMethod::Sha256); - - let mut receiver_amount = get::(receiver_storage_slot); - receiver_amount = receiver_amount + args.amount; - store(receiver_storage_slot, receiver_amount); - } -} +{{#include ../../../examples/subcurrency/src/main.sw:body}} ```