Skip to content
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

Pascal case for Enums, calculator program + Modified Hello program #12

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
16 changes: 8 additions & 8 deletions docs/introduction/calculator-your-first-seahorse-program.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ The operation will be an enum (enumerated type), which is a type that can simply

```
class Operation(Enum):
ADD = 0
SUB = 1
MUL = 2
DIV = 3
Add = 0
Sub = 1
Mul = 2
Div = 3
```

For parsing purposes, each variant in the enum needs a unique number associated with it. These have no bearing on the generated code.
Expand All @@ -168,13 +168,13 @@ def do_operation(owner: Signer, calculator: Calculator, op: Operation, num: i64)
assert owner.key() == calculator.owner, 'This is not your calculator!'


if op == Operation.ADD:
if op == Operation.Add:
calculator.display += num
elif op == Operation.SUB:
elif op == Operation.Sub:
calculator.display -= num
elif op == Operation.MUL:
elif op == Operation.Mul:
calculator.display *= num
elif op == Operation.DIV:
elif op == Operation.Div:
calculator.display //= num
```

Expand Down
16 changes: 8 additions & 8 deletions examples/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Calculator(Account):
display: i64

class Operation(Enum):
ADD = 0
SUB = 1
MUL = 2
DIV = 3
Add = 0
Sub = 1
Mul = 2
Div = 3

@instruction
def init_calculator(owner: Signer, calculator: Empty[Calculator]):
Expand All @@ -34,11 +34,11 @@ def reset_calculator(owner: Signer, calculator: Calculator):
def do_operation(owner: Signer, calculator: Calculator, op: Operation, num: i64):
assert owner.key() == calculator.owner, 'This is not your calculator!'

if op == Operation.ADD:
if op == Operation.Add:
calculator.display += num
elif op == Operation.SUB:
elif op == Operation.Sub:
calculator.display -= num
elif op == Operation.MUL:
elif op == Operation.Mul:
calculator.display *= num
elif op == Operation.DIV:
elif op == Operation.Div:
calculator.display //= num
23 changes: 19 additions & 4 deletions examples/hello.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# hello
# Built with Seahorse v0.2.3
# Built with Seahorse v0.2.0
#
# Greets users to Solana by printing a message and minting them a token!

from seahorse.prelude import *

declare_id('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS')
declare_id('')
lostintime101 marked this conversation as resolved.
Show resolved Hide resolved


class Hello(Account):
bump: u8


@instruction
def init(owner: Signer, hello: Empty[Hello], mint: Empty[TokenMint]):
def init_token(owner: Signer, hello: Empty[Hello], mint: Empty[TokenMint]):

bump = hello.bump()

hello = hello.init(
Expand All @@ -31,13 +32,27 @@ def init(owner: Signer, hello: Empty[Hello], mint: Empty[TokenMint]):
hello.bump = bump


@instruction
def init_user_token_account(user: Signer, user_acc: Empty[TokenAccount], mint: TokenMint):

user_acc.init(
payer = user,
seeds = ["hello-token", mint],
mint = mint,
authority = user
)


@instruction
def say_hello(user_acc: TokenAccount, hello: Hello, mint: TokenMint):

bump = hello.bump
print(f'Hello {user_acc.authority()}, have a token!')

mint.mint(
authority = hello,
to = user_acc,
amount = u64(1),
signer = ['hello', bump]
)

print(f'Hello {user_acc.authority()}, have a token!')
Loading