@paxoslabs/amplify-sdk 0.1.1 → 0.2.0-alpha.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 (53) hide show
  1. package/CHANGELOG.md +0 -2
  2. package/README.md +42 -0
  3. package/dist/{chunk-DTFLXAZJ.js → chunk-4NQCOMCZ.js} +147 -5
  4. package/dist/chunk-4NQCOMCZ.js.map +1 -0
  5. package/dist/{chunk-KYR6BFAE.js → chunk-5VOI4WPL.js} +38 -33
  6. package/dist/chunk-5VOI4WPL.js.map +1 -0
  7. package/dist/{chunk-ZILA73XN.mjs → chunk-5Y7M43XN.mjs} +144 -4
  8. package/dist/chunk-5Y7M43XN.mjs.map +1 -0
  9. package/dist/{chunk-VIULRHK6.mjs → chunk-7PCUTZF6.mjs} +3 -3
  10. package/dist/{chunk-VIULRHK6.mjs.map → chunk-7PCUTZF6.mjs.map} +1 -1
  11. package/dist/{chunk-XXHRCCZS.mjs → chunk-AQFFMJX7.mjs} +3 -3
  12. package/dist/chunk-AQFFMJX7.mjs.map +1 -0
  13. package/dist/{chunk-REKEQLQA.mjs → chunk-BOW2QPQC.mjs} +3 -3
  14. package/dist/{chunk-REKEQLQA.mjs.map → chunk-BOW2QPQC.mjs.map} +1 -1
  15. package/dist/{chunk-HI44AMLC.mjs → chunk-E7R7TL7J.mjs} +24 -19
  16. package/dist/chunk-E7R7TL7J.mjs.map +1 -0
  17. package/dist/{chunk-CUGK4ZBJ.mjs → chunk-IMS2HBJ7.mjs} +305 -30
  18. package/dist/chunk-IMS2HBJ7.mjs.map +1 -0
  19. package/dist/{chunk-LSNRG5Z2.js → chunk-KB4EPCK2.js} +4 -4
  20. package/dist/{chunk-LSNRG5Z2.js.map → chunk-KB4EPCK2.js.map} +1 -1
  21. package/dist/{chunk-ITB7FXG4.js → chunk-OJOKPBK7.js} +3 -3
  22. package/dist/chunk-OJOKPBK7.js.map +1 -0
  23. package/dist/{chunk-B2QCI6ET.js → chunk-UMRTU75Q.js} +309 -30
  24. package/dist/chunk-UMRTU75Q.js.map +1 -0
  25. package/dist/{chunk-GS7TDQXA.js → chunk-XUS6KI3G.js} +8 -8
  26. package/dist/{chunk-GS7TDQXA.js.map → chunk-XUS6KI3G.js.map} +1 -1
  27. package/dist/core.js +16 -16
  28. package/dist/core.js.map +1 -1
  29. package/dist/core.mjs +6 -6
  30. package/dist/core.mjs.map +1 -1
  31. package/dist/display.d.mts +1 -1
  32. package/dist/display.d.ts +1 -1
  33. package/dist/display.js +12 -12
  34. package/dist/display.mjs +4 -4
  35. package/dist/{exchange-rate-BfPH_fQt.d.mts → exchange-rate-DCB2CY9v.d.mts} +5 -0
  36. package/dist/{exchange-rate-Cp1ddpw4.d.ts → exchange-rate-DOmoqut9.d.ts} +5 -0
  37. package/dist/index.d.mts +455 -8
  38. package/dist/index.d.ts +455 -8
  39. package/dist/index.js +402 -215
  40. package/dist/index.js.map +1 -1
  41. package/dist/index.mjs +292 -123
  42. package/dist/index.mjs.map +1 -1
  43. package/dist/utils.js +5 -5
  44. package/dist/utils.mjs +2 -2
  45. package/package.json +2 -1
  46. package/dist/chunk-B2QCI6ET.js.map +0 -1
  47. package/dist/chunk-CUGK4ZBJ.mjs.map +0 -1
  48. package/dist/chunk-DTFLXAZJ.js.map +0 -1
  49. package/dist/chunk-HI44AMLC.mjs.map +0 -1
  50. package/dist/chunk-ITB7FXG4.js.map +0 -1
  51. package/dist/chunk-KYR6BFAE.js.map +0 -1
  52. package/dist/chunk-XXHRCCZS.mjs.map +0 -1
  53. package/dist/chunk-ZILA73XN.mjs.map +0 -1
