Skip to content

Commit

Permalink
feat: add reverse functions for depth and amount (#887)
Browse files Browse the repository at this point in the history
* feat: add reverse functions for dept and amount

* feat: add reverse depth and amount with linting

---------

Co-authored-by: Ferenc Sárai <ferenc.sarai@solarpunk.buzz>
  • Loading branch information
ferencsarai and Ferenc Sárai authored Nov 28, 2023
1 parent ed67bdc commit 85c67eb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/utils/stamps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NumberString } from '../types'

/**
* Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
*
Expand Down Expand Up @@ -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)
}
32 changes: 32 additions & 0 deletions test/unit/utils/stamp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,36 @@ 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(4166666.6666666665 / 86400)).to.eql('20000000000')
})
it('should return 414720000 for < 0 value', () => {
expect(Utils.getAmountForTtl(-1)).to.eql('414720000')
})
})
})

0 comments on commit 85c67eb

Please sign in to comment.