Skip to content

Commit

Permalink
fix deposit, add extra bit to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PiVortex committed Sep 3, 2024
1 parent c6f454f commit 8be5e86
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/deploy.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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"
);
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions tests/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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?;

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

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(())
}

Expand Down

0 comments on commit 8be5e86

Please sign in to comment.