Skip to content

Commit

Permalink
Body by ref detection improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinketer22 committed Jul 13, 2023
1 parent e0c4b17 commit 368bfc2
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,48 @@ export function computeExternalMessageFees(msgPrices: MsgPrices, cell: Cell) {
return computeFwdFees(msgPrices, BigInt(storageStats.cells), BigInt(storageStats.bits));
}

export function computeMessageForwardFees(msgPrices: MsgPrices, msg: Message, bodyRef: boolean = false) {
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) {
const code = collectCellStats(msg.init.code as Cell);
const data = collectCellStats(msg.init.data as Cell);
storageStats.bits += code.bits + data.bits;
storageStats.cells += 2 + code.cells + data.cells;
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, !bodyRef);
storageStats.bits += bc.bits;
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;
Expand Down

0 comments on commit 368bfc2

Please sign in to comment.