From 8be5e86e67a31a4acc642cc2d4cfeb705f5d1c57 Mon Sep 17 00:00:00 2001 From: PiVortex Date: Tue, 3 Sep 2024 11:50:38 +0100 Subject: [PATCH] fix deposit, add extra bit to tests --- src/deploy.rs | 12 +++++++----- src/lib.rs | 4 ++-- tests/sandbox.rs | 18 +++++++++++++++--- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/deploy.rs b/src/deploy.rs index d61e370..b2d8efd 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -1,5 +1,5 @@ use near_sdk::serde::Serialize; -use near_sdk::{env, log, near, AccountId, NearToken, Promise, PromiseError, PublicKey}; +use near_sdk::{env, log, near, AccountId, NearToken, Promise, PromiseError, PublicKey, require}; use crate::{Contract, ContractExt, NEAR_PER_STORAGE, NO_DEPOSIT, TGAS}; @@ -21,7 +21,7 @@ impl Contract { // Assert the sub-account is valid let current_account = env::current_account_id().to_string(); let subaccount: AccountId = format!("{name}.{current_account}").parse().unwrap(); - assert!( + require!( env::is_valid_account_id(subaccount.as_bytes()), "Invalid subaccount" ); @@ -31,10 +31,12 @@ 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); - assert!( + 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)); + require!( attached >= minimum_needed, - "Attach at least {minimum_needed} yⓃ" + "Attach at least {minimum_needed} yⓃ", ); let init_args = near_sdk::serde_json::to_vec(&DonationInitArgs { beneficiary }).unwrap(); 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 da1a13f..69f8481 100644 --- a/tests/sandbox.rs +++ b/tests/sandbox.rs @@ -15,11 +15,12 @@ async fn main() -> Result<(), Box> { 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?; @@ -46,6 +47,17 @@ 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(()) }