@gearbox-protocol/sdk 8.1.0-next.2 → 8.1.0

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.
Files changed (60) hide show
  1. package/dist/cjs/sdk/GearboxSDK.js +8 -20
  2. package/dist/cjs/sdk/abi/oracles.js +0 -141
  3. package/dist/cjs/sdk/chain/chains.js +3 -0
  4. package/dist/cjs/sdk/market/pricefeeds/AbstractPriceFeed.js +1 -2
  5. package/dist/cjs/sdk/market/pricefeeds/PriceFeedsRegister.js +23 -17
  6. package/dist/cjs/sdk/market/pricefeeds/PythPriceFeed.js +1 -9
  7. package/dist/cjs/sdk/market/pricefeeds/{updates/PriceUpdatesCache.js → RedstoneCache.js} +12 -12
  8. package/dist/cjs/sdk/market/pricefeeds/RedstonePriceFeed.js +7 -9
  9. package/dist/cjs/sdk/market/pricefeeds/{updates/RedstoneUpdater.js → RedstoneUpdater.js} +45 -21
  10. package/dist/cjs/sdk/market/pricefeeds/index.js +0 -2
  11. package/dist/cjs/sdk/sdk-gov-legacy/tokens/normal.js +5 -0
  12. package/dist/cjs/sdk/sdk-gov-legacy/tokens/token.js +5 -0
  13. package/dist/cjs/sdk/sdk-legacy/apy/index.js +1 -0
  14. package/dist/cjs/sdk/utils/retry.js +2 -4
  15. package/dist/esm/sdk/GearboxSDK.js +8 -20
  16. package/dist/esm/sdk/abi/oracles.js +0 -140
  17. package/dist/esm/sdk/chain/chains.js +3 -0
  18. package/dist/esm/sdk/market/pricefeeds/AbstractPriceFeed.js +1 -2
  19. package/dist/esm/sdk/market/pricefeeds/PriceFeedsRegister.js +24 -18
  20. package/dist/esm/sdk/market/pricefeeds/PythPriceFeed.js +1 -9
  21. package/dist/esm/sdk/market/pricefeeds/{updates/PriceUpdatesCache.js → RedstoneCache.js} +8 -8
  22. package/dist/esm/sdk/market/pricefeeds/RedstonePriceFeed.js +5 -8
  23. package/dist/esm/sdk/market/pricefeeds/{updates/RedstoneUpdater.js → RedstoneUpdater.js} +44 -21
  24. package/dist/esm/sdk/market/pricefeeds/index.js +0 -1
  25. package/dist/esm/sdk/sdk-gov-legacy/tokens/normal.js +5 -0
  26. package/dist/esm/sdk/sdk-gov-legacy/tokens/token.js +5 -0
  27. package/dist/esm/sdk/sdk-legacy/apy/index.js +1 -0
  28. package/dist/esm/sdk/utils/retry.js +2 -4
  29. package/dist/types/sdk/GearboxSDK.d.ts +1 -5
  30. package/dist/types/sdk/abi/oracles.d.ts +0 -212
  31. package/dist/types/sdk/chain/chains.d.ts +1 -1
  32. package/dist/types/sdk/market/pricefeeds/AbstractPriceFeed.d.ts +2 -2
  33. package/dist/types/sdk/market/pricefeeds/PriceFeedsRegister.d.ts +4 -4
  34. package/dist/types/sdk/market/pricefeeds/PythPriceFeed.d.ts +4 -218
  35. package/dist/types/sdk/market/pricefeeds/RedstoneCache.d.ts +25 -0
  36. package/dist/types/sdk/market/pricefeeds/RedstonePriceFeed.d.ts +4 -4
  37. package/dist/types/sdk/market/pricefeeds/{updates/RedstoneUpdater.d.ts → RedstoneUpdater.d.ts} +18 -11
  38. package/dist/types/sdk/market/pricefeeds/index.d.ts +0 -1
  39. package/dist/types/sdk/market/pricefeeds/types.d.ts +2 -5
  40. package/dist/types/sdk/sdk-gov-legacy/tokens/normal.d.ts +1 -1
  41. package/dist/types/sdk/sdk-legacy/apy/index.d.ts +1 -1
  42. package/dist/types/sdk/utils/retry.d.ts +0 -1
  43. package/package.json +1 -4
  44. package/dist/cjs/sdk/market/pricefeeds/isUpdatablePriceFeed.js +0 -30
  45. package/dist/cjs/sdk/market/pricefeeds/updates/PriceUpdateTx.js +0 -52
  46. package/dist/cjs/sdk/market/pricefeeds/updates/PythUpdater.js +0 -189
  47. package/dist/cjs/sdk/market/pricefeeds/updates/index.js +0 -31
  48. package/dist/cjs/sdk/market/pricefeeds/updates/types.js +0 -16
  49. package/dist/esm/sdk/market/pricefeeds/isUpdatablePriceFeed.js +0 -6
  50. package/dist/esm/sdk/market/pricefeeds/updates/PriceUpdateTx.js +0 -28
  51. package/dist/esm/sdk/market/pricefeeds/updates/PythUpdater.js +0 -169
  52. package/dist/esm/sdk/market/pricefeeds/updates/index.js +0 -6
  53. package/dist/esm/sdk/market/pricefeeds/updates/types.js +0 -0
  54. package/dist/types/sdk/market/pricefeeds/isUpdatablePriceFeed.d.ts +0 -2
  55. package/dist/types/sdk/market/pricefeeds/updates/PriceUpdateTx.d.ts +0 -10
  56. package/dist/types/sdk/market/pricefeeds/updates/PriceUpdatesCache.d.ts +0 -17
  57. package/dist/types/sdk/market/pricefeeds/updates/PythUpdater.d.ts +0 -40
  58. package/dist/types/sdk/market/pricefeeds/updates/RedstoneUpdater.test.d.ts +0 -1
  59. package/dist/types/sdk/market/pricefeeds/updates/index.d.ts +0 -3
  60. package/dist/types/sdk/market/pricefeeds/updates/types.d.ts +0 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "8.1.0-next.2",
