Skip to content

Commit a789994

Browse files
authored
fix: Add byte length override for transfer fee estimates (#1763)
Co-authored-by: janniks <janniks@users.noreply.github.com>
1 parent 4b6e45f commit a789994

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

packages/transactions/src/fetch.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export async function fetchNonce(
129129
* Estimate the total transaction fee in microstacks for a token transfer
130130
*
131131
* ⚠ Only sensible for token transfer transactions!
132-
* @param opts.transaction - The token transfer transaction to estimate fees for
132+
* @param opts.transaction - The token transfer transaction to estimate fees for (or its estimated length in bytes)
133133
* @param opts.api - Optional API info (`.url` & `.fetch`) used for fetch call
134134
* @return A promise that resolves to number of microstacks per byte
135135
*/
@@ -138,10 +138,10 @@ export async function fetchFeeEstimateTransfer({
138138
network: _network,
139139
client: _client,
140140
}: {
141-
/** The token transfer transaction to estimate fees for */
142-
transaction: StacksTransactionWire;
141+
/** The token transfer transaction to estimate fees for (or its estimated length in bytes) */
142+
transaction: StacksTransactionWire | number;
143143
} & NetworkClientParam): Promise<bigint> {
144-
const network = _network ?? deriveNetworkFromTx(txOpt);
144+
const network = typeof txOpt === 'number' ? 'mainnet' : _network ?? deriveNetworkFromTx(txOpt);
145145
const client = Object.assign({}, clientFromNetwork(networkFrom(network)), _client);
146146

147147
const url = `${client.baseUrl}${TRANSFER_FEE_ESTIMATE_PATH}`;
@@ -157,7 +157,10 @@ export async function fetchFeeEstimateTransfer({
157157
}
158158

159159
const feeRateResult = await response.text();
160-
const txBytes = BigInt(Math.ceil(txOpt.serializeBytes().byteLength));
160+
const txBytes =
161+
typeof txOpt === 'number'
162+
? BigInt(txOpt)
163+
: BigInt(Math.ceil(txOpt.serializeBytes().byteLength));
161164
const feeRate = BigInt(feeRateResult);
162165
return feeRate * txBytes;
163166
}

0 commit comments

Comments
 (0)