forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document CEI pattern analysis in Sway book (FuelLabs#3393)
close FuelLabs#3298 TODO: - [x] change the code example to a more realistic one - [x] include code from a separate file using `{{#include ...}}`
- Loading branch information
1 parent
7fb338f
commit 9dbfc03
Showing
6 changed files
with
114 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out | ||
target |
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,13 @@ | ||
[[package]] | ||
name = 'cei_analysis' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-52AF21993C880521' | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-52AF21993C880521' | ||
dependencies = ['core'] |
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,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "cei_analysis" | ||
|
||
[dependencies] | ||
std = { path = "../../sway-lib-std" } |
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,33 @@ | ||
contract; | ||
|
||
dep other_contract; | ||
|
||
use other_contract::*; | ||
|
||
use std::auth::msg_sender; | ||
|
||
abi MyContract { | ||
#[storage(read, write)] | ||
fn withdraw(external_contract_id: ContractId); | ||
} | ||
|
||
storage { | ||
balances: StorageMap<Identity, u64> = StorageMap {}, | ||
} | ||
|
||
impl MyContract for Contract { | ||
#[storage(read, write)] | ||
fn withdraw(external_contract_id: ContractId) { | ||
let sender = msg_sender().unwrap(); | ||
let bal = storage.balances.get(sender); | ||
|
||
assert(bal > 0); | ||
|
||
// External call | ||
let caller = abi(OtherContract, external_contract_id.into()); | ||
caller.external_call { coins: bal }(); | ||
|
||
// Storage update _after_ external call | ||
storage.balances.insert(sender, 0); | ||
} | ||
} |
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,5 @@ | ||
library other_contract; | ||
|
||
abi OtherContract { | ||
fn external_call(); | ||
} |