Skip to content

Commit

Permalink
feat: add reverse functions for dept and amount
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferenc Sárai committed Nov 27, 2023
1 parent ed67bdc commit fe57134
Show file tree
Hide file tree
Showing 2 changed files with 60 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

Check failure on line 66 in src/utils/stamps.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Insert `·`
}

/**
* 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)

Check failure on line 78 in src/utils/stamps.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Replace `(gigabytes·<=·1)` with `gigabytes·<=·1`
}
33 changes: 33 additions & 0 deletions test/unit/utils/stamp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Check failure on line 56 in test/unit/utils/stamp.spec.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

This number literal will lose precision at runtime
})
it('should return 414720000 for < 0 value', () => {
expect(Utils.getAmountForTtl(-1)).to.eql('414720000')
})
})

Check failure on line 62 in test/unit/utils/stamp.spec.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Delete `⏎`
})

0 comments on commit fe57134

Please sign in to comment.