forked from ameliatastic/seahorse-lang
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcalculator.py
44 lines (34 loc) · 1.13 KB
/
calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# calculator
# Built with Seahorse v0.1.0
#
# Gives users their own on-chain four-function calculator!
from seahorse.prelude import *
declare_id('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS')
class Calculator(Account):
owner: Pubkey
display: i64
class Operation(Enum):
ADD = 0
SUB = 1
MUL = 2
DIV = 3
@instruction
def init_calculator(owner: Signer, calculator: Empty[Calculator]):
calculator = calculator.init(payer = owner, seeds = ['Calculator', owner])
calculator.owner = owner.key()
@instruction
def reset_calculator(owner: Signer, calculator: Calculator):
print(owner.key(), 'is resetting a calculator', calculator.key())
assert owner.key() == calculator.owner, 'This is not your calculator!'
calculator.display = 0
@instruction
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:
calculator.display += num
elif op == Operation.SUB:
calculator.display -= num
elif op == Operation.MUL:
calculator.display *= num
elif op == Operation.DIV:
calculator.display //= num