-
Notifications
You must be signed in to change notification settings - Fork 39
/
AccountState.ts
51 lines (45 loc) · 1.69 KB
/
AccountState.ts
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
/**
* Copyright (c) Whales Corp.
* All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Builder } from "../boc/Builder";
import { Slice } from "../boc/Slice";
import { loadStateInit, StateInit, storeStateInit } from "./StateInit";
// Source: https://github.com/ton-blockchain/ton/blob/24dc184a2ea67f9c47042b4104bbb4d82289fac1/crypto/block/block.tlb#L239
// account_uninit$00 = AccountState;
// account_active$1 _:StateInit = AccountState;
// account_frozen$01 state_hash:bits256 = AccountState;
export type AccountState =
| AccountStateUninit
| AccountStateActive
| AccountStateFrozen;
export type AccountStateUninit = { type: 'uninit' };
export type AccountStateActive = { type: 'active', state: StateInit };
export type AccountStateFrozen = { type: 'frozen', stateHash: bigint };
export function loadAccountState(cs: Slice): AccountState {
if (cs.loadBit()) {
return { type: 'active', state: loadStateInit(cs) };
} else if (cs.loadBit()) {
return { type: 'frozen', stateHash: cs.loadUintBig(256) };
} else {
return { type: 'uninit' };
}
}
export function storeAccountState(src: AccountState) {
return (builder: Builder) => {
if (src.type === 'active') {
builder.storeBit(true);
builder.store(storeStateInit(src.state));
} else if (src.type === 'frozen') {
builder.storeBit(false);
builder.storeBit(true);
builder.storeUint(src.stateHash, 256);
} else if (src.type === 'uninit') {
builder.storeBit(false);
builder.storeBit(false);
}
}
}