UNPKG

@pgchain/blockchain-libs

Version:
132 lines 5.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Algod = void 0; const buffer_1 = require("buffer"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const precondtion_1 = require("../../../basic/precondtion"); const exceptions_1 = require("../../../basic/request/exceptions"); const restful_1 = require("../../../basic/request/restful"); const provider_1 = require("../../../types/provider"); const abc_1 = require("../../abc"); const ONE_MIN_IN_NANO_SECONDS = 60 * 1e9; class Algod extends abc_1.SimpleClient { constructor(url, indexer) { super(); this.restful = new restful_1.RestfulRequest(url); this._indexer = indexer ? new restful_1.RestfulRequest(indexer.url, indexer.apiKey ? { 'x-api-key': indexer.apiKey } : undefined) : undefined; } get indexer() { return (0, precondtion_1.checkIsDefined)(this._indexer, 'Please config indexer first'); } async getInfo() { const resp = await this.restful .get('/v2/status') .then((i) => i.json()); const bestBlockNumber = Number(resp['last-round']) || 0; const isReady = bestBlockNumber > 0 && Number(resp['catchup-time']) === 0 && Number(resp['time-since-last-round']) < ONE_MIN_IN_NANO_SECONDS; return { isReady, bestBlockNumber, }; } async getAddress(address) { const resp = await this.restful .get(`/v2/accounts/${address}`) .then((i) => i.json()); const balance = new bignumber_js_1.default(resp.amount || 0); const assets = resp.assets || []; return { balance, existing: balance.gt(0) || (Array.isArray(assets) && assets.length > 0), }; } async getBalance(address, coin) { const resp = await this.restful .get(`/v2/accounts/${address}`) .then((i) => i.json()); let balance; if (typeof (coin === null || coin === void 0 ? void 0 : coin.tokenAddress) === 'undefined') { balance = new bignumber_js_1.default(resp.amount || 0); } else { const targetAssetId = Number(coin.tokenAddress); const assets = resp.assets || []; const asset = assets.find((i) => i['asset-id'] === targetAssetId); balance = new bignumber_js_1.default((asset === null || asset === void 0 ? void 0 : asset.amount) || 0); } return balance; } getFeePricePerUnit() { return Promise.resolve({ normal: { price: new bignumber_js_1.default(1), waitingBlock: 10 }, }); } async getTransactionStatus(txid) { const is404Error = (e) => { var _a; return e instanceof exceptions_1.ResponseError && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 404; }; try { return await this.getPendingTransactionStatus(txid); } catch (e) { if (is404Error(e)) { try { return await this.getConfirmedTransactionStatus(txid); } catch (e) { if (is404Error(e)) { return provider_1.TransactionStatus.NOT_FOUND; } throw e; } } throw e; } } async getPendingTransactionStatus(txid) { const pendingTx = await this.restful .get(`/v2/transactions/pending/${txid}`) .then((i) => i.json()); const confirmedBlock = Number(pendingTx['confirmed-round'] || 0); if (Number.isFinite(confirmedBlock) && confirmedBlock > 0) { return provider_1.TransactionStatus.CONFIRM_AND_SUCCESS; } else if (pendingTx['pool-error']) { return provider_1.TransactionStatus.INVALID; } else { return provider_1.TransactionStatus.PENDING; } } async getConfirmedTransactionStatus(txid) { const resp = await this.indexer .get(`/v2/transactions/${txid}`) .then((i) => i.json()); (0, precondtion_1.check)(Number(resp === null || resp === void 0 ? void 0 : resp.transaction['confirmed-round']) > 0, 'Illegal state'); return provider_1.TransactionStatus.CONFIRM_AND_SUCCESS; } async broadcastTransaction(rawTx) { const resp = await this.restful.post('/v2/transactions', buffer_1.Buffer.from(rawTx, 'base64').toString('hex'), false, { 'Content-Type': 'application/x-binary' }); return resp.txid; } async getSuggestedParams() { const resp = await this.restful .get('/v2/transactions/params') .then((i) => i.json()); return { flatFee: false, fee: Number(resp['fee'] || 0), firstRound: Number(resp['last-round']), lastRound: Number(resp['last-round']) + 1000, genesisID: resp['genesis-id'], genesisHash: resp['genesis-hash'], }; } } exports.Algod = Algod; //# sourceMappingURL=algod.js.map