Skip to content

Commit

Permalink
Merge pull request #148 from ctindogaru/impose-min-deposit
Browse files Browse the repository at this point in the history
Enforce minimum deposit on create method from the factory
  • Loading branch information
TrevorJTClarke authored Apr 1, 2022
2 parents 5ed2721 + fc82b27 commit dfd1e2b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions sputnikdao-factory2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = { version = "4.0.0-pre.4", features = ["unstable"] }

[dev-dependencies]
near-sdk-sim = "4.0.0-pre.4"
Binary file modified sputnikdao-factory2/res/sputnikdao_factory2.wasm
Binary file not shown.
10 changes: 9 additions & 1 deletion sputnikdao-factory2/src/factory_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ impl FactoryManager {
assert!(env::storage_has_key(&code_hash), "Contract doesn't exist");
// Load input (wasm code).
let code = env::storage_read(&code_hash).expect("ERR_NO_HASH");
// Schedule a Promise tx to account_id
// Compute storage cost.
let code_len = code.len();
let storage_cost = ((code_len + 32) as Balance) * env::storage_byte_cost();
assert!(
attached_deposit >= storage_cost,
"ERR_NOT_ENOUGH_DEPOSIT:{}",
storage_cost
);
// Schedule a Promise tx to account_id.
let promise_id = env::promise_batch_create(&account_id);
// Create account first.
env::promise_batch_action_create_account(promise_id);
Expand Down
20 changes: 18 additions & 2 deletions sputnikdao-factory2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,24 @@ mod tests {
use near_sdk::test_utils::{accounts, VMContextBuilder};
use near_sdk::{testing_env, PromiseResult};

use near_sdk_sim::to_yocto;

use super::*;

#[test]
#[should_panic(expected = "ERR_NOT_ENOUGH_DEPOSIT")]
fn test_create_error() {
let mut context = VMContextBuilder::new();
testing_env!(context
.current_account_id(accounts(0))
.predecessor_account_id(accounts(0))
.build());
let mut factory = SputnikDAOFactory::new();

testing_env!(context.attached_deposit(to_yocto("5")).build());
factory.create("test".parse().unwrap(), "{}".as_bytes().to_vec().into());
}

#[test]
fn test_basics() {
let mut context = VMContextBuilder::new();
Expand All @@ -408,7 +424,7 @@ mod tests {
.build());
let mut factory = SputnikDAOFactory::new();

testing_env!(context.attached_deposit(10).build());
testing_env!(context.attached_deposit(to_yocto("6")).build());
factory.create("test".parse().unwrap(), "{}".as_bytes().to_vec().into());

testing_env!(
Expand All @@ -420,7 +436,7 @@ mod tests {
);
factory.on_create(
format!("test.{}", accounts(0)).parse().unwrap(),
U128(10),
U128(to_yocto("6")),
accounts(0),
);
assert_eq!(
Expand Down

0 comments on commit dfd1e2b

Please sign in to comment.