UNPKG

@rarimo/providers-near

Version:

Features of the Rarimo SDK that provide access to wallets and the ability to interact with them on the NEAR blockchain.

196 lines (195 loc) 7.89 kB
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _async_to_generator(fn) { return function() { var self = this, args = arguments; return new Promise(function(resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _check_private_redeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } function _class_apply_descriptor_get(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } function _class_apply_descriptor_set(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } } function _class_extract_field_descriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); } function _class_private_field_get(receiver, privateMap) { var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get"); return _class_apply_descriptor_get(receiver, descriptor); } function _class_private_field_init(obj, privateMap, value) { _check_private_redeclaration(obj, privateMap); privateMap.set(obj, value); } function _class_private_field_set(receiver, privateMap, value) { var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set"); _class_apply_descriptor_set(receiver, descriptor, value); return value; } function _class_private_method_get(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; } function _class_private_method_init(obj, privateSet) { _check_private_redeclaration(obj, privateSet); privateSet.add(obj); } import { ProviderEventBus, ProviderEventBusEvents, Providers } from "@rarimo/provider"; import { ChainNames, NEAR_CHAIN_IDS } from "@rarimo/shared"; import { getNearExplorerAddressUrl, getNearExplorerTxUrl, handleNearError } from "./helpers"; import { NearRawProvider } from "./near-raw-provider"; var _provider = new WeakMap(), _chainId = new WeakMap(), _address = new WeakMap(), _updateProviderState = new WeakSet(), _emitEvent = new WeakSet(); export class NearProvider extends ProviderEventBus { static get providerType() { return Providers.Near; } get isConnected() { return Boolean(_class_private_field_get(this, _chainId) && _class_private_field_get(this, _address)); } get chainId() { return _class_private_field_get(this, _chainId); } get address() { return _class_private_field_get(this, _address); } init() { var _this = this; return _async_to_generator(function*() { try { yield _class_private_field_get(_this, _provider).init(); _class_private_method_get(_this, _updateProviderState, updateProviderState).call(_this); _class_private_method_get(_this, _emitEvent, emitEvent).call(_this, ProviderEventBusEvents.Initiated); } catch (error) { handleNearError(error); } })(); } switchChain(chainId) { var _this = this; return _async_to_generator(function*() { _class_private_field_set(_this, _chainId, chainId); _class_private_method_get(_this, _emitEvent, emitEvent).call(_this, ProviderEventBusEvents.ChainChanged); })(); } connect() { var _this = this; return _async_to_generator(function*() { try { yield _class_private_field_get(_this, _provider).signIn(); yield _class_private_method_get(_this, _updateProviderState, updateProviderState).call(_this); _class_private_method_get(_this, _emitEvent, emitEvent).call(_this, ProviderEventBusEvents.Connect); } catch (error) { handleNearError(error); } })(); } disconnect() { var _this = this; return _async_to_generator(function*() { try { yield _class_private_field_get(_this, _provider).signOut(); _class_private_method_get(_this, _updateProviderState, updateProviderState).call(_this); _class_private_method_get(_this, _emitEvent, emitEvent).call(_this, ProviderEventBusEvents.Disconnect); } catch (error) { handleNearError(error); } })(); } getHashFromTxResponse(txResponse) { const transactionResponse = txResponse; return transactionResponse.transaction.hash; } getTxUrl(chain, txHash) { return getNearExplorerTxUrl(chain, txHash); } getAddressUrl(chain, address) { return getNearExplorerAddressUrl(chain, address); } signAndSendTx(txRequestBody) { var _this = this; return _async_to_generator(function*() { try { return yield _class_private_field_get(_this, _provider).signAndSendTx(txRequestBody); } catch (error) { handleNearError(error); } })(); } /** * @description In most cases, instead of using this constructor, pass the NearProvider class to {@link @rarimo/provider!createProvider}. */ constructor(){ super(); _class_private_method_init(this, _updateProviderState); _class_private_method_init(this, _emitEvent); _class_private_field_init(this, _provider, { writable: true, value: void 0 }); _class_private_field_init(this, _chainId, { writable: true, value: void 0 }); _class_private_field_init(this, _address, { writable: true, value: void 0 }); _class_private_field_set(this, _provider, _class_private_field_set(this, _provider, new NearRawProvider({}))); } } /*#__PURE__*/ /*#__PURE__*/ /*#__PURE__*/ /*#__PURE__*/ /*#__PURE__*/ function updateProviderState() { var _class_private_field_get_selector, _class_private_field_get1; const networkId = (_class_private_field_get_selector = _class_private_field_get(this, _provider).selector) === null || _class_private_field_get_selector === void 0 ? void 0 : _class_private_field_get_selector.options.network.networkId; _class_private_field_set(this, _address, ((_class_private_field_get1 = _class_private_field_get(this, _provider)) === null || _class_private_field_get1 === void 0 ? void 0 : _class_private_field_get1.accountId) || ''); _class_private_field_set(this, _chainId, networkId || NEAR_CHAIN_IDS[ChainNames.NearTestNet]); } function emitEvent(event) { this.emit(event, { address: _class_private_field_get(this, _address), chainId: _class_private_field_get(this, _chainId), isConnected: this.isConnected }); } //# sourceMappingURL=near-provider.js.map