UNPKG

nanocurrency-rpc-bindings

Version:

JavaScript and TypeScript bindings for Nano currency RPC calls

127 lines 5.18 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getWork = exports.getDifficulty = exports.processBlock = exports.sendFunds = exports.getAccountRepresentative = exports.getAccountHistory = exports.getAccountInfo = exports.getAccountBalance = exports.getWalletBalances = exports.sendWalletRequest = void 0; const axios_1 = __importDefault(require("axios")); const rpc_types_1 = require("./rpc.types"); function sendWalletRequest(action, walletUrl) { return __awaiter(this, void 0, void 0, function* () { console.debug("Sending wallet action", action); const response = yield axios_1.default.post(walletUrl, action); if (response.status != 200) throw new Error(`Unknown response code from server: ${response.status} ${response.statusText}`); if (response.data.error) throw new Error(response.data.error); return response.data; }); } exports.sendWalletRequest = sendWalletRequest; function getWalletBalances(walletId, walletUrl) { return __awaiter(this, void 0, void 0, function* () { const payload = { action: rpc_types_1.WalletRpcAction.wallet_balances, wallet: walletId, }; return sendWalletRequest(payload, walletUrl); }); } exports.getWalletBalances = getWalletBalances; function getAccountBalance(account, walletUrl) { return __awaiter(this, void 0, void 0, function* () { const request = { action: rpc_types_1.NodeRpcAction.account_balance, account }; return sendWalletRequest(request, walletUrl); }); } exports.getAccountBalance = getAccountBalance; function getAccountInfo(account, walletUrl) { return __awaiter(this, void 0, void 0, function* () { const request = { action: rpc_types_1.NodeRpcAction.account_info, account, representative: true }; return sendWalletRequest(request, walletUrl); }); } exports.getAccountInfo = getAccountInfo; function getAccountHistory(account, walletUrl) { return __awaiter(this, void 0, void 0, function* () { const request = { action: rpc_types_1.NodeRpcAction.account_history, account, count: 1 }; return sendWalletRequest(request, walletUrl); }); } exports.getAccountHistory = getAccountHistory; function getAccountRepresentative(account, walletUrl) { return __awaiter(this, void 0, void 0, function* () { const request = { action: rpc_types_1.NodeRpcAction.account_representative, account }; return sendWalletRequest(request, walletUrl); }); } exports.getAccountRepresentative = getAccountRepresentative; function sendFunds(walletId, source, destination, amountRaw, walletUrl, txId) { return __awaiter(this, void 0, void 0, function* () { const request = { action: rpc_types_1.WalletRpcAction.send, wallet: walletId, source, destination, amount: amountRaw, id: txId, }; return sendWalletRequest(request, walletUrl); }); } exports.sendFunds = sendFunds; function processBlock(subtype, block, walletUrl) { return __awaiter(this, void 0, void 0, function* () { const request = { action: rpc_types_1.NodeRpcAction.process, json_block: true, subtype, block, }; return sendWalletRequest(request, walletUrl); }); } exports.processBlock = processBlock; function getDifficulty(walletUrl) { return __awaiter(this, void 0, void 0, function* () { const request = { action: rpc_types_1.NodeRpcAction.active_difficulty, }; return sendWalletRequest(request, walletUrl); }); } exports.getDifficulty = getDifficulty; function getWork(blockHash, walletUrl) { return __awaiter(this, void 0, void 0, function* () { const request = { action: rpc_types_1.NodeRpcAction.work_generate, hash: blockHash }; return sendWalletRequest(request, walletUrl); }); } exports.getWork = getWork; //# sourceMappingURL=rpc.js.map