-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
156 additions
and
4 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
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,19 @@ | ||
use anyhow::Result; | ||
use clap::Subcommand; | ||
|
||
use self::new::NewCommand; | ||
|
||
mod new; | ||
|
||
#[derive(Subcommand)] | ||
pub enum Commands { | ||
New(NewCommand), | ||
} | ||
|
||
impl Commands { | ||
pub fn run(&self) -> Result<()> { | ||
match self { | ||
Commands::New(cmd) => cmd.run(), | ||
} | ||
} | ||
} |
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,61 @@ | ||
use anyhow::Result; | ||
use std::{ | ||
ffi::OsString, | ||
fs::{ | ||
self, | ||
create_dir, | ||
File, | ||
}, | ||
io::Write, | ||
path::Path, | ||
}; | ||
use walkdir::WalkDir; | ||
|
||
use clap::Args; | ||
|
||
/// Creates a new templated `folidity` counter project. | ||
/// with a basic contract, README and approval teal code. | ||
#[derive(Args)] | ||
pub struct NewCommand { | ||
/// Path to the new project. | ||
/// If empty, the project will be created in the current dir. | ||
#[clap(value_parser)] | ||
name: Option<OsString>, | ||
} | ||
|
||
impl NewCommand { | ||
pub fn run(&self) -> Result<()> { | ||
let out_dir = self.name.clone().unwrap_or(OsString::from(".")); | ||
let out_path = Path::new(&out_dir); | ||
if out_path.exists() { | ||
for entry in WalkDir::new(out_path) | ||
.follow_links(true) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
{ | ||
let f_name = entry.file_name().to_string_lossy(); | ||
let sec = entry.metadata()?.modified()?; | ||
|
||
if f_name.ends_with(".fol") && sec.elapsed()?.as_secs() < 86400 { | ||
anyhow::bail!( | ||
"Project with this name already exist in {}", | ||
out_dir.to_str().unwrap() | ||
); | ||
} | ||
} | ||
} else { | ||
create_dir(&out_dir).map(|_| anyhow::anyhow!("Cannot create project directory."))?; | ||
} | ||
|
||
let contract_content = include_str!("../../../../examples/counter/counter.fol"); | ||
let readme_content = include_str!("../../../../examples/counter/README.md"); | ||
|
||
let mut contract_file = File::create(Path::new(&out_dir).join("counter.fol"))?; | ||
contract_file.write_all(contract_content.to_string().as_bytes())?; | ||
|
||
let mut readme_file = File::create(Path::new(&out_dir).join("README.md"))?; | ||
readme_file.write_all(readme_content.to_string().as_bytes())?; | ||
|
||
Ok(()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,22 @@ | ||
use clap::Parser; | ||
use cmd::Commands; | ||
|
||
mod cmd; | ||
|
||
#[derive(Parser)] | ||
#[command(author = env!("CARGO_PKG_AUTHORS"), version = concat!("version ", env!("CARGO_PKG_VERSION")), about = env!("CARGO_PKG_DESCRIPTION"), subcommand_required = true)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
let cli = Cli::parse(); | ||
match cli.command.run() { | ||
Ok(()) => {} | ||
Err(err) => { | ||
eprintln!("{err:?}"); | ||
std::process::exit(1); | ||
} | ||
} | ||
} |
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,3 @@ | ||
# Verifiable counter | ||
|
||
This is a simple counter smart contract. |
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,40 @@ | ||
state CounterState { | ||
counter: int, | ||
} st [ | ||
# example bounds | ||
counter < 1000, | ||
counter > -1000 | ||
] | ||
|
||
# This is an constructor. | ||
@init | ||
# Anyone can call this function. | ||
@(any) | ||
fn () initialise() when () -> CounterState { | ||
move CounterState : { 0 }; | ||
} | ||
|
||
@(any) | ||
fn () incr_by(value: int) when CounterState s -> CounterState | ||
st [ | ||
value > 100, | ||
value < 100 | ||
] { | ||
let value = s.counter + value; | ||
move CounterState : { value }; | ||
} | ||
|
||
@(any) | ||
fn () decr_by(value: int) when CounterState s -> CounterState | ||
st [ | ||
value > 100, | ||
value < 100 | ||
] { | ||
let value = s.counter - value; | ||
move CounterState : { value }; | ||
} | ||
|
||
@(any) | ||
view(CounterState s) fn int get_value() { | ||
return s.counter; | ||
} |
Empty file.
Empty file.