Skip to content

Commit

Permalink
Merge pull request #42 from FuelLabs/add/predicates
Browse files Browse the repository at this point in the history
feat: Predicate
  • Loading branch information
calldelegation authored Aug 16, 2024
2 parents 973aa3e + 11731e8 commit fdee575
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Forc build all examples

on:
# Allow to run manually
workflow_dispatch:
push:
branches:
- main
Expand Down Expand Up @@ -68,8 +70,9 @@ jobs:
'cheatsheet',
'storage_map',
'variables',
'math_lib'
'math_lib',
'imports',
'predicate',
]

name: Forc build [ ${{ matrix.dir }} ]
Expand Down
3 changes: 2 additions & 1 deletion docs/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ msg
cheatsheet
enums
Forc
toml
toml
bytecode
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
- [Storage Map](./storage-map.md)
- [Vector](./vector.md)
- [Base Asset](./base-asset.md)
- [Predicate](./predicate.md)
18 changes: 18 additions & 0 deletions docs/src/predicate.md
Original file line number Diff line number Diff line change
@@ -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}}
```
2 changes: 2 additions & 0 deletions examples/predicate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
13 changes: 13 additions & 0 deletions examples/predicate/Forc.lock
Original file line number Diff line number Diff line change
@@ -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"]
7 changes: 7 additions & 0 deletions examples/predicate/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Kin Chan"]
entry = "main.sw"
license = "Apache-2.0"
name = "predicate"

[dependencies]
50 changes: 50 additions & 0 deletions examples/predicate/src/main.sw
Original file line number Diff line number Diff line change
@@ -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()
}
4 changes: 2 additions & 2 deletions examples/solidity_cheatsheet/Forc.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[[package]]
name = "core"
source = "path+from-root-148AAAB4460F1A9D"
source = "path+from-root-2AB5BCE55EAAEFF4"

[[package]]
name = "solidity_cheatsheet"
Expand All @@ -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"]
2 changes: 1 addition & 1 deletion examples/solidity_cheatsheet/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit fdee575

Please sign in to comment.