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.

148 lines (147 loc) 6.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "NearRawProvider", { enumerable: true, get: function() { return NearRawProvider; } }); const _core = require("@near-wallet-selector/core"); const _mynearwallet = require("@near-wallet-selector/my-near-wallet"); const _shared = require("@rarimo/shared"); const _nearapijs = require("near-api-js"); const _helpers = require("./helpers"); const _types = require("./types"); 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 _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } let NearRawProvider = class NearRawProvider { init() { var _this = this; return _async_to_generator(function*() { _this.selector = yield (0, _core.setupWalletSelector)({ network: _shared.NEAR_CHAIN_IDS[_shared.ChainNames.NearTestNet], modules: [ (0, _mynearwallet.setupMyNearWallet)() ] }); const isSignedIn = _this.selector.isSignedIn(); if (isSignedIn) { var _this_selector_store_getState, _this_selector, _this_selector_store, _this_selector_store_getState_accounts, _this_selector_store_getState_accounts_; _this.wallet = yield _this.selector.wallet(); var _this_selector_store_getState_accounts__accountId; _this.accountId = (_this_selector_store_getState_accounts__accountId = (_this_selector_store_getState = (_this_selector = _this.selector) === null || _this_selector === void 0 ? void 0 : (_this_selector_store = _this_selector.store) === null || _this_selector_store === void 0 ? void 0 : _this_selector_store.getState()) === null || _this_selector_store_getState === void 0 ? void 0 : (_this_selector_store_getState_accounts = _this_selector_store_getState.accounts) === null || _this_selector_store_getState_accounts === void 0 ? void 0 : (_this_selector_store_getState_accounts_ = _this_selector_store_getState_accounts[0]) === null || _this_selector_store_getState_accounts_ === void 0 ? void 0 : _this_selector_store_getState_accounts_.accountId) !== null && _this_selector_store_getState_accounts__accountId !== void 0 ? _this_selector_store_getState_accounts__accountId : ''; } return isSignedIn; })(); } signIn() { var _this = this; return _async_to_generator(function*() { if (!_this.selector || Boolean(_this.accountId)) return; _this.wallet = yield _this.selector.wallet(_types.ENearWalletId.MyNearWallet); yield _this.wallet.signIn({ contractId: _this.createAccessKeyFor, methodNames: [], accounts: [] }); })(); } signOut() { var _this = this; return _async_to_generator(function*() { if (!_this.wallet) return; yield _this.wallet.signOut(); _this.wallet = null; _this.accountId = ''; })(); } // Call a method that changes the contract's state signAndSendTx({ contractId , method , args ={} , gas =_helpers.MAX_GAS_LIMIT , deposit =_helpers.NO_DEPOSIT }) { var _this = this; return _async_to_generator(function*() { if (!_this.wallet) return; // Sign a transaction with the "FunctionCall" action const outcome = yield _this.wallet.signAndSendTransaction({ signerId: _this.accountId, receiverId: contractId, actions: [ { type: 'FunctionCall', params: { methodName: method, args, gas, deposit } } ] }); return outcome ? _nearapijs.providers.getTransactionLastResult(outcome) : null; })(); } // Get transaction result from the network getTransactionResult(txhash) { var _this = this; return _async_to_generator(function*() { if (!_this.selector) return; const { network } = _this.selector.options; const provider = new _nearapijs.providers.JsonRpcProvider({ url: network.nodeUrl }); // Retrieve transaction result from the network const transaction = yield provider.txStatus(txhash, 'unnused'); return _nearapijs.providers.getTransactionLastResult(transaction); })(); } constructor({ createAccessKeyFor ='' }){ _define_property(this, "selector", null); _define_property(this, "wallet", null); _define_property(this, "createAccessKeyFor", void 0); _define_property(this, "accountId", ''); _define_property(this, "isNear", true); this.createAccessKeyFor = createAccessKeyFor; } }; //# sourceMappingURL=near-raw-provider.js.map