diff --git a/.github/workflows/actions.yaml b/.github/workflows/actions.yaml index 3249255..4ed1fe3 100644 --- a/.github/workflows/actions.yaml +++ b/.github/workflows/actions.yaml @@ -1,6 +1,8 @@ name: Forc build all examples on: + # Allow to run manually + workflow_dispatch: push: branches: - main @@ -68,8 +70,9 @@ jobs: 'cheatsheet', 'storage_map', 'variables', - 'math_lib' + 'math_lib', 'imports', + 'predicate', ] name: Forc build [ ${{ matrix.dir }} ] diff --git a/docs/spell-check-custom-words.txt b/docs/spell-check-custom-words.txt index aa32556..e58088f 100644 --- a/docs/spell-check-custom-words.txt +++ b/docs/spell-check-custom-words.txt @@ -9,4 +9,5 @@ msg cheatsheet enums Forc -toml \ No newline at end of file +toml +bytecode \ No newline at end of file diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 0d8d2f5..bb80aed 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -24,3 +24,4 @@ - [Storage Map](./storage-map.md) - [Vector](./vector.md) - [Base Asset](./base-asset.md) +- [Predicate](./predicate.md) diff --git a/docs/src/predicate.md b/docs/src/predicate.md new file mode 100644 index 0000000..5e55a07 --- /dev/null +++ b/docs/src/predicate.md @@ -0,0 +1,18 @@ +# Predicate + +Examples of a predicate program type in Sway + +| | Predicates | Contracts | +|--------------------------------|------------|-----------| +| Access data on chain | ❌ | ✅ | +| Read data from smart contracts | ❌ | ✅ | +| Check date or time | ❌ | ✅ | +| Read block hash or number | ❌ | ✅ | +| Read input coins | ✅ | ✅ | +| Read output coins | ✅ | ✅ | +| Read transaction scripts | ✅ | ✅ | +| Read transaction bytecode | ✅ | ✅ | + +```sway +{{#include ../examples/predicate/src/main.sw}} +``` diff --git a/examples/predicate/.gitignore b/examples/predicate/.gitignore new file mode 100644 index 0000000..77d3844 --- /dev/null +++ b/examples/predicate/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/examples/predicate/Forc.lock b/examples/predicate/Forc.lock new file mode 100644 index 0000000..1c96714 --- /dev/null +++ b/examples/predicate/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = "core" +source = "path+from-root-2AB5BCE55EAAEFF4" + +[[package]] +name = "predicate" +source = "member" +dependencies = ["std"] + +[[package]] +name = "std" +source = "git+https://github.com/fuellabs/sway?tag=v0.62.0#efda0397c7bee77de73bd726ec0b732d57614973" +dependencies = ["core"] diff --git a/examples/predicate/Forc.toml b/examples/predicate/Forc.toml new file mode 100644 index 0000000..970e36b --- /dev/null +++ b/examples/predicate/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Kin Chan"] +entry = "main.sw" +license = "Apache-2.0" +name = "predicate" + +[dependencies] diff --git a/examples/predicate/src/main.sw b/examples/predicate/src/main.sw new file mode 100644 index 0000000..8f2f44f --- /dev/null +++ b/examples/predicate/src/main.sw @@ -0,0 +1,50 @@ +predicate; + +use std::{ + auth::predicate_address, + inputs::{ + input_amount, + }, + outputs::{ + output_amount, + }, + tx::{ + tx_id, + tx_witnesses_count + }, + constants::ZERO_B256, +}; + +// Configurables +configurable { + FLOOR: u64 = 1, +} + +fn input_output_checks() -> bool { + if (100 >= input_amount(0).unwrap() && 100 >= output_amount(0)) { + return true + } + return false +} + +// Primitive Arguments +fn main(a: u64, b: str, c: bool, d: b256) -> bool { + // This predicate's own address + let this_predicate_root = predicate_address(); + + if (a == 0 || b == "a" || c == false || d == ZERO_B256) { + return false + } + + if tx_witnesses_count() < 1 { + return false + } + + // While loop + let mut x = 10; + while x != FLOOR { + x -= 1; + } + + return input_output_checks() +} diff --git a/examples/solidity_cheatsheet/Forc.lock b/examples/solidity_cheatsheet/Forc.lock index 1060f6d..10962a4 100644 --- a/examples/solidity_cheatsheet/Forc.lock +++ b/examples/solidity_cheatsheet/Forc.lock @@ -1,6 +1,6 @@ [[package]] name = "core" -source = "path+from-root-148AAAB4460F1A9D" +source = "path+from-root-2AB5BCE55EAAEFF4" [[package]] name = "solidity_cheatsheet" @@ -9,5 +9,5 @@ dependencies = ["std"] [[package]] name = "std" -source = "git+https://github.com/fuellabs/sway?tag=v0.59.0#d9985d8111f94235edba9a08fc71a9513ec2a95c" +source = "git+https://github.com/fuellabs/sway?tag=v0.62.0#efda0397c7bee77de73bd726ec0b732d57614973" dependencies = ["core"] diff --git a/examples/solidity_cheatsheet/src/main.sw b/examples/solidity_cheatsheet/src/main.sw index 1e62c3f..7d9eec9 100644 --- a/examples/solidity_cheatsheet/src/main.sw +++ b/examples/solidity_cheatsheet/src/main.sw @@ -39,6 +39,6 @@ impl SolidityCheatsheet for Contract { } fn get_u256_number() -> u256 { - return u256::from(12) + u256::zero(); // big number equivalent + return u256::from((u64::min(), u64::min(), u64::min(), u64::min())); // big number equivalent } }