Skip to content

Commit 4503365

Browse files
authored
Merge pull request #5 from near-examples/fix-deposit
Fix contract
2 parents b70459c + 4eba7fb commit 4503365

File tree

4 files changed

+47
-23
lines changed

4 files changed

+47
-23
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ crate-type = ["cdylib", "rlib"]
1313

1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515
[dependencies]
16-
near-sdk = { version = "5.1.0", features = ["unstable"] }
16+
near-sdk = { version = "5.3.0", features = ["unstable"] }
1717

1818
[dev-dependencies]
19-
near-sdk = { version = "5.1.0", features = ["unit-testing"] }
19+
near-sdk = { version = "5.3.0", features = ["unit-testing"] }
2020
near-workspaces = { version = "0.10.0", features = ["unstable"] }
2121
tokio = { version = "1.12.0", features = ["full"] }
2222
serde_json = "1"

src/deploy.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ 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);
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));
3537
assert!(
3638
attached >= minimum_needed,
3739
"Attach at least {minimum_needed} yⓃ"
@@ -74,13 +76,11 @@ impl Contract {
7476
#[callback_result] create_deploy_result: Result<(), PromiseError>,
7577
) -> bool {
7678
if let Ok(_result) = create_deploy_result {
77-
log!(format!("Correctly created and deployed to {account}"));
79+
log!("Correctly created and deployed to {account}");
7880
return true;
7981
};
8082

81-
log!(format!(
82-
"Error creating {account}, returning {attached}yⓃ to {user}"
83-
));
83+
log!("Error creating {account}, returning {attached}yⓃ to {user}");
8484
Promise::new(user).transfer(attached);
8585
false
8686
}

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

+38-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
use near_workspaces::types::{AccountId, KeyType, NearToken, SecretKey};
1+
use near_workspaces::types::{AccountId, NearToken};
22
use serde_json::json;
33

4+
const TEN_NEAR: NearToken = NearToken::from_near(10);
5+
46
#[tokio::test]
57
async fn main() -> Result<(), Box<dyn std::error::Error>> {
68
let sandbox = near_workspaces::sandbox().await?;
7-
let contract_wasm = near_workspaces::compile_project("./").await?;
8-
let contract = sandbox.dev_deploy(&contract_wasm).await?;
9+
let root = sandbox.root_account()?;
910

10-
let alice = sandbox
11-
.create_tla(
12-
"alice.test.near".parse().unwrap(),
13-
SecretKey::from_random(KeyType::ED25519),
14-
)
15-
.await?
16-
.unwrap();
11+
// Create accounts
12+
let alice = create_subaccount(&root, "alice").await?;
13+
let bob = create_subaccount(&root, "bob").await?;
1714

18-
let bob = sandbox.dev_create_account().await?;
15+
let contract_wasm = near_workspaces::compile_project("./").await?;
16+
let contract = sandbox.dev_deploy(&contract_wasm).await?;
1917

20-
let res = contract
21-
.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")
2221
.args_json(json!({"name": "donation_for_alice", "beneficiary": alice.id()}))
2322
.max_gas()
24-
.deposit(NearToken::from_near(5))
23+
.deposit(NearToken::from_millinear(1700))
2524
.transact()
2625
.await?;
2726

@@ -48,5 +47,30 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4847

4948
assert!(res.is_success());
5049

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+
5161
Ok(())
5262
}
63+
64+
async fn create_subaccount(
65+
root: &near_workspaces::Account,
66+
name: &str,
67+
) -> Result<near_workspaces::Account, Box<dyn std::error::Error>> {
68+
let subaccount = root
69+
.create_subaccount(name)
70+
.initial_balance(TEN_NEAR)
71+
.transact()
72+
.await?
73+
.unwrap();
74+
75+
Ok(subaccount)
76+
}

0 commit comments

Comments
 (0)