forked from aptos-labs/aptos-core
-
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.
[move-examples] a small framework for testing and building move
To expedite onboarding into smart contracts, this is a small set that allows us to have our own demo modules, test them, and build them as a precursor to either the user adopting move-cli, borrowing our code for their own CI/CD, or exploring something altogether differently.
- Loading branch information
Showing
9 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
[package] | ||
name = "move-examples" | ||
version = "0.1.0" | ||
authors = ["Aptos Foundation <[email protected]>"] | ||
repository = "https://github.com/aptos-core/aptos-labs" | ||
homepage = "https://aptoslabs.com" | ||
license = "Apache-2.0" | ||
publish = false | ||
edition = "2018" | ||
|
||
[dependencies] | ||
aptos-vm = { path = "../aptos-vm" } | ||
aptos-workspace-hack = { path = "../../crates/aptos-workspace-hack" } | ||
framework = { path = "../framework" } | ||
structopt = "0.3.21" | ||
move-compiler = { git = "https://github.com/diem/move", rev = "98ed299a7e3a9223019c9bdf4dd92fea9faef860" } | ||
move-package = { git = "https://github.com/diem/move", rev = "98ed299a7e3a9223019c9bdf4dd92fea9faef860" } | ||
move-stdlib = { git = "https://github.com/diem/move", rev = "98ed299a7e3a9223019c9bdf4dd92fea9faef860" } | ||
move-unit-test = { git = "https://github.com/diem/move", rev = "98ed299a7e3a9223019c9bdf4dd92fea9faef860" } |
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,9 @@ | ||
[package] | ||
name = "Examples" | ||
version = "0.0.0" | ||
|
||
[addresses] | ||
HelloBlockchain = "0xe110" | ||
|
||
[dependencies] | ||
AptosFramework = { local = "../framework/aptos-framework" } |
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,55 @@ | ||
module HelloBlockchain::Message { | ||
use Std::ASCII; | ||
use Std::Errors; | ||
use Std::Event; | ||
use Std::Signer; | ||
|
||
struct MessageHolder has key { | ||
message: ASCII::String, | ||
message_change_events: Event::EventHandle<MessageChangeEvent>, | ||
} | ||
|
||
struct MessageChangeEvent has drop, store { | ||
from_message: ASCII::String, | ||
to_message: ASCII::String, | ||
} | ||
|
||
/// There is no message present | ||
const ENO_MESSAGE: u64 = 0; | ||
|
||
public fun get_message(addr: address): ASCII::String acquires MessageHolder { | ||
assert!(exists<MessageHolder>(addr), Errors::not_published(ENO_MESSAGE)); | ||
*&borrow_global<MessageHolder>(addr).message | ||
} | ||
|
||
public(script) fun set_message(account: signer, message_bytes: vector<u8>) | ||
acquires MessageHolder { | ||
let message = ASCII::string(message_bytes); | ||
let account_addr = Signer::address_of(&account); | ||
if (!exists<MessageHolder>(account_addr)) { | ||
move_to(&account, MessageHolder { | ||
message, | ||
message_change_events: Event::new_event_handle<MessageChangeEvent>(&account), | ||
}) | ||
} else { | ||
let old_message_holder = borrow_global_mut<MessageHolder>(account_addr); | ||
let from_message = *&old_message_holder.message; | ||
Event::emit_event(&mut old_message_holder.message_change_events, MessageChangeEvent { | ||
from_message, | ||
to_message: copy message, | ||
}); | ||
old_message_holder.message = message; | ||
} | ||
} | ||
|
||
#[test(account = @0x1)] | ||
public(script) fun sender_can_set_message(account: signer) acquires MessageHolder { | ||
let addr = Signer::address_of(&account); | ||
set_message(account, b"Hello, Blockchain"); | ||
|
||
assert!( | ||
get_message(addr) == ASCII::string(b"Hello, Blockchain"), | ||
ENO_MESSAGE | ||
); | ||
} | ||
} |
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,25 @@ | ||
#[test_only] | ||
module HelloBlockchain::MessageTests { | ||
use Std::Signer; | ||
use Std::UnitTest; | ||
use Std::Vector; | ||
use Std::ASCII; | ||
|
||
use HelloBlockchain::Message; | ||
|
||
fun get_account(): signer { | ||
Vector::pop_back(&mut UnitTest::create_signers_for_testing(1)) | ||
} | ||
|
||
#[test] | ||
public(script) fun sender_can_set_message() { | ||
let account = get_account(); | ||
let addr = Signer::address_of(&account); | ||
Message::set_message(account, b"Hello, Blockchain"); | ||
|
||
assert!( | ||
Message::get_message(addr) == ASCII::string(b"Hello, Blockchain"), | ||
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,32 @@ | ||
// Copyright (c) The Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use structopt::StructOpt; | ||
|
||
#[derive(Debug, StructOpt)] | ||
#[structopt(about = "Lightweight Move package builder")] | ||
struct Args { | ||
input_path: std::path::PathBuf, | ||
#[structopt( | ||
long, | ||
short = "o", | ||
about = "Optional output path, defaults to input_path/out" | ||
)] | ||
output_path: Option<std::path::PathBuf>, | ||
} | ||
|
||
fn main() { | ||
let args = Args::from_args(); | ||
|
||
let build_config = move_package::BuildConfig { | ||
dev_mode: false, | ||
generate_abis: false, | ||
generate_docs: true, | ||
install_dir: args.output_path, | ||
..Default::default() | ||
}; | ||
|
||
build_config | ||
.compile_package(&args.input_path, &mut std::io::stdout()) | ||
.unwrap(); | ||
} |
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,27 @@ | ||
// Copyright (c) The Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use aptos_vm::natives::aptos_natives; | ||
use framework::diem_framework_named_addresses; | ||
use move_compiler::shared::NumericalAddress; | ||
use move_unit_test::UnitTestingConfig; | ||
|
||
#[test] | ||
fn move_unit_tests() { | ||
let mut named_addresses = diem_framework_named_addresses(); | ||
named_addresses.insert( | ||
"HelloBlockchain".to_owned(), | ||
NumericalAddress::parse_str("0xe110").unwrap(), | ||
); | ||
|
||
let config = | ||
UnitTestingConfig::default_with_bound(Some(100_000)).with_named_addresses(named_addresses); | ||
|
||
move_unit_test::cargo_runner::run_tests_with_config_and_filter( | ||
config, | ||
".", | ||
r"sources/.*\.move$", | ||
Some(&move_stdlib::move_stdlib_modules_full_path()), | ||
Some(aptos_natives()), | ||
); | ||
} |
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