Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed Sep 17, 2024
1 parent 8902c58 commit 29db114
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 35 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ lazy_static = "1.4.0"
libc = "0.2.153"
log = "0.4.14"
memory-db = { version = "0.27.0", default-features = false }
mockall = { version = "0.12" }
num = { version = "0.4", features = ["rand"] }
paste = "1.0.14"
pretty_env_logger = "0.5.0"
Expand Down
2 changes: 1 addition & 1 deletion gear-programs/vara-tokenizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sails-idl-gen.workspace = true
vara-tokenizer = { path = ".", features = ["wasm-binary"] }
vara-tokenizer-client = { path = "client" }
sails-rs = { workspace = true, features = ["gtest", "gclient"] }
tokio = { version = "1.39", features = ["rt", "macros"] }
tokio = { workspace = true, features = ["rt", "macros"] }
gclient.workspace = true
gtest.workspace = true
vft-client = { path = "../vft-client" }
Expand Down
3 changes: 0 additions & 3 deletions gear-programs/vara-tokenizer/app/src/admin_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ impl AdminService {

pub fn revoke_admin_role(&mut self, from: ActorId) {
ensure_is_admin();
if storage().admins.len() == 1 {
panic!("Can't revoke last admin role")
}
storage_mut().admins.remove(&from);
}
}
13 changes: 8 additions & 5 deletions gear-programs/vara-tokenizer/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ mod tokenizer_service;
mod vft_funcs;

use admin_service::{AdminConfig, AdminService};
use sails_rs::{gstd::msg, prelude::*};
use sails_rs::{collections::HashSet, gstd::msg, prelude::*};
use tokenizer_service::TokenizerService;

pub struct VaraTokenizerProgram(());

