Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add reverse functions for dept and amount #887

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})
})
})
Loading