diff --git a/src/types/StorageExtraInfo.ts b/src/types/StorageExtraInfo.ts index c11d979..b9bebbb 100644 --- a/src/types/StorageExtraInfo.ts +++ b/src/types/StorageExtraInfo.ts @@ -1,5 +1,5 @@ /** - * Copyright (c) Whales Corp. + * Copyright (c) Whales Corp. * All Rights Reserved. * * This source code is licensed under the MIT license found in the @@ -32,11 +32,11 @@ export function loadStorageExtraInfo(slice: Slice): StorageExtraInfo | null { export function storeStorageExtraInfo(src: StorageExtraInfo | null) { return (builder: Builder) => { - if (src === null) { + if (src === null || typeof src === 'undefined') { builder.storeUint(0, 3); } else { builder.storeUint(1, 3); builder.storeUint(src.dictHash, 256); } }; -} \ No newline at end of file +} diff --git a/src/types/StorageInfo.spec.ts b/src/types/StorageInfo.spec.ts index 84e5569..1daa5d9 100644 --- a/src/types/StorageInfo.spec.ts +++ b/src/types/StorageInfo.spec.ts @@ -1,5 +1,6 @@ import { Cell } from '../boc/Cell'; -import { loadStorageInfo } from './StorageInfo'; +import {loadStorageInfo, StorageInfo, storeStorageInfo} from './StorageInfo'; +import {Builder} from "../boc/Builder"; describe('StorageInfo', () => { @@ -18,4 +19,41 @@ describe('StorageInfo', () => { duePayment: null }); }); -}); \ No newline at end of file + + it.each([ + ['9c7b98a341201e6492f2bcf144215e1ddbef6774126caa57784fbce25597d4f7', { + used: { cells: 22n, bits: 5705n }, + storageExtra: null, + lastPaid: 1748811232, + duePayment: null, + }], + ['7672b3010077582fa477bf2b070183412aa0d1a9b98ba8c437b9d90b37b6a559', { + used: { cells: 22n, bits: 5705n }, + storageExtra: { + dictHash: 0n, + }, + lastPaid: 1748811232, + duePayment: null, + }], + ])('should store and store storage info %s', (hash: string, data: StorageInfo) => { + const builder = new Builder(); + storeStorageInfo(data)(builder); + const c = builder.endCell(); + expect(c.hash().toString('hex')).toEqual(hash); + }); + + // Source: https://github.com/ton-blockchain/ton/blob/b3b2bd1c3c645a931a9aa6cbfc915c258c9012cc/crypto/block/block.tlb#L256-L257 + // storage_info$_ used:StorageUsed last_paid:uint32 + // due_payment:(Maybe Grams) = StorageInfo; + it('should store and store storage info (old)', () => { + const builder = new Builder(); + // @ts-ignore + storeStorageInfo({ + used: { cells: 22n, bits: 5705n }, + lastPaid: 1748811232, + duePayment: null, + })(builder); + const c = builder.endCell(); + expect(c.hash().toString('hex')).toEqual('9c7b98a341201e6492f2bcf144215e1ddbef6774126caa57784fbce25597d4f7'); + }); +});