@xyo-network/react-price-forecast-plugin 2.60.0 → 2.61.0-rc.1

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 (97) hide show
  1. package/dist/Plugin.d.mts.map +1 -0
  2. package/dist/Plugin.d.ts +3 -0
  3. package/dist/Plugin.d.ts.map +1 -0
  4. package/dist/components/DetailsBox.d.mts.map +1 -0
  5. package/dist/components/DetailsBox.d.ts +9 -0
  6. package/dist/components/DetailsBox.d.ts.map +1 -0
  7. package/dist/components/DetailsBox.stories.d.mts +6 -0
  8. package/dist/components/DetailsBox.stories.d.mts.map +1 -0
  9. package/dist/components/DetailsBox.stories.d.ts +6 -0
  10. package/dist/components/DetailsBox.stories.d.ts.map +1 -0
  11. package/dist/components/index.d.mts.map +1 -0
  12. package/dist/components/index.d.ts +2 -0
  13. package/dist/components/index.d.ts.map +1 -0
  14. package/dist/index.d.mts.map +1 -0
  15. package/dist/{esm/index.js → index.d.ts} +1 -1
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +276 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/index.mjs +258 -0
  20. package/dist/index.mjs.map +1 -0
  21. package/dist/lib/DataLineStyles.d.mts.map +1 -0
  22. package/dist/lib/DataLineStyles.d.ts +5 -0
  23. package/dist/lib/DataLineStyles.d.ts.map +1 -0
  24. package/dist/lib/DataPointStyles.d.mts.map +1 -0
  25. package/dist/lib/DataPointStyles.d.ts +8 -0
  26. package/dist/lib/DataPointStyles.d.ts.map +1 -0
  27. package/dist/lib/ForecastLineChartConfigBuilder.d.mts +47 -0
  28. package/dist/lib/ForecastLineChartConfigBuilder.d.mts.map +1 -0
  29. package/dist/{types/lib → lib}/ForecastLineChartConfigBuilder.d.ts +0 -4
  30. package/dist/lib/ForecastLineChartConfigBuilder.d.ts.map +1 -0
  31. package/dist/lib/MockSourcePayloads.d.mts.map +1 -0
  32. package/dist/lib/MockSourcePayloads.d.ts +18 -0
  33. package/dist/lib/MockSourcePayloads.d.ts.map +1 -0
  34. package/dist/lib/SourcePayloads.d.mts +30 -0
  35. package/dist/lib/SourcePayloads.d.mts.map +1 -0
  36. package/dist/{types/lib → lib}/SourcePayloads.d.ts +2 -2
  37. package/dist/lib/SourcePayloads.d.ts.map +1 -0
  38. package/dist/lib/index.d.mts.map +1 -0
  39. package/dist/{esm/lib/index.js → lib/index.d.ts} +1 -1
  40. package/dist/lib/index.d.ts.map +1 -0
  41. package/package.json +30 -21
  42. package/dist/cjs/Plugin.js +0 -15
  43. package/dist/cjs/Plugin.js.map +0 -1
  44. package/dist/cjs/components/DetailsBox.js +0 -33
  45. package/dist/cjs/components/DetailsBox.js.map +0 -1
  46. package/dist/cjs/components/index.js +0 -5
  47. package/dist/cjs/components/index.js.map +0 -1
  48. package/dist/cjs/index.js +0 -6
  49. package/dist/cjs/index.js.map +0 -1
  50. package/dist/cjs/lib/DataLineStyles.js +0 -10
  51. package/dist/cjs/lib/DataLineStyles.js.map +0 -1
  52. package/dist/cjs/lib/DataPointStyles.js +0 -12
  53. package/dist/cjs/lib/DataPointStyles.js.map +0 -1
  54. package/dist/cjs/lib/ForecastLineChartConfigBuilder.js +0 -133
  55. package/dist/cjs/lib/ForecastLineChartConfigBuilder.js.map +0 -1
  56. package/dist/cjs/lib/MockSourcePayloads.js +0 -24
  57. package/dist/cjs/lib/MockSourcePayloads.js.map +0 -1
  58. package/dist/cjs/lib/SourcePayloads.js +0 -49
  59. package/dist/cjs/lib/SourcePayloads.js.map +0 -1
  60. package/dist/cjs/lib/index.js +0 -7
  61. package/dist/cjs/lib/index.js.map +0 -1
  62. package/dist/esm/Plugin.js +0 -14
  63. package/dist/esm/Plugin.js.map +0 -1
  64. package/dist/esm/components/DetailsBox.js +0 -27
  65. package/dist/esm/components/DetailsBox.js.map +0 -1
  66. package/dist/esm/components/index.js +0 -2
  67. package/dist/esm/components/index.js.map +0 -1
  68. package/dist/esm/index.js.map +0 -1
  69. package/dist/esm/lib/DataLineStyles.js +0 -6
  70. package/dist/esm/lib/DataLineStyles.js.map +0 -1
  71. package/dist/esm/lib/DataPointStyles.js +0 -8
  72. package/dist/esm/lib/DataPointStyles.js.map +0 -1
  73. package/dist/esm/lib/ForecastLineChartConfigBuilder.js +0 -130
  74. package/dist/esm/lib/ForecastLineChartConfigBuilder.js.map +0 -1
  75. package/dist/esm/lib/MockSourcePayloads.js +0 -20
  76. package/dist/esm/lib/MockSourcePayloads.js.map +0 -1
  77. package/dist/esm/lib/SourcePayloads.js +0 -41
  78. package/dist/esm/lib/SourcePayloads.js.map +0 -1
  79. package/dist/esm/lib/index.js.map +0 -1
  80. package/dist/types/Plugin.d.ts.map +0 -1
  81. package/dist/types/components/DetailsBox.d.ts.map +0 -1
  82. package/dist/types/components/index.d.ts.map +0 -1
  83. package/dist/types/index.d.ts.map +0 -1
  84. package/dist/types/lib/DataLineStyles.d.ts.map +0 -1
  85. package/dist/types/lib/DataPointStyles.d.ts.map +0 -1
  86. package/dist/types/lib/ForecastLineChartConfigBuilder.d.ts.map +0 -1
  87. package/dist/types/lib/MockSourcePayloads.d.ts.map +0 -1
  88. package/dist/types/lib/SourcePayloads.d.ts.map +0 -1
  89. package/dist/types/lib/index.d.ts.map +0 -1
  90. /package/dist/{types/Plugin.d.ts → Plugin.d.mts} +0 -0
  91. /package/dist/{types/components/DetailsBox.d.ts → components/DetailsBox.d.mts} +0 -0
  92. /package/dist/{types/components/index.d.ts → components/index.d.mts} +0 -0
  93. /package/dist/{types/index.d.ts → index.d.mts} +0 -0
  94. /package/dist/{types/lib/DataLineStyles.d.ts → lib/DataLineStyles.d.mts} +0 -0
  95. /package/dist/{types/lib/DataPointStyles.d.ts → lib/DataPointStyles.d.mts} +0 -0
  96. /package/dist/{types/lib/MockSourcePayloads.d.ts → lib/MockSourcePayloads.d.mts} +0 -0
  97. /package/dist/{types/lib/index.d.ts → lib/index.d.mts} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Plugin.d.ts","sourceRoot":"","sources":["../src/Plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,mBAAmB,EAAE,MAAM,mCAAmC,CAAA;AAIlG,eAAO,MAAM,yBAAyB,EAAE,mBAUvC,CAAA"}
