diff --git a/Cargo.toml b/Cargo.toml index 3c7d3db..742ac64 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,10 @@ crate-type = ["cdylib", "rlib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -near-sdk = { version = "5.1.0", features = ["unstable"] } +near-sdk = { version = "5.3.0", features = ["unstable"] } [dev-dependencies] -near-sdk = { version = "5.1.0", features = ["unit-testing"] } +near-sdk = { version = "5.3.0", features = ["unit-testing"] } near-workspaces = { version = "0.10.0", features = ["unstable"] } tokio = { version = "1.12.0", features = ["full"] } serde_json = "1" diff --git a/src/deploy.rs b/src/deploy.rs index d921ce0..413d179 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -31,7 +31,9 @@ impl Contract { let code = self.code.clone().unwrap(); let contract_bytes = code.len() as u128; - let minimum_needed = NEAR_PER_STORAGE.saturating_mul(contract_bytes); + let contract_storage_cost = NEAR_PER_STORAGE.saturating_mul(contract_bytes); + // Require a little more since storage cost is not exact + let minimum_needed = contract_storage_cost.saturating_add(NearToken::from_millinear(100)); assert!( attached >= minimum_needed, "Attach at least {minimum_needed} yⓃ" @@ -74,13 +76,11 @@ impl Contract { #[callback_result] create_deploy_result: Result<(), PromiseError>, ) -> bool { if let Ok(_result) = create_deploy_result { - log!(format!("Correctly created and deployed to {account}")); + log!("Correctly created and deployed to {account}"); return true; }; - log!(format!( - "Error creating {account}, returning {attached}yⓃ to {user}" - )); + log!("Error creating {account}, returning {attached}yⓃ to {user}"); Promise::new(user).transfer(attached); false } diff --git a/src/lib.rs b/src/lib.rs index a3cc42e..2e24752 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,9 +5,9 @@ use near_sdk::{near, Gas, NearToken}; mod deploy; mod manager; -const NEAR_PER_STORAGE: NearToken = NearToken::from_yoctonear(10u128.pow(18)); // 10e18yⓃ +const NEAR_PER_STORAGE: NearToken = NearToken::from_yoctonear(10u128.pow(19)); // 10e19yⓃ const DEFAULT_CONTRACT: &[u8] = include_bytes!("./donation-contract/donation.wasm"); -const TGAS: Gas = Gas::from_tgas(1); // 10e12yⓃ +const TGAS: Gas = Gas::from_tgas(1); const NO_DEPOSIT: NearToken = NearToken::from_near(0); // 0yⓃ // Define the contract structure diff --git a/tests/sandbox.rs b/tests/sandbox.rs index a6f89dd..69f8481 100644 --- a/tests/sandbox.rs +++ b/tests/sandbox.rs @@ -1,27 +1,26 @@ -use near_workspaces::types::{AccountId, KeyType, NearToken, SecretKey}; +use near_workspaces::types::{AccountId, NearToken}; use serde_json::json; +const TEN_NEAR: NearToken = NearToken::from_near(10); + #[tokio::test] async fn main() -> Result<(), Box> { let sandbox = near_workspaces::sandbox().await?; - let contract_wasm = near_workspaces::compile_project("./").await?; - let contract = sandbox.dev_deploy(&contract_wasm).await?; + let root = sandbox.root_account()?; - let alice = sandbox - .create_tla( - "alice.test.near".parse().unwrap(), - SecretKey::from_random(KeyType::ED25519), - ) - .await? - .unwrap(); + // Create accounts + let alice = create_subaccount(&root, "alice").await?; + let bob = create_subaccount(&root, "bob").await?; - let bob = sandbox.dev_create_account().await?; + let contract_wasm = near_workspaces::compile_project("./").await?; + let contract = sandbox.dev_deploy(&contract_wasm).await?; - let res = contract - .call("create_factory_subaccount_and_deploy") + // Launch new donation contract through factory + let res = alice + .call(contract.id(), "create_factory_subaccount_and_deploy") .args_json(json!({"name": "donation_for_alice", "beneficiary": alice.id()})) .max_gas() - .deposit(NearToken::from_near(5)) + .deposit(NearToken::from_millinear(1700)) .transact() .await?; @@ -48,5 +47,30 @@ async fn main() -> Result<(), Box> { assert!(res.is_success()); + // Try to create new donation contract with insufficient deposit + let res = alice + .call(contract.id(), "create_factory_subaccount_and_deploy") + .args_json(json!({"name": "donation_for_alice_2", "beneficiary": alice.id()})) + .max_gas() + .deposit(NearToken::from_millinear(1500)) + .transact() + .await?; + + assert!(res.is_failure()); + Ok(()) } + +async fn create_subaccount( + root: &near_workspaces::Account, + name: &str, +) -> Result> { + let subaccount = root + .create_subaccount(name) + .initial_balance(TEN_NEAR) + .transact() + .await? + .unwrap(); + + Ok(subaccount) +} \ No newline at end of file