From a7899940c0cf8698b1fc9a406b2923d3bf8231f4 Mon Sep 17 00:00:00 2001 From: janniks <6362150+janniks@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:10:39 +0100 Subject: [PATCH] fix: Add byte length override for transfer fee estimates (#1763) Co-authored-by: janniks --- packages/transactions/src/fetch.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/transactions/src/fetch.ts b/packages/transactions/src/fetch.ts index 2c45a9bd2..b7552529d 100644 --- a/packages/transactions/src/fetch.ts +++ b/packages/transactions/src/fetch.ts @@ -129,7 +129,7 @@ export async function fetchNonce( * Estimate the total transaction fee in microstacks for a token transfer * * ⚠ Only sensible for token transfer transactions! - * @param opts.transaction - The token transfer transaction to estimate fees for + * @param opts.transaction - The token transfer transaction to estimate fees for (or its estimated length in bytes) * @param opts.api - Optional API info (`.url` & `.fetch`) used for fetch call * @return A promise that resolves to number of microstacks per byte */ @@ -138,10 +138,10 @@ export async function fetchFeeEstimateTransfer({ network: _network, client: _client, }: { - /** The token transfer transaction to estimate fees for */ - transaction: StacksTransactionWire; + /** The token transfer transaction to estimate fees for (or its estimated length in bytes) */ + transaction: StacksTransactionWire | number; } & NetworkClientParam): Promise { - const network = _network ?? deriveNetworkFromTx(txOpt); + const network = typeof txOpt === 'number' ? 'mainnet' : _network ?? deriveNetworkFromTx(txOpt); const client = Object.assign({}, clientFromNetwork(networkFrom(network)), _client); const url = `${client.baseUrl}${TRANSFER_FEE_ESTIMATE_PATH}`; @@ -157,7 +157,10 @@ export async function fetchFeeEstimateTransfer({ } const feeRateResult = await response.text(); - const txBytes = BigInt(Math.ceil(txOpt.serializeBytes().byteLength)); + const txBytes = + typeof txOpt === 'number' + ? BigInt(txOpt) + : BigInt(Math.ceil(txOpt.serializeBytes().byteLength)); const feeRate = BigInt(feeRateResult); return feeRate * txBytes; }