3
+ "version": "8.1.0",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",
@@ -55,12 +55,9 @@
55
55
  "typecheck:ci": "tsc --noEmit"
56
56
  },
57
57
  "dependencies": {
58
- "@pythnetwork/price-service-sdk": "^1.8.0",
59
58
  "@redstone-finance/evm-connector": "^0.7.5",
60
59
  "@redstone-finance/protocol": "^0.7.5",
61
- "@types/bn.js": "^5.2.0",
62
60
  "abitype": "^1.0.8",
63
- "bn.js": "^5.2.2",
64
61
  "date-fns": "^4.1.0",
65
62
  "decimal.js-light": "^2.5.1",
66
63
  "keyv": "^5.3.3",
@@ -1,30 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var isUpdatablePriceFeed_exports = {};
20
- __export(isUpdatablePriceFeed_exports, {
21
- isUpdatablePriceFeed: () => isUpdatablePriceFeed
22
- });
23
- module.exports = __toCommonJS(isUpdatablePriceFeed_exports);
24
- function isUpdatablePriceFeed(pf) {
25
- return pf.updatable;
26
- }
27
- // Annotate the CommonJS export names for ESM import in node:
28
- 0 && (module.exports = {
29
- isUpdatablePriceFeed
30
- });
@@ -1,52 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var PriceUpdateTx_exports = {};
20
- __export(PriceUpdateTx_exports, {
21
- PriceUpdateTx: () => PriceUpdateTx
22
- });
23
- module.exports = __toCommonJS(PriceUpdateTx_exports);
24
- const MAX_DATA_TIMESTAMP_DELAY_SECONDS = 10n * 60n;
25
- const MAX_DATA_TIMESTAMP_AHEAD_SECONDS = 60n;
26
- class PriceUpdateTx {
27
- raw;
28
- data;
29
- constructor(raw, data) {
30
- this.raw = raw;
31
- this.data = data;
32
- }
33
- get pretty() {
34
- const cached = this.data.cached ? " (cached)" : "";
35
- return `${this.name} feed ${this.data.dataFeedId} at ${this.data.priceFeed} with timestamp ${this.data.timestamp}${cached}`;
36
- }
37
- validateTimestamp(blockTimestamp) {
38
- const { timestamp: expectedPayloadTimestamp } = this.data;
39
- if (blockTimestamp < expectedPayloadTimestamp) {
40
- if (BigInt(expectedPayloadTimestamp) - blockTimestamp > MAX_DATA_TIMESTAMP_AHEAD_SECONDS) {
41
- return "in future";
42
- }
43
- } else if (blockTimestamp - BigInt(expectedPayloadTimestamp) > MAX_DATA_TIMESTAMP_DELAY_SECONDS) {
44
- return "too old";
45
- }
46
- return "valid";
47
- }
48
- }
49
- // Annotate the CommonJS export names for ESM import in node:
50
- 0 && (module.exports = {
51
- PriceUpdateTx
52
- });
@@ -1,189 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var PythUpdater_exports = {};
20
- __export(PythUpdater_exports, {
21
- PythUpdater: () => PythUpdater
22
- });
23
- module.exports = __toCommonJS(PythUpdater_exports);
24
- var import_price_service_sdk = require("@pythnetwork/price-service-sdk");
25
- var import_viem = require("viem");
26
- var import_base = require("../../../base/index.js");
27
- var import_utils = require("../../../utils/index.js");
28
- var import_PriceUpdatesCache = require("./PriceUpdatesCache.js");
29
- var import_PriceUpdateTx = require("./PriceUpdateTx.js");
30
- class PythUpdateTx extends import_PriceUpdateTx.PriceUpdateTx {
31
- name = "pyth";
32
- }
33
- class PythUpdater extends import_base.SDKConstruct {
34
- #logger;
35
- #cache;
36
- #historicalTimestamp;
37
- #api;
38
- #ignoreMissingFeeds;
39
- constructor(sdk, opts = {}) {
40
- super(sdk);
41
- this.#logger = (0, import_utils.childLogger)("PythUpdater", sdk.logger);
42
- this.#ignoreMissingFeeds = opts.ignoreMissingFeeds;
43
- this.#api = opts.apiProxy ?? "https://hermes.pyth.network/v2/updates/price/";
44
- this.#api = this.#api.endsWith("/") ? this.#api : this.#api + "/";
45
- let ts = opts.historicTimestamp;
46
- if (ts) {
47
- this.#historicalTimestamp = ts === true ? Number(this.sdk.timestamp) : ts;
48
- }
49
- this.#cache = new import_PriceUpdatesCache.PriceUpdatesCache({
50
- // currently staleness period is 240 seconds on all networks, add some buffer
51
- // this period of 4 minutes is selected based on time that is required for user to sign transaction with wallet
52
- // so it's unlikely to decrease
53
- ttl: opts.cacheTTL ?? 225 * 1e3,
54
- historical: !!ts
55
- });
56
- }
57
- async getUpdateTxs(feeds, logContext = {}) {
58
- this.#logger?.debug(
59
- logContext,
60
- `generating update transactions for ${feeds.length} pyth price feeds`
61
- );
62
- const pythFeeds = new Map(
63
- feeds.filter(isPyth).map((f) => [f.priceFeedId, f])
64
- );
65
- const payloads = await this.#getPayloads(new Set(pythFeeds.keys()));
66
- const results = payloads.map((p) => {
67
- const priceFeed = pythFeeds.get(p.dataFeedId);
68
- if (!priceFeed) {
69
- throw new Error(`cannot find price feed for ${p.dataFeedId}`);
70
- }
71
- const { dataFeedId, timestamp, cached, data } = p;
72
- return new PythUpdateTx(priceFeed.createPriceUpdateTx(data), {
73
- dataFeedId,
74
- priceFeed: priceFeed.address,
75
- timestamp,
76
- cached
77
- });
78
- });
79
- this.#logger?.debug(
80
- logContext,
81
- `generated ${results.length} update transactions for pyth price feeds`
82
- );
83
- return results;
84
- }
85
- /**
86
- * Gets pyth payloads
87
- * @param dataFeedsIds
88
- * @returns
89
- */
90
- async #getPayloads(dataFeedsIds) {
91
- this.#logger?.debug(
92
- `getting pyth payloads for ${dataFeedsIds.size} price feeds: ${Array.from(dataFeedsIds).join(", ")}`
93
- );
94
- const fromCache = [];
95
- const uncached = [];
96
- for (const priceFeedsId of dataFeedsIds) {
97
- const cached = this.#cache.get(priceFeedsId);
98
- if (cached) {
99
- fromCache.push({ ...cached, cached: true });
100
- } else {
101
- uncached.push(priceFeedsId);
102
- }
103
- }
104
- const fromPyth = await this.#fetchPayloads(
105
- new Set(uncached)
106
- );
107
- for (const resp of fromPyth) {
108
- this.#cache.set(resp, resp.dataFeedId);
109
- }
110
- this.#logger?.debug(
111
- `got ${fromPyth.length} new pyth updates and ${fromCache.length} from cache`
112
- );
113
- return [...fromCache, ...fromPyth];
114
- }
115
- /**
116
- * Fetches pyth payloads from Hermes API
117
- * @param dataFeedsIds
118
- * @returns
119
- */
120
- async #fetchPayloads(dataFeedsIds) {
121
- if (dataFeedsIds.size === 0) {
122
- return [];
123
- }
124
- const ids = Array.from(dataFeedsIds);
125
- const tsStr = this.#historicalTimestamp ? ` with historical timestamp ${this.#historicalTimestamp}` : "";
126
- this.#logger?.debug(
127
- `fetching pyth payloads for ${dataFeedsIds.size} price feeds: ${ids.join(", ")}${tsStr}`
128
- );
129
- const url = new URL(this.#api + (this.#historicalTimestamp ?? "latest"));
130
- url.searchParams.append("parsed", "false");
131
- if (this.#ignoreMissingFeeds) {
132
- url.searchParams.append("ignore_invalid_price_ids", "true");
133
- }
134
- for (const id of dataFeedsIds) {
135
- url.searchParams.append("ids[]", id);
136
- }
137
- const resp = await (0, import_utils.retry)(
138
- async () => {
139
- const resp2 = await fetch(url.toString());
140
- if (!resp2.ok) {
141
- const body = await resp2.text();
142
- throw new Error(
143
- `failed to fetch pyth payloads: ${resp2.statusText}: ${body}`
144
- );
145
- }
146
- const data = await resp2.json();
147
- return data;
148
- },
149
- { attempts: 3, exponent: 2, interval: 200 }
150
- );
151
- return respToCalldata(resp);
152
- }
153
- }
154
- function isPyth(pf) {
155
- return pf.contractType === "PRICE_FEED::PYTH";
156
- }
157
- function respToCalldata(resp) {
158
- const updates = splitAccumulatorUpdates(resp.binary.data[0]);
159
- return updates.map(({ data, dataFeedId, timestamp }) => {
160
- return {
161
- dataFeedId,
162
- data: (0, import_viem.encodeAbiParameters)(
163
- [{ type: "uint256" }, { type: "bytes[]" }],
164
- [BigInt(timestamp), [data]]
165
- ),
166
- timestamp,
167
- cached: false
168
- };
169
- });
170
- }
171
- function splitAccumulatorUpdates(binary) {
172
- const data = Buffer.from(binary, "hex");
173
- const parsed = (0, import_price_service_sdk.parseAccumulatorUpdateData)(data);
174
- const results = [];
175
- for (let i = 0; i < parsed.updates.length; i++) {
176
- const upd = parsed.updates[i].message;
177
- const msg = (0, import_price_service_sdk.parsePriceFeedMessage)(upd);
178
- results.push({
179
- dataFeedId: (0, import_viem.toHex)(msg.feedId),
180
- timestamp: msg.publishTime.toNumber(),
181
- data: (0, import_viem.toHex)((0, import_price_service_sdk.sliceAccumulatorUpdateData)(data, i, i + 1))
182
- });
183
- }
184
- return results;
185
- }
186
- // Annotate the CommonJS export names for ESM import in node:
187
- 0 && (module.exports = {
188
- PythUpdater
189
- });
@@ -1,31 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var updates_exports = {};
20
- __export(updates_exports, {
21
- PythUpdater: () => import_PythUpdater.PythUpdater,
22
- RedstoneUpdater: () => import_RedstoneUpdater.RedstoneUpdater
23
- });
24
- module.exports = __toCommonJS(updates_exports);
25
- var import_PythUpdater = require("./PythUpdater.js");
26
- var import_RedstoneUpdater = require("./RedstoneUpdater.js");
27
- // Annotate the CommonJS export names for ESM import in node:
28
- 0 && (module.exports = {
29
- PythUpdater,
30
- RedstoneUpdater
31
- });
@@ -1,16 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
- var types_exports = {};
16
- module.exports = __toCommonJS(types_exports);
@@ -1,6 +0,0 @@
1
- function isUpdatablePriceFeed(pf) {
2
- return pf.updatable;
3
- }
4
- export {
5
- isUpdatablePriceFeed
6
- };
@@ -1,28 +0,0 @@
1
- const MAX_DATA_TIMESTAMP_DELAY_SECONDS = 10n * 60n;
2
- const MAX_DATA_TIMESTAMP_AHEAD_SECONDS = 60n;
3
- class PriceUpdateTx {
4
- raw;
5
- data;
6
- constructor(raw, data) {
7
- this.raw = raw;
8
- this.data = data;
9
- }
10
- get pretty() {
11
- const cached = this.data.cached ? " (cached)" : "";
12
- return `${this.name} feed ${this.data.dataFeedId} at ${this.data.priceFeed} with timestamp ${this.data.timestamp}${cached}`;
13
- }
14
- validateTimestamp(blockTimestamp) {
15
- const { timestamp: expectedPayloadTimestamp } = this.data;
16
- if (blockTimestamp < expectedPayloadTimestamp) {
17
- if (BigInt(expectedPayloadTimestamp) - blockTimestamp > MAX_DATA_TIMESTAMP_AHEAD_SECONDS) {
18
- return "in future";
19
- }
20
- } else if (blockTimestamp - BigInt(expectedPayloadTimestamp) > MAX_DATA_TIMESTAMP_DELAY_SECONDS) {
21
- return "too old";
22
- }
23
- return "valid";
24
- }
25
- }
26
- export {
27
- PriceUpdateTx
28
- };
@@ -1,169 +0,0 @@
1
- import {
2
- parseAccumulatorUpdateData,
3
- parsePriceFeedMessage,
4
- sliceAccumulatorUpdateData
5
- } from "@pythnetwork/price-service-sdk";
6
- import { encodeAbiParameters, toHex } from "viem";
7
- import { SDKConstruct } from "../../../base/index.js";
8
- import { childLogger, retry } from "../../../utils/index.js";
9
- import { PriceUpdatesCache } from "./PriceUpdatesCache.js";
10
- import { PriceUpdateTx } from "./PriceUpdateTx.js";
11
- class PythUpdateTx extends PriceUpdateTx {
12
- name = "pyth";
13
- }
14
- class PythUpdater extends SDKConstruct {
15
- #logger;
16
- #cache;
17
- #historicalTimestamp;
18
- #api;
19
- #ignoreMissingFeeds;
20
- constructor(sdk, opts = {}) {
21
- super(sdk);
22
- this.#logger = childLogger("PythUpdater", sdk.logger);
23
- this.#ignoreMissingFeeds = opts.ignoreMissingFeeds;
24
- this.#api = opts.apiProxy ?? "https://hermes.pyth.network/v2/updates/price/";
25
- this.#api = this.#api.endsWith("/") ? this.#api : this.#api + "/";
26
- let ts = opts.historicTimestamp;
27
- if (ts) {
28
- this.#historicalTimestamp = ts === true ? Number(this.sdk.timestamp) : ts;
29
- }
30
- this.#cache = new PriceUpdatesCache({
31
- // currently staleness period is 240 seconds on all networks, add some buffer
32
- // this period of 4 minutes is selected based on time that is required for user to sign transaction with wallet
33
- // so it's unlikely to decrease
34
- ttl: opts.cacheTTL ?? 225 * 1e3,
35
- historical: !!ts
36
- });
37
- }
38
- async getUpdateTxs(feeds, logContext = {}) {
39
- this.#logger?.debug(
40
- logContext,
41
- `generating update transactions for ${feeds.length} pyth price feeds`
42
- );
43
- const pythFeeds = new Map(
44
- feeds.filter(isPyth).map((f) => [f.priceFeedId, f])
45
- );
46
- const payloads = await this.#getPayloads(new Set(pythFeeds.keys()));
47
- const results = payloads.map((p) => {
48
- const priceFeed = pythFeeds.get(p.dataFeedId);
49
- if (!priceFeed) {
50
- throw new Error(`cannot find price feed for ${p.dataFeedId}`);
51
- }
52
- const { dataFeedId, timestamp, cached, data } = p;
53
- return new PythUpdateTx(priceFeed.createPriceUpdateTx(data), {
54
- dataFeedId,
55
- priceFeed: priceFeed.address,
56
- timestamp,
57
- cached
58
- });
59
- });
60
- this.#logger?.debug(
61
- logContext,
62
- `generated ${results.length} update transactions for pyth price feeds`
63
- );
64
- return results;
65
- }
66
- /**
67
- * Gets pyth payloads
68
- * @param dataFeedsIds
69
- * @returns
70
- */
71
- async #getPayloads(dataFeedsIds) {
72
- this.#logger?.debug(
73
- `getting pyth payloads for ${dataFeedsIds.size} price feeds: ${Array.from(dataFeedsIds).join(", ")}`
74
- );
75
- const fromCache = [];
76
- const uncached = [];
77
- for (const priceFeedsId of dataFeedsIds) {
78
- const cached = this.#cache.get(priceFeedsId);
79
- if (cached) {
80
- fromCache.push({ ...cached, cached: true });
81
- } else {
82
- uncached.push(priceFeedsId);
83
- }
84
- }
85
- const fromPyth = await this.#fetchPayloads(
86
- new Set(uncached)
87
- );
88
- for (const resp of fromPyth) {
89
- this.#cache.set(resp, resp.dataFeedId);
90
- }
91
- this.#logger?.debug(
92
- `got ${fromPyth.length} new pyth updates and ${fromCache.length} from cache`
93
- );
94
- return [...fromCache, ...fromPyth];
95
- }
96
- /**
97
- * Fetches pyth payloads from Hermes API
98
- * @param dataFeedsIds
99
- * @returns
100
- */
101
- async #fetchPayloads(dataFeedsIds) {
102
- if (dataFeedsIds.size === 0) {
103
- return [];
104
- }
105
- const ids = Array.from(dataFeedsIds);
106
- const tsStr = this.#historicalTimestamp ? ` with historical timestamp ${this.#historicalTimestamp}` : "";
107
- this.#logger?.debug(
108
- `fetching pyth payloads for ${dataFeedsIds.size} price feeds: ${ids.join(", ")}${tsStr}`
109
- );
110
- const url = new URL(this.#api + (this.#historicalTimestamp ?? "latest"));
111
- url.searchParams.append("parsed", "false");
112
- if (this.#ignoreMissingFeeds) {
113
- url.searchParams.append("ignore_invalid_price_ids", "true");
114
- }
115
- for (const id of dataFeedsIds) {
116
- url.searchParams.append("ids[]", id);
117
- }
118
- const resp = await retry(
119
- async () => {
120
- const resp2 = await fetch(url.toString());
121
- if (!resp2.ok) {
122
- const body = await resp2.text();
123
- throw new Error(
124
- `failed to fetch pyth payloads: ${resp2.statusText}: ${body}`
125
- );
126
- }
127
- const data = await resp2.json();
128
- return data;
129
- },
130
- { attempts: 3, exponent: 2, interval: 200 }
131
- );
132
- return respToCalldata(resp);
133
- }
134
- }
135
- function isPyth(pf) {
136
- return pf.contractType === "PRICE_FEED::PYTH";
137
- }
138
- function respToCalldata(resp) {
139
- const updates = splitAccumulatorUpdates(resp.binary.data[0]);
140
- return updates.map(({ data, dataFeedId, timestamp }) => {
141
- return {
142
- dataFeedId,
143
- data: encodeAbiParameters(
144
- [{ type: "uint256" }, { type: "bytes[]" }],
145
- [BigInt(timestamp), [data]]
146
- ),
147
- timestamp,
148
- cached: false
149
- };
150
- });
151
- }
152
- function splitAccumulatorUpdates(binary) {
153
- const data = Buffer.from(binary, "hex");
154
- const parsed = parseAccumulatorUpdateData(data);
155
- const results = [];
156
- for (let i = 0; i < parsed.updates.length; i++) {
157
- const upd = parsed.updates[i].message;
158
- const msg = parsePriceFeedMessage(upd);
159
- results.push({
160
- dataFeedId: toHex(msg.feedId),
161
- timestamp: msg.publishTime.toNumber(),
162
- data: toHex(sliceAccumulatorUpdateData(data, i, i + 1))
163
- });
164
- }
165
- return results;
166
- }
167
- export {
168
- PythUpdater
169
- };
@@ -1,6 +0,0 @@
1
- import { PythUpdater } from "./PythUpdater.js";
2
- import { RedstoneUpdater } from "./RedstoneUpdater.js";
3
- export {
4
- PythUpdater,
5
- RedstoneUpdater
6
- };
File without changes
@@ -1,2 +0,0 @@
1
- import type { IPriceFeedContract, IUpdatablePriceFeedContract } from "./types.js";
2
- export declare function isUpdatablePriceFeed(pf: IPriceFeedContract): pf is IUpdatablePriceFeedContract;
@@ -1,10 +0,0 @@
1
- import type { IPriceUpdateTx, RawTx } from "../../../types/index.js";
2
- import type { IPriceUpdateTask } from "./types.js";
3
- export declare abstract class PriceUpdateTx<T extends IPriceUpdateTask = IPriceUpdateTask> implements IPriceUpdateTx<T> {
4
- readonly raw: RawTx;
5
- readonly data: T;
6
- abstract readonly name: string;
7
- constructor(raw: RawTx, data: T);
8
- get pretty(): string;
9
- validateTimestamp(blockTimestamp: bigint): "valid" | "too old" | "in future";
10
- }
@@ -1,17 +0,0 @@
1
- import type { TimestampedCalldata } from "./types.js";
2
- export interface PriceUpdatesCacheOptions {
3
- /**
4
- * Assume that in historical mode we only need to fetch once and then reuse from cache forever
5
- */
6
- historical: boolean;
7
- /**
8
- * TTL in milliseconds
9
- */
10
- ttl: number;
11
- }
12
- export declare class PriceUpdatesCache {
13
- #private;
14
- constructor(opts: PriceUpdatesCacheOptions);
15
- get(...path: Array<number | string>): Omit<TimestampedCalldata, "cached"> | undefined;
16
- set(value: Omit<TimestampedCalldata, "cached">, ...path: Array<number | string>): void;
17
- }
@@ -1,40 +0,0 @@
1
- import { SDKConstruct } from "../../../base/index.js";
2
- import type { GearboxSDK } from "../../../GearboxSDK.js";
3
- import type { IPriceFeedContract } from "../types.js";
4
- import { PriceUpdateTx } from "./PriceUpdateTx.js";
5
- import type { IPriceUpdater, IPriceUpdateTask } from "./types.js";
6
- export type PythUpdateTask = IPriceUpdateTask;
7
- declare class PythUpdateTx extends PriceUpdateTx<PythUpdateTask> {
8
- readonly name = "pyth";
9
- }
10
- export interface PythOptions {
11
- /**
12
- * Fixed pyth historic timestamp in seconds
13
- * Set to true to enable pyth historical mode using timestamp from attach block
14
- */
15
- historicTimestamp?: number | true;
16
- /**
17
- * Override Hermes API with this proxy. Can be used to set caching proxies, to avoid rate limiting
18
- */
19
- apiProxy?: string;
20
- /**
21
- * TTL for pyth cache in milliseconds
22
- * If 0, disables caching
23
- * If not set, uses some default value
24
- * Cache is always enabled in historical mode
25
- */
26
- cacheTTL?: number;
27
- /**
28
- * When true, no error will be thrown when pyth is unable to fetch data for some feeds
29
- */
30
- ignoreMissingFeeds?: boolean;
31
- }
32
- /**
33
- * Class to update multiple pyth price feeds at once
34
- */
35
- export declare class PythUpdater extends SDKConstruct implements IPriceUpdater<PythUpdateTask> {
36
- #private;
37
- constructor(sdk: GearboxSDK, opts?: PythOptions);
38
- getUpdateTxs(feeds: IPriceFeedContract[], logContext?: Record<string, any>): Promise<PythUpdateTx[]>;
39
- }
40
- export {};
@@ -1,3 +0,0 @@
1
- export { type PythOptions, PythUpdater } from "./PythUpdater.js";
2
- export { type RedstoneOptions, RedstoneUpdater } from "./RedstoneUpdater.js";
3
- export type * from "./types.js";
@@ -1,21 +0,0 @@
1
- import type { Address } from "viem";
2
- import type { IPriceUpdateTx } from "../../../types/index.js";
3
- import type { IPriceFeedContract } from "../types.js";
4
- export interface IPriceUpdateTask {
5
- dataFeedId: string;
6
- priceFeed: Address;
7
- timestamp: number;
8
- cached: boolean;
9
- }
10
- export interface IPriceUpdater<T extends IPriceUpdateTask = IPriceUpdateTask> {
11
- getUpdateTxs: (feeds: IPriceFeedContract[], logContext?: Record<string, any>) => Promise<IPriceUpdateTx<T>[]>;
12
- }
13
- export interface TimestampedCalldata {
14
- dataFeedId: string;
15
- data: `0x${string}`;
16
- /**
17
- * This timestamp is in seconds
18
- */
19
- timestamp: number;
20
- cached: boolean;
21
- }