Skip to content

Commit

Permalink
Merge pull request #5 from near-examples/fix-deposit
Browse files Browse the repository at this point in the history
Fix contract
  • Loading branch information
gagdiez authored Sep 17, 2024
2 parents b70459c + 4eba7fb commit 4503365
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ 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);
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));
assert!(
attached >= minimum_needed,
"Attach at least {minimum_needed} yⓃ"
Expand Down Expand Up @@ -74,13 +76,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
}
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
52 changes: 38 additions & 14 deletions tests/sandbox.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
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<dyn std::error::Error>> {
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")
// 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 @@ -48,5 +47,30 @@ 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(())
}

async fn create_subaccount(
root: &near_workspaces::Account,
name: &str,
) -> Result<near_workspaces::Account, Box<dyn std::error::Error>> {
let subaccount = root
.create_subaccount(name)
.initial_balance(TEN_NEAR)
.transact()
.await?
.unwrap();

Ok(subaccount)
}

0 comments on commit 4503365

Please sign in to comment.