@@ -0,0 +1,3 @@
1
+ import { PayloadRenderPlugin } from '@xyo-network/react-payload-plugin';
2
+ export declare const PriceForecastRenderPlugin: PayloadRenderPlugin;
3
+ //# sourceMappingURL=Plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Plugin.d.ts","sourceRoot":"","sources":["../src/Plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,mBAAmB,EAAE,MAAM,mCAAmC,CAAA;AAIlG,eAAO,MAAM,yBAAyB,EAAE,mBAUvC,CAAA"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DetailsBox.d.ts","sourceRoot":"","sources":["../../src/components/DetailsBox.tsx"],"names":[],"mappings":";AAAA,OAAO,uBAAuB,CAAA;AAI9B,OAAO,EAAE,YAAY,EAAW,MAAM,uBAAuB,CAAA;AAE7D,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAqBpD,MAAM,WAAW,4BAA6B,SAAQ,YAAY;IAChE,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,eAAO,MAAM,uBAAuB,EAAE,KAAK,CAAC,EAAE,CAAC,4BAA4B,CAuB1E,CAAA"}
@@ -0,0 +1,9 @@
1
+ /// <reference types="react" />
2
+ import 'chartjs-adapter-luxon';
3
+ import { FlexBoxProps } from '@xylabs/react-flexbox';
4
+ import { Payload } from '@xyo-network/payload-model';
5
+ export interface PriceForecastDetailsBoxProps extends FlexBoxProps {
6
+ payload?: Payload;
7
+ }
8
+ export declare const PriceForecastDetailsBox: React.FC<PriceForecastDetailsBoxProps>;
9
+ //# sourceMappingURL=DetailsBox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DetailsBox.d.ts","sourceRoot":"","sources":["../../src/components/DetailsBox.tsx"],"names":[],"mappings":";AAAA,OAAO,uBAAuB,CAAA;AAI9B,OAAO,EAAE,YAAY,EAAW,MAAM,uBAAuB,CAAA;AAE7D,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAqBpD,MAAM,WAAW,4BAA6B,SAAQ,YAAY;IAChE,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,eAAO,MAAM,uBAAuB,EAAE,KAAK,CAAC,EAAE,CAAC,4BAA4B,CAuB1E,CAAA"}
@@ -0,0 +1,6 @@
1
+ declare const StorybookEntry: import("@storybook/types").ComponentAnnotations<import("@storybook/react/dist/types-0a347bb9").R, import("./DetailsBox").PriceForecastDetailsBoxProps>;
2
+ declare const Default: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-0a347bb9").R, import("./DetailsBox").PriceForecastDetailsBoxProps>;
3
+ declare const WithData: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-0a347bb9").R, import("./DetailsBox").PriceForecastDetailsBoxProps>;
4
+ export { Default, WithData };
5
+ export default StorybookEntry;
6
+ //# sourceMappingURL=DetailsBox.stories.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DetailsBox.stories.d.ts","sourceRoot":"","sources":["../../src/components/DetailsBox.stories.tsx"],"names":[],"mappings":"AAoBA,QAAA,MAAM,cAAc,wJASqB,CAAA;AAqCzC,QAAA,MAAM,OAAO,oJAAoB,CAAA;AAGjC,QAAA,MAAM,QAAQ,oJAAoB,CAAA;AAKlC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAA;AAG5B,eAAe,cAAc,CAAA"}
@@ -0,0 +1,6 @@
1
+ declare const StorybookEntry: import("@storybook/types").ComponentAnnotations<import("@storybook/react/dist/types-0a347bb9").R, import("./DetailsBox").PriceForecastDetailsBoxProps>;
2
+ declare const Default: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-0a347bb9").R, import("./DetailsBox").PriceForecastDetailsBoxProps>;
3
+ declare const WithData: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-0a347bb9").R, import("./DetailsBox").PriceForecastDetailsBoxProps>;
4
+ export { Default, WithData };
5
+ export default StorybookEntry;
6
+ //# sourceMappingURL=DetailsBox.stories.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DetailsBox.stories.d.ts","sourceRoot":"","sources":["../../src/components/DetailsBox.stories.tsx"],"names":[],"mappings":"AAoBA,QAAA,MAAM,cAAc,wJASqB,CAAA;AAqCzC,QAAA,MAAM,OAAO,oJAAoB,CAAA;AAGjC,QAAA,MAAM,QAAQ,oJAAoB,CAAA;AAKlC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAA;AAG5B,eAAe,cAAc,CAAA"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './DetailsBox';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,UAAU,CAAA"}
@@ -1,3 +1,3 @@
1
1
  export * from './components';
