/
mapping_app.py
57 lines (40 loc) · 1.52 KB
/
mapping_app.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
45
46
47
48
49
50
51
52
53
54
55
56
57
from beaker import *
from pyteal import *
from beaker.lib.storage import BoxMapping
# Our custom Struct
class GroceryItem(abi.NamedTuple):
item: abi.Field[abi.String]
purchased: abi.Field[abi.Bool]
class GroceryStates:
grocery_item = BoxMapping(abi.String, GroceryItem)
app = Application("Grocery CheckList with Beaker", state=GroceryStates())
### Add Grocery with Boxes ###
@app.external
def addGrocery(item_name: abi.String) -> Expr:
purchased = abi.Bool()
grocery_tuple = GroceryItem()
return Seq(
purchased.set(Int(0)),
grocery_tuple.set(item_name, purchased),
app.state.grocery_item[item_name.get()].set(grocery_tuple),
)
### Update Grocery Item ###
@app.external
def updatePurchased(item_name: abi.String, *, output: GroceryItem) -> Expr:
existing_grocery_item = GroceryItem()
new_purchased = abi.Bool()
return Seq(
existing_grocery_item.decode(app.state.grocery_item[item_name.get()].get()),
new_purchased.set(Int(1)),
existing_grocery_item.set(item_name, new_purchased),
app.state.grocery_item[item_name.get()].set(existing_grocery_item),
app.state.grocery_item[item_name.get()].store_into(output),
)
### Read Grocery Item ###
@app.external
def readItem(item_name: abi.String, *, output: GroceryItem) -> Expr:
return app.state.grocery_item[item_name.get()].store_into(output)
### delete ###
@app.external
def deleteGrocery(item_name: abi.String) -> Expr:
return Pop(app.state.grocery_item[item_name.get()].delete())