-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add guesser arg
#101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
chadoh
wants to merge
3
commits into
main
Choose a base branch
from
feat/tutorial-step-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [package] | ||
| name = "guess-the-number" | ||
| description = "Admin sets up the pot, anyone can guess to win it" | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| repository.workspace = true | ||
| publish = false | ||
| version.workspace = true | ||
|
|
||
| [package.metadata.stellar] | ||
| # Set contract metadata for authors, homepage, and version based on the Cargo.toml package values | ||
| cargo_inherit = true | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
| doctest = false | ||
|
|
||
| [dependencies] | ||
| soroban-sdk = { git = "https://github.com/AhaLabs/rs-soroban-sdk", rev = "5a99659f1483c926ff87ea45f8823b8c00dc4cbd" } | ||
| admin-sep = { git = "https://github.com/AhaLabs/admin-sep", rev = "bf195f4d67cc96587974212f998680ccf9a61cd7" } | ||
|
|
||
| [dev-dependencies] | ||
| soroban-sdk = { git = "https://github.com/AhaLabs/rs-soroban-sdk", rev = "5a99659f1483c926ff87ea45f8823b8c00dc4cbd", features = ["testutils"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #![no_std] | ||
| use admin_sep::{Administratable, Upgradable}; | ||
| use soroban_sdk::{contract, contractimpl, symbol_short, Address, Env, Symbol}; | ||
|
|
||
| #[contract] | ||
| pub struct GuessTheNumber; | ||
|
|
||
| #[contractimpl] | ||
| impl Administratable for GuessTheNumber {} | ||
|
|
||
| #[contractimpl] | ||
| impl Upgradable for GuessTheNumber {} | ||
|
|
||
| const THE_NUMBER: Symbol = symbol_short!("n"); | ||
|
|
||
| #[contractimpl] | ||
| impl GuessTheNumber { | ||
| pub fn __constructor(env: &Env, admin: &Address) { | ||
| Self::set_admin(env, admin); | ||
| } | ||
|
|
||
| /// Update the number. Only callable by admin. | ||
| pub fn reset(env: &Env) { | ||
| Self::require_admin(env); | ||
| let new_number: u64 = env.prng().gen_range(1..=10); | ||
| env.storage().instance().set(&THE_NUMBER, &new_number); | ||
| } | ||
|
|
||
| /// Guess a number between 1 and 10 | ||
| pub fn guess(env: &Env, guesser: Address, a_number: u64) -> bool { | ||
| guesser.require_auth(); | ||
| a_number == env.storage().instance().get::<_, u64>(&THE_NUMBER).unwrap() | ||
| } | ||
| } | ||
|
|
||
| mod test; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #![cfg(test)] | ||
| extern crate std; | ||
|
|
||
| use super::*; | ||
| use soroban_sdk::{ | ||
| testutils::{Address as _, MockAuth, MockAuthInvoke}, | ||
| Address, Env, IntoVal, Val, Vec, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn only_admin_can_reset() { | ||
| let env = &Env::default(); | ||
|
|
||
| let admin = Address::generate(env); | ||
| let user = Address::generate(env); | ||
|
|
||
| let client = generate_client(env, &admin); | ||
|
|
||
| set_caller(&client, "reset", &user, ()); | ||
| assert!(client.try_reset().is_err()); | ||
|
|
||
| set_caller(&client, "reset", &admin, ()); | ||
| assert!(client.try_reset().is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn one_guess_in_ten_returns_true() { | ||
| let env = &Env::default(); | ||
|
|
||
| let admin = Address::generate(env); | ||
| let user = Address::generate(env); | ||
|
|
||
| let client = generate_client(env, &admin); | ||
|
|
||
| // set the number | ||
| set_caller(&client, "reset", &admin, ()); | ||
| client.reset(); | ||
|
|
||
| let trues: std::vec::Vec<_> = (1u64..=10) | ||
| .filter(|number| { | ||
| set_caller(&client, "guess", &user, (&user, number)); | ||
| client.guess(&user, number) | ||
| }) | ||
| .collect(); | ||
| assert_eq!(trues.len(), 1); | ||
| } | ||
|
|
||
| fn generate_client<'a>(env: &Env, admin: &Address) -> GuessTheNumberClient<'a> { | ||
| let contract_id = env.register(GuessTheNumber, (admin,)); | ||
| GuessTheNumberClient::new(env, &contract_id) | ||
| } | ||
|
|
||
| fn set_caller<T>(client: &GuessTheNumberClient, method: &str, caller: &Address, args: T) | ||
| where | ||
| T: IntoVal<Env, Vec<Val>>, | ||
| { | ||
| // clear previous auth mocks | ||
| client.env.set_auths(&[]); | ||
|
|
||
| // mock auth as passed-in address | ||
| client.env.mock_auths(&[MockAuth { | ||
| address: caller, | ||
| invoke: &MockAuthInvoke { | ||
| contract: &client.address, | ||
| fn_name: method, | ||
| args: args.into_val(&client.env), | ||
| sub_invokes: &[], | ||
| }, | ||
| }]); | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tutorial should explain why you need to pass
guesseras an argument, instead of just requiring that the invoker—whoever they may be—signed the transaction. soroban-sdk is ~ToO fLeXiBLe~ to allow you to just check who the invoker is. This is unexpected! Make sure people understand it.