2
2
  export * from './Plugin';
3
- //# sourceMappingURL=index.js.map
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,UAAU,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,276 @@
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
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ PriceForecastDetailsBox: () => PriceForecastDetailsBox,
24
+ PriceForecastRenderPlugin: () => PriceForecastRenderPlugin
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/components/DetailsBox.tsx
29
+ var import_chartjs_adapter_luxon = require("chartjs-adapter-luxon");
30
+ var import_material2 = require("@mui/material");
31
+ var import_react_async_effect = require("@xylabs/react-async-effect");
32
+ var import_react_flexbox = require("@xylabs/react-flexbox");
33
+ var import_chart = require("chart.js");
34
+ var import_react = require("react");
35
+ var import_react_chartjs_2 = require("react-chartjs-2");
36
+
37
+ // src/lib/DataLineStyles.ts
38
+ var import_material = require("@mui/material");
39
+ var DataLineStyles = (color) => ({
40
+ backgroundColor: color ? (0, import_material.alpha)(color, 0.5) : void 0,
41
+ borderColor: color
42
+ });
43
+
44
+ // src/lib/DataPointStyles.ts
45
+ var DataPointStyles = (pointHoverBackgroundColor) => ({
46
+ pointHitRadius: 20,
47
+ pointHoverBackgroundColor,
48
+ pointHoverRadius: 10,
49
+ pointRadius: 5,
50
+ pointStyle: "circle"
51
+ });
52
+
53
+ // src/lib/MockSourcePayloads.ts
54
+ var MockSourcePayloads = () => {
55
+ const tenMin = 6e5;
56
+ return [
57
+ {
58
+ baseFee: 38.90155387825,
59
+ feePerGas: { high: 47.9945864396, low: 39.006868093, medium: 39.306868093, veryHigh: 44.45384380525 },
60
+ priorityFeePerGas: { high: 1.0266666666666666, low: -0.41000000000000003, medium: 0.38, veryHigh: 1.3900000000000001 },
61
+ schema: "network.xyo.blockchain.ethereum.gas",
62
+ timestamp: Date.now() - tenMin
63
+ },
64
+ {
65
+ baseFee: 38.90155387825,
66
+ feePerGas: { high: 47.9945864396, low: 39.006868093, medium: 100, veryHigh: 44.45384380525 },
67
+ priorityFeePerGas: { high: 1.0266666666666666, low: -0.41000000000000003, medium: 0.38, veryHigh: 1.3900000000000001 },
68
+ schema: "network.xyo.blockchain.ethereum.gas",
69
+ timestamp: Date.now()
70
+ }
71
+ ];
72
+ };
73
+
74
+ // src/lib/SourcePayloads.ts
75
+ var SourcePayloads = class {
76
+ constructor(sourcePayloads) {
77
+ this.sourcePayloads = sourcePayloads;
78
+ }
79
+ sourcePrices = [];
80
+ get payloads() {
81
+ return this.sourcePayloads;
82
+ }
83
+ static async build(jsonPath) {
84
+ const sourcePayloads = await this.fetchSourcePayloads();
85
+ const instance = new this(sourcePayloads);
86
+ const paths = jsonPath.split(".");
87
+ instance.sourcePrices = sourcePayloads.map((payload) => {
88
+ return { x: payload.timestamp, y: instance.jsonPathTraverser(payload, paths) };
89
+ });
90
+ return instance;
91
+ }
92
+ // TODO - fetch from archivist
93
+ static async fetchSourcePayloads() {
94
+ const payloads = await Promise.resolve(MockSourcePayloads());
95
+ return payloads;
96
+ }
97
+ jsonPathTraverser(obj, path) {
98
+ let result = obj;
99
+ for (const key of path) {
100
+ if (key in result) {
101
+ const foundKey = key;
102
+ result = result[foundKey];
103
+ } else {
104
+ result = void 0;
105
+ break;
106
+ }
107
+ }
108
+ return result;
109
+ }
110
+ };
111
+
112
+ // src/lib/ForecastLineChartConfigBuilder.ts
113
+ var defaultOptions = () => ({
114
+ plugins: {
115
+ legend: {
116
+ position: "top"
117
+ }
118
+ },
119
+ responsive: true
120
+ });
121
+ var ForecastLineChartConfigBuilder = class _ForecastLineChartConfigBuilder {
122
+ constructor(theme, payload) {
123
+ this.payload = payload;
124
+ this.themeColors = this.parseTheme(theme);
125
+ }
126
+ data = {
127
+ datasets: []
128
+ };
129
+ options = defaultOptions();
130
+ themeColors;
131
+ get forecastPayload() {
132
+ if (this.payload) {
133
+ return this.payload;
134
+ } else {
135
+ throw Error("ForecastPayload was not defined");
136
+ }
137
+ }
138
+ static async create(theme, payload, sourcePayloadConfig) {
139
+ const instance = new _ForecastLineChartConfigBuilder(theme, payload);
140
+ await instance.build(sourcePayloadConfig?.fetch);
141
+ instance.refreshValues();
142
+ return instance;
143
+ }
144
+ async build(includeSources) {
145
+ this.buildOptions();
146
+ await this.buildData(includeSources);
147
+ return this;
148
+ }
149
+ async buildData(includeSources) {
150
+ const forecastData = this.generateDataSetForecastData();
151
+ const datasets = [forecastData];
152
+ if (includeSources) {
153
+ const sourceData = await this.generateDataSetSourcePayloads();
154
+ datasets.unshift(sourceData);
155
+ const lastSourceDataItem = sourceData.data.at(-1);
156
+ forecastData.data.unshift(lastSourceDataItem);
157
+ }
158
+ this.data = {
159
+ datasets
160
+ };
161
+ return this;
162
+ }
163
+ buildOptions() {
164
+ if (this.options.plugins) {
165
+ this.options.plugins.title = this.generateTitle();
166
+ this.options.plugins.legend = this.generateLegend();
167
+ }
168
+ this.options.scales = this.generateScales();
169
+ return this;
170
+ }
171
+ refreshValues() {
172
+ this.data = { ...this.data };
173
+ this.options = { ...this.options };
174
+ }
175
+ generateLegend() {
176
+ return {
177
+ labels: {
178
+ pointStyle: "circle",
179
+ usePointStyle: true
180
+ }
181
+ };
182
+ }
183
+ generateScales() {
184
+ return {
185
+ x: {
186
+ grid: {
187
+ color: this.themeColors?.gridColor
188
+ },
189
+ time: {
190
+ unit: "minute"
191
+ },
192
+ type: "time"
193
+ },
194
+ y: {
195
+ grid: {
196
+ color: this.themeColors?.gridColor
197
+ }
198
+ }
199
+ };
200
+ }
201
+ generateTitle() {
202
+ return {
203
+ display: true,
204
+ text: `Gas Price Forecaster (GWEI over time from ${this.forecastPayload?.values[0].timestamp ? new Date(this.forecastPayload.values[0].timestamp).toLocaleDateString() : ""})`
205
+ };
206
+ }
207
+ parseTheme(theme) {
208
+ const dark = theme.palette.mode === "dark";
209
+ return {
210
+ dataSetColorPrimary: theme.palette.primary.light,
211
+ dataSetColorSecondary: theme.palette.secondary.light,
212
+ gridColor: dark ? theme.palette.grey[800] : theme.palette.grey[300]
213
+ };
214
+ }
215
+ generateDataSetForecastData() {
216
+ return {
217
+ borderDash: [5],
218
+ borderDashOffset: 0.5,
219
+ data: this.forecastPayload.values.map((price) => ({ x: price.timestamp ?? 0, y: price.value })),
220
+ label: "Forecast Price",
221
+ ...DataPointStyles(this.themeColors?.dataSetColorPrimary),
222
+ ...DataLineStyles(this.themeColors?.dataSetColorPrimary)
223
+ };
224
+ }
225
+ async generateDataSetSourcePayloads() {
226
+ const { sourcePrices } = await SourcePayloads.build("feePerGas.medium");
227
+ return {
228
+ data: sourcePrices,
229
+ label: "Source Prices",
230
+ ...DataLineStyles(this.themeColors?.dataSetColorSecondary),
231
+ ...DataPointStyles(this.themeColors?.dataSetColorSecondary)
232
+ };
233
+ }
234
+ };
235
+
236
+ // src/components/DetailsBox.tsx
237
+ var import_jsx_runtime = require("react/jsx-runtime");
238
+ import_chart.Chart.register(import_chart.CategoryScale, import_chart.TimeScale, import_chart.PointElement, import_chart.LineElement, import_chart.LinearScale, import_chart.Title, import_chart.Tooltip, import_chart.Legend);
239
+ var PriceForecastDetailsBox = ({ payload, ...props }) => {
240
+ const priceForecastPayload = payload;
241
+ const theme = (0, import_material2.useTheme)();
242
+ const [data, setData] = (0, import_react.useState)({ datasets: [] });
243
+ const [options, setOptions] = (0, import_react.useState)({});
244
+ (0, import_react_async_effect.useAsyncEffect)(
245
+ // eslint-disable-next-line react-hooks/exhaustive-deps
246
+ async (mounted) => {
247
+ const { data: data2, options: options2 } = await ForecastLineChartConfigBuilder.create(theme, priceForecastPayload, { fetch: true });
248
+ if (mounted()) {
249
+ setData(data2);
250
+ setOptions(options2);
251
+ }
252
+ },
253
+ [priceForecastPayload, theme]
254
+ );
255
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_flexbox.FlexCol, { ...props, busy: priceForecastPayload === void 0, minHeight: "25vh", children: priceForecastPayload ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_chartjs_2.Line, { options, data }) : null });
256
+ };
257
+
258
+ // src/Plugin.ts
259
+ var import_react_payload_plugin = require("@xyo-network/react-payload-plugin");
260
+ var PriceForecastRenderPlugin = {
261
+ ...(0, import_react_payload_plugin.createPayloadRenderPlugin)({
262
+ canRender: () => true,
263
+ components: {
264
+ box: {
265
+ detailsBox: PriceForecastDetailsBox
266
+ }
267
+ },
268
+ name: "PriceForecast"
269
+ })
270
+ };
271
+ // Annotate the CommonJS export names for ESM import in node:
272
+ 0 && (module.exports = {
273
+ PriceForecastDetailsBox,
274
+ PriceForecastRenderPlugin
275
+ });
276
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/components/DetailsBox.tsx","../src/lib/DataLineStyles.ts","../src/lib/DataPointStyles.ts","../src/lib/MockSourcePayloads.ts","../src/lib/SourcePayloads.ts","../src/lib/ForecastLineChartConfigBuilder.ts","../src/Plugin.ts"],"sourcesContent":["export * from './components'\nexport * from './Plugin'\n","import 'chartjs-adapter-luxon'\n\nimport { useTheme } from '@mui/material'\nimport { useAsyncEffect } from '@xylabs/react-async-effect'\nimport { FlexBoxProps, FlexCol } from '@xylabs/react-flexbox'\nimport { ForecastPayload } from '@xyo-network/diviner-forecasting-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport {\n CategoryScale,\n Chart as ChartJS,\n ChartData,\n ChartOptions,\n Legend,\n LinearScale,\n LineElement,\n PointElement,\n TimeScale,\n Title,\n Tooltip,\n} from 'chart.js'\nimport { useState } from 'react'\nimport { Line } from 'react-chartjs-2'\n\nimport { ForecastLineChartConfigBuilder } from '../lib'\n\nChartJS.register(CategoryScale, TimeScale, PointElement, LineElement, LinearScale, Title, Tooltip, Legend)\n\nexport interface PriceForecastDetailsBoxProps extends FlexBoxProps {\n payload?: Payload\n}\n\nexport const PriceForecastDetailsBox: React.FC<PriceForecastDetailsBoxProps> = ({ payload, ...props }) => {\n const priceForecastPayload = payload as ForecastPayload | undefined\n const theme = useTheme()\n const [data, setData] = useState<ChartData<'line'>>({ datasets: [] })\n const [options, setOptions] = useState<ChartOptions<'line'>>({})\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n const { data, options } = await ForecastLineChartConfigBuilder.create(theme, priceForecastPayload, { fetch: true })\n if (mounted()) {\n setData(data)\n setOptions(options)\n }\n },\n [priceForecastPayload, theme],\n )\n\n return (\n <FlexCol {...props} busy={priceForecastPayload === undefined} minHeight=\"25vh\">\n {priceForecastPayload ? <Line options={options} data={data} /> : null}\n </FlexCol>\n )\n}\n","import { alpha } from '@mui/material'\n\nexport const DataLineStyles = (color?: string) => ({\n backgroundColor: color ? alpha(color, 0.5) : undefined,\n borderColor: color,\n})\n","export const DataPointStyles = (pointHoverBackgroundColor?: string) => ({\n pointHitRadius: 20,\n pointHoverBackgroundColor,\n pointHoverRadius: 10,\n pointRadius: 5,\n pointStyle: 'circle',\n})\n","export const MockSourcePayloads = () => {\n const tenMin = 600000\n return [\n {\n baseFee: 38.90155387825,\n feePerGas: { high: 47.9945864396, low: 39.006868093, medium: 39.306868093, veryHigh: 44.45384380525 },\n priorityFeePerGas: { high: 1.0266666666666666, low: -0.41000000000000003, medium: 0.38, veryHigh: 1.3900000000000001 },\n schema: 'network.xyo.blockchain.ethereum.gas',\n timestamp: Date.now() - tenMin,\n },\n {\n baseFee: 38.90155387825,\n feePerGas: { high: 47.9945864396, low: 39.006868093, medium: 100, veryHigh: 44.45384380525 },\n priorityFeePerGas: { high: 1.0266666666666666, low: -0.41000000000000003, medium: 0.38, veryHigh: 1.3900000000000001 },\n schema: 'network.xyo.blockchain.ethereum.gas',\n timestamp: Date.now(),\n },\n ]\n}\n","import { Payload } from '@xyo-network/payload-model'\nimport { Point } from 'chart.js'\n\nimport { MockSourcePayloads } from './MockSourcePayloads'\n\nexport class SourcePayloads {\n sourcePrices: Point[] = []\n\n constructor(public sourcePayloads: Payload[]) {}\n\n get payloads() {\n return this.sourcePayloads\n }\n\n static async build(jsonPath: string) {\n const sourcePayloads = await this.fetchSourcePayloads()\n const instance = new this(sourcePayloads)\n\n const paths = jsonPath.split('.')\n instance.sourcePrices = sourcePayloads.map((payload) => {\n return { x: payload.timestamp, y: instance.jsonPathTraverser(payload, paths) }\n })\n return instance\n }\n\n // TODO - fetch from archivist\n static async fetchSourcePayloads() {\n const payloads = await Promise.resolve(MockSourcePayloads())\n return payloads\n }\n\n jsonPathTraverser(obj: Payload, path: string[]) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let result: any = obj\n for (const key of path) {\n if (key in result) {\n const foundKey = key as keyof typeof result\n result = result[foundKey]\n } else {\n result = undefined\n break\n }\n }\n\n return result\n }\n}\n","import { Theme } from '@mui/material'\nimport { ForecastPayload } from '@xyo-network/diviner-forecasting-model'\nimport { ChartData, ChartDataset, ChartOptions, LegendOptions, Point, ScaleChartOptions } from 'chart.js'\n// eslint-disable-next-line import/no-unresolved\nimport { _DeepPartialObject } from 'chart.js/dist/types/utils'\n\nimport { DataLineStyles } from './DataLineStyles'\nimport { DataPointStyles } from './DataPointStyles'\nimport { SourcePayloads } from './SourcePayloads'\n\ninterface SourcePayloadConfig {\n fetch: boolean\n sampleSize?: number\n}\n\ninterface ThemeColors {\n dataSetColorPrimary: string\n dataSetColorSecondary: string\n gridColor: string\n}\n\nconst defaultOptions: () => ChartOptions<'line'> = () => ({\n plugins: {\n legend: {\n position: 'top' as const,\n },\n },\n responsive: true,\n})\n\nexport class ForecastLineChartConfigBuilder {\n data: ChartData<'line'> = {\n datasets: [],\n }\n options: ChartOptions<'line'> = defaultOptions()\n themeColors: ThemeColors | undefined\n\n constructor(\n theme: Theme,\n private payload?: ForecastPayload,\n ) {\n this.themeColors = this.parseTheme(theme)\n }\n\n get forecastPayload() {\n if (this.payload) {\n return this.payload\n } else {\n throw Error('ForecastPayload was not defined')\n }\n }\n\n static async create(theme: Theme, payload?: ForecastPayload, sourcePayloadConfig?: SourcePayloadConfig) {\n const instance = new ForecastLineChartConfigBuilder(theme, payload)\n\n await instance.build(sourcePayloadConfig?.fetch)\n\n instance.refreshValues()\n\n return instance\n }\n\n async build(includeSources?: boolean) {\n this.buildOptions()\n await this.buildData(includeSources)\n return this\n }\n\n async buildData(includeSources?: boolean) {\n const forecastData = this.generateDataSetForecastData()\n\n const datasets: ChartDataset<'line'>[] = [forecastData]\n\n if (includeSources) {\n // build data from sources in forecastPayload\n const sourceData = await this.generateDataSetSourcePayloads()\n datasets.unshift(sourceData)\n\n // add last source point as first item in prediction to connect the lines\n const lastSourceDataItem = sourceData.data.at(-1) as Point\n forecastData.data.unshift(lastSourceDataItem)\n }\n\n this.data = {\n datasets,\n }\n\n return this\n }\n\n buildOptions() {\n if (this.options.plugins) {\n this.options.plugins.title = this.generateTitle()\n this.options.plugins.legend = this.generateLegend()\n }\n this.options.scales = this.generateScales()\n\n return this\n }\n\n refreshValues() {\n this.data = { ...this.data }\n this.options = { ...this.options }\n }\n\n protected generateLegend(): _DeepPartialObject<LegendOptions<'line'>> {\n return {\n labels: {\n pointStyle: 'circle',\n usePointStyle: true,\n },\n }\n }\n\n protected generateScales(): _DeepPartialObject<ScaleChartOptions<'line'>['scales']> {\n return {\n x: {\n grid: {\n color: this.themeColors?.gridColor,\n },\n time: {\n unit: 'minute',\n },\n type: 'time',\n },\n y: {\n grid: {\n color: this.themeColors?.gridColor,\n },\n },\n }\n }\n\n protected generateTitle() {\n return {\n display: true,\n text: `Gas Price Forecaster (GWEI over time from ${\n this.forecastPayload?.values[0].timestamp ? new Date(this.forecastPayload.values[0].timestamp).toLocaleDateString() : ''\n })`,\n }\n }\n\n protected parseTheme(theme: Theme) {\n const dark = theme.palette.mode === 'dark'\n return {\n dataSetColorPrimary: theme.palette.primary.light,\n dataSetColorSecondary: theme.palette.secondary.light,\n gridColor: dark ? theme.palette.grey[800] : theme.palette.grey[300],\n }\n }\n\n private generateDataSetForecastData(): ChartDataset<'line'> {\n return {\n borderDash: [5],\n borderDashOffset: 0.5,\n data: this.forecastPayload.values.map((price) => ({ x: price.timestamp ?? 0, y: price.value })),\n label: 'Forecast Price',\n ...DataPointStyles(this.themeColors?.dataSetColorPrimary),\n ...DataLineStyles(this.themeColors?.dataSetColorPrimary),\n }\n }\n\n private async generateDataSetSourcePayloads(): Promise<ChartDataset<'line'>> {\n const { sourcePrices } = await SourcePayloads.build('feePerGas.medium')\n return {\n data: sourcePrices,\n label: 'Source Prices',\n ...DataLineStyles(this.themeColors?.dataSetColorSecondary),\n ...DataPointStyles(this.themeColors?.dataSetColorSecondary),\n }\n }\n}\n","import { createPayloadRenderPlugin, PayloadRenderPlugin } from '@xyo-network/react-payload-plugin'\n\nimport { PriceForecastDetailsBox } from './components'\n\nexport const PriceForecastRenderPlugin: PayloadRenderPlugin = {\n ...createPayloadRenderPlugin({\n canRender: () => true,\n components: {\n box: {\n detailsBox: PriceForecastDetailsBox,\n },\n },\n name: 'PriceForecast',\n }),\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mCAAO;AAEP,IAAAA,mBAAyB;AACzB,gCAA+B;AAC/B,2BAAsC;AAGtC,mBAYO;AACP,mBAAyB;AACzB,6BAAqB;;;ACrBrB,sBAAsB;AAEf,IAAM,iBAAiB,CAAC,WAAoB;AAAA,EACjD,iBAAiB,YAAQ,uBAAM,OAAO,GAAG,IAAI;AAAA,EAC7C,aAAa;AACf;;;ACLO,IAAM,kBAAkB,CAAC,+BAAwC;A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AAK;AAAA,EAC9B;AAEA,SACE,4CAAC,gCAAS,GAAG,OAAO,MAAM,yBAAyB,QAAW,WAAU,QACrE,iCAAuB,4CAAC,+BAAK,SAAkB,MAAY,IAAK,MACnE;AAEJ;;;AMtDA,kCAA+D;AAIxD,IAAM,4BAAiD;AAAA,EAC5D,OAAG,uDAA0B;AAAA,IAC3B,WAAW,MAAM;AAAA,IACjB,YAAY;AAAA,MACV,KAAK;AAAA,QACH,YAAY;AAAA,MACd;AAAA,IACF;AAAA,IACA,MAAM;AAAA,EACR,CAAC;AACH;","names":["import_material","ChartJS","data","options"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,258 @@
1
+ // src/components/DetailsBox.tsx
2
+ import "chartjs-adapter-luxon";
3
+ import { useTheme } from "@mui/material";
4
+ import { useAsyncEffect } from "@xylabs/react-async-effect";
5
+ import { FlexCol } from "@xylabs/react-flexbox";
6
+ import {
7
+ CategoryScale,
8
+ Chart as ChartJS,
9
+ Legend,
10
+ LinearScale,
11
+ LineElement,
12
+ PointElement,
13
+ TimeScale,
14
+ Title,
15
+ Tooltip
16
+ } from "chart.js";
17
+ import { useState } from "react";
18
+ import { Line } from "react-chartjs-2";
19
+
20
+ // src/lib/DataLineStyles.ts
21
+ import { alpha } from "@mui/material";
22
+ var DataLineStyles = (color) => ({
23
+ backgroundColor: color ? alpha(color, 0.5) : void 0,
24
+ borderColor: color
25
+ });
26
+
27
+ // src/lib/DataPointStyles.ts
28
+ var DataPointStyles = (pointHoverBackgroundColor) => ({
29
+ pointHitRadius: 20,
30
+ pointHoverBackgroundColor,
31
+ pointHoverRadius: 10,
32
+ pointRadius: 5,
33
+ pointStyle: "circle"
34
+ });
35
+
36
+ // src/lib/MockSourcePayloads.ts
37
+ var MockSourcePayloads = () => {
38
+ const tenMin = 6e5;
39
+ return [
40
+ {
41
+ baseFee: 38.90155387825,
42
+ feePerGas: { high: 47.9945864396, low: 39.006868093, medium: 39.306868093, veryHigh: 44.45384380525 },
43
+ priorityFeePerGas: { high: 1.0266666666666666, low: -0.41000000000000003, medium: 0.38, veryHigh: 1.3900000000000001 },
44
+ schema: "network.xyo.blockchain.ethereum.gas",
45
+ timestamp: Date.now() - tenMin
46
+ },
47
+ {
48
+ baseFee: 38.90155387825,
49
+ feePerGas: { high: 47.9945864396, low: 39.006868093, medium: 100, veryHigh: 44.45384380525 },
50
+ priorityFeePerGas: { high: 1.0266666666666666, low: -0.41000000000000003, medium: 0.38, veryHigh: 1.3900000000000001 },
51
+ schema: "network.xyo.blockchain.ethereum.gas",
52
+ timestamp: Date.now()
53
+ }
54
+ ];
55
+ };
56
+
57
+ // src/lib/SourcePayloads.ts
58
+ var SourcePayloads = class {
59
+ constructor(sourcePayloads) {
60
+ this.sourcePayloads = sourcePayloads;
61
+ }
62
+ sourcePrices = [];
63
+ get payloads() {
64
+ return this.sourcePayloads;
65
+ }
66
+ static async build(jsonPath) {
67
+ const sourcePayloads = await this.fetchSourcePayloads();
68
+ const instance = new this(sourcePayloads);
69
+ const paths = jsonPath.split(".");
70
+ instance.sourcePrices = sourcePayloads.map((payload) => {
71
+ return { x: payload.timestamp, y: instance.jsonPathTraverser(payload, paths) };
72
+ });
73
+ return instance;
74
+ }
75
+ // TODO - fetch from archivist
76
+ static async fetchSourcePayloads() {
77
+ const payloads = await Promise.resolve(MockSourcePayloads());
78
+ return payloads;
79
+ }
80
+ jsonPathTraverser(obj, path) {
81
+ let result = obj;
82
+ for (const key of path) {
83
+ if (key in result) {
84
+ const foundKey = key;
85
+ result = result[foundKey];
86
+ } else {
87
+ result = void 0;
88
+ break;
89
+ }
90
+ }
91
+ return result;
92
+ }
93
+ };
94
+
95
+ // src/lib/ForecastLineChartConfigBuilder.ts
96
+ var defaultOptions = () => ({
97
+ plugins: {
98
+ legend: {
99
+ position: "top"
100
+ }
101
+ },
102
+ responsive: true
103
+ });
104
+ var ForecastLineChartConfigBuilder = class _ForecastLineChartConfigBuilder {
105
+ constructor(theme, payload) {
106
+ this.payload = payload;
107
+ this.themeColors = this.parseTheme(theme);
108
+ }
109
+ data = {
110
+ datasets: []
111
+ };
112
+ options = defaultOptions();
113
+ themeColors;
114
+ get forecastPayload() {
115
+ if (this.payload) {
116
+ return this.payload;
117
+ } else {
118
+ throw Error("ForecastPayload was not defined");
119
+ }
120
+ }
121
+ static async create(theme, payload, sourcePayloadConfig) {
122
+ const instance = new _ForecastLineChartConfigBuilder(theme, payload);
123
+ await instance.build(sourcePayloadConfig?.fetch);
124
+ instance.refreshValues();
125
+ return instance;
126
+ }
127
+ async build(includeSources) {
128
+ this.buildOptions();
129
+ await this.buildData(includeSources);
130
+ return this;
131
+ }
132
+ async buildData(includeSources) {
133
+ const forecastData = this.generateDataSetForecastData();
134
+ const datasets = [forecastData];
135
+ if (includeSources) {
136
+ const sourceData = await this.generateDataSetSourcePayloads();
137
+ datasets.unshift(sourceData);
138
+ const lastSourceDataItem = sourceData.data.at(-1);
139
+ forecastData.data.unshift(lastSourceDataItem);
140
+ }
141
+ this.data = {
142
+ datasets
143
+ };
144
+ return this;
145
+ }
146
+ buildOptions() {
147
+ if (this.options.plugins) {
148
+ this.options.plugins.title = this.generateTitle();
149
+ this.options.plugins.legend = this.generateLegend();
150
+ }
151
+ this.options.scales = this.generateScales();
152
+ return this;
153
+ }
154
+ refreshValues() {
155
+ this.data = { ...this.data };
156
+ this.options = { ...this.options };
157
+ }
158
+ generateLegend() {
159
+ return {
160
+ labels: {
161
+ pointStyle: "circle",
162
+ usePointStyle: true
163
+ }
164
+ };
165
+ }
166
+ generateScales() {
167
+ return {
168
+ x: {
169
+ grid: {
170
+ color: this.themeColors?.gridColor
171
+ },
172
+ time: {
173
+ unit: "minute"
174
+ },
175
+ type: "time"
176
+ },
177
+ y: {
178
+ grid: {
179
+ color: this.themeColors?.gridColor
180
+ }
181
+ }
182
+ };
183
+ }
184
+ generateTitle() {
185
+ return {
186
+ display: true,
187
+ text: `Gas Price Forecaster (GWEI over time from ${this.forecastPayload?.values[0].timestamp ? new Date(this.forecastPayload.values[0].timestamp).toLocaleDateString() : ""})`
188
+ };
189
+ }
190
+ parseTheme(theme) {
191
+ const dark = theme.palette.mode === "dark";
192
+ return {
193
+ dataSetColorPrimary: theme.palette.primary.light,
194
+ dataSetColorSecondary: theme.palette.secondary.light,
195
+ gridColor: dark ? theme.palette.grey[800] : theme.palette.grey[300]
196
+ };
197
+ }
198
+ generateDataSetForecastData() {
199
+ return {
200
+ borderDash: [5],
201
+ borderDashOffset: 0.5,
202
+ data: this.forecastPayload.values.map((price) => ({ x: price.timestamp ?? 0, y: price.value })),
203
+ label: "Forecast Price",
204
+ ...DataPointStyles(this.themeColors?.dataSetColorPrimary),
205
+ ...DataLineStyles(this.themeColors?.dataSetColorPrimary)
206
+ };
207
+ }
208
+ async generateDataSetSourcePayloads() {
209
+ const { sourcePrices } = await SourcePayloads.build("feePerGas.medium");
210
+ return {
211
+ data: sourcePrices,
212
+ label: "Source Prices",
213
+ ...DataLineStyles(this.themeColors?.dataSetColorSecondary),
214
+ ...DataPointStyles(this.themeColors?.dataSetColorSecondary)
215
+ };
216
+ }
217
+ };
218
+
219
+ // src/components/DetailsBox.tsx
220
+ import { jsx } from "react/jsx-runtime";
221
+ ChartJS.register(CategoryScale, TimeScale, PointElement, LineElement, LinearScale, Title, Tooltip, Legend);
222
+ var PriceForecastDetailsBox = ({ payload, ...props }) => {
223
+ const priceForecastPayload = payload;
224
+ const theme = useTheme();
225
+ const [data, setData] = useState({ datasets: [] });
226
+ const [options, setOptions] = useState({});
227
+ useAsyncEffect(
228
+ // eslint-disable-next-line react-hooks/exhaustive-deps
229
+ async (mounted) => {
230
+ const { data: data2, options: options2 } = await ForecastLineChartConfigBuilder.create(theme, priceForecastPayload, { fetch: true });
231
+ if (mounted()) {
232
+ setData(data2);
233
+ setOptions(options2);
234
+ }
235
+ },
236
+ [priceForecastPayload, theme]
237
+ );
238
+ return /* @__PURE__ */ jsx(FlexCol, { ...props, busy: priceForecastPayload === void 0, minHeight: "25vh", children: priceForecastPayload ? /* @__PURE__ */ jsx(Line, { options, data }) : null });
239
+ };
240
+
241
+ // src/Plugin.ts
242
+ import { createPayloadRenderPlugin } from "@xyo-network/react-payload-plugin";
243
+ var PriceForecastRenderPlugin = {
244
+ ...createPayloadRenderPlugin({
245
+ canRender: () => true,
246
+ components: {
247
+ box: {
248
+ detailsBox: PriceForecastDetailsBox
249
+ }
250
+ },
251
+ name: "PriceForecast"
252
+ })
253
+ };
254
+ export {
255
+ PriceForecastDetailsBox,
256
+ PriceForecastRenderPlugin
257
+ };
258
+ //# sourceMappingURL=index.mjs.map