Skip to content

Commit 775970a

Browse files
committed
fix: number shouldn't be public & make it return the number over option
1 parent 8fba2e3 commit 775970a

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

contracts/guess-the-number/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl GuessTheNumber {
5252
pub fn guess(env: &Env, a_number: u64, guesser: Address) -> Result<bool, Error> {
5353
let xlm_client = xlm::token_client(env);
5454
let contract_address = env.current_contract_address();
55-
let guessed_it = a_number == Self::number(env).unwrap();
55+
let guessed_it = a_number == Self::number(env);
5656
if guessed_it {
5757
let balance = xlm_client.balance(&contract_address);
5858
if balance == 0 {
@@ -88,8 +88,11 @@ impl GuessTheNumber {
8888
}
8989

9090
/// readonly function to get the current number
91-
pub fn number(env: &Env) -> Option<u64> {
92-
env.storage().instance().get(THE_NUMBER)
91+
/// `pub(crate)` makes it accessible in the same crate, but not outside of it
92+
pub(crate) fn number(env: &Env) -> u64 {
93+
// We can unwrap because the number is set in the constructor
94+
// and then only reset by the admin
95+
unsafe { env.storage().instance().get(THE_NUMBER).unwrap_unchecked() }
9396
}
9497

9598
/// readonly function to get the current admin

contracts/guess-the-number/src/test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use soroban_sdk::{
99
Address, Env, IntoVal, Val, Vec,
1010
};
1111

12-
1312
fn init_test<'a>(env: &'a Env) -> (Address, StellarAssetClient<'a>, GuessTheNumberClient<'a>) {
1413
let admin = Address::generate(env);
1514
let client = generate_client(env, &admin);
@@ -27,7 +26,10 @@ fn constructed_correctly() {
2726
assert_eq!(client.admin(), Some(admin.clone()));
2827
// Check that the contract has a balance of 1 XLM
2928
assert_eq!(sac.balance(&client.address), xlm::to_stroops(1));
30-
29+
// Need to use `as_contract` to call a function in the context of the contract
30+
// Since the method `number` is not in the client, but is visibile in the crate
31+
let number = env.as_contract(&client.address, || GuessTheNumber::number(env));
32+
assert_eq!(number, 4);
3133
}
3234

3335
#[test]
@@ -117,11 +119,10 @@ fn reset_and_guess() {
117119
let alice = Address::generate(env);
118120
// Mint tokens to the user. On testnet you use friendbot to fund the account.
119121
sac.mint(&alice, &xlm::to_stroops(2));
120-
121-
122+
122123
// Reset the number
123124
client.reset();
124-
125+
125126
// Guess again, this should be correct now
126127
assert!(client.guess(&10, &alice));
127128
}
@@ -134,7 +135,6 @@ fn generate_client<'a>(env: &Env, admin: &Address) -> GuessTheNumberClient<'a> {
134135
GuessTheNumberClient::new(env, &contract_id)
135136
}
136137

137-
138138
// This lets you mock the auth context for a function call
139139
fn set_caller<T>(client: &GuessTheNumberClient, fn_name: &str, caller: &Address, args: T)
140140
where

0 commit comments

Comments
 (0)