Skip to content

Commit

Permalink
Merge pull request #20 from aSpite/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Volkov authored Jan 24, 2024
2 parents ba5fc7c + 873dd86 commit 13459c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/types/Message.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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 { beginCell } from "../boc/Builder";
import { Cell } from "../boc/Cell";
import {loadMessage, storeMessage} from "./Message";

describe('Message', () => {
it('should handle edge case with extra currency', () => {
const tx = 'te6cckEBBwEA3QADs2gB7ix8WDhQdzzFOCf6hmZ2Dzw2vFNtbavUArvbhXqqqmEAMpuMhx8zp7O3wqMokkuyFkklKpftc4Dh9_5bvavmCo-UXR6uVOIGMkCwAAAAAAC3GwLLUHl_4AYCAQCA_____________________________________________________________________________________gMBPAUEAwFDoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOAUACAAAAAAAAAANoAAAAAEIDF-r-4Q';
const cell = Cell.fromBase64(tx);
const message = loadMessage(cell.beginParse());
let stored = beginCell()
.store(storeMessage(message))
.endCell();
expect(stored.equals(cell)).toBe(true);
});
});
12 changes: 3 additions & 9 deletions src/types/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ export function storeMessage(message: Message, opts?: { forceRef?: boolean }) {
let needRef = false;
if (opts && opts.forceRef) {
needRef = true;
} else if (builder.availableBits - 2 /* At least on byte for ref flag */ >= initCell.bits) {
needRef = false;
} else {
needRef = true;
needRef = builder.availableBits - 2 /* At least two bits for ref flags */ < initCell.bits + message.body.bits.length;
}

// Persist init
Expand All @@ -82,12 +80,8 @@ export function storeMessage(message: Message, opts?: { forceRef?: boolean }) {
if (opts && opts.forceRef) {
needRef = true;
} else {
if (builder.availableBits - 1 /* At least on byte for ref flag */ >= message.body.bits.length &&
builder.refs + message.body.refs.length <= 4) {
needRef = false;
} else {
needRef = true;
}
needRef = builder.availableBits - 1 /* At least one bit for ref flag */ < message.body.bits.length ||
builder.refs + message.body.refs.length > 4;
}
if (needRef) {
builder.storeBit(true);
Expand Down

0 comments on commit 13459c3

Please sign in to comment.