forked from ameliatastic/seahorse-lang
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstored_mutables.py
81 lines (60 loc) · 1.69 KB
/
stored_mutables.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from seahorse.prelude import *
from util.more_data import MoreData
declare_id('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS')
class MyEvent(Event):
nums: List[i32]
class Deep:
num: i32
def __init__(self, num: i32):
self.num = num
class Nested:
deep: Deep
def __init__(self, num: i32):
self.deep = Deep(num)
def reset(self):
self.deep = Deep(0)
class Flag(Enum):
OFF = 1
ON = 2
class Data(Account):
array_2d: Array[Array[i32, 2], 2]
int_list: List[i32]
int_list_2d: List[List[i32]]
string: str
nested: Nested
nested_list: List[Nested]
flag: Flag
more_data: MoreData
@instruction
def init(signer: Signer, data: Empty[Data]):
init_data = data.init(
payer=signer,
seeds=[signer],
padding=1024
)
init_data.int_list = [1, 2]
init_data.int_list_2d = [[3, 4], [5, 6]]
init_data.string = 'Hello'
init_data.nested = Nested(7)
init_data.nested_list = [Nested(8), Nested(9)]
init_data.more_data = MoreData(10)
@instruction
def test_stored_mutables(signer: Signer, data: Data):
# Modify everything in the Data account
# [[0, 0], [0, 0]] -> [[1, 0], [0, 0]]
data.array_2d[0][0] = 1
# [1, 2] -> [1, 2, 0]
data.int_list.append(0)
# [[3, 4], [5, 6]] -> [[3, 0], [5, 6]]
data.int_list_2d[0][-1] = 0
# "Hello" -> "Hello World"
data.string = data.string + ' World'
# N(D(7)) -> N(D(0))
data.nested.reset()
# [N(D(8)), N(D(9))] -> [N(D(0)), N(D(9)), N(D(10))]
data.nested_list[0].reset()
data.nested_list.append(Nested(10))
# OFF -> ON
data.flag = Flag.ON
# MD(10) -> MD(11)
data.more_data = MoreData(11)