Skip to content

Conversation

@chadoh
Copy link
Member

@chadoh chadoh commented Aug 29, 2025

Fixes theahaco/scaffold-stellar#165

  • The HelloWorld contract is so simple as to be obtuse. It's confusing in its pointlessness. And it doesn't show off anything interesting about blockchain programming.

  • GuessTheNumber represents a most-basic starting point for a contract that can show some interesting blockchain functionality. Already, it shows:

  • This represents the probable starting point for the contract. In its current state, it has many purposeful ommissions/shortcomings. These will be fixed via an onboarding tutorial, with steps roughly as:

    1. Remove unwrap in guess, since it causes error if admin did not yet call reset. Walk learner through:

      • Remove after_deploy script to see this error in action
      • Extract the number-setting logic to separate function (maybe set_number)
      • Call that function in both reset and __constructor
      • Replace the unwrap with an expect
      • Note public vs private methods, and have them explore this
         stellar contract invoke --id guess_the_number --network local -- -h
        
    2. guess is currently a view call; no authentication is needed. This means that people do not need to be signed in and there would be no way to pay them. The first thing to do is require authentication; see feat: add guesser arg #101 for an example.

    3. No XLM is currently owned by contract or awarded to winning guess; need to require XLM transfer from admin in reset as well as nominal fee to call guess (which gets added to pot). With upcoming Stellar Registry functionality, we would like this to look like:

      use stellar_registry::import_asset;
      import_asset!(xlm);
      
      #[contract]
      pub struct GuessTheNumber;
      
      #[contractimpl]
      impl GuessTheNumber {
          pub fn reset(env: &Env) {
              Self::require_admin();
              let x = xlm::client(env);
              x.transfer(10_000_000_0, &admin, env.current_contract_address());
              self.set_number();
          }
      
          pub fn guess(env: &Env, guesser: Address, a_number: u64) -> bool {
              guesser.require_auth();
              if guessed_it = a_number == env.storage().instance().get::<_, u64>(&THE_NUMBER).unwrap() {
                  let tx = x.transfer(
                      x.balance(self.current_contract_address()),
                      self.current_contract_address(),
                      guesser,
                  );
                  if tx.is_err {
                      panic!("transfer failed!");
                  }
              } else {
                  let tx = x.transfer(1_000_000_0, guesser, self.current_contract_address());
                  if tx.is_err {
                      panic!("transfer failed!");
                  }
              }
              guessed_it
          }
      }
    4. Those is_err checks are bad! Introduce #[contracterror], return Result<bool, Error>, flatten in the errors from the XLM cross-contract call into this contract's errors, and do the x.transfer calls with question marks to automatically unwrap the results (rust docs).

    5. Inspect contract storage (for example, via block explorer) to see stored number; need to obfuscate with fun crypto tricks (how???)

  • Ideally, this tutorial replaces the main Stellar Developer Docs > Build tutorial, but could also live in Aha-maintained docs (a gitbook? Scaffold Stellar docs?) or possibly in the UI of the app itself

@chadoh chadoh force-pushed the feat/guess-the-number branch 6 times, most recently from 3384e79 to 1c7b612 Compare September 9, 2025 17:42
@pselle pselle force-pushed the feat/guess-the-number branch from f2c553c to 135ba07 Compare October 1, 2025 18:34
@chadoh chadoh marked this pull request as ready for review October 2, 2025 15:59
chadoh and others added 4 commits October 2, 2025 12:19
- The HelloWorld contract is so simple as to be obtuse. It's confusing
  in its pointlessness. And it doesn't show off anything interesting
  about blockchain programming.

- GuessTheNumber represents a most-basic starting point for a contract
  that can show some interesting blockchain functionality. Already, it
  shows:

  - storing data
  - constructor
  - admin-guarded methods
  - forthcoming contracttrait composability pattern
    (stellar/rs-soroban-sdk#1522)
  - forthcoming admin SEP (https://github.com/AhaLabs/admin-sep)

- This represents the probable starting point for the contract. In its
  current state, it has many purposeful ommissions/shortcomings.

  - `unwrap` in `guess` causes error if admin did not yet call `reset`;
    need to extract number-setting logic to separate function and call
    it in both `reset` and `__constructor`
  - no XLM is currently owned by contract or awarded to winning guess;
    need to require XLM transfer from admin in `reset` as well as
    nominal fee to call `guess` (which gets added to pot)
  - can inspect contract storage to see stored number; need to obfuscate
    with fun crypto tricks

- These shortcomings would be addressed through an onboarding tutorial
  in the Stellar docs and maybe in the UI of the app itself, which so
  far has some very basic info about interacting with the contract
@chadoh chadoh force-pushed the feat/guess-the-number branch from 135ba07 to 6cd970d Compare October 2, 2025 16:35
Copy link
Member Author

@chadoh chadoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reviewed it and it looks good to me! I can't mark my own PR as "approved", though. @pselle @zachfedor anything you'd like addressed before we merge this?

@zachfedor
Copy link
Contributor

zachfedor commented Oct 2, 2025

@chadoh the build is failing, other than that it looks good. thanks @pselle for adding the submit button!

@chadoh chadoh merged commit 1e27c74 into main Oct 2, 2025
1 check failed
@chadoh chadoh deleted the feat/guess-the-number branch October 2, 2025 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create better initial example contract

4 participants