Skip to content

Commit

Permalink
feat: add stamp effective byte util
Browse files Browse the repository at this point in the history
  • Loading branch information
Levente Kiss committed Nov 29, 2023
1 parent ed67bdc commit 55c2c32
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/utils/stamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,44 @@ export function getStampMaximumCapacityBytes(depth: number): number {
return 2 ** depth * 4096
}


/**
* Based on https://docs.ethswarm.org/docs/learn/technology/contracts/postage-stamp/#effective-utilisation-table

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

View workflow job for this annotation

GitHub Actions / check (16.x)

Delete `⏎`
*/
const utilisationRateMap: Record<number, number> = {
22: 0.2867,
23: 0.4956,
24: 0.6433,
25: 0.7478,
26: 0.8217,
27: 0.8739,
28: 0.9108,
29: 0.9369,
30: 0.9554,
31: 0.9685,
32: 0.9777,
33: 0.9842,
34: 0.9889,
}

/**
* Utility function that calculates the effective volume of a postage batch based on its depth.
*
* Below 22 depth the effective volume is 0
* Above 34 it's always > 99%
*
* @returns {number} The effective volume of the postage batch in bytes.
*/
export function getStampEffectiveBytes(depth: number): number {
if(depth < 22) {
return 0
}

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

View workflow job for this annotation

GitHub Actions / check (16.x)

Insert `·`

const utilRate = utilisationRateMap[depth] ?? 0.99

return getStampMaximumCapacityBytes(depth) * utilRate;
}

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

View workflow job for this annotation

GitHub Actions / check (16.x)

Delete `;`
/**
* Utility function that calculates the cost of a postage batch based on its depth and amount.
*
Expand Down

0 comments on commit 55c2c32

Please sign in to comment.