From c6f454f07210605e200a01b4bd83aceb48b949af Mon Sep 17 00:00:00 2001 From: PiVortex Date: Tue, 3 Sep 2024 10:12:38 +0100 Subject: [PATCH 1/3] update sdk ot 5.3.0, fix errors and tests --- Cargo.toml | 4 ++-- src/deploy.rs | 6 ++---- tests/sandbox.rs | 34 +++++++++++++++++++++++----------- 3 files changed, 27 insertions(+), 17 deletions(-) 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..d61e370 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -74,13 +74,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/tests/sandbox.rs b/tests/sandbox.rs index a6f89dd..da1a13f 100644 --- a/tests/sandbox.rs +++ b/tests/sandbox.rs @@ -1,21 +1,19 @@ -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") @@ -50,3 +48,17 @@ async fn main() -> Result<(), Box> { 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 From 8be5e86e67a31a4acc642cc2d4cfeb705f5d1c57 Mon Sep 17 00:00:00 2001 From: PiVortex Date: Tue, 3 Sep 2024 11:50:38 +0100 Subject: [PATCH 2/3] 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(()) } From 4eba7fb1d44847ac56a99e54840848bfd439d8d2 Mon Sep 17 00:00:00 2001 From: PiVortex Date: Tue, 3 Sep 2024 12:30:17 +0100 Subject: [PATCH 3/3] change require back to assert --- src/deploy.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/deploy.rs b/src/deploy.rs index b2d8efd..413d179 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, require}; +use near_sdk::{env, log, near, AccountId, NearToken, Promise, PromiseError, PublicKey}; 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(); - require!( + assert!( env::is_valid_account_id(subaccount.as_bytes()), "Invalid subaccount" ); @@ -34,9 +34,9 @@ impl Contract { 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!( + assert!( 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();