@@ -1,29 +1,92 @@
1
1
  import { defineChain } from 'viem';
2
2
  import { swellchain, sei, plumeMainnet, form, boba, mainnet } from 'viem/chains';
3
3
 
4
- // src/types/amplify-sdk-api.ts
5
- var APIError = class _APIError extends Error {
6
- constructor(message, options) {
7
- super(message);
8
- this.name = "APIError";
9
- this.statusCode = options?.statusCode;
10
- this.endpoint = options?.endpoint;
11
- this.cause = options?.cause;
12
- if (Error.captureStackTrace) {
13
- Error.captureStackTrace(this, _APIError);
4
+ // src/types/telemetry.ts
5
+ var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
6
+ LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG";
7
+ LogLevel2[LogLevel2["INFO"] = 1] = "INFO";
8
+ LogLevel2[LogLevel2["WARN"] = 2] = "WARN";
9
+ LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR";
10
+ LogLevel2[LogLevel2["NONE"] = 4] = "NONE";
11
+ return LogLevel2;
12
+ })(LogLevel || {});
13
+
14
+ // src/lib/logger.ts
15
+ var ConsoleLogger = class {
16
+ constructor(level = 3 /* ERROR */) {
17
+ this.level = level;
18
+ }
19
+ /**
20
+ * Update log level
21
+ */
22
+ setLevel(level) {
23
+ this.level = level;
24
+ }
25
+ /**
26
+ * Get current log level
27
+ */
28
+ getLevel() {
29
+ return this.level;
30
+ }
31
+ debug(message, context) {
32
+ if (this.level <= 0 /* DEBUG */) {
33
+ if (context) {
34
+ console.debug("[Amplify SDK]", message, context);
35
+ } else {
36
+ console.debug("[Amplify SDK]", message);
37
+ }
38
+ }
39
+ }
40
+ info(message, context) {
41
+ if (this.level <= 1 /* INFO */) {
42
+ if (context) {
43
+ console.info("[Amplify SDK]", message, context);
44
+ } else {
45
+ console.info("[Amplify SDK]", message);
46
+ }
47
+ }
48
+ }
49
+ warn(message, context) {
50
+ if (this.level <= 2 /* WARN */) {
51
+ if (context) {
52
+ console.warn("[Amplify SDK]", message, context);
53
+ } else {
54
+ console.warn("[Amplify SDK]", message);
55
+ }
56
+ }
57
+ }
58
+ error(message, context) {
59
+ if (this.level <= 3 /* ERROR */) {
60
+ if (context) {
61
+ console.error("[Amplify SDK]", message, context);
62
+ } else {
63
+ console.error("[Amplify SDK]", message);
64
+ }
14
65
  }
15
66
  }
16
67
  };
17
- function isValidYieldType(value) {
18
- return typeof value === "string" && (value === "PRIME" || value === "TBILL" || value === "LENDING");
68
+ var currentLogger = new ConsoleLogger(3 /* ERROR */);
69
+ var defaultLogger = currentLogger;
70
+ function setLogger(logger) {
71
+ currentLogger = logger;
19
72
  }
20
- function isValidAddress(value) {
21
- return typeof value === "string" && /^0x[a-fA-F0-9]{40}$/.test(value);
73
+ function getLogger() {
74
+ return currentLogger;
22
75
  }
23
- function isValidChainId(value) {
24
- return typeof value === "number" && value > 0 && Number.isInteger(value);
76
+ function setLogLevel(level) {
77
+ if (defaultLogger && typeof defaultLogger.setLevel === "function") {
78
+ defaultLogger.setLevel(level);
79
+ }
25
80
  }
26
81
 
82
+ // src/constants/index.ts
83
+ var ATOMIC_QUEUE_CONTRACT_ADDRESS = "0x228c44bb4885c6633f4b6c83f14622f37d5112e5";
84
+ var DEFAULT_DEADLINE = 3;
85
+ var NATIVE_TOKEN_FOR_BRIDGE_FEE = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
86
+ var CHAINLINK_ADDRESS = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
87
+ var DEFAULT_APPROVAL_AMOUNT = BigInt(2) ** BigInt(256) - BigInt(1);
88
+ var API_BASE_URL = "https://api.paxoslabs.com";
89
+
27
90
  // src/lib/vault-cache.ts
28
91
  var DEFAULT_TTL = 6e5;
29
92
  var VaultCache = class {
@@ -191,7 +254,6 @@ var VaultCache = class {
191
254
  };
192
255
 
193
256
  // src/api/amplify-sdk-client.ts
194
- var API_BASE_URL = "https://api.paxoslabs.com";
195
257
  var DEFAULT_TIMEOUT = 1e4;
196
258
  function createTimeoutSignal(timeoutMs) {
197
259
  const controller = new AbortController();
@@ -617,7 +679,8 @@ var sdkConfig = {
617
679
  isInitialized: false,
618
680
  initializedAt: null,
619
681
  isInitializing: false,
620
- initPromise: null
682
+ initPromise: null,
683
+ telemetryEnabled: true
621
684
  };
622
685
  var ERROR_MESSAGES = {
623
686
  INVALID_API_KEY: "Invalid API key format. Expected format: pxl_<type>_<string>",
@@ -650,6 +713,8 @@ function clearConfig() {
650
713
  sdkConfig.initializedAt = null;
651
714
  sdkConfig.isInitializing = false;
652
715
  sdkConfig.initPromise = null;
716
+ sdkConfig.telemetryEnabled = true;
717
+ resetTelemetry();
653
718
  }
654
719
  function getRequestHeaders() {
655
720
  const apiKey = getAPIKey();
@@ -661,7 +726,7 @@ function getRequestHeaders() {
661
726
  }
662
727
  return headers;
663
728
  }
664
- async function initAmplifySDK(apiKey) {
729
+ async function initAmplifySDK(apiKey, options) {
665
730
  validateAPIKey(apiKey);
666
731
  if (sdkConfig.isInitialized && sdkConfig.apiKey === apiKey) {
667
732
  return;
@@ -674,6 +739,13 @@ async function initAmplifySDK(apiKey) {
674
739
  clearConfig();
675
740
  clearCache();
676
741
  }
742
+ if (options?.logger) {
743
+ setLogger(options.logger);
744
+ }
745
+ if (options?.logLevel !== void 0) {
746
+ setLogLevel(options.logLevel);
747
+ }
748
+ sdkConfig.telemetryEnabled = options?.telemetry !== false;
677
749
  sdkConfig.isInitializing = true;
678
750
  sdkConfig.initPromise = (async () => {
679
751
  try {
@@ -682,6 +754,10 @@ async function initAmplifySDK(apiKey) {
682
754
  await cache.refresh();
683
755
  sdkConfig.isInitialized = true;
684
756
  sdkConfig.initializedAt = /* @__PURE__ */ new Date();
757
+ if (sdkConfig.telemetryEnabled) {
758
+ initTelemetry(apiKey).catch(() => {
759
+ });
760
+ }
685
761
  } catch (error) {
686
762
  clearConfig();
687
763
  clearCache();
@@ -699,6 +775,213 @@ async function initAmplifySDK(apiKey) {
699
775
  })();
700
776
  await sdkConfig.initPromise;
701
777
  }
778
+
779
+ // src/lib/telemetry.ts
780
+ var SDK_VERSION = "0.1.1-beta.1";
781
+ var MAX_ERROR_BUFFER_SIZE = 10;
782
+ var CONFIG_FETCH_TIMEOUT = 5e3;
783
+ var telemetryState = {
784
+ enabled: false,
785
+ isInitialized: false,
786
+ isInitializing: false,
787
+ initPromise: null,
788
+ config: null,
789
+ apiKey: null
790
+ };
791
+ var posthogClient = null;
792
+ var errorBuffer = [];
793
+ var bufferFullWarningLogged = false;
794
+ function createTimeoutSignal2(timeoutMs) {
795
+ const controller = new AbortController();
796
+ setTimeout(() => controller.abort(), timeoutMs);
797
+ return controller.signal;
798
+ }
799
+ function hashApiKey(apiKey) {
800
+ const FNV_OFFSET = BigInt("0xcbf29ce484222325");
801
+ const FNV_PRIME = BigInt("0x100000001b3");
802
+ let hash = FNV_OFFSET;
803
+ for (let i = 0; i < apiKey.length; i++) {
804
+ hash ^= BigInt(apiKey.charCodeAt(i));
805
+ hash = hash * FNV_PRIME & BigInt("0xffffffffffffffff");
806
+ }
807
+ return `sdk_${hash.toString(16).slice(0, 12)}`;
808
+ }
809
+ async function fetchTelemetryConfig(apiKey) {
810
+ const endpoint = "/v1/earn-sdk/config";
811
+ const url = `${API_BASE_URL}${endpoint}`;
812
+ try {
813
+ const headers = getRequestHeaders();
814
+ if (!headers["x-api-key"]) {
815
+ headers["x-api-key"] = apiKey;
816
+ }
817
+ const response = await fetch(url, {
818
+ method: "GET",
819
+ headers,
820
+ signal: createTimeoutSignal2(CONFIG_FETCH_TIMEOUT)
821
+ });
822
+ if (!response.ok) {
823
+ return null;
824
+ }
825
+ const data = await response.json();
826
+ if (!data || typeof data.posthogApiKey !== "string") {
827
+ return null;
828
+ }
829
+ return {
830
+ posthogApiKey: data.posthogApiKey,
831
+ posthogHost: data.posthogHost || "https://us.i.posthog.com"
832
+ };
833
+ } catch {
834
+ return null;
835
+ }
836
+ }
837
+ async function flushErrorBuffer() {
838
+ if (errorBuffer.length === 0 || !posthogClient) {
839
+ return;
840
+ }
841
+ for (const bufferedError of errorBuffer) {
842
+ const payload = {
843
+ error_type: bufferedError.name,
844
+ error_message: bufferedError.message,
845
+ error_code: bufferedError.code,
846
+ sdk_version: SDK_VERSION,
847
+ buffered: true,
848
+ ...bufferedError.context
849
+ };
850
+ posthogClient.capture("sdk_error", payload);
851
+ }
852
+ errorBuffer.length = 0;
853
+ }
854
+ async function initTelemetry(apiKey) {
855
+ if (telemetryState.isInitialized) {
856
+ return;
857
+ }
858
+ if (telemetryState.isInitializing && telemetryState.initPromise) {
859
+ await telemetryState.initPromise;
860
+ return;
861
+ }
862
+ telemetryState.isInitializing = true;
863
+ telemetryState.apiKey = apiKey;
864
+ telemetryState.initPromise = (async () => {
865
+ try {
866
+ const config = await fetchTelemetryConfig(apiKey);
867
+ if (!config) {
868
+ telemetryState.enabled = false;
869
+ telemetryState.isInitialized = true;
870
+ return;
871
+ }
872
+ telemetryState.config = config;
873
+ const posthogModule = await import('posthog-js');
874
+ const posthog = posthogModule.default;
875
+ posthog.init(config.posthogApiKey, {
876
+ api_host: config.posthogHost,
877
+ autocapture: false,
878
+ capture_pageview: false,
879
+ capture_pageleave: false,
880
+ disable_session_recording: true,
881
+ persistence: "memory",
882
+ // Don't persist to localStorage
883
+ loaded: (ph) => {
884
+ ph.identify(hashApiKey(apiKey));
885
+ }
886
+ });
887
+ posthogClient = posthog;
888
+ telemetryState.enabled = true;
889
+ telemetryState.isInitialized = true;
890
+ await flushErrorBuffer();
891
+ posthog.capture("sdk_initialized", {
892
+ sdk_version: SDK_VERSION
893
+ });
894
+ } catch (error) {
895
+ const logger = getLogger();
896
+ logger.warn("Telemetry initialization failed", {
897
+ error: error instanceof Error ? error.message : String(error)
898
+ });
899
+ telemetryState.enabled = false;
900
+ telemetryState.isInitialized = true;
901
+ } finally {
902
+ telemetryState.isInitializing = false;
903
+ telemetryState.initPromise = null;
904
+ }
905
+ })();
906
+ await telemetryState.initPromise;
907
+ }
908
+ function trackError(error, context) {
909
+ const logger = getLogger();
910
+ logger.error(error.message, {
911
+ name: error.name,
912
+ stack: error.stack,
913
+ ...context
914
+ });
915
+ const errorDetails = {
916
+ name: error.name,
917
+ message: error.message,
918
+ code: error.code,
919
+ context: context || {},
920
+ timestamp: Date.now()
921
+ };
922
+ if (!posthogClient || !telemetryState.enabled) {
923
+ if (errorBuffer.length < MAX_ERROR_BUFFER_SIZE) {
924
+ errorBuffer.push(errorDetails);
925
+ } else if (!bufferFullWarningLogged) {
926
+ logger.warn(
927
+ `Telemetry error buffer full (${MAX_ERROR_BUFFER_SIZE} errors). Additional errors will not be tracked until telemetry initializes.`
928
+ );
929
+ bufferFullWarningLogged = true;
930
+ }
931
+ return;
932
+ }
933
+ const payload = {
934
+ error_type: error.name,
935
+ error_message: error.message,
936
+ error_code: error.code,
937
+ endpoint: error.endpoint,
938
+ status_code: error.statusCode,
939
+ sdk_version: SDK_VERSION,
940
+ ...context
941
+ };
942
+ posthogClient.capture("sdk_error", payload);
943
+ }
944
+ function resetTelemetry() {
945
+ telemetryState.enabled = false;
946
+ telemetryState.isInitialized = false;
947
+ telemetryState.isInitializing = false;
948
+ telemetryState.initPromise = null;
949
+ telemetryState.config = null;
950
+ telemetryState.apiKey = null;
951
+ posthogClient = null;
952
+ errorBuffer.length = 0;
953
+ bufferFullWarningLogged = false;
954
+ }
955
+
956
+ // src/types/amplify-sdk-api.ts
957
+ var APIError = class _APIError extends Error {
958
+ constructor(message, options) {
959
+ super(message);
960
+ this.name = "APIError";
961
+ this.statusCode = options?.statusCode;
962
+ this.endpoint = options?.endpoint;
963
+ this.cause = options?.cause;
964
+ if (Error.captureStackTrace) {
965
+ Error.captureStackTrace(this, _APIError);
966
+ }
967
+ try {
968
+ trackError(this, {
969
+ endpoint: this.endpoint,
970
+ statusCode: this.statusCode
971
+ });
972
+ } catch {
973
+ }
974
+ }
975
+ };
976
+ function isValidYieldType(value) {
977
+ return typeof value === "string" && (value === "PRIME" || value === "TBILL" || value === "LENDING");
978
+ }
979
+ function isValidAddress(value) {
980
+ return typeof value === "string" && /^0x[a-fA-F0-9]{40}$/.test(value);
981
+ }
982
+ function isValidChainId(value) {
983
+ return typeof value === "number" && value > 0 && Number.isInteger(value);
984
+ }
702
985
  var rari = defineChain({
703
986
  id: 1380012617,
704
987
  name: "Rari Chain",
@@ -801,14 +1084,6 @@ function clearChainsCache() {
801
1084
  chainsCache = null;
802
1085
  }
803
1086
 
804
- // src/constants/index.ts
805
- var ATOMIC_QUEUE_CONTRACT_ADDRESS = "0x228c44bb4885c6633f4b6c83f14622f37d5112e5";
806
- var DEFAULT_DEADLINE = 3;
807
- var NATIVE_TOKEN_FOR_BRIDGE_FEE = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
808
- var CHAINLINK_ADDRESS = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
809
- var DEFAULT_APPROVAL_AMOUNT = BigInt(2) ** BigInt(256) - BigInt(1);
810
- var NUCLEUS_BASE_URL = "https://backend.nucleusearn.io/v1/protocol";
811
-
812
- export { APIError, ATOMIC_QUEUE_CONTRACT_ADDRESS, CHAINLINK_ADDRESS, DEFAULT_APPROVAL_AMOUNT, DEFAULT_DEADLINE, NATIVE_TOKEN_FOR_BRIDGE_FEE, NUCLEUS_BASE_URL, clearChainsCache, fetchSupportedAssets, fetchVaults, findVaultByConfig, getAssetsFromCache, getCache, getChainFromConfig, getWithdrawSupportedAssets, initAmplifySDK, initializeCache, refreshVaultCache };
813
- //# sourceMappingURL=chunk-CUGK4ZBJ.mjs.map
814
- //# sourceMappingURL=chunk-CUGK4ZBJ.mjs.map
1087
+ export { APIError, API_BASE_URL, ATOMIC_QUEUE_CONTRACT_ADDRESS, CHAINLINK_ADDRESS, DEFAULT_APPROVAL_AMOUNT, DEFAULT_DEADLINE, LogLevel, NATIVE_TOKEN_FOR_BRIDGE_FEE, clearChainsCache, fetchSupportedAssets, fetchVaults, findVaultByConfig, getAssetsFromCache, getCache, getChainFromConfig, getLogger, getWithdrawSupportedAssets, initAmplifySDK, initializeCache, refreshVaultCache, setLogLevel, setLogger };
1088
+ //# sourceMappingURL=chunk-IMS2HBJ7.mjs.map
1089
+ //# sourceMappingURL=chunk-IMS2HBJ7.mjs.map