Skip to content

Commit 8be5e86

Browse files
author
PiVortex
committed
fix deposit, add extra bit to tests
1 parent c6f454f commit 8be5e86

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/deploy.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use near_sdk::serde::Serialize;
2-
use near_sdk::{env, log, near, AccountId, NearToken, Promise, PromiseError, PublicKey};
2+
use near_sdk::{env, log, near, AccountId, NearToken, Promise, PromiseError, PublicKey, require};
33

44
use crate::{Contract, ContractExt, NEAR_PER_STORAGE, NO_DEPOSIT, TGAS};
55

@@ -21,7 +21,7 @@ impl Contract {
2121
// Assert the sub-account is valid
2222
let current_account = env::current_account_id().to_string();
2323
let subaccount: AccountId = format!("{name}.{current_account}").parse().unwrap();
24-
assert!(
24+
require!(
2525
env::is_valid_account_id(subaccount.as_bytes()),
2626
"Invalid subaccount"
2727
);
@@ -31,10 +31,12 @@ impl Contract {
3131

3232
let code = self.code.clone().unwrap();
3333
let contract_bytes = code.len() as u128;
34-
let minimum_needed = NEAR_PER_STORAGE.saturating_mul(contract_bytes);
35-
assert!(
34+
let contract_storage_cost = NEAR_PER_STORAGE.saturating_mul(contract_bytes);
35+
// Require a little more since storage cost is not exact
36+
let minimum_needed = contract_storage_cost.saturating_add(NearToken::from_millinear(100));
37+
require!(
3638
attached >= minimum_needed,
37-
"Attach at least {minimum_needed} yⓃ"
39+
"Attach at least {minimum_needed} yⓃ",
3840
);
3941

4042
let init_args = near_sdk::serde_json::to_vec(&DonationInitArgs { beneficiary }).unwrap();

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use near_sdk::{near, Gas, NearToken};
55
mod deploy;
66
mod manager;
77

8-
const NEAR_PER_STORAGE: NearToken = NearToken::from_yoctonear(10u128.pow(18)); // 10e18yⓃ
8+
const NEAR_PER_STORAGE: NearToken = NearToken::from_yoctonear(10u128.pow(19)); // 10e19yⓃ
99
const DEFAULT_CONTRACT: &[u8] = include_bytes!("./donation-contract/donation.wasm");
10-
const TGAS: Gas = Gas::from_tgas(1); // 10e12yⓃ
10+
const TGAS: Gas = Gas::from_tgas(1);
1111
const NO_DEPOSIT: NearToken = NearToken::from_near(0); // 0yⓃ
1212

1313
// Define the contract structure

tests/sandbox.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1515
let contract_wasm = near_workspaces::compile_project("./").await?;
1616
let contract = sandbox.dev_deploy(&contract_wasm).await?;
1717

18-
let res = contract
19-
.call("create_factory_subaccount_and_deploy")
18+
// Launch new donation contract through factory
19+
let res = alice
20+
.call(contract.id(), "create_factory_subaccount_and_deploy")
2021
.args_json(json!({"name": "donation_for_alice", "beneficiary": alice.id()}))
2122
.max_gas()
22-
.deposit(NearToken::from_near(5))
23+
.deposit(NearToken::from_millinear(1700))
2324
.transact()
2425
.await?;
2526

@@ -46,6 +47,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4647

4748
assert!(res.is_success());
4849

50+
// Try to create new donation contract with insufficient deposit
51+
let res = alice
52+
.call(contract.id(), "create_factory_subaccount_and_deploy")
53+
.args_json(json!({"name": "donation_for_alice_2", "beneficiary": alice.id()}))
54+
.max_gas()
55+
.deposit(NearToken::from_millinear(1500))
56+
.transact()
57+
.await?;
58+
59+
assert!(res.is_failure());
60+
4961
Ok(())
5062
}
5163

0 commit comments

Comments
 (0)