UNPKG

@swtc/rpc

Version:
513 lines 22.9 kB
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()); }); }; import axios from "axios"; import { Factory as WalletFactory } from "@swtc/wallet"; import { Factory as TransactionFactory } from "@swtc/transaction"; import { RpcError } from "./errors"; const intercept_response = response => { if (response && response.data && response.data.result && response.data.result.status !== "success") { throw new RpcError(response.data.result); } return response; }; const Factory = (chain_or_wallet = WalletFactory("jingtum")) => { var _a; let Wallet; if (typeof chain_or_wallet === "function") { Wallet = chain_or_wallet; } else { Wallet = WalletFactory(chain_or_wallet); } if (!Wallet.hasOwnProperty("KeyPair")) { throw Error("transaction needs a Wallet class"); } const Transaction = TransactionFactory(Wallet); const utils = Transaction.utils; const FREEZE = { reserved: 20.0, each_freezed: 5.0 }; const intercept_request = config => { if (config.method !== "post") { throw new RpcError({ error: "validationError", error_code: -8888, error_message: "only post requests allowed for rpc." }); } if (config.data && typeof config.data === "object") { if (!config.data.hasOwnProperty("method")) { throw new RpcError({ error: "validationError", error_code: -8888, error_message: "method required for rpc." }); } if (config.data.hasOwnProperty("params")) { const params = config.data.params[0]; if (params && typeof params === "object" && params.hasOwnProperty("account")) { params.account = params.account.trim(); if (!Wallet.isValidAddress(params.account.trim())) { throw new RpcError({ error: "validationError", error_code: -8888, error_message: "invalid account specified" }); } } } } return config; }; return _a = class Remote { constructor(options = {}) { this._token = Wallet.config.currency; this.AbiCoder = null; this.Tum3 = null; this._timeout = 50 * 1000; this._issuer = Wallet.config.issuer; this._backend = "rpc"; this._solidity = false; this._server = options.server || Wallet.config.XLIB.default_rpc || "http://bcapps.ca:5050"; this._solidity = options.solidity ? true : false; if (this._solidity) { try { this.AbiCoder = null; this.Tum3 = null; } catch (error) { throw Error("install tum3-eth-abi and swtc-tum3 to enable solidity support"); } } this._issuer = options.issuer || Wallet.config.issuer; this._axios = axios.create({ baseURL: this._server.replace(/\/$/, ""), timeout: this._timeout }); this._axios.interceptors.request.use(intercept_request, error => { throw new RpcError(error); }); this._axios.interceptors.response.use(intercept_response, error => { if (error.response) { throw new RpcError(error.response.data.result); } throw new RpcError(error); }); options.hasOwnProperty("CURRENCIES") && Object.assign(Wallet.config.CURRENCIES, options.CURRENCIES); options.hasOwnProperty("XLIB") && Object.assign(Wallet.config.XLIB, options.XLIB); } config(options = {}) { if ("server" in options) { this._server = options.server; this._axios = axios.create({ baseURL: this._server.replace(/\/$/, ""), timeout: this._timeout }); this._axios.interceptors.request.use(intercept_request, error => { throw new RpcError(error); }); this._axios.interceptors.response.use(intercept_response, error => { if (error.response) { throw new RpcError(error.response.data.result); } throw new RpcError(error); }); } if ("issuer" in options && Wallet.isValidAddress(options.issuer)) { this._issuer = options.issuer; Wallet.config.issuer = options.issuer; } this._solidity = options.hasOwnProperty("solidity") ? options.solidity : this._solidity; if (this._solidity) { try { this.AbiCoder = null; this.Tum3 = null; } catch (error) { throw Error("install tum3-eth-abi and swtc-tum3 to enable solidity support"); } } options.hasOwnProperty("CURRENCIES") && Object.assign(Wallet.config.CURRENCIES, options.CURRENCIES); options.hasOwnProperty("XLIB") && Object.assign(Wallet.config.XLIB, options.XLIB); return { token: this._token, issuer: this._issuer, server: this._server, solidity: this._solidity, backend: this._backend, currencies: Wallet.config.CURRENCIES }; } postRequest(data = {}, config = {}) { return this._axios .post("", data, config) .then(response => Promise.resolve(response.data.result)); } buildPaymentTx(options) { return Transaction.buildPaymentTx(options, this); } buildOfferCreateTx(options) { return Transaction.buildOfferCreateTx(options, this); } buildOfferCancelTx(options) { return Transaction.buildOfferCancelTx(options, this); } buildRelationTx(options) { return Transaction.buildRelationTx(options, this); } buildAccountSetTx(options) { return Transaction.buildAccountSetTx(options, this); } buildSignTx(options) { return Transaction.buildSignTx(options, this); } buildContractDeployTx(options) { return Transaction.deployContractTx(options, this); } deployContractTx(options) { return Transaction.deployContractTx(options, this); } buildContractCallTx(options) { return Transaction.callContractTx(options, this); } callContractTx(options) { return Transaction.callContractTx(options, this); } initContract(options) { return Transaction.initContractTx(options, this); } invokeContract(options) { return Transaction.invokeContractTx(options, this); } initContractTx(options) { return Transaction.initContractTx(options, this); } invokeContractTx(options) { return Transaction.invokeContractTx(options, this); } buildContractInitTx(options) { return Transaction.initContractTx(options, this); } buildContractInvokeTx(options) { return Transaction.invokeContractTx(options, this); } buildBrokerageTx(options) { return Transaction.buildBrokerageTx(options, this); } buildIssueSetTx(options) { return Transaction.buildIssueSetTx(options, this); } buildSetBlackListTx(options) { return Transaction.buildSetBlackListTx(options, this); } buildManageIssuerTx(options) { return Transaction.buildManageIssuerTx(options, this); } buildRemoveBlackListTx(options) { return Transaction.buildRemoveBlackListTx(options, this); } buildTokenDelTx(options) { return Transaction.buildTokenDelTx(options, this); } buildTokenTransferTx(options) { return Transaction.buildTransferTokenTx(options); } buildTokenPublishTx(options) { return Transaction.buildTransferTokenTx(options); } buildSignerListTx(options) { return Transaction.buildSignerListTx(options, this); } buildTx(tx_json) { return Transaction.buildTx(tx_json, this); } makeCurrency(currency = this._token, issuer = this._issuer) { return Wallet.makeCurrency(currency, issuer); } makeAmount(value = 1, currency = this._token, issuer = this._issuer) { return Wallet.makeAmount(value, currency, issuer); } getAccountInfo(address, params = { account: "" }) { return this.rpcAccountInfo(Object.assign(Object.assign({}, params), { account: address })); } getAccountOffers(address, params = { account: "" }) { return this.rpcAccountOffers(Object.assign(Object.assign({}, params), { account: address })); } getAccountSequence(address, params = { account: "" }) { return __awaiter(this, void 0, void 0, function* () { const data = yield this.getAccountInfo(address, params); return data.account_data.Sequence; }); } getAccountTrusts(address, params = { account: "" }) { return this.rpcAccountLines(Object.assign(Object.assign({}, params), { account: address })); } getAccountRelation(address, params = { account: "" }) { return this.rpcAccountRelation(Object.assign(Object.assign({}, params), { account: address })); } getAccountObjects(address, params = { account: "" }) { return this.rpcAccountObjects(Object.assign(Object.assign({}, params), { account: address })); } getAccountSignerList(address, params = { account: "" }) { return this.getAccountObjects(address, Object.assign(Object.assign({}, params), { type: "SignerList" })); } getAccountTx(address, params = { account: "" }) { return this.rpcAccountTx(Object.assign(Object.assign({}, params), { account: address })); } getAccountCurrencies(address, params = { account: "" }) { return this.rpcAccountCurrencies(Object.assign(Object.assign({}, params), { account: address })); } getBrokerage(address, params = { account: "" }) { return this.rpcFeeInfo(Object.assign(Object.assign({}, params), { account: address })); } getBookOffers(taker_gets, taker_pays, params = { taker_gets: {}, taker_pays: {} }) { return this.rpcBookOffers(Object.assign(Object.assign({}, params), { taker_gets, taker_pays })); } getSkywellPathFind(params) { return this.rpcSkywellPathFind(params); } submit(tx_blob, params = { tx_blob: "" }) { return this.rpcSubmit(Object.assign(Object.assign({}, params), { tx_blob })); } submitMultisigned(tx_json, params = { tx_json: {} }) { return this.rpcSubmitMultisigned(Object.assign(Object.assign({}, params), { tx_json })); } getVersion() { return this.rpcVersion(); } getRandom() { return this.rpcRandom(); } getServerInfo() { return this.rpcServerInfo(); } getServerState() { return this.rpcServerState(); } getLedgerClosed() { return this.rpcLedgerClosed(); } getLedgerCurrent() { return this.rpcLedgerCurrent(); } getBlacklistInfo(params = {}) { return this.rpcBlacklistInfo(params); } getLedger(params = {}) { return this.rpcLedger(params); } getLedgerEntry(params = {}) { return this.rpcLedgerEntry(params); } getLedgerData(params = {}) { return this.rpcLedgerData(params); } getTxHistory(start = 0, params = { start: 0 }) { return this.rpcTxHistory(Object.assign(Object.assign({}, params), { start })); } getTx(transaction, params = { transaction: "" }) { return this.rpcTx(Object.assign(Object.assign({}, params), { transaction })); } getTxEntry(tx_hash, params = { tx_hash: "" }) { return this.rpcTxEntry(Object.assign(Object.assign({}, params), { tx_hash })); } rpcVersion() { return this.postRequest({ method: "version", params: [] }); } rpcRandom() { return this.postRequest({ method: "random", params: [] }); } rpcServerInfo() { return this.postRequest({ method: "server_info", params: [] }); } rpcServerState() { return this.postRequest({ method: "server_state", params: [] }); } rpcLedgerClosed() { return this.postRequest({ method: "ledger_closed", params: [] }); } rpcLedgerCurrent() { return this.postRequest({ method: "ledger_current", params: [] }); } rpcLedger(params = {}) { return this.postRequest({ method: "ledger", params: [params] }); } rpcLedgerData(params = {}) { return this.postRequest({ method: "ledger_data", params: [params] }); } rpcTxHistory(params = { start: 0 }) { return this.postRequest({ method: "tx_history", params: [params] }); } rpcTx(params) { return this.postRequest({ method: "tx", params: [params] }); } rpcTxEntry(params) { return this.postRequest({ method: "transaction_entry", params: [params] }); } rpcSubmit(params) { return this.postRequest({ method: "submit", params: [params] }); } rpcSubmitMultisigned(params) { return this.postRequest({ method: "submit_multisigned", params: [params] }); } rpcFeeInfo(params) { return this.postRequest({ method: "Fee_Info", params: [params] }); } rpcBlacklistInfo(params = {}) { return this.postRequest({ method: "blacklist_info", params: [params] }); } rpcAccountInfo(params) { return this.postRequest({ method: "account_info", params: [params] }); } rpcAccountObjects(params) { return this.postRequest({ method: "account_objects", params: [params] }); } rpcAccountCurrencies(params) { return this.postRequest({ method: "account_currencies", params: [params] }); } rpcAccountLines(params) { return this.postRequest({ method: "account_lines", params: [params] }); } rpcAccountRelation(params) { return this.postRequest({ method: "account_relation", params: [params] }); } rpcAccountOffers(params) { return this.postRequest({ method: "account_offers", params: [params] }); } rpcAccountTx(params) { return this.postRequest({ method: "account_tx", params: [Object.assign({ ledger_index_min: -1 }, params)] }); } getAccountBalances(address, params = { account: "" }) { return __awaiter(this, void 0, void 0, function* () { if (!Wallet.isValidAddress(address.trim())) { throw new Error("invalid address"); } const p_info = this.getAccountInfo(address, params); const p_trust = this.getAccountTrusts(address, params); const p_freeze = this.getAccountRelation(address, params); const p_offer = this.getAccountOffers(address, params); const data = yield Promise.all([p_info, p_trust, p_freeze, p_offer]); return this.processBalance({ native: data[0], lines: data[1], lines2: data[2], orders: data[3] }); }); } rpcBookOffers(params) { return this.postRequest({ method: "book_offers", params: [params] }); } rpcLedgerEntry(params = {}) { return this.postRequest({ method: "ledger_entry", params: [params] }); } rpcSkywellPathFind(params) { return this.postRequest({ method: "skywell_path_find", params: [params] }); } rpcPeers() { return this.postRequest({ method: "peers", params: [] }); } processBalance(data, condition = {}) { const swt_value = Number(data.native.account_data.Balance) / 1000000.0; const freeze0 = FREEZE.reserved + (data.lines.lines.length + data.orders.offers.length) * FREEZE.each_freezed; const swt_data = { value: swt_value, currency: Wallet.token, issuer: "", freezed: freeze0 }; data.orders.offers.forEach(off => { const taker_gets = utils.parseAmount(off.taker_gets); if (taker_gets.currency === swt_data.currency && taker_gets.issuer === swt_data.issuer) { swt_data.freezed += parseFloat(taker_gets.value); } }); const _data = []; if ((!condition.currency && !condition.issuer) || (condition.currency && condition.currency === this._token)) { _data.push(swt_data); } for (const item of data.lines.lines) { if (condition.currency && condition.currency === this._token) { break; } const tmpBal = { value: item.balance, currency: item.currency, issuer: item.account, freezed: 0 }; let freezed = 0; data.orders.offers.forEach(off => { const taker_gets = utils.parseAmount(off.taker_gets); if (taker_gets.currency === tmpBal.currency && taker_gets.issuer === tmpBal.issuer) { freezed += parseFloat(taker_gets.value); } }); for (const l of data.lines2.lines) { if (l.currency === tmpBal.currency && l.issuer === tmpBal.issuer) { freezed += parseFloat(l.limit); } } tmpBal.freezed = parseFloat(`${tmpBal.freezed}`) + freezed; tmpBal.freezed = Number(tmpBal.freezed.toFixed(6)); if (condition.currency && condition.currency !== tmpBal.currency) { continue; } if (condition.issuer && condition.issuer !== tmpBal.issuer) { continue; } _data.push(tmpBal); } const _ret = { balances: _data, sequence: data.native.account_data.Sequence }; return _ret; } rpcAccountErc(params) { return this.postRequest({ method: "account_erc", params: [params] }); } rpcErcInfo(params) { return this.postRequest({ method: "erc_info", params: [params] }); } }, _a.Wallet = Wallet, _a.Transaction = Transaction, _a.utils = utils, _a; }; export { Factory }; //# sourceMappingURL=factory.js.map