UNPKG

@ledgerhq/coin-tezos

Version:
129 lines 4.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createApi = createApi; const index_1 = require("@ledgerhq/coin-framework/api/index"); const logs_1 = require("@ledgerhq/logs"); const config_1 = __importDefault(require("../config")); const logic_1 = require("../logic"); const tzkt_1 = __importDefault(require("../network/tzkt")); function createApi(config) { config_1.default.setCoinConfig(() => ({ ...config, status: { type: "active" } })); return { broadcast: logic_1.broadcast, combine: logic_1.combine, craftTransaction: craft, estimateFees: estimate, getBalance: balance, lastBlock: logic_1.lastBlock, listOperations: operations, getBlock(_height) { throw new Error("getBlock is not supported"); }, getBlockInfo(_height) { throw new Error("getBlockInfo is not supported"); }, }; } function isTezosTransactionType(type) { return ["send", "delegate", "undelegate"].includes(type); } async function balance(address) { const value = await (0, logic_1.getBalance)(address); return [ { value, asset: { type: "native" }, }, ]; } async function craft(transactionIntent, customFees) { if (!isTezosTransactionType(transactionIntent.type)) { throw new index_1.IncorrectTypeError(transactionIntent.type); } // note that an estimation is always necessary to get gasLimit and storageLimit, if even using custom fees const fee = await estimate(transactionIntent).then(fees => ({ fees: (customFees ?? fees.value).toString(), gasLimit: fees.parameters?.gasLimit?.toString(), storageLimit: fees.parameters?.storageLimit?.toString(), })); const { contents } = await (0, logic_1.craftTransaction)({ address: transactionIntent.sender }, { type: transactionIntent.type, recipient: transactionIntent.recipient, amount: transactionIntent.amount, fee, }); return (0, logic_1.rawEncode)(contents); } async function estimate(transactionIntent) { const senderAccountInfo = await tzkt_1.default.getAccountByAddress(transactionIntent.sender); if (senderAccountInfo.type !== "user") throw new Error("unexpected account type"); const { estimatedFees: value, gasLimit, storageLimit, taquitoError, } = await (0, logic_1.estimateFees)({ account: { address: transactionIntent.sender, revealed: senderAccountInfo.revealed, balance: BigInt(senderAccountInfo.balance), // NOTE: previously we checked for .sender.xpub xpub: transactionIntent.senderPublicKey ?? senderAccountInfo.publicKey, }, transaction: { mode: transactionIntent.type, recipient: transactionIntent.recipient, amount: transactionIntent.amount, }, }); if (taquitoError !== undefined) { throw new Error(`Fees estimation failed: ${taquitoError}`); } return { value, parameters: { gasLimit, storageLimit, }, }; } async function fetchNextPage(address, state) { const [operations, newNextCursor] = await (0, logic_1.listOperations)(address, { limit: state.pageSize, token: state.nextCursor, sort: "Ascending", minHeight: state.minHeight, }); const newCurrentIteration = state.currentIteration + 1; let continueIteration = newNextCursor !== ""; if (newCurrentIteration >= state.maxIterations) { (0, logs_1.log)("coin:tezos", "(api/operations): max iterations reached", state.maxIterations); continueIteration = false; } const accumulated = operations.concat(state.accumulator); return { ...state, continueIterations: continueIteration, currentIteration: newCurrentIteration, nextCursor: newNextCursor, accumulator: accumulated, }; } async function operationsFromHeight(address, start) { const firstState = { pageSize: 200, maxIterations: 10, currentIteration: 0, minHeight: start, continueIterations: true, accumulator: [], }; let state = await fetchNextPage(address, firstState); while (state.continueIterations) { state = await fetchNextPage(address, state); } return [state.accumulator, state.nextCursor || ""]; } async function operations(address, { minHeight }) { return operationsFromHeight(address, minHeight); } //# sourceMappingURL=index.js.map