@fastnear/api 0.8.0 → 0.9.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.
@@ -26,27 +26,23 @@ __export(state_exports, {
26
26
  WIDGET_URL: () => WIDGET_URL,
27
27
  _adapter: () => _adapter,
28
28
  _config: () => _config,
29
- _eventListeners: () => _eventListeners,
30
29
  _state: () => _state,
31
30
  _txHistory: () => _txHistory,
32
31
  _unbroadcastedEvents: () => _unbroadcastedEvents,
32
+ events: () => events,
33
33
  getConfig: () => getConfig,
34
34
  getTxHistory: () => getTxHistory,
35
35
  getWalletAdapterState: () => getWalletAdapterState,
36
- notifyAccountListeners: () => notifyAccountListeners,
37
- notifyTxListeners: () => notifyTxListeners,
38
- onAccount: () => onAccount,
39
36
  onAdapterStateUpdate: () => onAdapterStateUpdate,
40
- onTx: () => onTx,
41
37
  resetTxHistory: () => resetTxHistory,
42
38
  setConfig: () => setConfig,
43
- updateState: () => updateState,
39
+ update: () => update,
44
40
  updateTxHistory: () => updateTxHistory
45
41
  });
46
42
  module.exports = __toCommonJS(state_exports);
47
43
  var import_utils = require("@fastnear/utils");
48
44
  var import_wallet_adapter = require("@fastnear/wallet-adapter");
49
- const WIDGET_URL = "https://wallet-adapter.fastnear.com";
45
+ const WIDGET_URL = "https://js.cdn.fastnear.com";
50
46
  const DEFAULT_NETWORK_ID = "mainnet";
