@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.
206 lines (205 loc) • 8.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "NearProvider", {
enumerable: true,
get: function() {
return NearProvider;
}
});
const _provider = require("@rarimo/provider");
const _shared = require("@rarimo/shared");
const _helpers = require("./helpers");
const _nearrawprovider = require("./near-raw-provider");
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);
}
var _provider1 = new WeakMap(), _chainId = new WeakMap(), _address = new WeakMap(), _updateProviderState = new WeakSet(), _emitEvent = new WeakSet();
let NearProvider = class NearProvider extends _provider.ProviderEventBus {
static get providerType() {
return _provider.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, _provider1).init();
_class_private_method_get(_this, _updateProviderState, updateProviderState).call(_this);
_class_private_method_get(_this, _emitEvent, emitEvent).call(_this, _provider.ProviderEventBusEvents.Initiated);
} catch (error) {
(0, _helpers.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, _provider.ProviderEventBusEvents.ChainChanged);
})();
}
connect() {
var _this = this;
return _async_to_generator(function*() {
try {
yield _class_private_field_get(_this, _provider1).signIn();
yield _class_private_method_get(_this, _updateProviderState, updateProviderState).call(_this);
_class_private_method_get(_this, _emitEvent, emitEvent).call(_this, _provider.ProviderEventBusEvents.Connect);
} catch (error) {
(0, _helpers.handleNearError)(error);
}
})();
}
disconnect() {
var _this = this;
return _async_to_generator(function*() {
try {
yield _class_private_field_get(_this, _provider1).signOut();
_class_private_method_get(_this, _updateProviderState, updateProviderState).call(_this);
_class_private_method_get(_this, _emitEvent, emitEvent).call(_this, _provider.ProviderEventBusEvents.Disconnect);
} catch (error) {
(0, _helpers.handleNearError)(error);
}
})();
}
getHashFromTxResponse(txResponse) {
const transactionResponse = txResponse;
return transactionResponse.transaction.hash;
}
getTxUrl(chain, txHash) {
return (0, _helpers.getNearExplorerTxUrl)(chain, txHash);
}
getAddressUrl(chain, address) {
return (0, _helpers.getNearExplorerAddressUrl)(chain, address);
}
signAndSendTx(txRequestBody) {
var _this = this;
return _async_to_generator(function*() {
try {
return yield _class_private_field_get(_this, _provider1).signAndSendTx(txRequestBody);
} catch (error) {
(0, _helpers.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, _provider1, {
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, _provider1, _class_private_field_set(this, _provider1, new _nearrawprovider.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, _provider1).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, _provider1)) === null || _class_private_field_get1 === void 0 ? void 0 : _class_private_field_get1.accountId) || '');
_class_private_field_set(this, _chainId, networkId || _shared.NEAR_CHAIN_IDS[_shared.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