Skip to content

Commit 1204bd9

Browse files
authored
feat: add new function to calculate storage unit expiry timestamp (#2645)
## Why is this change needed? Expose a function that calculates the expiry timestamp from just the block timestamp. ## Merge Checklist _Choose all relevant options below by adding an `x` now or at any time before submitting for review_ - [x] PR title adheres to the [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) standard - [x] PR has a [changeset](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#35-adding-changesets) - [x] PR has been tagged with a change label(s) (i.e. documentation, feature, bugfix, or chore) - [ ] PR includes [documentation](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#32-writing-docs) if necessary. <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on updating the `@farcaster/core` and `@farcaster/hub-nodejs` packages to version `0.18.6` and `0.15.6` respectively, along with introducing a new function for calculating storage unit expiry timestamps. ### Detailed summary - Updated `@farcaster/core` to version `0.18.6`. - Updated `@farcaster/hub-nodejs` to version `0.15.6`. - Added `getStorageExpiryTimestampFromBlockTimestamp` function in `limits.ts`. - Modified `getStorageUnitExpiry` to use the new expiry function. - Added tests for `getStorageExpiryTimestampFromBlockTimestamp` in `limits.test.ts`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 80637a9 commit 1204bd9

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

packages/core/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @farcaster/core
22

3+
## 0.18.6
4+
5+
### Patch Changes
6+
7+
- feat: Create a new function to calculate storage unit expiry timestamp
8+
39
## 0.18.5
410

511
### Patch Changes

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@farcaster/core",
3-
"version": "0.18.5",
3+
"version": "0.18.6",
44
"main": "./dist/index.js",
55
"module": "./dist/index.mjs",
66
"types": "./dist/index.d.ts",

packages/core/src/limits.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { getDefaultStoreLimit, getStoreLimit, getStoreLimits } from "./limits";
1+
import {
2+
getDefaultStoreLimit,
3+
getStorageExpiryTimestampFromBlockTimestamp,
4+
getStoreLimit,
5+
getStoreLimits,
6+
LEGACY_STORAGE_UNIT_CUTOFF_TIMESTAMP,
7+
UNIT_TYPE_2024__CUTOFF_TIMESTAMP,
8+
} from "./limits";
29
import { StorageUnitType, StoreType } from "./protobufs";
310

411
describe("getDefaultStoreLimit", () => {
@@ -23,6 +30,25 @@ describe("getDefaultStoreLimit", () => {
2330
});
2431
});
2532

33+
describe("getStorageExpiryTimestampFromBlockTimestamp", () => {
34+
test("calculates correct expiry timestamp for legacy, 2024, and 2025 storage units", () => {
35+
const ONE_YEAR_IN_SECONDS = 365 * 24 * 60 * 60;
36+
const legacyTimestamp = LEGACY_STORAGE_UNIT_CUTOFF_TIMESTAMP - 1;
37+
const unitType2024Timestamp = UNIT_TYPE_2024__CUTOFF_TIMESTAMP - 1;
38+
const unitType2025Timestamp = UNIT_TYPE_2024__CUTOFF_TIMESTAMP + 1;
39+
40+
expect(getStorageExpiryTimestampFromBlockTimestamp(legacyTimestamp)).toEqual(
41+
legacyTimestamp + ONE_YEAR_IN_SECONDS * 3,
42+
);
43+
expect(getStorageExpiryTimestampFromBlockTimestamp(unitType2024Timestamp)).toEqual(
44+
unitType2024Timestamp + ONE_YEAR_IN_SECONDS * 2,
45+
);
46+
expect(getStorageExpiryTimestampFromBlockTimestamp(unitType2025Timestamp)).toEqual(
47+
unitType2025Timestamp + ONE_YEAR_IN_SECONDS,
48+
);
49+
});
50+
});
51+
2652
describe("getStoreLimit", () => {
2753
test("returns correct limits for multiple unit types", () => {
2854
// single unit type

packages/core/src/limits.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,18 @@ export const getStorageUnitType = (event: StorageRentOnChainEvent) => {
9292
};
9393

9494
export const getStorageUnitExpiry = (event: StorageRentOnChainEvent) => {
95-
if (event.blockTimestamp < LEGACY_STORAGE_UNIT_CUTOFF_TIMESTAMP) {
95+
return getStorageExpiryTimestampFromBlockTimestamp(event.blockTimestamp);
96+
};
97+
98+
export const getStorageExpiryTimestampFromBlockTimestamp = (blockTimestamp: number) => {
99+
if (blockTimestamp < LEGACY_STORAGE_UNIT_CUTOFF_TIMESTAMP) {
96100
// Legacy storage units expire after 2 years
97-
return event.blockTimestamp + ONE_YEAR_IN_SECONDS * 3;
98-
} else if (event.blockTimestamp < UNIT_TYPE_2024__CUTOFF_TIMESTAMP) {
101+
return blockTimestamp + ONE_YEAR_IN_SECONDS * 3;
102+
} else if (blockTimestamp < UNIT_TYPE_2024__CUTOFF_TIMESTAMP) {
99103
// 2024 storage units expire after 2 years
100-
return event.blockTimestamp + ONE_YEAR_IN_SECONDS * 2;
104+
return blockTimestamp + ONE_YEAR_IN_SECONDS * 2;
101105
} else {
102106
// 2025 storage units expire after 1 year
103-
return event.blockTimestamp + ONE_YEAR_IN_SECONDS;
107+
return blockTimestamp + ONE_YEAR_IN_SECONDS;
104108
}
105109
};

packages/hub-nodejs/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @farcaster/hub-nodejs
22

3+
## 0.15.6
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
- @farcaster/core@0.18.6
9+
310
## 0.15.5
411

512
### Patch Changes

packages/hub-nodejs/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@farcaster/hub-nodejs",
3-
"version": "0.15.5",
3+
"version": "0.15.6",
44
"main": "./dist/index.js",
55
"module": "./dist/index.mjs",
66
"types": "./dist/index.d.ts",
@@ -20,7 +20,7 @@
2020
"url": "https://github.com/farcasterxyz/hub-monorepo/blob/main/packages/hub-nodejs"
2121
},
2222
"dependencies": {
23-
"@farcaster/core": "0.18.5",
23+
"@farcaster/core": "0.18.6",
2424
"@grpc/grpc-js": "~1.11.1",
2525
"@noble/hashes": "^1.3.0",
2626
"neverthrow": "^6.0.0"

0 commit comments

Comments
 (0)