Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
283 changes: 184 additions & 99 deletions savings_goals/src/lib.rs

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions savings_goals/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fn test_add_to_goal_increments() {
}

#[test]
#[should_panic] // It will panic because the goal doesn't exist
fn test_add_to_non_existent_goal() {
let env = Env::default();
let contract_id = env.register_contract(None, SavingsGoalContract);
Expand All @@ -68,7 +67,8 @@ fn test_add_to_non_existent_goal() {

client.init();
env.mock_all_auths();
client.add_to_goal(&user, &99, &500);
let res = client.try_add_to_goal(&user, &99, &500);
assert_eq!(res, Err(Ok(SavingsGoalError::GoalNotFound)));
}

#[test]
Expand Down Expand Up @@ -170,7 +170,6 @@ fn test_edge_cases_large_amounts() {
}

#[test]
#[should_panic]
fn test_zero_amount_fails() {
let env = Env::default();
let contract_id = env.register_contract(None, SavingsGoalContract);
Expand All @@ -179,7 +178,8 @@ fn test_zero_amount_fails() {

client.init();
env.mock_all_auths();
client.create_goal(&user, &String::from_str(&env, "Fail"), &0, &2000000000);
let res = client.try_create_goal(&user, &String::from_str(&env, "Fail"), &0, &2000000000);
assert_eq!(res, Err(Ok(SavingsGoalError::TargetAmountMustBePositive)));
}

#[test]
Expand Down Expand Up @@ -228,7 +228,6 @@ fn test_withdraw_from_goal() {
}

#[test]
#[should_panic(expected = "Insufficient balance")]
fn test_withdraw_too_much() {
let env = Env::default();
let contract_id = env.register_contract(None, SavingsGoalContract);
Expand All @@ -242,11 +241,11 @@ fn test_withdraw_too_much() {
client.unlock_goal(&user, &id);
client.add_to_goal(&user, &id, &100);

client.withdraw_from_goal(&user, &id, &200);
let res = client.try_withdraw_from_goal(&user, &id, &200);
assert_eq!(res, Err(Ok(SavingsGoalError::InsufficientBalance)));
}

#[test]
#[should_panic(expected = "Cannot withdraw from a locked goal")]
fn test_withdraw_locked() {
let env = Env::default();
let contract_id = env.register_contract(None, SavingsGoalContract);
Expand All @@ -259,11 +258,11 @@ fn test_withdraw_locked() {

// Goal is locked by default
client.add_to_goal(&user, &id, &500);
client.withdraw_from_goal(&user, &id, &100);
let res = client.try_withdraw_from_goal(&user, &id, &100);
assert_eq!(res, Err(Ok(SavingsGoalError::GoalLocked)));
}

#[test]
#[should_panic(expected = "Only the goal owner can withdraw funds")]
fn test_withdraw_unauthorized() {
let env = Env::default();
let contract_id = env.register_contract(None, SavingsGoalContract);
Expand All @@ -278,7 +277,8 @@ fn test_withdraw_unauthorized() {
client.unlock_goal(&user, &id);
client.add_to_goal(&user, &id, &500);

client.withdraw_from_goal(&other, &id, &100);
let res = client.try_withdraw_from_goal(&other, &id, &100);
assert_eq!(res, Err(Ok(SavingsGoalError::Unauthorized)));
}

#[test]
Expand Down Expand Up @@ -610,7 +610,6 @@ fn test_unlock_goal_success() {
}

#[test]
#[should_panic(expected = "Only the goal owner can lock this goal")]
fn test_lock_goal_unauthorized_panics() {
let env = Env::default();
let contract_id = env.register_contract(None, SavingsGoalContract);
Expand All @@ -629,11 +628,11 @@ fn test_lock_goal_unauthorized_panics() {

client.unlock_goal(&user, &id);

client.lock_goal(&other, &id);
let res = client.try_lock_goal(&other, &id);
assert_eq!(res, Err(Ok(SavingsGoalError::Unauthorized)));
}

#[test]
#[should_panic(expected = "Only the goal owner can unlock this goal")]
fn test_unlock_goal_unauthorized_panics() {
let env = Env::default();
let contract_id = env.register_contract(None, SavingsGoalContract);
Expand All @@ -650,11 +649,11 @@ fn test_unlock_goal_unauthorized_panics() {
&2000000000,
);

client.unlock_goal(&other, &id);
let res = client.try_unlock_goal(&other, &id);
assert_eq!(res, Err(Ok(SavingsGoalError::Unauthorized)));
}

#[test]
#[should_panic(expected = "Cannot withdraw from a locked goal")]
fn test_withdraw_after_lock_fails() {
let env = Env::default();
let contract_id = env.register_contract(None, SavingsGoalContract);
Expand All @@ -674,7 +673,8 @@ fn test_withdraw_after_lock_fails() {
client.add_to_goal(&user, &id, &500);
client.lock_goal(&user, &id);

client.withdraw_from_goal(&user, &id, &100);
let res = client.try_withdraw_from_goal(&user, &id, &100);
assert_eq!(res, Err(Ok(SavingsGoalError::GoalLocked)));
}

#[test]
Expand Down Expand Up @@ -704,7 +704,6 @@ fn test_withdraw_after_unlock_succeeds() {
}

#[test]
#[should_panic(expected = "Goal not found")]
fn test_lock_nonexistent_goal_panics() {
let env = Env::default();
let contract_id = env.register_contract(None, SavingsGoalContract);
Expand All @@ -714,7 +713,8 @@ fn test_lock_nonexistent_goal_panics() {
client.init();
env.mock_all_auths();

client.lock_goal(&user, &99);
let res = client.try_lock_goal(&user, &99);
assert_eq!(res, Err(Ok(SavingsGoalError::GoalNotFound)));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,27 +228,16 @@
"v0": {
"topics": [
{
"symbol": "log"
"symbol": "fn_return"
},
{
"symbol": "add_to_goal"
}
],
"data": {
"vec": [
{
"string": "caught panic 'Goal not found' from contract function 'Symbol(obj#13)'"
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
},
{
"u32": 99
},
{
"i128": {
"hi": 0,
"lo": 500
}
}
]
"error": {
"contract": 7
}
}
}
}
Expand All @@ -268,12 +257,12 @@
},
{
"error": {
"wasm_vm": "invalid_action"
"contract": 7
}
}
],
"data": {
"string": "caught error from function"
"string": "escalating Ok(ScErrorType::Contract) frame-exit to Err"
}
}
}
Expand All @@ -293,14 +282,14 @@
},
{
"error": {
"wasm_vm": "invalid_action"
"contract": 7
}
}
],
"data": {
"vec": [
{
"string": "contract call failed"
"string": "contract try_call failed"
},
{
"symbol": "add_to_goal"
Expand All @@ -327,31 +316,6 @@
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "error"
},
{
"error": {
"wasm_vm": "invalid_action"
}
}
],
"data": {
"string": "escalating error to panic"
}
}
}
},
"failed_call": false
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -734,21 +734,16 @@
"v0": {
"topics": [
{
"symbol": "log"
"symbol": "fn_return"
},
{
"symbol": "lock_goal"
}
],
"data": {
"vec": [
{
"string": "caught panic 'Only the goal owner can lock this goal' from contract function 'Symbol(lock_goal)'"
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M"
},
{
"u32": 1
}
]
"error": {
"contract": 3
}
}
}
}
Expand All @@ -768,12 +763,12 @@
},
{
"error": {
"wasm_vm": "invalid_action"
"contract": 3
}
}
],
"data": {
"string": "caught error from function"
"string": "escalating Ok(ScErrorType::Contract) frame-exit to Err"
}
}
}
Expand All @@ -793,14 +788,14 @@
},
{
"error": {
"wasm_vm": "invalid_action"
"contract": 3
}
}
],
"data": {
"vec": [
{
"string": "contract call failed"
"string": "contract try_call failed"
},
{
"symbol": "lock_goal"
Expand All @@ -821,31 +816,6 @@
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "error"
},
{
"error": {
"wasm_vm": "invalid_action"
}
}
],
"data": {
"string": "escalating error to panic"
}
}
}
},
"failed_call": false
}
]
}
Loading