forked from ton-blockchain/liquid-staking-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fees.ts
183 lines (155 loc) · 6.13 KB
/
fees.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* https://github.com/ton-community/ton/blob/v12.3.3/src/block/fees.ts
* Actualized to the current ton-core types
* Currently only message related function
*/
import { Cell, Slice, Message, loadMessageRelaxed, Dictionary } from 'ton-core';
export type MsgPrices = ReturnType<typeof configParseMsgPrices>
//
// Source: https://github.com/ton-foundation/ton/blob/ae5c0720143e231c32c3d2034cfe4e533a16d969/crypto/block/transaction.cpp#L425
//
/*
export function computeStorageFees(data: {
now: number
lastPaid: number
storagePrices: StoragePrices[]
storageStat: { cells: number, bits: number, publicCells: number }
special: boolean
masterchain: boolean
}) {
const {
lastPaid,
now,
storagePrices,
storageStat,
special,
masterchain
} = data;
if (now <= lastPaid || storagePrices.length === 0 || now < storagePrices[0].utime_since.toNumber() || special) {
return new BN(0);
}
let upto = Math.max(lastPaid, storagePrices[0].utime_since.toNumber());
let total = new BN(0);
for (let i = 0; i < storagePrices.length && upto < now; i++) {
let valid_until = (i < storagePrices.length - 1 ? Math.min(now, storagePrices[i + 1].utime_since.toNumber()) : now);
let payment = new BN(0);
if (upto < valid_until) {
let delta = valid_until - upto;
payment = payment.add(new BN(storageStat.cells).mul(masterchain ? storagePrices[i].mc_cell_price_ps : storagePrices[i].cell_price_ps));
payment = payment.add(new BN(storageStat.bits).mul(masterchain ? storagePrices[i].mc_bit_price_ps : storagePrices[i].bit_price_ps));
payment = payment.mul(new BN(delta));
}
upto = valid_until;
total = total.add(payment);
}
return shr16ceil(total);
}
*/
//
// Source: https://github.com/ton-foundation/ton/blob/ae5c0720143e231c32c3d2034cfe4e533a16d969/crypto/block/transaction.cpp#L1218
//
export const configParseMsgPrices = (sc: Slice) => {
let magic = sc.loadUint(8);
if(magic != 0xea) {
throw Error("Invalid message prices magic number!");
}
return {
lumpPrice:sc.loadUintBig(64),
bitPrice: sc.loadUintBig(64),
cellPrice: sc.loadUintBig(64),
ihrPriceFactor: sc.loadUintBig(32),
firstFrac: sc.loadUintBig(16),
nextFrac: sc.loadUintBig(16)
};
}
export const getMsgPrices = (configRaw: Cell, workchain: 0 | -1 ) => {
const config = configRaw.beginParse().loadDictDirect(Dictionary.Keys.Int(32), Dictionary.Values.Cell());
const prices = config.get(25 + workchain);
if(prices === undefined) {
throw Error("No prices defined in config");
}
return configParseMsgPrices(prices.beginParse());
}
export function computeFwdFees(msgPrices: MsgPrices, cells: bigint, bits: bigint) {
return msgPrices.lumpPrice + (shr16ceil((msgPrices.bitPrice * bits)
+ (msgPrices.cellPrice * cells))
);
}
//
// Source: https://github.com/ton-foundation/ton/blob/ae5c0720143e231c32c3d2034cfe4e533a16d969/crypto/block/transaction.cpp#L761
//
/*
export function computeGasPrices(gasUsed: BN, prices: { flatLimit: BN, flatPrice: BN, price: BN }) {
if (gasUsed.lte(prices.flatLimit)) {
return prices.flatPrice;
} else {
// td::rshift(gas_price256 * (gas_used - cfg.flat_gas_limit), 16, 1) + cfg.flat_gas_price
return prices.flatPrice.add(prices.price.mul(gasUsed.sub(prices.flatLimit)).shrn(16));
}
}
*/
//
// Source: https://github.com/ton-foundation/ton/blob/ae5c0720143e231c32c3d2034cfe4e533a16d969/crypto/block/transaction.cpp#L530
//
export function computeExternalMessageFees(msgPrices: MsgPrices, cell: Cell) {
// Collect stats
let storageStats = collectCellStats(cell, true);
return computeFwdFees(msgPrices, BigInt(storageStats.cells), BigInt(storageStats.bits));
}
export function computeMessageForwardFees(msgPrices: MsgPrices, msg: Message) {
// let msg = loadMessageRelaxed(cell.beginParse());
let storageStats: { bits: number, cells: number } = { bits: 0, cells: 0 };
if( msg.info.type !== "internal") {
throw Error("Helper intended for internal messages");
}
const defaultFwd = computeDefaultForwardFee(msgPrices);
// If message forward fee matches default than msg cell is flat
let skipRef = msg.info.forwardFee == defaultFwd;
// Init
if (msg.init) {
if(msg.init.code) {
const code = collectCellStats(msg.init.code);
storageStats.bits += code.bits;
storageStats.cells += code.cells;
}
if(msg.init.data) {
const data = collectCellStats(msg.init.data);
storageStats.bits += data.bits;
storageStats.cells += data.cells;
}
// If message remaining fee exceeds fees fraction from init data, than body is by ref
const tempFees = computeFwdFees(msgPrices, BigInt(storageStats.cells), BigInt(storageStats.bits));
const tempFrac = tempFees - ((tempFees * msgPrices.firstFrac) >> BigInt(16));
skipRef = tempFrac == msg.info.forwardFee
}
// Body
let bc = collectCellStats(msg.body, skipRef);
storageStats.bits += bc.bits;
storageStats.cells += bc.cells;
// NOTE: Extra currencies are ignored for now
let fees = computeFwdFees(msgPrices, BigInt(storageStats.cells), BigInt(storageStats.bits));
let res = (fees * msgPrices.firstFrac) >> BigInt(16);
let remaining = fees - res;
return { fees: res, remaining };
}
export function computeDefaultForwardFee(msgPrices: MsgPrices) {
return msgPrices.lumpPrice - ((msgPrices.lumpPrice * msgPrices.firstFrac) >> BigInt(16));
}
export function collectCellStats(cell: Cell, skipRoot: boolean = false): { bits: number, cells: number } {
let bits = skipRoot ? 0 : cell.bits.length;
let cells = skipRoot ? 0 : 1;
for (let ref of cell.refs) {
let r = collectCellStats(ref);
cells += r.cells;
bits += r.bits;
}
return { bits, cells };
}
function shr16ceil(src: bigint) {
let rem = src % BigInt(65536);
let res = src >> BigInt(16);
if (rem != BigInt(0)) {
res += BigInt(1);
}
return res;
}