51
47
  const NETWORKS = {
52
48
  testnet: {
@@ -65,7 +61,7 @@ let _state = (0, import_utils.lsGet)("state") || {};
65
61
  const onAdapterStateUpdate = /* @__PURE__ */ __name((state) => {
66
62
  console.log("Adapter state update:", state);
67
63
  const { accountId, lastWalletId, privateKey } = state;
68
- updateState({
64
+ update({
69
65
  accountId: accountId || void 0,
70
66
  lastWalletId: lastWalletId || void 0,
71
67
  ...privateKey ? { privateKey } : {}
@@ -92,15 +88,59 @@ try {
92
88
  (0, import_utils.lsSet)("nonce", null);
93
89
  }
94
90
  let _txHistory = (0, import_utils.lsGet)("txHistory") || {};
95
- const _eventListeners = {
96
- account: /* @__PURE__ */ new Set(),
97
- tx: /* @__PURE__ */ new Set()
98
- };
99
91
  const _unbroadcastedEvents = {
100
92
  account: [],
101
93
  tx: []
102
94
  };
103
- const updateState = /* @__PURE__ */ __name((newState) => {
95
+ const events = {
96
+ _eventListeners: {
97
+ account: /* @__PURE__ */ new Set(),
98
+ tx: /* @__PURE__ */ new Set()
99
+ },
100
+ notifyAccountListeners: /* @__PURE__ */ __name((accountId) => {
101
+ if (events._eventListeners.account.size === 0) {
102
+ _unbroadcastedEvents.account.push(accountId);
103
+ return;
104
+ }
105
+ events._eventListeners.account.forEach((callback) => {
106
+ try {
107
+ callback(accountId);
108
+ } catch (e) {
109
+ console.error(e);
110
+ }
111
+ });
112
+ }, "notifyAccountListeners"),
113
+ notifyTxListeners: /* @__PURE__ */ __name((tx) => {
114
+ if (events._eventListeners.tx.size === 0) {
115
+ _unbroadcastedEvents.tx.push(tx);
116
+ return;
117
+ }
118
+ events._eventListeners.tx.forEach((callback) => {
119
+ try {
120
+ callback(tx);
121
+ } catch (e) {
122
+ console.error(e);
123
+ }
124
+ });
125
+ }, "notifyTxListeners"),
126
+ onAccount: /* @__PURE__ */ __name((callback) => {
127
+ events._eventListeners.account.add(callback);
128
+ if (_unbroadcastedEvents.account.length > 0) {
129
+ const accountEvent = _unbroadcastedEvents.account;
130
+ _unbroadcastedEvents.account = [];
131
+ accountEvent.forEach(events.notifyAccountListeners);
132
+ }
133
+ }, "onAccount"),
134
+ onTx: /* @__PURE__ */ __name((callback) => {
135
+ events._eventListeners.tx.add(callback);
136
+ if (_unbroadcastedEvents.tx.length > 0) {
137
+ const txEvent = _unbroadcastedEvents.tx;
138
+ _unbroadcastedEvents.tx = [];
139
+ txEvent.forEach(events.notifyTxListeners);
140
+ }
141
+ }, "onTx")
142
+ };
143
+ const update = /* @__PURE__ */ __name((newState) => {
104
144
  const oldState = _state;
105
145
  _state = { ..._state, ...newState };
106
146
  (0, import_utils.lsSet)("state", {
@@ -114,12 +154,12 @@ const updateState = /* @__PURE__ */ __name((newState) => {
114
154
  (0, import_utils.lsSet)("nonce", null);
115
155
  }
116
156
  if (newState.accountId !== oldState.accountId) {
117
- notifyAccountListeners(newState.accountId);
157
+ events.notifyAccountListeners(newState.accountId);
118
158
  }
119
159
  if (newState.hasOwnProperty("lastWalletId") && newState.lastWalletId !== oldState.lastWalletId || newState.hasOwnProperty("accountId") && newState.accountId !== oldState.accountId || newState.hasOwnProperty("privateKey") && newState.privateKey !== oldState.privateKey) {
120
160
  _adapter.setState(getWalletAdapterState());
121
161
  }
122
- }, "updateState");
162
+ }, "update");
123
163
  const updateTxHistory = /* @__PURE__ */ __name((txStatus) => {
124
164
  const txId = txStatus.txId;
125
165
  _txHistory[txId] = {
@@ -128,50 +168,8 @@ const updateTxHistory = /* @__PURE__ */ __name((txStatus) => {
128
168
  updateTimestamp: Date.now()
129
169
  };
130
170
  (0, import_utils.lsSet)("txHistory", _txHistory);
131
- notifyTxListeners(_txHistory[txId]);
171
+ events.notifyTxListeners(_txHistory[txId]);
132
172
  }, "updateTxHistory");
133
- const notifyAccountListeners = /* @__PURE__ */ __name((accountId) => {
134
- if (_eventListeners.account.size === 0) {
135
- _unbroadcastedEvents.account.push(accountId);
136
- return;
137
- }
138
- _eventListeners.account.forEach((callback) => {
139
- try {
140
- callback(accountId);
141
- } catch (e) {
142
- console.error(e);
143
- }
144
- });
145
- }, "notifyAccountListeners");
146
- const notifyTxListeners = /* @__PURE__ */ __name((tx) => {
147
- if (_eventListeners.tx.size === 0) {
148
- _unbroadcastedEvents.tx.push(tx);
149
- return;
150
- }
151
- _eventListeners.tx.forEach((callback) => {
152
- try {
153
- callback(tx);
154
- } catch (e) {
155
- console.error(e);
156
- }
157
- });
158
- }, "notifyTxListeners");
159
- const onAccount = /* @__PURE__ */ __name((callback) => {
160
- _eventListeners.account.add(callback);
161
- if (_unbroadcastedEvents.account.length > 0) {
162
- const events = _unbroadcastedEvents.account;
163
- _unbroadcastedEvents.account = [];
164
- events.forEach(notifyAccountListeners);
165
- }
166
- }, "onAccount");
167
- const onTx = /* @__PURE__ */ __name((callback) => {
168
- _eventListeners.tx.add(callback);
169
- if (_unbroadcastedEvents.tx.length > 0) {
170
- const events = _unbroadcastedEvents.tx;
171
- _unbroadcastedEvents.tx = [];
172
- events.forEach(notifyTxListeners);
173
- }
174
- }, "onTx");
175
173
  const getConfig = /* @__PURE__ */ __name(() => {
176
174
  return _config;
177
175
  }, "getConfig");
@@ -193,21 +191,17 @@ const resetTxHistory = /* @__PURE__ */ __name(() => {
193
191
  WIDGET_URL,
194
192
  _adapter,
195
193
  _config,
196
- _eventListeners,
197
194
  _state,
198
195
  _txHistory,
199
196
  _unbroadcastedEvents,
197
+ events,
200
198
  getConfig,
201
199
  getTxHistory,
202
200
  getWalletAdapterState,
203
- notifyAccountListeners,
204
- notifyTxListeners,
205
- onAccount,
206
201
  onAdapterStateUpdate,
207
- onTx,
208
202
  resetTxHistory,
209
203
  setConfig,
210
- updateState,
204
+ update,
211
205
  updateTxHistory
212
206
  });
213
207
  //# sourceMappingURL=state.cjs.map
@@ -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://wallet-adapter.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 updateState({\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// Event listeners\nexport const _eventListeners: EventListeners = {\n account: new Set(),\n tx: new Set(),\n};\n\nexport const _unbroadcastedEvents: UnbroadcastedEvents = {\n account: [],\n tx: [],\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 updateState = (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 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 notifyTxListeners(_txHistory[txId]);\n}\n\n\n\n// Event Notifiers\nexport const notifyAccountListeners = (accountId: string) => {\n if (_eventListeners.account.size === 0) {\n _unbroadcastedEvents.account.push(accountId);\n return;\n }\n _eventListeners.account.forEach((callback) => {\n try {\n callback(accountId);\n } catch (e) {\n console.error(e);\n }\n });\n}\n\nexport const notifyTxListeners = (tx: TxStatus) => {\n if (_eventListeners.tx.size === 0) {\n _unbroadcastedEvents.tx.push(tx);\n return;\n }\n _eventListeners.tx.forEach((callback) => {\n try {\n callback(tx);\n } catch (e) {\n console.error(e);\n }\n });\n}\n\n// Event Handlers\nexport const onAccount = (callback: (accountId: string) => void) => {\n _eventListeners.account.add(callback);\n if (_unbroadcastedEvents.account.length > 0) {\n const events = _unbroadcastedEvents.account;\n _unbroadcastedEvents.account = [];\n events.forEach(notifyAccountListeners);\n }\n};\n\nexport const onTx = (callback: (tx: TxStatus) => void): void => {\n _eventListeners.tx.add(callback);\n if (_unbroadcastedEvents.tx.length > 0) {\n const events = _unbroadcastedEvents.tx;\n _unbroadcastedEvents.tx = [];\n events.forEach(notifyTxListeners);\n }\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;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,oBAAM,QAAQ,KAAK;AAAA,EACrD,GAAG,SAAS,kBAAkB;AAChC;AAGO,IAAI,aAAmB,oBAAM,OAAO,KAAK,CAAC;AAG1C,MAAM,uBAAuB,wBAAC,UAA8B;AACjE,UAAQ,IAAI,yBAAyB,KAAK;AAC1C,QAAM,EAAE,WAAW,cAAc,WAAW,IAAI;AAChD,cAAY;AAAA,IACV,WAAW,aAAa;AAAA,IACxB,cAAc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,wBAAC,aAA2C;AAC9D,kBAAgB,GAAG,IAAI,QAAQ;AAC/B,MAAI,qBAAqB,GAAG,SAAS,GAAG;AACtC,UAAM,SAAS,qBAAqB;AACpC,yBAAqB,KAAK,CAAC;AAC3B,WAAO,QAAQ,iBAAiB;AAAA,EAClC;AACF,GAPoB;AASb,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: 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,SAAS;AAAA,EACX;AAAA,EACA,SAAS;AAAA,IACP,WAAW;AAAA,IACX,SAAS;AAAA,EACX;AACF;AAmDO,IAAI,cAAyB,oBAAM,QAAQ,KAAK;AAAA,EACrD,GAAG,SAAS,kBAAkB;AAChC;AAGO,IAAI,aAAmB,oBAAM,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;AAAA,IACrB,WAA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AAI;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,18 +1,5 @@
1
- import { WalletAdapter } from '@fastnear/wallet-adapter';
2
1
  import * as borsh from 'borsh';
3
2
 
4
- declare const WIDGET_URL = "https://wallet-adapter.fastnear.com";
5
- declare const DEFAULT_NETWORK_ID = "mainnet";
6
- declare const NETWORKS: {
7
- testnet: {
8
- networkId: string;
9
- nodeUrl: string;
10
- };
11
- mainnet: {
12
- networkId: string;
13
- nodeUrl: string;
14
- };
15
- };
16
3
  interface NetworkConfig {
17
4
  networkId: string;
18
5
  nodeUrl?: string;
@@ -21,63 +8,42 @@ interface NetworkConfig {
21
8
  explorerUrl?: string;
22
9
  [key: string]: any;
23
10
  }
24
- interface AppState {
25
- accountId?: string | null;
26
- privateKey?: string | null;
27
- lastWalletId?: string | null;
28
- publicKey?: string | null;
29
- accessKeyContractId?: string | null;
30
- [key: string]: any;
31
- }
32
11
  interface TxStatus {
33
12
  txId: string;
34
13
  updateTimestamp?: number;
35
14
  [key: string]: any;
36
15
  }
37
16
  type TxHistory = Record<string, TxStatus>;
38
- interface EventListeners {
39
- account: Set<(accountId: string) => void>;
40
- tx: Set<(tx: TxStatus) => void>;
41
- }
42
- interface UnbroadcastedEvents {
43
- account: string[];
44
- tx: TxStatus[];
45
- }
46
- interface WalletAdapterState {
47
- publicKey?: string | null;
48
- privateKey?: string | null;
49
- accountId?: string | null;
50
- lastWalletId?: string | null;
51
- networkId: string;
52
- }
53
- declare let _config: NetworkConfig;
54
- declare let _state: AppState;
55
- declare const onAdapterStateUpdate: (state: WalletAdapterState) => void;
56
- declare const getWalletAdapterState: () => WalletAdapterState;
57
- declare let _adapter: WalletAdapter;
58
- declare let _txHistory: TxHistory;
59
- declare const _eventListeners: EventListeners;
60
- declare const _unbroadcastedEvents: UnbroadcastedEvents;
61
- declare const updateState: (newState: Partial<AppState>) => void;
62
- declare const updateTxHistory: (txStatus: TxStatus) => void;
63
- declare const notifyAccountListeners: (accountId: string) => void;
64
- declare const notifyTxListeners: (tx: TxStatus) => void;
65
- declare const onAccount: (callback: (accountId: string) => void) => void;
66
- declare const onTx: (callback: (tx: TxStatus) => void) => void;
67
- declare const getConfig: () => NetworkConfig;
68
- declare const getTxHistory: () => TxHistory;
69
- declare const setConfig: (newConf: NetworkConfig) => void;
70
- declare const resetTxHistory: () => void;
71
17
 
72
18
  declare const MaxBlockDelayMs: number;
73
19
  interface AccessKeyWithError {
74
- nonce: number;
75
- permission?: any;
20
+ result: {
21
+ nonce: number;
22
+ permission?: any;
23
+ error?: string;
24
+ };
25
+ }
26
+ interface WalletTxResult {
27
+ url?: string;
28
+ outcomes?: Array<{
29
+ transaction: {
30
+ hash: string;
31
+ };
32
+ }>;
33
+ rejected?: boolean;
76
34
  error?: string;
77
35
  }
78
36
  interface BlockView {
37
+ result: {
38
+ header: {
39
+ hash: string;
40
+ timestamp_nanosec: string;
41
+ };
42
+ };
43
+ }
44
+ interface LastKnownBlock {
79
45
  header: {
80
- prev_hash: string;
46
+ hash: string;
81
47
  timestamp_nanosec: string;
82
48
  };
83
49
  }
@@ -110,6 +76,17 @@ declare const accountId: () => string | null | undefined;
110
76
  declare const publicKey: () => string | null | undefined;
111
77
  declare const config: (newConfig?: Record<string, any>) => NetworkConfig;
112
78
  declare const authStatus: () => string | Record<string, any>;
79
+ declare const getPublicKeyForContract: (opts?: any) => string | null | undefined;
80
+ declare const selected: () => {
81
+ network: string;
82
+ nodeUrl: string | undefined;
83
+ walletUrl: string | undefined;
84
+ helperUrl: string | undefined;
85
+ explorerUrl: string | undefined;
86
+ account: string | null | undefined;
87
+ contract: string | null | undefined;
88
+ publicKey: string | null | undefined;
89
+ };
113
90
  declare const requestSignIn: ({ contractId }: {
114
91
  contractId: string;
115
92
  }) => Promise<void>;
@@ -177,6 +154,8 @@ declare const exp: {
177
154
  };
178
155
  };
179
156
  declare const utils: {};
157
+ declare const state: {};
158
+ declare const event: any;
180
159
  declare const actions: {
181
160
  functionCall: ({ methodName, gas, deposit, args, argsBase64, }: {
182
161
  methodName: string;
@@ -251,4 +230,4 @@ declare const actions: {
251
230
  };
252
231
  };
253
232
 
254
- export { type AccessKeyView, type AppState, DEFAULT_NETWORK_ID, type EventListeners, MaxBlockDelayMs, NETWORKS, type NetworkConfig, type TxHistory, type TxStatus, type UnbroadcastedEvents, WIDGET_URL, type WalletAdapterState, _adapter, _config, _eventListeners, _state, _txHistory, _unbroadcastedEvents, accountId, actions, afterTxSent, authStatus, config, exp, generateTxId, getConfig, getTxHistory, getWalletAdapterState, localTxHistory, notifyAccountListeners, notifyTxListeners, onAccount, onAdapterStateUpdate, onTx, publicKey, queryAccessKey, queryAccount, queryBlock, queryTx, requestSignIn, resetTxHistory, sendRpc, sendTx, sendTxToRpc, setConfig, signOut, updateState, updateTxHistory, utils, view, withBlockId };
233
+ export { type AccessKeyView, type AccessKeyWithError, type BlockView, type LastKnownBlock, MaxBlockDelayMs, type WalletTxResult, accountId, actions, afterTxSent, authStatus, config, event, exp, generateTxId, getPublicKeyForContract, localTxHistory, publicKey, queryAccessKey, queryAccount, queryBlock, queryTx, requestSignIn, selected, sendRpc, sendTx, sendTxToRpc, signOut, state, utils, view, withBlockId };
package/dist/esm/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  /* ⋈ 🏃🏻💨 FastNEAR API - ESM (@fastnear/api version 0.8.0) */
2
2
  /* https://www.npmjs.com/package/@fastnear/api/v/0.8.0 */
3
- export * from "./state.js";
4
3
  export * from "./near.js";
5
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// See tsup.config.ts for additional banner/footer js\nexport * from \"./state.js\"\nexport * from \"./near.js\";\n"],"mappings":";;AACA,cAAc;AACd,cAAc;","names":[]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// See tsup.config.ts for additional banner/footer js\nexport * from \"./near.js\";\n"],"mappings":";;AACA,cAAc;","names":[]}
package/dist/esm/near.js CHANGED
@@ -25,7 +25,7 @@ import {
25
25
  DEFAULT_NETWORK_ID,
26
26
  NETWORKS,
27
27
  getTxHistory,
28
- updateState,
28
+ update,
29
29
  updateTxHistory
30
30
  } from "./state.js";
31
31
  import {
@@ -35,6 +35,7 @@ import {
35
35
  } from "./state.js";
36
36
  import { sha256 } from "@noble/hashes/sha2";
37
37
  import * as reExportAllUtils from "@fastnear/utils";
38
+ import * as stateExports from "./state.js";
38
39
  Big.DP = 27;
39
40
  const MaxBlockDelayMs = 1e3 * 60 * 60 * 6;
40
41
  function withBlockId(params, blockId) {
@@ -125,7 +126,7 @@ const config = /* @__PURE__ */ __name((newConfig) => {
125
126
  if (newConfig) {
126
127
  if (newConfig.networkId && current.networkId !== newConfig.networkId) {
127
128
  setConfig(newConfig.networkId);
128
- updateState({ accountId: null, privateKey: null, lastWalletId: null });
129
+ update({ accountId: null, privateKey: null, lastWalletId: null });
129
130
  lsSet("block", null);
130
131
  resetTxHistory();
131
132
  }
@@ -139,9 +140,32 @@ const authStatus = /* @__PURE__ */ __name(() => {
139
140
  }
140
141
  return "SignedIn";
141
142
  }, "authStatus");
143
+ const getPublicKeyForContract = /* @__PURE__ */ __name((opts) => {
144
+ return publicKey();
145
+ }, "getPublicKeyForContract");
146
+ const selected = /* @__PURE__ */ __name(() => {
147
+ const network = getConfig().networkId;
148
+ const nodeUrl = getConfig().nodeUrl;
149
+ const walletUrl = getConfig().walletUrl;
150
+ const helperUrl = getConfig().helperUrl;
151
+ const explorerUrl = getConfig().explorerUrl;
152
+ const account = accountId();
153
+ const contract = _state.accessKeyContractId;
154
+ const publicKey2 = getPublicKeyForContract();
155
+ return {
156
+ network,
157
+ nodeUrl,
158
+ walletUrl,
159
+ helperUrl,
160
+ explorerUrl,
161
+ account,
162
+ contract,
163
+ publicKey: publicKey2
164
+ };
165
+ }, "selected");
142
166
  const requestSignIn = /* @__PURE__ */ __name(async ({ contractId }) => {
143
167
  const privateKey = privateKeyFromRandom();
144
- updateState({ accessKeyContractId: contractId, accountId: null, privateKey });
168
+ update({ accessKeyContractId: contractId, accountId: null, privateKey });
145
169
  const pubKey = publicKeyFromPrivate(privateKey);
146
170
  const result = await _adapter.signIn({
147
171
  networkId: getConfig().networkId,
@@ -158,7 +182,7 @@ const requestSignIn = /* @__PURE__ */ __name(async ({ contractId }) => {
158
182
  }, 100);
159
183
  }
160
184
  } else if (result.accountId) {
161
- updateState({ accountId: result.accountId });
185
+ update({ accountId: result.accountId });
162
186
  }
163
187
  }, "requestSignIn");
164
188
  const view = /* @__PURE__ */ __name(async ({
@@ -215,7 +239,7 @@ const localTxHistory = /* @__PURE__ */ __name(() => {
215
239
  return getTxHistory();
216
240
  }, "localTxHistory");
217
241
  const signOut = /* @__PURE__ */ __name(() => {
218
- updateState({ accountId: null, privateKey: null, contractId: null });
242
+ update({ accountId: null, privateKey: null, contractId: null });
219
243
  setConfig(NETWORKS[DEFAULT_NETWORK_ID]);
220
244
  }, "signOut");
221
245
  const sendTx = /* @__PURE__ */ __name(async ({
@@ -233,6 +257,14 @@ const sendTx = /* @__PURE__ */ __name(async ({
233
257
  updateTxHistory({ status: "Pending", txId, tx: jsonTx, finalState: false });
234
258
  const url = new URL(typeof window !== "undefined" ? window.location.href : "");
235
259
  url.searchParams.set("txIds", txId);
260
+ const existingParams = new URLSearchParams(window.location.search);
261
+ existingParams.forEach((value, key) => {
262
+ if (!url.searchParams.has(key)) {
263
+ url.searchParams.set(key, value);
264
+ }
265
+ });
266
+ url.searchParams.delete("errorCode");
267
+ url.searchParams.delete("errorMessage");
236
268
  try {
237
269
  const result = await _adapter.sendTransactions({
238
270
  transactions: [jsonTx],
@@ -279,10 +311,10 @@ const sendTx = /* @__PURE__ */ __name(async ({
279
311
  let nonce = lsGet("nonce");
280
312
  if (nonce == null) {
281
313
  const accessKey = await queryAccessKey({ accountId: signerId, publicKey: publicKey2 });
282
- if (accessKey.error) {
283
- throw new Error(`Access key error: ${accessKey.error} when attempting to get nonce for ${signerId} for public key ${publicKey2}`);
314
+ if (accessKey.result.error) {
315
+ throw new Error(`Access key error: ${accessKey.result.error} when attempting to get nonce for ${signerId} for public key ${publicKey2}`);
284
316
  }
285
- nonce = accessKey.nonce;
317
+ nonce = accessKey.result.nonce;
286
318
  lsSet("nonce", nonce);
287
319
  }
288
320
  let lastKnownBlock = lsGet("block");
@@ -290,15 +322,15 @@ const sendTx = /* @__PURE__ */ __name(async ({
290
322
  const latestBlock = await queryBlock({ blockId: "final" });
291
323
  lastKnownBlock = {
292
324
  header: {
293
- prev_hash: latestBlock.header.prev_hash,
294
- timestamp_nanosec: latestBlock.header.timestamp_nanosec
325
+ hash: latestBlock.result.header.hash,
326
+ timestamp_nanosec: latestBlock.result.header.timestamp_nanosec
295
327
  }
296
328
  };
297
329
  lsSet("block", lastKnownBlock);
298
330
  }
299
331
  nonce += 1;
300
332
  lsSet("nonce", nonce);
301
- const blockHash = lastKnownBlock.header.prev_hash;
333
+ const blockHash = lastKnownBlock.header.hash;
302
334
  const plainTransactionObj = {
303
335
  signerId,
304
336
  publicKey: publicKey2,
@@ -338,6 +370,12 @@ for (const key in reExportAllUtils) {
338
370
  exp.utils[key] = reExportAllUtils[key];
339
371
  }
340
372
  const utils = exp.utils;
373
+ const state = {};
374
+ for (const key in stateExports) {
375
+ state[key] = stateExports[key];
376
+ }
377
+ const event = state["events"];
378
+ delete state["events"];
341
379
  try {
342
380
  if (typeof window !== "undefined") {
343
381
  const url = new URL(window.location.href);
@@ -345,16 +383,22 @@ try {
345
383
  const pubKey = url.searchParams.get("public_key");
346
384
  const errCode = url.searchParams.get("errorCode");
347
385
  const errMsg = url.searchParams.get("errorMessage");
386
+ const decodedErrMsg = errMsg ? decodeURIComponent(errMsg) : null;
348
387
  const txHashes = url.searchParams.get("transactionHashes");
349
388
  const txIds = url.searchParams.get("txIds");
350
389
  if (errCode || errMsg) {
351
- console.warn(new Error(`Wallet error: ${errCode} ${errMsg}`));
390
+ console.warn(new Error(`Wallet raises:
391
+ code: ${errCode}
392
+ message: ${decodedErrMsg}`));
352
393
  }
353
394
  if (accId && pubKey) {
354
395
  if (pubKey === _state.publicKey) {
355
- updateState({ accountId: accId });
396
+ update({ accountId: accId });
356
397
  } else {
357
- console.error(new Error("Public key mismatch from wallet redirect"), pubKey, _state.publicKey);
398
+ if (authStatus() === "SignedIn") {
399
+ console.warn("Public key mismatch from wallet redirect", pubKey, _state.publicKey);
400
+ }
401
+ url.searchParams.delete("public_key");
358
402
  }
359
403
  }
360
404
  if (txHashes || txIds) {
@@ -378,14 +422,6 @@ try {
378
422
  console.error(new Error("Transaction hash mismatch from wallet redirect"), idArr, hashArr);
379
423
  }
380
424
  }
381
- url.searchParams.delete("account_id");
382
- url.searchParams.delete("public_key");
383
- url.searchParams.delete("errorCode");
384
- url.searchParams.delete("errorMessage");
385
- url.searchParams.delete("all_keys");
386
- url.searchParams.delete("transactionHashes");
387
- url.searchParams.delete("txIds");
388
- window.history.replaceState({}, "", url.toString());
389
425
  }
390
426
  } catch (e) {
391
427
  console.error("Error handling wallet redirect:", e);
@@ -457,8 +493,10 @@ export {
457
493
  afterTxSent,
458
494
  authStatus,
459
495
  config,
496
+ event,
460
497
  exp,
461
498
  generateTxId,
499
+ getPublicKeyForContract,
462
500
  localTxHistory,
463
501
  publicKey,
464
502
  queryAccessKey,
@@ -466,10 +504,12 @@ export {
466
504
  queryBlock,
467
505
  queryTx,
468
506
  requestSignIn,
507
+ selected,
469
508
  sendRpc,
470
509
  sendTx,
471
510
  sendTxToRpc,
472
511
  signOut,
512
+ state,
473
513
  utils,
474
514
  view,
475
515
  withBlockId