From 1b91f3f4eb8ca969462ec4fcb4a73f20e6d91759 Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Thu, 9 Jan 2025 20:53:06 +0100 Subject: [PATCH] feat(treasury-factory): admin full access key, reduce deposit (#205) - add admin full access key to accounts created by treasury factory - reduce web4 contract deposit to 2 N and socialdb deposit to 1. Total factory deposit is now 9 NEAR --- treasury-factory/src/lib.rs | 11 +++++--- treasury-factory/tests/test_basics.rs | 38 ++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/treasury-factory/src/lib.rs b/treasury-factory/src/lib.rs index f694d622..b996d8b6 100644 --- a/treasury-factory/src/lib.rs +++ b/treasury-factory/src/lib.rs @@ -1,6 +1,6 @@ // Find all our documentation at https://docs.near.org mod web4; -use near_sdk::{env, near, serde_json::json, AccountId, Gas, NearToken, Promise}; +use near_sdk::{env, near, serde_json::json, AccountId, Gas, NearToken, Promise, PublicKey}; use web4::types::{Web4Request, Web4Response}; pub mod external; pub use crate::external::*; @@ -31,13 +31,18 @@ impl Contract { create_dao_args: String, ) -> Promise { let new_instance_contract_id: AccountId = format!("{}.near", name).parse().unwrap(); + let admin_full_access_public_key: PublicKey = + "ed25519:DuAFUPhxv3zBDbZP8oCwC1KQPVzaUY88s5tECv8JDPMg" + .parse() + .unwrap(); + Promise::new("near".parse().unwrap()) .function_call( "create_account_advanced".to_string(), json!({ "new_account_id": new_instance_contract_id.clone(), "options": { - "full_access_keys": [env::signer_account_pk()], + "full_access_keys": [env::signer_account_pk(),admin_full_access_public_key], "contract_bytes_base64": include_str!("../treasury_web4.wasm.base64.txt") } }) @@ -50,7 +55,7 @@ impl Contract { .then( instance_contract::ext(new_instance_contract_id.clone()) .with_attached_deposit( - env::attached_deposit().saturating_sub(NearToken::from_near(6)), + env::attached_deposit().saturating_sub(NearToken::from_near(1)), ) .update_widgets(widget_reference_account_id, social_db_account_id), ) diff --git a/treasury-factory/tests/test_basics.rs b/treasury-factory/tests/test_basics.rs index c86a5bf2..a9d6d816 100644 --- a/treasury-factory/tests/test_basics.rs +++ b/treasury-factory/tests/test_basics.rs @@ -3,6 +3,8 @@ use lazy_static::lazy_static; use near_sdk::base64::{engine::general_purpose, Engine as _}; use near_sdk::serde::Deserialize; use near_sdk::{AccountId, NearToken}; +use near_workspaces::types::AccessKeyPermission; +use near_workspaces::types::PublicKey; use serde_json::{json, Value}; use std::fs; use std::sync::{Mutex, Once}; @@ -212,8 +214,10 @@ async fn test_factory() -> Result<(), Box> { }, }); - let create_treasury_instance_result = treasury_factory_contract - .call("create_instance") + let user_account = worker.dev_create_account().await?; + + let create_treasury_instance_result = user_account + .call(treasury_factory_contract.id(), "create_instance") .args_json(json!( { "sputnik_dao_factory_account_id": SPUTNIKDAO_FACTORY_CONTRACT_ACCOUNT, @@ -224,7 +228,7 @@ async fn test_factory() -> Result<(), Box> { } )) .max_gas() - .deposit(NearToken::from_near(10)) + .deposit(NearToken::from_near(9)) .transact() .await?; @@ -295,5 +299,33 @@ async fn test_factory() -> Result<(), Box> { ), deployed_widgets_json_string ); + + let admin_full_access_public_key: PublicKey = + "ed25519:DuAFUPhxv3zBDbZP8oCwC1KQPVzaUY88s5tECv8JDPMg" + .parse() + .unwrap(); + let admin_access_key = worker + .view_access_key( + &instance_account_id.parse().unwrap(), + &admin_full_access_public_key, + ) + .await?; + assert!( + matches!(admin_access_key.permission, AccessKeyPermission::FullAccess), + "Expected FullAccess permission" + ); + + let user_full_access_public_key = user_account.secret_key().public_key(); + let user_access_key = worker + .view_access_key( + &instance_account_id.parse().unwrap(), + &user_full_access_public_key, + ) + .await?; + assert!( + matches!(user_access_key.permission, AccessKeyPermission::FullAccess), + "Expected FullAccess permission" + ); + Ok(()) }