@defisaver/positions-sdk 2.1.169-custom-fetching-4-dev → 2.1.169-custom-fetching-5-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/services/merkl.d.ts +6 -4
- package/cjs/services/merkl.js +21 -12
- package/esm/services/merkl.d.ts +6 -4
- package/esm/services/merkl.js +21 -12
- package/package.json +1 -1
- package/src/services/merkl.ts +20 -11
package/cjs/services/merkl.d.ts
CHANGED
|
@@ -13,10 +13,12 @@ export declare const fetchAllMerklCampaigns: (query: Record<string, string>) =>
|
|
|
13
13
|
export declare const fetchMerklOpportunitiesByIds: (ids: string[]) => Promise<MerklOpportunity[]>;
|
|
14
14
|
/**
|
|
15
15
|
* Returns the reward APR Merkl recorded for the opportunity at the given unix timestamp (seconds),
|
|
16
|
-
* or `undefined` when no campaign was computing for it then. Records are
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
16
|
+
* or `undefined` when no campaign was computing for it then. Records are buckets keyed by their
|
|
17
|
+
* start, newest first, and each carries the last value recorded in the bucket. The hourly bucket of
|
|
18
|
+
* the timestamp's hour is used when Merkl still has it — it keeps hourly history for roughly the
|
|
19
|
+
* last ten weeks — and the daily bucket of the timestamp's day otherwise. Either window is a day
|
|
20
|
+
* wide to tolerate computation gaps and aligned to the bucket, so every block within one hour asks
|
|
21
|
+
* for (and caches) the same records.
|
|
20
22
|
*/
|
|
21
23
|
export declare const fetchMerklOpportunityAprAt: (opportunityId: string, timestamp: number) => Promise<number | undefined>;
|
|
22
24
|
/** Drops every cached historical response (campaign lists, opportunities, APR buckets). */
|
package/cjs/services/merkl.js
CHANGED
|
@@ -98,25 +98,34 @@ const fetchMerklOpportunitiesByIdsCached = (0, memoizee_1.default)(exports.fetch
|
|
|
98
98
|
maxAge: HISTORY_CACHE_TTL,
|
|
99
99
|
normalizer: ([ids]) => [...ids].sort().join(','),
|
|
100
100
|
});
|
|
101
|
-
|
|
102
|
-
* Returns the reward APR Merkl recorded for the opportunity at the given unix timestamp (seconds),
|
|
103
|
-
* or `undefined` when no campaign was computing for it then. Records are hourly buckets keyed by
|
|
104
|
-
* their start, newest first, so the bucket of the timestamp's hour carries the value in force at
|
|
105
|
-
* that moment; the window is a day wide to tolerate computation gaps. The window is aligned to the
|
|
106
|
-
* hour, so every block within one hour asks for (and caches) the same records.
|
|
107
|
-
*/
|
|
108
|
-
const fetchMerklOpportunityAprAt = (opportunityId, timestamp) => __awaiter(void 0, void 0, void 0, function* () {
|
|
109
|
-
var _a;
|
|
110
|
-
const to = Math.floor(timestamp / SECONDS_PER_HOUR) * SECONDS_PER_HOUR;
|
|
101
|
+
const fetchLatestMerklAprRecord = (opportunityId, grain, to) => __awaiter(void 0, void 0, void 0, function* () {
|
|
111
102
|
const records = yield merklGet(`${MERKL_OPPORTUNITIES_URL}/${opportunityId}/apr-records`, {
|
|
112
|
-
grain
|
|
103
|
+
grain,
|
|
113
104
|
from: (to - SECONDS_PER_DAY).toString(),
|
|
114
105
|
to: to.toString(),
|
|
115
106
|
items: '1',
|
|
116
107
|
}, `APR records for opportunity ${opportunityId}`);
|
|
117
108
|
if (!Array.isArray(records))
|
|
118
109
|
throw new Error(`Invalid Merkl APR records response for opportunity ${opportunityId}`);
|
|
119
|
-
return
|
|
110
|
+
return records[0];
|
|
111
|
+
});
|
|
112
|
+
/**
|
|
113
|
+
* Returns the reward APR Merkl recorded for the opportunity at the given unix timestamp (seconds),
|
|
114
|
+
* or `undefined` when no campaign was computing for it then. Records are buckets keyed by their
|
|
115
|
+
* start, newest first, and each carries the last value recorded in the bucket. The hourly bucket of
|
|
116
|
+
* the timestamp's hour is used when Merkl still has it — it keeps hourly history for roughly the
|
|
117
|
+
* last ten weeks — and the daily bucket of the timestamp's day otherwise. Either window is a day
|
|
118
|
+
* wide to tolerate computation gaps and aligned to the bucket, so every block within one hour asks
|
|
119
|
+
* for (and caches) the same records.
|
|
120
|
+
*/
|
|
121
|
+
const fetchMerklOpportunityAprAt = (opportunityId, timestamp) => __awaiter(void 0, void 0, void 0, function* () {
|
|
122
|
+
var _a;
|
|
123
|
+
const hour = Math.floor(timestamp / SECONDS_PER_HOUR) * SECONDS_PER_HOUR;
|
|
124
|
+
const hourly = yield fetchLatestMerklAprRecord(opportunityId, 'HOUR', hour);
|
|
125
|
+
if (hourly)
|
|
126
|
+
return hourly.apr;
|
|
127
|
+
const day = Math.floor(timestamp / SECONDS_PER_DAY) * SECONDS_PER_DAY;
|
|
128
|
+
return (_a = (yield fetchLatestMerklAprRecord(opportunityId, 'DAY', day))) === null || _a === void 0 ? void 0 : _a.apr;
|
|
120
129
|
});
|
|
121
130
|
exports.fetchMerklOpportunityAprAt = fetchMerklOpportunityAprAt;
|
|
122
131
|
const fetchMerklOpportunityAprAtCached = (0, memoizee_1.default)(exports.fetchMerklOpportunityAprAt, {
|
package/esm/services/merkl.d.ts
CHANGED
|
@@ -13,10 +13,12 @@ export declare const fetchAllMerklCampaigns: (query: Record<string, string>) =>
|
|
|
13
13
|
export declare const fetchMerklOpportunitiesByIds: (ids: string[]) => Promise<MerklOpportunity[]>;
|
|
14
14
|
/**
|
|
15
15
|
* Returns the reward APR Merkl recorded for the opportunity at the given unix timestamp (seconds),
|
|
16
|
-
* or `undefined` when no campaign was computing for it then. Records are
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
16
|
+
* or `undefined` when no campaign was computing for it then. Records are buckets keyed by their
|
|
17
|
+
* start, newest first, and each carries the last value recorded in the bucket. The hourly bucket of
|
|
18
|
+
* the timestamp's hour is used when Merkl still has it — it keeps hourly history for roughly the
|
|
19
|
+
* last ten weeks — and the daily bucket of the timestamp's day otherwise. Either window is a day
|
|
20
|
+
* wide to tolerate computation gaps and aligned to the bucket, so every block within one hour asks
|
|
21
|
+
* for (and caches) the same records.
|
|
20
22
|
*/
|
|
21
23
|
export declare const fetchMerklOpportunityAprAt: (opportunityId: string, timestamp: number) => Promise<number | undefined>;
|
|
22
24
|
/** Drops every cached historical response (campaign lists, opportunities, APR buckets). */
|
package/esm/services/merkl.js
CHANGED
|
@@ -89,25 +89,34 @@ const fetchMerklOpportunitiesByIdsCached = memoize(fetchMerklOpportunitiesByIds,
|
|
|
89
89
|
maxAge: HISTORY_CACHE_TTL,
|
|
90
90
|
normalizer: ([ids]) => [...ids].sort().join(','),
|
|
91
91
|
});
|
|
92
|
-
|
|
93
|
-
* Returns the reward APR Merkl recorded for the opportunity at the given unix timestamp (seconds),
|
|
94
|
-
* or `undefined` when no campaign was computing for it then. Records are hourly buckets keyed by
|
|
95
|
-
* their start, newest first, so the bucket of the timestamp's hour carries the value in force at
|
|
96
|
-
* that moment; the window is a day wide to tolerate computation gaps. The window is aligned to the
|
|
97
|
-
* hour, so every block within one hour asks for (and caches) the same records.
|
|
98
|
-
*/
|
|
99
|
-
export const fetchMerklOpportunityAprAt = (opportunityId, timestamp) => __awaiter(void 0, void 0, void 0, function* () {
|
|
100
|
-
var _a;
|
|
101
|
-
const to = Math.floor(timestamp / SECONDS_PER_HOUR) * SECONDS_PER_HOUR;
|
|
92
|
+
const fetchLatestMerklAprRecord = (opportunityId, grain, to) => __awaiter(void 0, void 0, void 0, function* () {
|
|
102
93
|
const records = yield merklGet(`${MERKL_OPPORTUNITIES_URL}/${opportunityId}/apr-records`, {
|
|
103
|
-
grain
|
|
94
|
+
grain,
|
|
104
95
|
from: (to - SECONDS_PER_DAY).toString(),
|
|
105
96
|
to: to.toString(),
|
|
106
97
|
items: '1',
|
|
107
98
|
}, `APR records for opportunity ${opportunityId}`);
|
|
108
99
|
if (!Array.isArray(records))
|
|
109
100
|
throw new Error(`Invalid Merkl APR records response for opportunity ${opportunityId}`);
|
|
110
|
-
return
|
|
101
|
+
return records[0];
|
|
102
|
+
});
|
|
103
|
+
/**
|
|
104
|
+
* Returns the reward APR Merkl recorded for the opportunity at the given unix timestamp (seconds),
|
|
105
|
+
* or `undefined` when no campaign was computing for it then. Records are buckets keyed by their
|
|
106
|
+
* start, newest first, and each carries the last value recorded in the bucket. The hourly bucket of
|
|
107
|
+
* the timestamp's hour is used when Merkl still has it — it keeps hourly history for roughly the
|
|
108
|
+
* last ten weeks — and the daily bucket of the timestamp's day otherwise. Either window is a day
|
|
109
|
+
* wide to tolerate computation gaps and aligned to the bucket, so every block within one hour asks
|
|
110
|
+
* for (and caches) the same records.
|
|
111
|
+
*/
|
|
112
|
+
export const fetchMerklOpportunityAprAt = (opportunityId, timestamp) => __awaiter(void 0, void 0, void 0, function* () {
|
|
113
|
+
var _a;
|
|
114
|
+
const hour = Math.floor(timestamp / SECONDS_PER_HOUR) * SECONDS_PER_HOUR;
|
|
115
|
+
const hourly = yield fetchLatestMerklAprRecord(opportunityId, 'HOUR', hour);
|
|
116
|
+
if (hourly)
|
|
117
|
+
return hourly.apr;
|
|
118
|
+
const day = Math.floor(timestamp / SECONDS_PER_DAY) * SECONDS_PER_DAY;
|
|
119
|
+
return (_a = (yield fetchLatestMerklAprRecord(opportunityId, 'DAY', day))) === null || _a === void 0 ? void 0 : _a.apr;
|
|
111
120
|
});
|
|
112
121
|
const fetchMerklOpportunityAprAtCached = memoize(fetchMerklOpportunityAprAt, {
|
|
113
122
|
promise: true,
|
package/package.json
CHANGED
package/src/services/merkl.ts
CHANGED
|
@@ -96,23 +96,32 @@ const fetchMerklOpportunitiesByIdsCached = memoize(fetchMerklOpportunitiesByIds,
|
|
|
96
96
|
normalizer: ([ids]: [string[]]) => [...ids].sort().join(','),
|
|
97
97
|
});
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
* Returns the reward APR Merkl recorded for the opportunity at the given unix timestamp (seconds),
|
|
101
|
-
* or `undefined` when no campaign was computing for it then. Records are hourly buckets keyed by
|
|
102
|
-
* their start, newest first, so the bucket of the timestamp's hour carries the value in force at
|
|
103
|
-
* that moment; the window is a day wide to tolerate computation gaps. The window is aligned to the
|
|
104
|
-
* hour, so every block within one hour asks for (and caches) the same records.
|
|
105
|
-
*/
|
|
106
|
-
export const fetchMerklOpportunityAprAt = async (opportunityId: string, timestamp: number): Promise<number | undefined> => {
|
|
107
|
-
const to = Math.floor(timestamp / SECONDS_PER_HOUR) * SECONDS_PER_HOUR;
|
|
99
|
+
const fetchLatestMerklAprRecord = async (opportunityId: string, grain: 'HOUR' | 'DAY', to: number): Promise<MerklAprRecord | undefined> => {
|
|
108
100
|
const records = await merklGet<unknown>(`${MERKL_OPPORTUNITIES_URL}/${opportunityId}/apr-records`, {
|
|
109
|
-
grain
|
|
101
|
+
grain,
|
|
110
102
|
from: (to - SECONDS_PER_DAY).toString(),
|
|
111
103
|
to: to.toString(),
|
|
112
104
|
items: '1',
|
|
113
105
|
}, `APR records for opportunity ${opportunityId}`);
|
|
114
106
|
if (!Array.isArray(records)) throw new Error(`Invalid Merkl APR records response for opportunity ${opportunityId}`);
|
|
115
|
-
return (records as MerklAprRecord[])[0]
|
|
107
|
+
return (records as MerklAprRecord[])[0];
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Returns the reward APR Merkl recorded for the opportunity at the given unix timestamp (seconds),
|
|
112
|
+
* or `undefined` when no campaign was computing for it then. Records are buckets keyed by their
|
|
113
|
+
* start, newest first, and each carries the last value recorded in the bucket. The hourly bucket of
|
|
114
|
+
* the timestamp's hour is used when Merkl still has it — it keeps hourly history for roughly the
|
|
115
|
+
* last ten weeks — and the daily bucket of the timestamp's day otherwise. Either window is a day
|
|
116
|
+
* wide to tolerate computation gaps and aligned to the bucket, so every block within one hour asks
|
|
117
|
+
* for (and caches) the same records.
|
|
118
|
+
*/
|
|
119
|
+
export const fetchMerklOpportunityAprAt = async (opportunityId: string, timestamp: number): Promise<number | undefined> => {
|
|
120
|
+
const hour = Math.floor(timestamp / SECONDS_PER_HOUR) * SECONDS_PER_HOUR;
|
|
121
|
+
const hourly = await fetchLatestMerklAprRecord(opportunityId, 'HOUR', hour);
|
|
122
|
+
if (hourly) return hourly.apr;
|
|
123
|
+
const day = Math.floor(timestamp / SECONDS_PER_DAY) * SECONDS_PER_DAY;
|
|
124
|
+
return (await fetchLatestMerklAprRecord(opportunityId, 'DAY', day))?.apr;
|
|
116
125
|
};
|
|
117
126
|
|
|
118
127
|
const fetchMerklOpportunityAprAtCached = memoize(fetchMerklOpportunityAprAt, {
|