-
Notifications
You must be signed in to change notification settings - Fork 39
/
CommonMessageInfoRelaxed.ts
126 lines (113 loc) · 3.98 KB
/
CommonMessageInfoRelaxed.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
124
125
126
/**
* 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 { Address } from "../address/Address";
import { ExternalAddress } from "../address/ExternalAddress";
import { Builder } from "../boc/Builder";
import { Slice } from "../boc/Slice";
import { Maybe } from "../utils/maybe";
import { CurrencyCollection, loadCurrencyCollection, storeCurrencyCollection } from "./CurrencyCollection";
// Source: https://github.com/ton-blockchain/ton/blob/24dc184a2ea67f9c47042b4104bbb4d82289fac1/crypto/block/block.tlb#L132
// int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
// src:MsgAddress dest:MsgAddressInt
// value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams
// created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
// ext_out_msg_info$11 src:MsgAddress dest:MsgAddressExt
// created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
export type CommonMessageInfoRelaxed =
| CommonMessageInfoRelaxedInternal
| CommonMessageInfoRelaxedExternalOut;
export type CommonMessageInfoRelaxedInternal = {
type: 'internal',
ihrDisabled: boolean,
bounce: boolean,
bounced: boolean,
src?: Maybe<Address>,
dest: Address,
value: CurrencyCollection,
ihrFee: bigint,
forwardFee: bigint,
createdLt: bigint,
createdAt: number
};
export type CommonMessageInfoRelaxedExternalOut = {
type: 'external-out',
src?: Maybe<Address>,
dest?: Maybe<ExternalAddress>,
createdLt: bigint,
createdAt: number
};
export function loadCommonMessageInfoRelaxed(slice: Slice): CommonMessageInfoRelaxed {
// Internal message
if (!slice.loadBit()) {
const ihrDisabled = slice.loadBit();
const bounce = slice.loadBit();
const bounced = slice.loadBit();
const src = slice.loadMaybeAddress();
const dest = slice.loadAddress();
const value = loadCurrencyCollection(slice);
const ihrFee = slice.loadCoins();
const forwardFee = slice.loadCoins();
const createdLt = slice.loadUintBig(64);
const createdAt = slice.loadUint(32);
return {
type: 'internal',
ihrDisabled,
bounce,
bounced,
src,
dest,
value,
ihrFee,
forwardFee,
createdLt,
createdAt,
};
}
// External In mesage
if (!slice.loadBit()) {
throw Error('External In message is not possible for CommonMessageInfoRelaxed');
}
// External Out message
const src = slice.loadMaybeAddress()!;
const dest = slice.loadMaybeExternalAddress();
const createdLt = slice.loadUintBig(64);
const createdAt = slice.loadUint(32);
return {
type: 'external-out',
src,
dest,
createdLt,
createdAt,
};
}
export function storeCommonMessageInfoRelaxed(source: CommonMessageInfoRelaxed) {
return (builder: Builder) => {
if (source.type === 'internal') {
builder.storeBit(0);
builder.storeBit(source.ihrDisabled);
builder.storeBit(source.bounce);
builder.storeBit(source.bounced);
builder.storeAddress(source.src);
builder.storeAddress(source.dest);
builder.store(storeCurrencyCollection(source.value));
builder.storeCoins(source.ihrFee);
builder.storeCoins(source.forwardFee);
builder.storeUint(source.createdLt, 64);
builder.storeUint(source.createdAt, 32);
} else if (source.type === 'external-out') {
builder.storeBit(1);
builder.storeBit(1);
builder.storeAddress(source.src);
builder.storeAddress(source.dest);
builder.storeUint(source.createdLt, 64);
builder.storeUint(source.createdAt, 32);
} else {
throw new Error('Unknown CommonMessageInfo type');
}
}
}