@fastnear/api
Version:
Interact with NEAR Protocol blockchain including transaction signing, utilities, and more.
282 lines • 9.59 kB
JavaScript
/* ⋈ 🏃🏻💨 FastNear API - ESM (@fastnear/api version 1.2.0) */
/* https://www.npmjs.com/package/@fastnear/api/v/1.2.0 */
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import {
lsSet,
lsGet,
publicKeyFromPrivate
} from "@fastnear/utils";
function mergeServiceConfig(base, override) {
return {
...base || {},
...override || {}
};
}
__name(mergeServiceConfig, "mergeServiceConfig");
function mergeServices(base, override) {
return {
rpc: mergeServiceConfig(base?.rpc, override?.rpc),
archival: mergeServiceConfig(base?.archival, override?.archival),
api: mergeServiceConfig(base?.api, override?.api),
tx: mergeServiceConfig(base?.tx, override?.tx),
transfers: mergeServiceConfig(base?.transfers, override?.transfers),
neardata: mergeServiceConfig(base?.neardata, override?.neardata),
fastdata: {
...base?.fastdata || {},
...override?.fastdata || {}
}
};
}
__name(mergeServices, "mergeServices");
function normalizeNetworkId(networkId) {
return networkId === "testnet" ? "testnet" : "mainnet";
}
__name(normalizeNetworkId, "normalizeNetworkId");
function normalizeApiKey(apiKey) {
if (typeof apiKey !== "string") {
return null;
}
const trimmed = apiKey.trim();
return trimmed ? trimmed : null;
}
__name(normalizeApiKey, "normalizeApiKey");
const DEFAULT_NETWORK_ID = "mainnet";
const NETWORKS = {
testnet: {
networkId: "testnet",
nodeUrl: "https://rpc.testnet.fastnear.com/",
services: {
rpc: { baseUrl: "https://rpc.testnet.fastnear.com/" },
archival: { baseUrl: "https://archival-rpc.testnet.near.org/" },
api: { baseUrl: "https://test.api.fastnear.com" },
tx: { baseUrl: "https://tx.test.fastnear.com" },
transfers: { baseUrl: null },
neardata: { baseUrl: "https://testnet.neardata.xyz" },
fastdata: { kvBaseUrl: "https://kv.test.fastnear.com" }
}
},
mainnet: {
networkId: "mainnet",
nodeUrl: "https://rpc.mainnet.fastnear.com/",
services: {
rpc: { baseUrl: "https://rpc.mainnet.fastnear.com/" },
archival: { baseUrl: "https://archival-rpc.mainnet.near.org/" },
api: { baseUrl: "https://api.fastnear.com" },
tx: { baseUrl: "https://tx.main.fastnear.com" },
transfers: { baseUrl: "https://transfers.main.fastnear.com" },
neardata: { baseUrl: "https://mainnet.neardata.xyz" },
fastdata: { kvBaseUrl: "https://kv.main.fastnear.com" }
}
}
};
function resolveConfig(input, base) {
const requested = input || {};
const baseConfig = base || NETWORKS[DEFAULT_NETWORK_ID];
const networkId = normalizeNetworkId(requested.networkId ?? baseConfig.networkId);
const networkDefaults = NETWORKS[networkId];
const services = mergeServices(
mergeServices(networkDefaults.services, baseConfig.services),
requested.services
);
const requestedRpcBaseUrl = requested.services?.rpc?.baseUrl;
const requestedNodeUrl = requested.nodeUrl;
const rpcBaseUrl = requestedRpcBaseUrl ?? requestedNodeUrl ?? services.rpc?.baseUrl ?? networkDefaults.nodeUrl ?? null;
const nodeUrl = requestedNodeUrl ?? rpcBaseUrl ?? networkDefaults.nodeUrl;
services.rpc = {
...services.rpc || {},
baseUrl: rpcBaseUrl
};
const next = {
...networkDefaults,
...baseConfig,
...requested,
networkId,
nodeUrl,
services
};
if (Object.prototype.hasOwnProperty.call(requested, "apiKey")) {
next.apiKey = normalizeApiKey(requested.apiKey);
} else {
next.apiKey = normalizeApiKey(baseConfig.apiKey);
}
return next;
}
__name(resolveConfig, "resolveConfig");
let _config = resolveConfig(lsGet("config"));
const _networkStates = {
mainnet: lsGet("state.mainnet") ?? {},
testnet: lsGet("state.testnet") ?? {}
};
function persistShape(slot) {
return {
accountId: slot.accountId,
privateKey: slot.privateKey,
lastWalletId: slot.lastWalletId,
accessKeyContractId: slot.accessKeyContractId
};
}
__name(persistShape, "persistShape");
const _legacyState = lsGet("state");
if (_legacyState && Object.keys(_legacyState).length > 0) {
_networkStates.mainnet = { ..._networkStates.mainnet, ..._legacyState };
lsSet("state.mainnet", persistShape(_networkStates.mainnet));
lsSet("state", null);
}
const _legacyNonce = lsGet("nonce");
if (_legacyNonce !== null && _legacyNonce !== void 0) {
lsSet("nonce.mainnet", _legacyNonce);
lsSet("nonce", null);
}
const _legacyBlock = lsGet("block");
if (_legacyBlock) {
lsSet("block.mainnet", _legacyBlock);
lsSet("block", null);
}
let _activeNetwork = normalizeNetworkId(_config.networkId);
let _state = _networkStates[_activeNetwork];
let _walletProvider = null;
const setWalletProvider = /* @__PURE__ */ __name((provider) => {
_walletProvider = provider;
}, "setWalletProvider");
const getWalletProvider = /* @__PURE__ */ __name(() => {
return _walletProvider;
}, "getWalletProvider");
for (const network of ["mainnet", "testnet"]) {
try {
const slot = _networkStates[network];
slot.publicKey = slot.privateKey ? publicKeyFromPrivate(slot.privateKey) : null;
} catch (e) {
console.error(`Error parsing private key for ${network}:`, e);
_networkStates[network].privateKey = null;
_networkStates[network].publicKey = null;
lsSet(`nonce.${network}`, null);
}
}
let _txHistory = lsGet("txHistory") || {};
const _unbroadcastedEvents = {
account: [],
tx: []
};
const events = {
_eventListeners: {
account: /* @__PURE__ */ new Set(),
tx: /* @__PURE__ */ new Set()
},
notifyAccountListeners: /* @__PURE__ */ __name((accountId) => {
if (events._eventListeners.account.size === 0) {
_unbroadcastedEvents.account.push(accountId);
return;
}
events._eventListeners.account.forEach((callback) => {
try {
callback(accountId);
} catch (e) {
console.error(e);
}
});
}, "notifyAccountListeners"),
notifyTxListeners: /* @__PURE__ */ __name((tx) => {
if (events._eventListeners.tx.size === 0) {
_unbroadcastedEvents.tx.push(tx);
return;
}
events._eventListeners.tx.forEach((callback) => {
try {
callback(tx);
} catch (e) {
console.error(e);
}
});
}, "notifyTxListeners"),
onAccount: /* @__PURE__ */ __name((callback) => {
events._eventListeners.account.add(callback);
if (_unbroadcastedEvents.account.length > 0) {
const accountEvent = _unbroadcastedEvents.account;
_unbroadcastedEvents.account = [];
accountEvent.forEach(events.notifyAccountListeners);
}
}, "onAccount"),
onTx: /* @__PURE__ */ __name((callback) => {
events._eventListeners.tx.add(callback);
if (_unbroadcastedEvents.tx.length > 0) {
const txEvent = _unbroadcastedEvents.tx;
_unbroadcastedEvents.tx = [];
txEvent.forEach(events.notifyTxListeners);
}
}, "onTx")
};
const updateAccountState = /* @__PURE__ */ __name((partial, network) => {
const target = network ?? _activeNetwork;
const oldSlot = _networkStates[target];
const newSlot = { ...oldSlot, ...partial };
if (Object.prototype.hasOwnProperty.call(partial, "privateKey") && newSlot.privateKey !== oldSlot.privateKey) {
newSlot.publicKey = newSlot.privateKey ? publicKeyFromPrivate(newSlot.privateKey) : null;
lsSet(`nonce.${target}`, null);
}
_networkStates[target] = newSlot;
if (target === _activeNetwork) _state = newSlot;
lsSet(`state.${target}`, persistShape(newSlot));
if (target === _activeNetwork && newSlot.accountId !== oldSlot.accountId) {
events.notifyAccountListeners(newSlot.accountId);
}
}, "updateAccountState");
const getAccountState = /* @__PURE__ */ __name((network) => _networkStates[network ?? _activeNetwork], "getAccountState");
const getActiveNetwork = /* @__PURE__ */ __name(() => _activeNetwork, "getActiveNetwork");
const setActiveNetwork = /* @__PURE__ */ __name((network) => {
_activeNetwork = normalizeNetworkId(network);
_state = _networkStates[_activeNetwork];
}, "setActiveNetwork");
const update = /* @__PURE__ */ __name((newState) => {
updateAccountState(newState, _activeNetwork);
}, "update");
const updateTxHistory = /* @__PURE__ */ __name((txStatus) => {
const txId = txStatus.txId;
_txHistory[txId] = {
..._txHistory[txId] || {},
...txStatus,
updateTimestamp: Date.now()
};
lsSet("txHistory", _txHistory);
events.notifyTxListeners(_txHistory[txId]);
}, "updateTxHistory");
const getConfig = /* @__PURE__ */ __name(() => {
return _config;
}, "getConfig");
const getTxHistory = /* @__PURE__ */ __name(() => {
return _txHistory;
}, "getTxHistory");
const setConfig = /* @__PURE__ */ __name((newConf) => {
const partial = typeof newConf === "string" ? { networkId: newConf } : newConf;
const nextNetworkId = normalizeNetworkId(partial.networkId ?? _config.networkId);
const base = nextNetworkId !== _config.networkId ? NETWORKS[nextNetworkId] : _config;
_config = resolveConfig(partial, base);
lsSet("config", _config);
}, "setConfig");
const resetTxHistory = /* @__PURE__ */ __name(() => {
_txHistory = {};
lsSet("txHistory", _txHistory);
}, "resetTxHistory");
export {
DEFAULT_NETWORK_ID,
NETWORKS,
_config,
_state,
_txHistory,
_unbroadcastedEvents,
events,
getAccountState,
getActiveNetwork,
getConfig,
getTxHistory,
getWalletProvider,
resetTxHistory,
resolveConfig,
setActiveNetwork,
setConfig,
setWalletProvider,
update,
updateAccountState,
updateTxHistory
};
//# sourceMappingURL=state.js.map