Skip to content

Commit

Permalink
Added hiro api key to estimate transaction fee
Browse files Browse the repository at this point in the history
  • Loading branch information
SDargarh committed May 28, 2024
1 parent 7c7013d commit bc38e7f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@

### 1.0.5 (2024-05-28)

- Removed private key from fees estimation
- Removed private key from fees estimation
- Added hiro api key to estimate transaction fee
1 change: 1 addition & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
HIRO_BASE_URL : "https://api.mainnet.hiro.so/extended/v1/",
HIRO_API_KEY: "f3e5f7f95761a13cba428934a4985fbf2",
HIRO_BASE_TEST_URL : "https://api.testnet.hiro.so/extended/v1/",
QUICKNODE_BASE_URL : "https://docs-demo.stacks-mainnet.quiknode.pro/extended/v1/",
QUICKNODE_BASE_TEST_URL : "https://docs-demo.stacks-testnet.quiknode.pro/extended/v1/"
Expand Down
5 changes: 4 additions & 1 deletion src/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { generatePostConditions } = require('./utils/generatePostConditions')
const { generateFunctionArgs } = require('./utils/generateFunctionArgs');
const { generatePayload } = require('./utils/generatePaylod');
const { generateStacksTransactionObject } = require('./utils/generateStacksTransactionObject');
const { estimateTransaction, estimateTransferUnsafe } = require('./utils/estimateTransaction');

module.exports = {
getActiveNetwork,
Expand All @@ -12,5 +13,7 @@ module.exports = {
generatePostConditions,
generateFunctionArgs,
generatePayload,
generateStacksTransactionObject
generateStacksTransactionObject,
estimateTransaction,
estimateTransferUnsafe
}
71 changes: 71 additions & 0 deletions src/helper/utils/estimateTransaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { HIRO_API_KEY } = require("../../constants/index.js");
const common_1 = require("@stacks/common");
const { serializePayload } = require('@stacks/transactions/dist/payload');
const network_1 = require("@stacks/network");
const { NoEstimateAvailableError } = require("@stacks/transactions/dist/errors");

async function estimateTransaction(transactionPayload, estimatedLen, network) {

const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-hiro-api-key": HIRO_API_KEY,
},
body: JSON.stringify({
transaction_payload: (0, common_1.bytesToHex)(
(0, serializePayload)(transactionPayload)
),
...(estimatedLen ? { estimated_len: estimatedLen } : {}),
}),
};
const derivedNetwork = network_1.StacksNetwork.fromNameOrNetwork(network ?? new network_1.StacksMainnet());
const url = derivedNetwork.getTransactionFeeEstimateApiUrl();
const response = await derivedNetwork.fetchFn(url, options);
if (!response.ok) {
const body = await response.text().then(str => {
try {
return JSON.parse(str);
}
catch (error) {
return str;
}
});
if (body?.reason === 'NoEstimateAvailable' ||
(typeof body === 'string' && body.includes('NoEstimateAvailable'))) {
throw new NoEstimateAvailableError(body?.reason_data?.message ?? '');
}
throw new Error(`Error estimating transaction fee. Response ${response.status}: ${response.statusText}. Attempted to fetch ${url} and failed with the message: "${body}"`);
}
const data = await response.json();
return data.estimations;

}

async function estimateTransferUnsafe(transaction, network) {
const requestHeaders = {
Accept: 'application/text',
"x-hiro-api-key": HIRO_API_KEY,
};
const fetchOptions = {
method: 'GET',
headers: requestHeaders,
};
const derivedNetwork = network_1.StacksNetwork.fromNameOrNetwork(network ?? deriveNetwork(transaction));
const url = derivedNetwork.getTransferFeeEstimateApiUrl();
const response = await derivedNetwork.fetchFn(url, fetchOptions);
if (!response.ok) {
let msg = '';
try {
msg = await response.text();
}
catch (error) { }
throw new Error(`Error estimating transaction fee. Response ${response.status}: ${response.statusText}. Attempted to fetch ${url} and failed with the message: "${msg}"`);
}
const feeRateResult = await response.text();
const txBytes = BigInt(transaction.serialize().byteLength);
const feeRate = BigInt(feeRateResult);
return feeRate * txBytes;
}

module.exports = { estimateTransaction, estimateTransferUnsafe }
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ class KeyringController {

async getFees(rawTransaction) {

const { wallet, network, address } = this.store.getState()
const { from } = rawTransaction
const { network } = this.store.getState()

const payload = helpers.generatePayload(rawTransaction);
let txOptions = await helpers.generateUnsignedTransaction(rawTransaction, undefined, network)
Expand All @@ -126,7 +125,7 @@ class KeyringController {
let fees
try{
const estimatedLen = transactions_1.estimateTransactionByteLength(transaction, network);
let fee = (await transactions_1.estimateTransaction(transaction.payload, estimatedLen, network));
let fee = (await helpers.estimateTransaction(transaction.payload, estimatedLen, network));
fees = {
slow: fee[0].fee,
standard: fee[1].fee,
Expand All @@ -136,11 +135,11 @@ class KeyringController {

}catch (error) {
if (error instanceof NoEstimateAvailableError) {
let fee = await transactions_1.estimateTransferUnsafe(transaction, network);
let fee = parseInt(await helpers.estimateTransferUnsafe(transaction, network));
fees = {
slow: fee,
standard: fee,
fast: fee,
standard: fee + parseInt(fee * 0.05),
fast: fee + parseInt(fee * 0.1),
}
return { fees: fees };

Expand Down

0 comments on commit bc38e7f

Please sign in to comment.