@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.
138 lines (137 loc) • 5.85 kB
JavaScript
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;
}
import { setupWalletSelector } from "@near-wallet-selector/core";
import { setupMyNearWallet } from "@near-wallet-selector/my-near-wallet";
import { ChainNames, NEAR_CHAIN_IDS } from "@rarimo/shared";
import { providers } from "near-api-js";
import { MAX_GAS_LIMIT, NO_DEPOSIT } from "./helpers";
import { ENearWalletId } from "./types";
export class NearRawProvider {
init() {
var _this = this;
return _async_to_generator(function*() {
_this.selector = yield setupWalletSelector({
network: NEAR_CHAIN_IDS[ChainNames.NearTestNet],
modules: [
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(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 =MAX_GAS_LIMIT , deposit =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 ? 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 providers.JsonRpcProvider({
url: network.nodeUrl
});
// Retrieve transaction result from the network
const transaction = yield provider.txStatus(txhash, 'unnused');
return 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