#[sails_rs::program]
impl VaraTokenizerProgram {
// Program's constructor
pub fn new(name: String, symbol: String, decimals: u8) -> Self {
pub fn new(name: String, symbol: String, decimals: u8, set_admin: bool) -> Self {
vft_service::Service::seed(name, symbol, decimals);
admin_service::init(AdminConfig {
admins: [msg::source()].into(),
});
let admins: HashSet<ActorId> = if set_admin {
[msg::source()].into()
} else {
[].into()
};
admin_service::init(AdminConfig { admins });
Self(())
}

Expand Down
2 changes: 1 addition & 1 deletion gear-programs/vara-tokenizer/app/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// macros
/// create static storage from given struct with `storage_mut` and `storage` accessors
macro_rules! static_storage {
($type:ty) => {
static mut STORAGE: Option<$type> = None;
Expand Down
14 changes: 6 additions & 8 deletions gear-programs/vara-tokenizer/app/src/tokenizer_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ impl TokenizerService {
let to = msg::source();
if let Err(err) = vft_funcs::mint(to, value.into()) {
// TODO reply with value `program::send_reply_with_value` when `sails` allows it
msg::send_bytes_with_gas(to, vec![], 0, value)
.expect("Failed to send message with value");
msg::send_bytes_with_gas(to, vec![], 0, value).expect("Failed to send value to user");
Err(err)
} else {
// drop send event result
_ = self.notify_on(TokenizerEvent::Minted { to, value });
self.notify_on(TokenizerEvent::Minted { to, value })
.expect("Failed to send `Minted` event");
Ok(value)
}
}
Expand All @@ -56,12 +55,11 @@ impl TokenizerService {
let from = msg::source();
vft_funcs::burn(from, value.into())?;

// drop send event result
_ = self.notify_on(TokenizerEvent::Burned { from, value });
self.notify_on(TokenizerEvent::Burned { from, value })
.expect("Failed to send `Burned` event");

// TODO reply with value `program::send_reply_with_value` when `sails` allows it
msg::send_bytes_with_gas(from, vec![], 0, value)
.expect("Failed to send message with value");
msg::send_bytes_with_gas(from, vec![], 0, value).expect("Failed to send value to user");
Ok(value)
}
}
Expand Down
2 changes: 1 addition & 1 deletion gear-programs/vara-tokenizer/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version.workspace = true
edition.workspace = true

[dependencies]
mockall = { version = "0.12", optional = true }
mockall = { workspace = true, optional = true }
sails-rs.workspace = true

[build-dependencies]
Expand Down
15 changes: 5 additions & 10 deletions gear-programs/vara-tokenizer/tests/gclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ async fn init_remoting() -> (GearApi, GClientRemoting, CodeId) {
#[ignore = "requires run gear node on GEAR_PATH"]
async fn factory_works() {
// arrange
let (api, remoting, program_code_id) = init_remoting().await;
let _admin_id =
ActorId::try_from(api.account_id().encode().as_ref()).expect("failed to create actor id");
let (_api, remoting, program_code_id) = init_remoting().await;

// act
let program_factory = vara_tokenizer_client::VaraTokenizerFactory::new(remoting.clone());
let program_id = program_factory
.new("Name".into(), "Symbol".into(), 10u8)
.new("Name".into(), "Symbol".into(), 10u8, true)
.send_recv(program_code_id, b"salt")
.await
.unwrap();
Expand All @@ -51,7 +50,7 @@ async fn mint_from_value_works() {
let program_factory = vara_tokenizer_client::VaraTokenizerFactory::new(remoting.clone());

let program_id = program_factory
.new("Name".into(), "Symbol".into(), 10u8)
.new("Name".into(), "Symbol".into(), 10u8, true)
.send_recv(program_code_id, b"salt")
.await
.unwrap();
Expand Down Expand Up @@ -103,15 +102,11 @@ async fn burn_and_return_value_works() {
let program_factory = vara_tokenizer_client::VaraTokenizerFactory::new(remoting.clone());

let program_id = program_factory
.new("Name".into(), "Symbol".into(), 10u8)
.new("Name".into(), "Symbol".into(), 10u8, true)
.send_recv(program_code_id, b"salt")
.await
.unwrap();

let _initial_balance = api
.free_balance(admin_id)
.await
.expect("Failed to get free balance");
let program_initial_balance = api
.free_balance(program_id)
.await
Expand Down
14 changes: 8 additions & 6 deletions gear-programs/vara-tokenizer/tests/gtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn factory_works() {
let program_factory = vara_tokenizer_client::VaraTokenizerFactory::new(remoting.clone());

let program_id = program_factory
.new("Name".into(), "Symbol".into(), 10u8)
.new("Name".into(), "Symbol".into(), 10u8, true)
.send_recv(program_code_id, b"salt")
.await
.unwrap();
Expand All @@ -44,7 +44,7 @@ async fn mint_from_value_works() {
let program_factory = vara_tokenizer_client::VaraTokenizerFactory::new(remoting.clone());

let program_id = program_factory
.new("Name".into(), "Symbol".into(), 10u8)
.new("Name".into(), "Symbol".into(), 10u8, true)
.send_recv(program_code_id, b"salt")
.await
.unwrap();
Expand All @@ -64,7 +64,8 @@ async fn mint_from_value_works() {

let balance = remoting.system().balance_of(ADMIN_ID);
let program_balance = remoting.system().balance_of(program_id);
// TODO update test after next `gtest` release
// TODO update test after next `gtest` release, fixing gas issues
// see https://github.com/gear-tech/gear/pull/4200 and other `gtest` related PRs
assert_eq!(balance, initial_balance - mint_value);
assert_eq!(program_balance, mint_value);
}
Expand All @@ -76,7 +77,7 @@ async fn burn_and_return_value_works() {
let program_factory = vara_tokenizer_client::VaraTokenizerFactory::new(remoting.clone());

let program_id = program_factory
.new("Name".into(), "Symbol".into(), 10u8)
.new("Name".into(), "Symbol".into(), 10u8, true)
.send_recv(program_code_id, b"salt")
.await
.unwrap();
Expand Down Expand Up @@ -121,7 +122,8 @@ async fn burn_and_return_value_works() {

let balance = remoting.system().balance_of(ADMIN_ID);
let program_balance = remoting.system().balance_of(program_id);
// TODO update test after next `gtest` release
// TODO update test after next `gtest` release, fixing gas issues
// see https://github.com/gear-tech/gear/pull/4200 and other `gtest` related PRs
dbg!(balance, program_balance, client_balance);
assert!(client_balance.is_zero());
// assert_eq!(balance, initial_balance);
Expand All @@ -135,7 +137,7 @@ async fn admin_service_works() {
let program_factory = vara_tokenizer_client::VaraTokenizerFactory::new(remoting.clone());

let program_id = program_factory
.new("Name".into(), "Symbol".into(), 10u8)
.new("Name".into(), "Symbol".into(), 10u8, true)
.send_recv(program_code_id, b"salt")
.await
.unwrap();
Expand Down

0 comments on commit 29db114

Please sign in to comment.