@ledgerhq/coin-tezos
Version:
124 lines • 5.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.estimateFees = estimateFees;
const ledger_signer_1 = require("@taquito/ledger-signer");
const utils_1 = require("@taquito/ledger-signer/dist/lib/utils");
const taquito_1 = require("@taquito/taquito");
const utils_2 = require("@taquito/utils");
const logs_1 = require("@ledgerhq/logs");
const tezosToolkit_1 = require("./tezosToolkit");
const errors_1 = require("../types/errors");
/**
* Fetch the transaction fees for a transaction
*
* @param {Account} account
* @param {Transaction} transaction
*/
async function estimateFees({ account, transaction, }) {
const encodedPubKey = (0, utils_2.b58cencode)((0, utils_1.compressPublicKey)(Buffer.from(account.xpub || "", "hex"), ledger_signer_1.DerivationType.ED25519), utils_2.prefix[utils_2.Prefix.EDPK]);
const tezosToolkit = (0, tezosToolkit_1.getTezosToolkit)();
tezosToolkit.setProvider({
signer: {
publicKeyHash: async () => account.address,
publicKey: async () => encodedPubKey,
sign: () => Promise.reject(new Error("unsupported")),
secretKey: () => Promise.reject(new Error("unsupported")),
},
});
const estimation = {
fees: BigInt(0),
gasLimit: BigInt(0),
storageLimit: BigInt(0),
estimatedFees: BigInt(0),
};
// For legacy compatibility
if (account.balance === BigInt(0)) {
return transaction.useAllAmount ? { ...estimation, amount: BigInt(0) } : estimation;
}
let amount = transaction.amount;
if (transaction.useAllAmount) {
amount = BigInt(1); // send max do a pre-estimation with minimum amount (taquito refuses 0)
}
try {
let estimate;
switch (transaction.mode) {
case "send":
estimate = await tezosToolkit.estimate.transfer({
mutez: true,
to: transaction.recipient,
amount: Number(amount),
storageLimit: taquito_1.DEFAULT_STORAGE_LIMIT.ORIGINATION, // https://github.com/TezTech/eztz/blob/master/PROTO_003_FEES.md for originating an account
});
break;
case "delegate":
estimate = await tezosToolkit.estimate.setDelegate({
source: account.address,
delegate: transaction.recipient,
});
break;
case "undelegate":
estimate = await tezosToolkit.estimate.setDelegate({
source: account.address,
});
break;
default:
throw new errors_1.UnsupportedTransactionMode("unsupported mode", { mode: transaction.mode });
}
// NOTE: if useAllAmount is true, is it for sure in the send mode (ie. transfer)?
if (transaction.useAllAmount) {
let totalFees;
if (estimate.burnFeeMutez > 0) {
// NOTE: from https://github.com/ecadlabs/taquito/blob/master/integration-tests/__tests__/contract/empty-implicit-account-into-new-implicit-account.spec.ts#L37
totalFees = estimate.suggestedFeeMutez + estimate.burnFeeMutez - 20 * taquito_1.COST_PER_BYTE; // 20 is storage buffer
}
else {
totalFees = estimate.suggestedFeeMutez;
}
const maxAmount = parseInt(account.balance.toString()) -
(totalFees + (account.revealed ? 0 : taquito_1.DEFAULT_FEE.REVEAL));
// NOTE: from https://github.com/ecadlabs/taquito/blob/a70c64c4b105381bb9f1d04c9c70e8ef26e9241c/integration-tests/contract-empty-implicit-account-into-new-implicit-account.spec.ts#L33
// Temporary fix, see https://gitlab.com/tezos/tezos/-/issues/1754
// we need to increase the gasLimit and fee returned by the estimation
const gasBuffer = 500;
const MINIMAL_FEE_PER_GAS_MUTEZ = 0.1;
const increasedFee = (gasBuffer, opSize) => {
return gasBuffer * MINIMAL_FEE_PER_GAS_MUTEZ + opSize;
};
const incr = increasedFee(gasBuffer, Number(estimate.opSize));
const maxMinusBuff = maxAmount - (gasBuffer - incr);
estimation.amount = maxMinusBuff > 0 ? BigInt(maxMinusBuff) : BigInt(0);
estimation.fees = BigInt(estimate.suggestedFeeMutez);
estimation.gasLimit = BigInt(estimate.gasLimit);
}
else {
estimation.fees = BigInt(estimate.suggestedFeeMutez);
estimation.gasLimit = BigInt(estimate.gasLimit);
estimation.amount = transaction.amount;
}
estimation.storageLimit = BigInt(estimate.storageLimit);
estimation.estimatedFees = estimation.fees;
if (!account.revealed) {
estimation.estimatedFees = estimation.estimatedFees + BigInt(taquito_1.DEFAULT_FEE.REVEAL);
}
}
catch (e) {
if (typeof e !== "object" || !e)
throw e;
if ("id" in e) {
estimation.taquitoError = e.id;
(0, logs_1.log)("taquito-error", "taquito got error " + e.id);
}
else if ("status" in e) {
// in case of http 400, log & ignore (more case to handle)
(0, logs_1.log)("taquito-network-error", String(e.message || ""), {
transaction: transaction,
});
throw e;
}
else {
throw e;
}
}
return estimation;
}
//# sourceMappingURL=estimateFees.js.map