Skip to content

Commit

Permalink
fix: Add byte length override for transfer fee estimates (#1763)
Browse files Browse the repository at this point in the history
Co-authored-by: janniks <[email protected]>
  • Loading branch information
janniks and janniks authored Nov 15, 2024
1 parent 4b6e45f commit a789994
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/transactions/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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<bigint> {
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}`;
Expand All @@ -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;
}
Expand Down

0 comments on commit a789994

Please sign in to comment.