From fe57134e8a348b065f7303241b0dcca7fa1d8d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20S=C3=A1rai?= Date: Mon, 27 Nov 2023 09:27:48 +0100 Subject: [PATCH] feat: add reverse functions for dept and amount --- src/utils/stamps.ts | 27 +++++++++++++++++++++++++++ test/unit/utils/stamp.spec.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/utils/stamps.ts b/src/utils/stamps.ts index aed75228..adf48d26 100644 --- a/src/utils/stamps.ts +++ b/src/utils/stamps.ts @@ -1,3 +1,5 @@ +import { NumberString } from '../types' + /** * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth. * @@ -50,3 +52,28 @@ export function getStampCostInBzz(depth: number, amount: number): number { export function getStampTtlSeconds(amount: number, pricePerBlock = 24_000, blockTime = 5): number { return (amount * blockTime) / pricePerBlock } + +/** + * Utility function that calculates the amount of tokens required to maintain a given Time To Live (TTL) for a postage batch. + * + * This function estimates the required amount based on the provided TTL in days. + * + * @param {number} days - The Time To Live (TTL) in days. + * @returns {NumberString} The estimated amount of tokens needed for the specified TTL. + */ +export function getAmountForTtl(days: number): NumberString { + // 414720000 = (24 * 60 * 60 * 24_000) / 5 + return ((days <= 0? 1 : days) * 414720000).toString() as NumberString +} + +/** + * Utility function that calculates the depth required for a postage batch to achieve the specified capacity in gigabytes. + * + * The depth is determined based on the given gigabytes, and the result is adjusted to a minimum depth of 18. + * + * @param {number} gigabytes - The desired capacity of the postage batch in gigabytes. + * @returns {number} The calculated depth necessary to achieve the specified capacity. + */ +export function getDepthForCapacity(gigabytes: number): number { + return (gigabytes <= 1) ? 18 : Math.ceil(Math.log2(Math.ceil(gigabytes)) + 18) +} diff --git a/test/unit/utils/stamp.spec.ts b/test/unit/utils/stamp.spec.ts index 00776b07..c2ba723f 100644 --- a/test/unit/utils/stamp.spec.ts +++ b/test/unit/utils/stamp.spec.ts @@ -27,4 +27,37 @@ describe('stamp', () => { expect(Utils.getStampCostInPlur(20, 20_000_000_000)).to.eql(20971520000000000) }) }) + describe('reverse depth', () => { + it('sholud return value 21', () => { + expect(Utils.getDepthForCapacity(8)).to.eql(21) + }) + it('sholud return value 20', () => { + expect(Utils.getDepthForCapacity(4)).to.eql(20) + }) + it('sholud return value 19', () => { + expect(Utils.getDepthForCapacity(2)).to.eql(19) + }) + it('sholud return value 18', () => { + expect(Utils.getDepthForCapacity(1)).to.eql(18) + }) + it('sholud return value 20 for 2.3', () => { + expect(Utils.getDepthForCapacity(2.3)).to.eql(20) + }) + it('sholud return value 18 for 0', () => { + expect(Utils.getDepthForCapacity(0)).to.eql(18) + }) + it('sholud return value 18 for negative value', () => { + expect(Utils.getDepthForCapacity(-3)).to.eql(18) + }) + }) + + describe('reverse amount', () => { + it('should return 20_000_000_000 for 48,225308641975309 day (4166666.666666666666 sec / 86400)', () => { + expect(Utils.getAmountForTtl(48.225308641975309)).to.eql('20000000000') + }) + it('should return 414720000 for < 0 value', () => { + expect(Utils.getAmountForTtl(-1)).to.eql('414720000') + }) + }) + })