@fastnear/api 0.9.3 → 0.9.5

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/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # near api
2
+
3
+ ## general
4
+
5
+ This is a workspace package from the [@fastnear/js-monorepo](https://github.com/fastnear/js-monorepo) that has the primary responsibility. It's what creates the global `near` object.
6
+
7
+ ## technical
8
+
9
+ ### Node.js decoupling
10
+
11
+ This library surgically removed ties to Node.js, replacing them with CommonJS and/or modern APIs available in browsers.
12
+
13
+ For instance `Buffer.from()` is an example of a Node.js feature that is commonly used in libraries doing binary encoding, cryptographic operations, and so on. There exists alternative with `Uint8Array` and `TextEncoder` to fill in pieces. This subject could be quite lengthy, and I mention a couple examples just to set the scene.
14
+
15
+ So it *is* possible to have a web3 library that's decoupled from Node.js
16
+
17
+ ### what this means
18
+
19
+ Some emergent behavior comes as a result of this.
20
+
21
+ - ability to run code in browser's dev console
22
+ - ability to create web3 projects entirely with static html
23
+
24
+ ### `near` global
25
+
26
+ In `tsup.config.ts`, you find TypeScript compilations targets. We feel preferential towards the IIFE version. ([MDN docs on IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE)) That file utilizes `esbuild`'s `banner` and `footer` to inject JavaScript that utilizes `Object.defineProperty` in a way to make it "not [configurable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#configurable)."
27
+
28
+ If you look in the `dist` directory under `umd` (Universal Module Definition, but it seems IIFE fits better as a term) there is one file. At the bottom of the file you'll see how the global `near` object can undergo some modifications, potentially hardening it further as this library develops.
29
+
30
+ ## alpha version
31
+
32
+ The focus thus far has been of a highly technical nature, and after releasing this alpha version the devs will let their minds gestate. then this file will fill out with more meaningful info and snippets. 🙏🏼
33
+
34
+ Make sure to visit the [project-level README](https://github.com/fastnear/js-monorepo#global-near-js)
@@ -1,5 +1,5 @@
1
- /* ⋈ 🏃🏻💨 FastNEAR API - CJS (@fastnear/api version 0.9.3) */
2
- /* https://www.npmjs.com/package/@fastnear/api/v/0.9.3 */
1
+ /* ⋈ 🏃🏻💨 FastNEAR API - CJS (@fastnear/api version 0.9.5) */
2
+ /* https://www.npmjs.com/package/@fastnear/api/v/0.9.5 */
3
3
  "use strict";
4
4
  var __defProp = Object.defineProperty;
5
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
package/dist/cjs/near.cjs CHANGED
@@ -1,5 +1,5 @@
1
- /* ⋈ 🏃🏻💨 FastNEAR API - CJS (@fastnear/api version 0.9.3) */
2
- /* https://www.npmjs.com/package/@fastnear/api/v/0.9.3 */
1
+ /* ⋈ 🏃🏻💨 FastNEAR API - CJS (@fastnear/api version 0.9.5) */
2
+ /* https://www.npmjs.com/package/@fastnear/api/v/0.9.5 */
3
3
  "use strict";
4
4
  var __create = Object.create;
5
5
  var __defProp = Object.defineProperty;
@@ -1,5 +1,5 @@
1
- /* ⋈ 🏃🏻💨 FastNEAR API - CJS (@fastnear/api version 0.9.3) */
2
- /* https://www.npmjs.com/package/@fastnear/api/v/0.9.3 */
1
+ /* ⋈ 🏃🏻💨 FastNEAR API - CJS (@fastnear/api version 0.9.5) */
2
+ /* https://www.npmjs.com/package/@fastnear/api/v/0.9.5 */
3
3
  "use strict";
4
4
  var __defProp = Object.defineProperty;
5
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -72,7 +72,7 @@ const getWalletAdapterState = /* @__PURE__ */ __name(() => {
72
72
  publicKey: _state.publicKey,
73
73
  accountId: _state.accountId,
74
74
  lastWalletId: _state.lastWalletId,
75
- networkId: DEFAULT_NETWORK_ID
75
+ networkId: _config.networkId
76
76
  };
77
77
  }, "getWalletAdapterState");
78
78
  let _adapter = new import_wallet_adapter.WalletAdapter({
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/state.ts"],"sourcesContent":["import {\n lsSet,\n lsGet,\n publicKeyFromPrivate,\n} from \"@fastnear/utils\";\nimport {WalletAdapter} from \"@fastnear/wallet-adapter\";\n\nexport const WIDGET_URL = \"https://js.cdn.fastnear.com\";\n\nexport const DEFAULT_NETWORK_ID = \"mainnet\";\nexport const NETWORKS = {\n testnet: {\n networkId: \"testnet\",\n nodeUrl: \"https://rpc.testnet.fastnear.com/\",\n },\n mainnet: {\n networkId: \"mainnet\",\n nodeUrl: \"https://rpc.mainnet.fastnear.com/\",\n },\n};\n\nexport interface NetworkConfig {\n networkId: string;\n nodeUrl?: string;\n walletUrl?: string;\n helperUrl?: string;\n explorerUrl?: string;\n\n [key: string]: any;\n}\n\nexport interface AppState {\n accountId?: string | null;\n privateKey?: string | null;\n lastWalletId?: string | null;\n publicKey?: string | null;\n accessKeyContractId?: string | null;\n\n [key: string]: any;\n}\n\nexport interface TxStatus {\n txId: string;\n updateTimestamp?: number;\n\n [key: string]: any;\n}\n\nexport type TxHistory = Record<string, TxStatus>;\n\nexport interface EventListeners {\n account: Set<(accountId: string) => void>;\n tx: Set<(tx: TxStatus) => void>;\n}\n\nexport interface UnbroadcastedEvents {\n account: string[];\n tx: TxStatus[];\n}\n\nexport interface WalletAdapterState {\n publicKey?: string | null;\n privateKey?: string | null;\n accountId?: string | null;\n lastWalletId?: string | null;\n networkId: string;\n}\n\n\n// Load config from localStorage or default to the network's config\nexport let _config: NetworkConfig = lsGet(\"config\") || {\n ...NETWORKS[DEFAULT_NETWORK_ID]\n};\n\n// Load application state from localStorage\nexport let _state: AppState = lsGet(\"state\") || {};\n\n// Triggered by the wallet adapter\nexport const onAdapterStateUpdate = (state: WalletAdapterState) => {\n console.log(\"Adapter state update:\", state);\n const { accountId, lastWalletId, privateKey } = state;\n update({\n accountId: accountId || undefined,\n lastWalletId: lastWalletId || undefined,\n ...(privateKey ? { privateKey } : {}),\n });\n}\n\nexport const getWalletAdapterState = (): WalletAdapterState => {\n return {\n publicKey: _state.publicKey,\n accountId: _state.accountId,\n lastWalletId: _state.lastWalletId,\n networkId: DEFAULT_NETWORK_ID,\n };\n}\n\n// We can create an adapter instance here\nexport let _adapter = new WalletAdapter({\n onStateUpdate: onAdapterStateUpdate,\n lastState: getWalletAdapterState(),\n widgetUrl: WIDGET_URL,\n});\n\n// Attempt to set publicKey if we have a privateKey\ntry {\n _state.publicKey = _state.privateKey\n ? publicKeyFromPrivate(_state.privateKey)\n : null;\n} catch (e) {\n console.error(\"Error parsing private key:\", e);\n _state.privateKey = null;\n lsSet(\"nonce\", null);\n}\n\n// Transaction history\nexport let _txHistory: TxHistory = lsGet(\"txHistory\") || {};\n\n\nexport const _unbroadcastedEvents: UnbroadcastedEvents = {\n account: [],\n tx: [],\n};\n\n// events / listeners\nexport const events = {\n _eventListeners: {\n account: new Set(),\n tx: new Set(),\n },\n\n notifyAccountListeners: (accountId: string) => {\n if (events._eventListeners.account.size === 0) {\n _unbroadcastedEvents.account.push(accountId);\n return;\n }\n events._eventListeners.account.forEach((callback: any) => {\n try {\n callback(accountId);\n } catch (e) {\n console.error(e);\n }\n });\n },\n\n notifyTxListeners: (tx: TxStatus) => {\n if (events._eventListeners.tx.size === 0) {\n _unbroadcastedEvents.tx.push(tx);\n return;\n }\n events._eventListeners.tx.forEach((callback: any) => {\n try {\n callback(tx);\n } catch (e) {\n console.error(e);\n }\n });\n },\n\n onAccount: (callback: (accountId: string) => void) => {\n events._eventListeners.account.add(callback);\n if (_unbroadcastedEvents.account.length > 0) {\n const accountEvent = _unbroadcastedEvents.account;\n _unbroadcastedEvents.account = [];\n accountEvent.forEach(events.notifyAccountListeners);\n }\n },\n\n onTx: (callback: (tx: TxStatus) => void): void => {\n events._eventListeners.tx.add(callback);\n if (_unbroadcastedEvents.tx.length > 0) {\n const txEvent = _unbroadcastedEvents.tx;\n _unbroadcastedEvents.tx = [];\n txEvent.forEach(events.notifyTxListeners);\n }\n }\n}\n\n// Mutators\n// @todo: in favor of limiting when out of alpha\n// but haven't given it enough thought ~ mike\nexport const update = (newState: Partial<AppState>) => {\n const oldState = _state;\n _state = {..._state, ...newState};\n\n lsSet(\"state\", {\n accountId: _state.accountId,\n privateKey: _state.privateKey,\n lastWalletId: _state.lastWalletId,\n accessKeyContractId: _state.accessKeyContractId,\n });\n\n if (\n newState.hasOwnProperty(\"privateKey\") &&\n newState.privateKey !== oldState.privateKey\n ) {\n _state.publicKey = newState.privateKey\n ? publicKeyFromPrivate(newState.privateKey as string)\n : null;\n lsSet(\"nonce\", null);\n }\n\n if (newState.accountId !== oldState.accountId) {\n events.notifyAccountListeners(newState.accountId as string);\n }\n\n if (\n (newState.hasOwnProperty(\"lastWalletId\") &&\n newState.lastWalletId !== oldState.lastWalletId) ||\n (newState.hasOwnProperty(\"accountId\") &&\n newState.accountId !== oldState.accountId) ||\n (newState.hasOwnProperty(\"privateKey\") &&\n newState.privateKey !== oldState.privateKey)\n ) {\n _adapter.setState(getWalletAdapterState());\n }\n}\n\nexport const updateTxHistory = (txStatus: TxStatus) => {\n const txId = txStatus.txId;\n _txHistory[txId] = {\n ...(_txHistory[txId] || {}),\n ...txStatus,\n updateTimestamp: Date.now(),\n };\n lsSet(\"txHistory\", _txHistory);\n events.notifyTxListeners(_txHistory[txId]);\n}\n\nexport const getConfig = (): NetworkConfig => {\n return _config;\n}\n\nexport const getTxHistory = (): TxHistory => {\n return _txHistory;\n}\n\n// Exposed \"write\" functions\nexport const setConfig = (newConf: NetworkConfig): void => {\n _config = { ...NETWORKS[newConf.networkId], ...newConf };\n lsSet(\"config\", _config);\n}\n\nexport const resetTxHistory = (): void => {\n _txHistory = {};\n lsSet(\"txHistory\", _txHistory);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAIO;AACP,4BAA4B;AAErB,MAAM,aAAa;AAEnB,MAAM,qBAAqB;AAC3B,MAAM,WAAW;AAAA,EACtB,SAAS;AAAA,IACP,WAAW;AAAA,IACX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SAAS,eAAe,SAAS,YACjC;AACA,WAAO,YAAY,SAAS,iBACxB,mCAAqB,SAAS,UAAoB,IAClD;AACJ,4BAAM,SAAS,IAAI;AAAA,EACrB;AAEA,MAAI,SAAS,cAAc,SAAS,WAAW;AAC7C,WAAO,uBAAuB,SAAS,SAAmB;AAAA,EAC5D;AAEA,MACG,SAAS,eAAe,cAAc,KACrC,SAAS,iBAAiB,SAAS,gBACpC,SAAS,eAAe,WAAW,KAClC,SAAS,cAAc,SAAS,aACjC,SAAS,eAAe,YAAY,KACnC,SAAS,eAAe,SAAS,YACnC;AACA,aAAS,SAAS,sBAAsB,CAAC;AAAA,EAC3C;AACF,GAnCsB;AAqCf,MAAM,kBAAkB,wBAAC,aAAuB;AACrD,QAAM,OAAO,SAAS;AACtB,aAAW,IAAI,IAAI;AAAA,IACjB,GAAI,WAAW,IAAI,KAAK,CAAC;AAAA,IACzB,GAAG;AAAA,IACH,iBAAiB,KAAK,IAAI;AAAA,EAC5B;AACA,0BAAM,aAAa,UAAU;AAC7B,SAAO,kBAAkB,WAAW,IAAI,CAAC;AAC3C,GAT+B;AAWxB,MAAM,YAAY,6BAAqB;AAC5C,SAAO;AACT,GAFyB;AAIlB,MAAM,eAAe,6BAAiB;AAC3C,SAAO;AACT,GAF4B;AAKrB,MAAM,YAAY,wBAAC,YAAiC;AACzD,YAAU,EAAE,GAAG,SAAS,QAAQ,SAAS,GAAG,GAAG,QAAQ;AACvD,0BAAM,UAAU,OAAO;AACzB,GAHyB;AAKlB,MAAM,iBAAiB,6BAAY;AACxC,eAAa,CAAC;AACd,0BAAM,aAAa,UAAU;AAC/B,GAH8B;","names":[]}
1
+ {"version":3,"sources":["../../src/state.ts"],"sourcesContent":["import {\n lsSet,\n lsGet,\n publicKeyFromPrivate,\n} from \"@fastnear/utils\";\nimport {WalletAdapter} from \"@fastnear/wallet-adapter\";\n\nexport const WIDGET_URL = \"https://js.cdn.fastnear.com\";\n\nexport const DEFAULT_NETWORK_ID = \"mainnet\";\nexport const NETWORKS = {\n testnet: {\n networkId: \"testnet\",\n nodeUrl: \"https://rpc.testnet.fastnear.com/\",\n },\n mainnet: {\n networkId: \"mainnet\",\n nodeUrl: \"https://rpc.mainnet.fastnear.com/\",\n },\n};\n\nexport interface NetworkConfig {\n networkId: string;\n nodeUrl?: string;\n walletUrl?: string;\n helperUrl?: string;\n explorerUrl?: string;\n\n [key: string]: any;\n}\n\nexport interface AppState {\n accountId?: string | null;\n privateKey?: string | null;\n lastWalletId?: string | null;\n publicKey?: string | null;\n accessKeyContractId?: string | null;\n\n [key: string]: any;\n}\n\nexport interface TxStatus {\n txId: string;\n updateTimestamp?: number;\n\n [key: string]: any;\n}\n\nexport type TxHistory = Record<string, TxStatus>;\n\nexport interface EventListeners {\n account: Set<(accountId: string) => void>;\n tx: Set<(tx: TxStatus) => void>;\n}\n\nexport interface UnbroadcastedEvents {\n account: string[];\n tx: TxStatus[];\n}\n\nexport interface WalletAdapterState {\n publicKey?: string | null;\n privateKey?: string | null;\n accountId?: string | null;\n lastWalletId?: string | null;\n networkId: string;\n}\n\n\n// Load config from localStorage or default to the network's config\nexport let _config: NetworkConfig = lsGet(\"config\") || {\n ...NETWORKS[DEFAULT_NETWORK_ID]\n};\n\n// Load application state from localStorage\nexport let _state: AppState = lsGet(\"state\") || {};\n\n// Triggered by the wallet adapter\nexport const onAdapterStateUpdate = (state: WalletAdapterState) => {\n console.log(\"Adapter state update:\", state);\n const { accountId, lastWalletId, privateKey } = state;\n update({\n accountId: accountId || undefined,\n lastWalletId: lastWalletId || undefined,\n ...(privateKey ? { privateKey } : {}),\n });\n}\n\nexport const getWalletAdapterState = (): WalletAdapterState => {\n return {\n publicKey: _state.publicKey,\n accountId: _state.accountId,\n lastWalletId: _state.lastWalletId,\n networkId: _config.networkId,\n };\n}\n\n// We can create an adapter instance here\nexport let _adapter = new WalletAdapter({\n onStateUpdate: onAdapterStateUpdate,\n lastState: getWalletAdapterState(),\n widgetUrl: WIDGET_URL,\n});\n\n// Attempt to set publicKey if we have a privateKey\ntry {\n _state.publicKey = _state.privateKey\n ? publicKeyFromPrivate(_state.privateKey)\n : null;\n} catch (e) {\n console.error(\"Error parsing private key:\", e);\n _state.privateKey = null;\n lsSet(\"nonce\", null);\n}\n\n// Transaction history\nexport let _txHistory: TxHistory = lsGet(\"txHistory\") || {};\n\n\nexport const _unbroadcastedEvents: UnbroadcastedEvents = {\n account: [],\n tx: [],\n};\n\n// events / listeners\nexport const events = {\n _eventListeners: {\n account: new Set(),\n tx: new Set(),\n },\n\n notifyAccountListeners: (accountId: string) => {\n if (events._eventListeners.account.size === 0) {\n _unbroadcastedEvents.account.push(accountId);\n return;\n }\n events._eventListeners.account.forEach((callback: any) => {\n try {\n callback(accountId);\n } catch (e) {\n console.error(e);\n }\n });\n },\n\n notifyTxListeners: (tx: TxStatus) => {\n if (events._eventListeners.tx.size === 0) {\n _unbroadcastedEvents.tx.push(tx);\n return;\n }\n events._eventListeners.tx.forEach((callback: any) => {\n try {\n callback(tx);\n } catch (e) {\n console.error(e);\n }\n });\n },\n\n onAccount: (callback: (accountId: string) => void) => {\n events._eventListeners.account.add(callback);\n if (_unbroadcastedEvents.account.length > 0) {\n const accountEvent = _unbroadcastedEvents.account;\n _unbroadcastedEvents.account = [];\n accountEvent.forEach(events.notifyAccountListeners);\n }\n },\n\n onTx: (callback: (tx: TxStatus) => void): void => {\n events._eventListeners.tx.add(callback);\n if (_unbroadcastedEvents.tx.length > 0) {\n const txEvent = _unbroadcastedEvents.tx;\n _unbroadcastedEvents.tx = [];\n txEvent.forEach(events.notifyTxListeners);\n }\n }\n}\n\n// Mutators\n// @todo: in favor of limiting when out of alpha\n// but haven't given it enough thought ~ mike\nexport const update = (newState: Partial<AppState>) => {\n const oldState = _state;\n _state = {..._state, ...newState};\n\n lsSet(\"state\", {\n accountId: _state.accountId,\n privateKey: _state.privateKey,\n lastWalletId: _state.lastWalletId,\n accessKeyContractId: _state.accessKeyContractId,\n });\n\n if (\n newState.hasOwnProperty(\"privateKey\") &&\n newState.privateKey !== oldState.privateKey\n ) {\n _state.publicKey = newState.privateKey\n ? publicKeyFromPrivate(newState.privateKey as string)\n : null;\n lsSet(\"nonce\", null);\n }\n\n if (newState.accountId !== oldState.accountId) {\n events.notifyAccountListeners(newState.accountId as string);\n }\n\n if (\n (newState.hasOwnProperty(\"lastWalletId\") &&\n newState.lastWalletId !== oldState.lastWalletId) ||\n (newState.hasOwnProperty(\"accountId\") &&\n newState.accountId !== oldState.accountId) ||\n (newState.hasOwnProperty(\"privateKey\") &&\n newState.privateKey !== oldState.privateKey)\n ) {\n _adapter.setState(getWalletAdapterState());\n }\n}\n\nexport const updateTxHistory = (txStatus: TxStatus) => {\n const txId = txStatus.txId;\n _txHistory[txId] = {\n ...(_txHistory[txId] || {}),\n ...txStatus,\n updateTimestamp: Date.now(),\n };\n lsSet(\"txHistory\", _txHistory);\n events.notifyTxListeners(_txHistory[txId]);\n}\n\nexport const getConfig = (): NetworkConfig => {\n return _config;\n}\n\nexport const getTxHistory = (): TxHistory => {\n return _txHistory;\n}\n\n// Exposed \"write\" functions\nexport const setConfig = (newConf: NetworkConfig): void => {\n _config = { ...NETWORKS[newConf.networkId], ...newConf };\n lsSet(\"config\", _config);\n}\n\nexport const resetTxHistory = (): void => {\n _txHistory = {};\n lsSet(\"txHistory\", _txHistory);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAIO;AACP,4BAA4B;AAErB,MAAM,aAAa;AAEnB,MAAM,qBAAqB;AAC3B,MAAM,WAAW;AAAA,EACtB,SAAS;AAAA,IACP,WAAW;AAAA,IACX,SAAS;AAAA,EACX;AAAA,EACA,SAAS;AAAA,IACP,WAAW;AAAA,IACX,SAAS;AAAA,EACX;AACF;AAmDO,IAAI,cAAyB,oBA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,IAAI;AAAA,EACrB;AAEA,MAAI,SAAS,cAAc,SAAS,WAAW;AAC7C,WAAO,uBAAuB,SAAS,SAAmB;AAAA,EAC5D;AAEA,MACG,SAAS,eAAe,cAAc,KACrC,SAAS,iBAAiB,SAAS,gBACpC,SAAS,eAAe,WAAW,KAClC,SAAS,cAAc,SAAS,aACjC,SAAS,eAAe,YAAY,KACnC,SAAS,eAAe,SAAS,YACnC;AACA,aAAS,SAAS,sBAAsB,CAAC;AAAA,EAC3C;AACF,GAnCsB;AAqCf,MAAM,kBAAkB,wBAAC,aAAuB;AACrD,QAAM,OAAO,SAAS;AACtB,aAAW,IAAI,IAAI;AAAA,IACjB,GAAI,WAAW,IAAI,KAAK,CAAC;AAAA,IACzB,GAAG;AAAA,IACH,iBAAiB,KAAK,IAAI;AAAA,EAC5B;AACA,0BAAM,aAAa,UAAU;AAC7B,SAAO,kBAAkB,WAAW,IAAI,CAAC;AAC3C,GAT+B;AAWxB,MAAM,YAAY,6BAAqB;AAC5C,SAAO;AACT,GAFyB;AAIlB,MAAM,eAAe,6BAAiB;AAC3C,SAAO;AACT,GAF4B;AAKrB,MAAM,YAAY,wBAAC,YAAiC;AACzD,YAAU,EAAE,GAAG,SAAS,QAAQ,SAAS,GAAG,GAAG,QAAQ;AACvD,0BAAM,UAAU,OAAO;AACzB,GAHyB;AAKlB,MAAM,iBAAiB,6BAAY;AACxC,eAAa,CAAC;AACd,0BAAM,aAAa,UAAU;AAC/B,GAH8B;","names":[]}
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /* ⋈ 🏃🏻💨 FastNEAR API - ESM (@fastnear/api version 0.9.3) */
2
- /* https://www.npmjs.com/package/@fastnear/api/v/0.9.3 */
1
+ /* ⋈ 🏃🏻💨 FastNEAR API - ESM (@fastnear/api version 0.9.5) */
2
+ /* https://www.npmjs.com/package/@fastnear/api/v/0.9.5 */
3
3
  export * from "./near.js";
4
4
  //# sourceMappingURL=index.js.map
package/dist/esm/near.js CHANGED
@@ -1,5 +1,5 @@
1
- /* ⋈ 🏃🏻💨 FastNEAR API - ESM (@fastnear/api version 0.9.3) */
2
- /* https://www.npmjs.com/package/@fastnear/api/v/0.9.3 */
1
+ /* ⋈ 🏃🏻💨 FastNEAR API - ESM (@fastnear/api version 0.9.5) */
2
+ /* https://www.npmjs.com/package/@fastnear/api/v/0.9.5 */
3
3
  var __defProp = Object.defineProperty;
4
4
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
5
  import Big from "big.js";
package/dist/esm/state.js CHANGED
@@ -1,5 +1,5 @@
1
- /* ⋈ 🏃🏻💨 FastNEAR API - ESM (@fastnear/api version 0.9.3) */
2
- /* https://www.npmjs.com/package/@fastnear/api/v/0.9.3 */
1
+ /* ⋈ 🏃🏻💨 FastNEAR API - ESM (@fastnear/api version 0.9.5) */
2
+ /* https://www.npmjs.com/package/@fastnear/api/v/0.9.5 */
3
3
  var __defProp = Object.defineProperty;
4
4
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
5
  import {
@@ -38,7 +38,7 @@ const getWalletAdapterState = /* @__PURE__ */ __name(() => {
38
38
  publicKey: _state.publicKey,
39
39
  accountId: _state.accountId,
40
40
  lastWalletId: _state.lastWalletId,
41
- networkId: DEFAULT_NETWORK_ID
41
+ networkId: _config.networkId
42
42
  };
43
43
  }, "getWalletAdapterState");
44
44
  let _adapter = new WalletAdapter({
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/state.ts"],"sourcesContent":["import {\n lsSet,\n lsGet,\n publicKeyFromPrivate,\n} from \"@fastnear/utils\";\nimport {WalletAdapter} from \"@fastnear/wallet-adapter\";\n\nexport const WIDGET_URL = \"https://js.cdn.fastnear.com\";\n\nexport const DEFAULT_NETWORK_ID = \"mainnet\";\nexport const NETWORKS = {\n testnet: {\n networkId: \"testnet\",\n nodeUrl: \"https://rpc.testnet.fastnear.com/\",\n },\n mainnet: {\n networkId: \"mainnet\",\n nodeUrl: \"https://rpc.mainnet.fastnear.com/\",\n },\n};\n\nexport interface NetworkConfig {\n networkId: string;\n nodeUrl?: string;\n walletUrl?: string;\n helperUrl?: string;\n explorerUrl?: string;\n\n [key: string]: any;\n}\n\nexport interface AppState {\n accountId?: string | null;\n privateKey?: string | null;\n lastWalletId?: string | null;\n publicKey?: string | null;\n accessKeyContractId?: string | null;\n\n [key: string]: any;\n}\n\nexport interface TxStatus {\n txId: string;\n updateTimestamp?: number;\n\n [key: string]: any;\n}\n\nexport type TxHistory = Record<string, TxStatus>;\n\nexport interface EventListeners {\n account: Set<(accountId: string) => void>;\n tx: Set<(tx: TxStatus) => void>;\n}\n\nexport interface UnbroadcastedEvents {\n account: string[];\n tx: TxStatus[];\n}\n\nexport interface WalletAdapterState {\n publicKey?: string | null;\n privateKey?: string | null;\n accountId?: string | null;\n lastWalletId?: string | null;\n networkId: string;\n}\n\n\n// Load config from localStorage or default to the network's config\nexport let _config: NetworkConfig = lsGet(\"config\") || {\n ...NETWORKS[DEFAULT_NETWORK_ID]\n};\n\n// Load application state from localStorage\nexport let _state: AppState = lsGet(\"state\") || {};\n\n// Triggered by the wallet adapter\nexport const onAdapterStateUpdate = (state: WalletAdapterState) => {\n console.log(\"Adapter state update:\", state);\n const { accountId, lastWalletId, privateKey } = state;\n update({\n accountId: accountId || undefined,\n lastWalletId: lastWalletId || undefined,\n ...(privateKey ? { privateKey } : {}),\n });\n}\n\nexport const getWalletAdapterState = (): WalletAdapterState => {\n return {\n publicKey: _state.publicKey,\n accountId: _state.accountId,\n lastWalletId: _state.lastWalletId,\n networkId: DEFAULT_NETWORK_ID,\n };\n}\n\n// We can create an adapter instance here\nexport let _adapter = new WalletAdapter({\n onStateUpdate: onAdapterStateUpdate,\n lastState: getWalletAdapterState(),\n widgetUrl: WIDGET_URL,\n});\n\n// Attempt to set publicKey if we have a privateKey\ntry {\n _state.publicKey = _state.privateKey\n ? publicKeyFromPrivate(_state.privateKey)\n : null;\n} catch (e) {\n console.error(\"Error parsing private key:\", e);\n _state.privateKey = null;\n lsSet(\"nonce\", null);\n}\n\n// Transaction history\nexport let _txHistory: TxHistory = lsGet(\"txHistory\") || {};\n\n\nexport const _unbroadcastedEvents: UnbroadcastedEvents = {\n account: [],\n tx: [],\n};\n\n// events / listeners\nexport const events = {\n _eventListeners: {\n account: new Set(),\n tx: new Set(),\n },\n\n notifyAccountListeners: (accountId: string) => {\n if (events._eventListeners.account.size === 0) {\n _unbroadcastedEvents.account.push(accountId);\n return;\n }\n events._eventListeners.account.forEach((callback: any) => {\n try {\n callback(accountId);\n } catch (e) {\n console.error(e);\n }\n });\n },\n\n notifyTxListeners: (tx: TxStatus) => {\n if (events._eventListeners.tx.size === 0) {\n _unbroadcastedEvents.tx.push(tx);\n return;\n }\n events._eventListeners.tx.forEach((callback: any) => {\n try {\n callback(tx);\n } catch (e) {\n console.error(e);\n }\n });\n },\n\n onAccount: (callback: (accountId: string) => void) => {\n events._eventListeners.account.add(callback);\n if (_unbroadcastedEvents.account.length > 0) {\n const accountEvent = _unbroadcastedEvents.account;\n _unbroadcastedEvents.account = [];\n accountEvent.forEach(events.notifyAccountListeners);\n }\n },\n\n onTx: (callback: (tx: TxStatus) => void): void => {\n events._eventListeners.tx.add(callback);\n if (_unbroadcastedEvents.tx.length > 0) {\n const txEvent = _unbroadcastedEvents.tx;\n _unbroadcastedEvents.tx = [];\n txEvent.forEach(events.notifyTxListeners);\n }\n }\n}\n\n// Mutators\n// @todo: in favor of limiting when out of alpha\n// but haven't given it enough thought ~ mike\nexport const update = (newState: Partial<AppState>) => {\n const oldState = _state;\n _state = {..._state, ...newState};\n\n lsSet(\"state\", {\n accountId: _state.accountId,\n privateKey: _state.privateKey,\n lastWalletId: _state.lastWalletId,\n accessKeyContractId: _state.accessKeyContractId,\n });\n\n if (\n newState.hasOwnProperty(\"privateKey\") &&\n newState.privateKey !== oldState.privateKey\n ) {\n _state.publicKey = newState.privateKey\n ? publicKeyFromPrivate(newState.privateKey as string)\n : null;\n lsSet(\"nonce\", null);\n }\n\n if (newState.accountId !== oldState.accountId) {\n events.notifyAccountListeners(newState.accountId as string);\n }\n\n if (\n (newState.hasOwnProperty(\"lastWalletId\") &&\n newState.lastWalletId !== oldState.lastWalletId) ||\n (newState.hasOwnProperty(\"accountId\") &&\n newState.accountId !== oldState.accountId) ||\n (newState.hasOwnProperty(\"privateKey\") &&\n newState.privateKey !== oldState.privateKey)\n ) {\n _adapter.setState(getWalletAdapterState());\n }\n}\n\nexport const updateTxHistory = (txStatus: TxStatus) => {\n const txId = txStatus.txId;\n _txHistory[txId] = {\n ...(_txHistory[txId] || {}),\n ...txStatus,\n updateTimestamp: Date.now(),\n };\n lsSet(\"txHistory\", _txHistory);\n events.notifyTxListeners(_txHistory[txId]);\n}\n\nexport const getConfig = (): NetworkConfig => {\n return _config;\n}\n\nexport const getTxHistory = (): TxHistory => {\n return _txHistory;\n}\n\n// Exposed \"write\" functions\nexport const setConfig = (newConf: NetworkConfig): void => {\n _config = { ...NETWORKS[newConf.networkId], ...newConf };\n lsSet(\"config\", _config);\n}\n\nexport const resetTxHistory = (): void => {\n _txHistory = {};\n lsSet(\"txHistory\", _txHistory);\n}\n"],"mappings":";;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAQ,qBAAoB;AAErB,MAAM,aAAa;AAEnB,MAAM,qBAAqB;AAC3B,MAAM,WAAW;AAAA,EACtB,SAAS;AAAA,IACP,WAAW;AAAA,IACX,SAAS;AAAA,EACX;AAAA,EACA,SAAS;AAAA,IACP,WAAW;AAAA,IACX,SAAS;AAAA,EACX;AACF;AAmDO,IAAI,UAAyB,MAAM,QAAQ,KAAK;AAAA,EACrD,GAAG,SAAS,kBAAkB;AAChC;AAGO,IAAI,SAAmB,MAAM,OAAO,KAAK,CAAC;AAG1C,MAAM,uBAAuB,wBAAC,UAA8B;AACjE,UAAQ,IAAI,yBAAyB,KAAK;AAC1C,QAAM,EAAE,WAAW,cAAc,WAAW,IAAI;AAChD,SAAO;AAAA,IACL,WAA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ACnC;AACA,aAAS,SAAS,sBAAsB,CAAC;AAAA,EAC3C;AACF,GAnCsB;AAqCf,MAAM,kBAAkB,wBAAC,aAAuB;AACrD,QAAM,OAAO,SAAS;AACtB,aAAW,IAAI,IAAI;AAAA,IACjB,GAAI,WAAW,IAAI,KAAK,CAAC;AAAA,IACzB,GAAG;AAAA,IACH,iBAAiB,KAAK,IAAI;AAAA,EAC5B;AACA,QAAM,aAAa,UAAU;AAC7B,SAAO,kBAAkB,WAAW,IAAI,CAAC;AAC3C,GAT+B;AAWxB,MAAM,YAAY,6BAAqB;AAC5C,SAAO;AACT,GAFyB;AAIlB,MAAM,eAAe,6BAAiB;AAC3C,SAAO;AACT,GAF4B;AAKrB,MAAM,YAAY,wBAAC,YAAiC;AACzD,YAAU,EAAE,GAAG,SAAS,QAAQ,SAAS,GAAG,GAAG,QAAQ;AACvD,QAAM,UAAU,OAAO;AACzB,GAHyB;AAKlB,MAAM,iBAAiB,6BAAY;AACxC,eAAa,CAAC;AACd,QAAM,aAAa,UAAU;AAC/B,GAH8B;","names":[]}
1
+ {"version":3,"sources":["../../src/state.ts"],"sourcesContent":["import {\n lsSet,\n lsGet,\n publicKeyFromPrivate,\n} from \"@fastnear/utils\";\nimport {WalletAdapter} from \"@fastnear/wallet-adapter\";\n\nexport const WIDGET_URL = \"https://js.cdn.fastnear.com\";\n\nexport const DEFAULT_NETWORK_ID = \"mainnet\";\nexport const NETWORKS = {\n testnet: {\n networkId: \"testnet\",\n nodeUrl: \"https://rpc.testnet.fastnear.com/\",\n },\n mainnet: {\n networkId: \"mainnet\",\n nodeUrl: \"https://rpc.mainnet.fastnear.com/\",\n },\n};\n\nexport interface NetworkConfig {\n networkId: string;\n nodeUrl?: string;\n walletUrl?: string;\n helperUrl?: string;\n explorerUrl?: string;\n\n [key: string]: any;\n}\n\nexport interface AppState {\n accountId?: string | null;\n privateKey?: string | null;\n lastWalletId?: string | null;\n publicKey?: string | null;\n accessKeyContractId?: string | null;\n\n [key: string]: any;\n}\n\nexport interface TxStatus {\n txId: string;\n updateTimestamp?: number;\n\n [key: string]: any;\n}\n\nexport type TxHistory = Record<string, TxStatus>;\n\nexport interface EventListeners {\n account: Set<(accountId: string) => void>;\n tx: Set<(tx: TxStatus) => void>;\n}\n\nexport interface UnbroadcastedEvents {\n account: string[];\n tx: TxStatus[];\n}\n\nexport interface WalletAdapterState {\n publicKey?: string | null;\n privateKey?: string | null;\n accountId?: string | null;\n lastWalletId?: string | null;\n networkId: string;\n}\n\n\n// Load config from localStorage or default to the network's config\nexport let _config: NetworkConfig = lsGet(\"config\") || {\n ...NETWORKS[DEFAULT_NETWORK_ID]\n};\n\n// Load application state from localStorage\nexport let _state: AppState = lsGet(\"state\") || {};\n\n// Triggered by the wallet adapter\nexport const onAdapterStateUpdate = (state: WalletAdapterState) => {\n console.log(\"Adapter state update:\", state);\n const { accountId, lastWalletId, privateKey } = state;\n update({\n accountId: accountId || undefined,\n lastWalletId: lastWalletId || undefined,\n ...(privateKey ? { privateKey } : {}),\n });\n}\n\nexport const getWalletAdapterState = (): WalletAdapterState => {\n return {\n publicKey: _state.publicKey,\n accountId: _state.accountId,\n lastWalletId: _state.lastWalletId,\n networkId: _config.networkId,\n };\n}\n\n// We can create an adapter instance here\nexport let _adapter = new WalletAdapter({\n onStateUpdate: onAdapterStateUpdate,\n lastState: getWalletAdapterState(),\n widgetUrl: WIDGET_URL,\n});\n\n// Attempt to set publicKey if we have a privateKey\ntry {\n _state.publicKey = _state.privateKey\n ? publicKeyFromPrivate(_state.privateKey)\n : null;\n} catch (e) {\n console.error(\"Error parsing private key:\", e);\n _state.privateKey = null;\n lsSet(\"nonce\", null);\n}\n\n// Transaction history\nexport let _txHistory: TxHistory = lsGet(\"txHistory\") || {};\n\n\nexport const _unbroadcastedEvents: UnbroadcastedEvents = {\n account: [],\n tx: [],\n};\n\n// events / listeners\nexport const events = {\n _eventListeners: {\n account: new Set(),\n tx: new Set(),\n },\n\n notifyAccountListeners: (accountId: string) => {\n if (events._eventListeners.account.size === 0) {\n _unbroadcastedEvents.account.push(accountId);\n return;\n }\n events._eventListeners.account.forEach((callback: any) => {\n try {\n callback(accountId);\n } catch (e) {\n console.error(e);\n }\n });\n },\n\n notifyTxListeners: (tx: TxStatus) => {\n if (events._eventListeners.tx.size === 0) {\n _unbroadcastedEvents.tx.push(tx);\n return;\n }\n events._eventListeners.tx.forEach((callback: any) => {\n try {\n callback(tx);\n } catch (e) {\n console.error(e);\n }\n });\n },\n\n onAccount: (callback: (accountId: string) => void) => {\n events._eventListeners.account.add(callback);\n if (_unbroadcastedEvents.account.length > 0) {\n const accountEvent = _unbroadcastedEvents.account;\n _unbroadcastedEvents.account = [];\n accountEvent.forEach(events.notifyAccountListeners);\n }\n },\n\n onTx: (callback: (tx: TxStatus) => void): void => {\n events._eventListeners.tx.add(callback);\n if (_unbroadcastedEvents.tx.length > 0) {\n const txEvent = _unbroadcastedEvents.tx;\n _unbroadcastedEvents.tx = [];\n txEvent.forEach(events.notifyTxListeners);\n }\n }\n}\n\n// Mutators\n// @todo: in favor of limiting when out of alpha\n// but haven't given it enough thought ~ mike\nexport const update = (newState: Partial<AppState>) => {\n const oldState = _state;\n _state = {..._state, ...newState};\n\n lsSet(\"state\", {\n accountId: _state.accountId,\n privateKey: _state.privateKey,\n lastWalletId: _state.lastWalletId,\n accessKeyContractId: _state.accessKeyContractId,\n });\n\n if (\n newState.hasOwnProperty(\"privateKey\") &&\n newState.privateKey !== oldState.privateKey\n ) {\n _state.publicKey = newState.privateKey\n ? publicKeyFromPrivate(newState.privateKey as string)\n : null;\n lsSet(\"nonce\", null);\n }\n\n if (newState.accountId !== oldState.accountId) {\n events.notifyAccountListeners(newState.accountId as string);\n }\n\n if (\n (newState.hasOwnProperty(\"lastWalletId\") &&\n newState.lastWalletId !== oldState.lastWalletId) ||\n (newState.hasOwnProperty(\"accountId\") &&\n newState.accountId !== oldState.accountId) ||\n (newState.hasOwnProperty(\"privateKey\") &&\n newState.privateKey !== oldState.privateKey)\n ) {\n _adapter.setState(getWalletAdapterState());\n }\n}\n\nexport const updateTxHistory = (txStatus: TxStatus) => {\n const txId = txStatus.txId;\n _txHistory[txId] = {\n ...(_txHistory[txId] || {}),\n ...txStatus,\n updateTimestamp: Date.now(),\n };\n lsSet(\"txHistory\", _txHistory);\n events.notifyTxListeners(_txHistory[txId]);\n}\n\nexport const getConfig = (): NetworkConfig => {\n return _config;\n}\n\nexport const getTxHistory = (): TxHistory => {\n return _txHistory;\n}\n\n// Exposed \"write\" functions\nexport const setConfig = (newConf: NetworkConfig): void => {\n _config = { ...NETWORKS[newConf.networkId], ...newConf };\n lsSet(\"config\", _config);\n}\n\nexport const resetTxHistory = (): void => {\n _txHistory = {};\n lsSet(\"txHistory\", _txHistory);\n}\n"],"mappings":";;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAQ,qBAAoB;AAErB,MAAM,aAAa;AAEnB,MAAM,qBAAqB;AAC3B,MAAM,WAAW;AAAA,EACtB,SAAS;AAAA,IACP,WAAW;AAAA,IACX,SAAS;AAAA,EACX;AAAA,EACA,SAAS;AAAA,IACP,WAAW;AAAA,IACX,SAAS;AAAA,EACX;AACF;AAmDO,IAAI,UAAyB,MAAM,QAAQ,KAAK;AAAA,EACrD,GAAG,SAAS,kBAAkB;AAChC;AAGO,IAAI,SAAmB,MAAM,OAAO,KAAK,CAAC;AAG1C,MAAM,uBAAuB,wBAAC,UAA8B;AACjE,UAAQ,IAAI,yBAAyB,KAAK;AAC1C,QAAM,EAAE,WAAW,cAAc,WAAW,IAAI;AAChD,SAAO;AAAA,IACL,WAAW,aAAa;AAAA,IACxB,cAAc,gBAAgB;AAAA,IAC9B,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;AAAA,EACrC,CAAC;AACH,GARoC;AAU7B,MAAM,wBAAwB,6BAA0B;AAC7D,SAAO;AAAA,IACL,WAAW,OAAO;AAAA,IAClB,WAAW,OAAO;AAAA,IAClB,cAAc,OAAO;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BAAiB,KAAK,IAAI;AAAA,EAC5B;AACA,QAAM,aAAa,UAAU;AAC7B,SAAO,kBAAkB,WAAW,IAAI,CAAC;AAC3C,GAT+B;AAWxB,MAAM,YAAY,6BAAqB;AAC5C,SAAO;AACT,GAFyB;AAIlB,MAAM,eAAe,6BAAiB;AAC3C,SAAO;AACT,GAF4B;AAKrB,MAAM,YAAY,wBAAC,YAAiC;AACzD,YAAU,EAAE,GAAG,SAAS,QAAQ,SAAS,GAAG,GAAG,QAAQ;AACvD,QAAM,UAAU,OAAO;AACzB,GAHyB;AAKlB,MAAM,iBAAiB,6BAAY;AACxC,eAAa,CAAC;AACd,QAAM,aAAa,UAAU;AAC/B,GAH8B;","names":[]}
@@ -1,5 +1,5 @@
1
- /* ⋈ 🏃🏻💨 FastNEAR API - IIFE/UMD (@fastnear/api version 0.9.3) */
2
- /* https://www.npmjs.com/package/@fastnear/api/v/0.9.3 */
1
+ /* ⋈ 🏃🏻💨 FastNEAR API - IIFE/UMD (@fastnear/api version 0.9.5) */
2
+ /* https://www.npmjs.com/package/@fastnear/api/v/0.9.5 */
3
3
  "use strict";
4
4
  var near = (() => {
5
5
  var __defProp = Object.defineProperty;
@@ -3928,7 +3928,7 @@ var near = (() => {
3928
3928
  publicKey: _state.publicKey,
3929
3929
  accountId: _state.accountId,
3930
3930
  lastWalletId: _state.lastWalletId,
3931
- networkId: DEFAULT_NETWORK_ID
3931
+ networkId: _config.networkId
3932
3932
  };
3933
3933
  }, "getWalletAdapterState");
3934
3934
  var _adapter = new WalletAdapter({