-
Notifications
You must be signed in to change notification settings - Fork 39
/
OutList.ts
124 lines (102 loc) · 3.18 KB
/
OutList.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* 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 { SendMode } from "./SendMode";
import {loadMessageRelaxed, MessageRelaxed, storeMessageRelaxed} from "./MessageRelaxed";
import {beginCell, Builder} from "../boc/Builder";
import { Cell } from "../boc/Cell";
import {Slice} from "../boc/Slice";
import {loadStorageInfo, storeStorageInfo} from "./StorageInto";
import {loadAccountStorage, storeAccountStorage} from "./AccountStorage";
import {Account} from "./Account";
import {loadMessage} from "./Message";
export interface OutActionSendMsg {
type: 'sendMsg',
mode: SendMode;
outMsg: MessageRelaxed;
}
export interface OutActionSetCode {
type: 'setCode',
newCode: Cell;
}
export type OutAction = OutActionSendMsg | OutActionSetCode;
export function storeOutAction(action: OutAction) {
switch (action.type) {
case 'sendMsg':
return storeOutActionSendMsg(action);
case 'setCode':
return storeOutActionSetCode(action);
default:
throw new Error(`Unknown action type ${(action as OutAction).type}`)
}
}
/*
action_send_msg#0ec3c86d mode:(## 8)
out_msg:^(MessageRelaxed Any) = OutAction;
*/
const outActionSendMsgTag = 0x0ec3c86d;
function storeOutActionSendMsg(action: OutActionSendMsg) {
return (builder: Builder) => {
builder.storeUint(outActionSendMsgTag, 32)
.storeUint(action.mode, 8)
.storeRef(beginCell().store(storeMessageRelaxed(action.outMsg)).endCell());
}
}
/*
action_set_code#ad4de08e new_code:^Cell = OutAction;
*/
const outActionSetCodeTag = 0xad4de08e;
function storeOutActionSetCode(action: OutActionSetCode) {
return (builder: Builder) => {
builder.storeUint(outActionSetCodeTag, 32).storeRef(action.newCode);
}
}
export function loadOutAction(slice: Slice): OutAction {
const tag = slice.loadUint(32);
if (tag === outActionSendMsgTag) {
const mode = slice.loadUint(8);
const outMsg = loadMessageRelaxed(slice.loadRef().beginParse());
return {
type: 'sendMsg',
mode,
outMsg
}
}
if (tag === outActionSetCodeTag) {
const newCode = slice.loadRef();
return {
type: 'setCode',
newCode
}
}
throw new Error(`Unknown out action tag 0x${tag.toString(16)}`);
}
/*
out_list_empty$_ = OutList 0;
out_list$_ {n:#} prev:^(OutList n) action:OutAction
= OutList (n + 1);
*/
export function storeOutList(actions: OutAction[]) {
const cell = actions.reduce((cell, action) => beginCell()
.storeRef(cell)
.store(storeOutAction(action))
.endCell(),
beginCell().endCell()
);
return (builder: Builder) => {
builder.storeSlice(cell.beginParse());
}
}
export function loadOutList(slice: Slice): OutAction[] {
const actions: OutAction[] = [];
while (slice.remainingRefs) {
const nextCell = slice.loadRef();
actions.push(loadOutAction(slice));
slice = nextCell.beginParse();
}
return actions.reverse();
}