forked from algorand/avm-debugger
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathappState.ts
More file actions
85 lines (73 loc) · 2.17 KB
/
appState.ts
File metadata and controls
85 lines (73 loc) · 2.17 KB
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
82
83
84
85
import type {
AvmValue,
AvmKeyValue,
ApplicationInitialStates,
} from '@algorandfoundation/algokit-utils/algod-client';
import { hexToBytes } from '@algorandfoundation/algokit-utils/common';
import { ByteArrayMap } from './utils';
export class AppState {
globalState: ByteArrayMap<AvmValue>;
localState: Map<string, ByteArrayMap<AvmValue>>;
boxState: ByteArrayMap<AvmValue>;
constructor() {
this.globalState = new ByteArrayMap<AvmValue>();
this.localState = new Map<string, ByteArrayMap<AvmValue>>();
this.boxState = new ByteArrayMap<AvmValue>();
}
public globalStateArray(): AvmKeyValue[] {
return createAvmKvArray(this.globalState);
}
public localStateArray(account: string): AvmKeyValue[] {
const map = this.localState.get(account);
if (!map) {
return [];
}
return createAvmKvArray(map);
}
public boxStateArray(): AvmKeyValue[] {
return createAvmKvArray(this.boxState);
}
public clone(): AppState {
const clone = new AppState();
clone.globalState = this.globalState.clone();
clone.localState = new Map(
Array.from(this.localState.entries(), ([key, value]) => [
key,
value.clone(),
]),
);
clone.boxState = this.boxState.clone();
return clone;
}
public static fromAppInitialState(
initialState: ApplicationInitialStates,
): AppState {
const state = new AppState();
if (initialState.appGlobals) {
for (const { key, value } of initialState.appGlobals.kvs) {
state.globalState.set(key, value);
}
}
for (const appLocal of initialState.appLocals || []) {
const map = new ByteArrayMap<AvmValue>();
for (const { key, value } of appLocal.kvs) {
map.set(key, value);
}
state.localState.set(appLocal.account!.toString(), map);
}
if (initialState.appBoxes) {
for (const { key, value } of initialState.appBoxes.kvs) {
state.boxState.set(key, value);
}
}
return state;
}
}
function createAvmKvArray(map: ByteArrayMap<AvmValue>): AvmKeyValue[] {
return Array.from(map.entriesHex())
.sort()
.map(([key, value]) => ({
key: hexToBytes(key),
value,
}));
}