-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcalculate-gas-fee.ts
More file actions
100 lines (84 loc) · 2.86 KB
/
calculate-gas-fee.ts
File metadata and controls
100 lines (84 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// @ts-nocheck
import Big from "big.js";
interface AxelarScanStatusFees {
base_fee: number;
source_base_fee: number;
destination_base_fee: number;
source_express_fee: {
total: number;
};
source_confirm_fee: number;
destination_express_fee: {
total: number;
};
source_token: {
gas_price: string;
gas_price_in_units: {
decimals: number;
value: string;
};
};
execute_gas_multiplier: number;
}
interface AxelarScanStatusResponse {
is_insufficient_fee: boolean;
status: string;
fees: AxelarScanStatusFees;
id: string;
}
const DEFAULT_SQUIDROUTER_GAS_ESTIMATE = "800000";
function calculateGasFeeInUnits(feeResponse: AxelarScanStatusFees, estimatedGas: string | number): string {
const baseFeeInUnitsBig = Big(feeResponse.source_base_fee);
const estimatedGasBig = Big(estimatedGas);
const sourceGasPriceBig = Big(feeResponse.source_token.gas_price);
const executionFeeUnits = estimatedGasBig.mul(sourceGasPriceBig);
const multiplier = feeResponse.execute_gas_multiplier;
const executionFeeWithMultiplier = executionFeeUnits.mul(multiplier);
const totalGasFee = baseFeeInUnitsBig.add(executionFeeWithMultiplier);
const sourceDecimals = feeResponse.source_token.gas_price_in_units.decimals;
const totalGasFeeRaw = totalGasFee.mul(Big(10).pow(sourceDecimals));
return totalGasFeeRaw.lt(0) ? "0" : totalGasFeeRaw.toFixed(0, 0);
}
async function getStatusAxelarScan(swapHash: string): Promise<AxelarScanStatusResponse> {
try {
const response = await fetch("https://api.axelarscan.io/gmp/searchGMP", {
body: JSON.stringify({
txHash: swapHash
}),
headers: {
"Content-Type": "application/json"
},
method: "POST"
});
if (!response.ok) {
throw new Error(`Error fetching status from axelar scan API: ${response.statusText}`);
}
const responseData = await response.json();
return (responseData as { data: unknown[] }).data[0] as AxelarScanStatusResponse;
} catch (error) {
if ((error as { response: unknown }).response) {
console.error(`Couldn't get status for ${swapHash} from AxelarScan:`, (error as { response: unknown }).response);
}
throw error;
}
}
async function main() {
const txHash = process.argv[2];
if (!txHash) {
console.error(" provide a transaction hash as an argument.");
process.exit(1);
}
console.log(`Fetching status for txHash: ${txHash}`);
try {
const axelarScanStatus = await getStatusAxelarScan(txHash);
if (!axelarScanStatus || !axelarScanStatus.fees) {
console.error("Could not retrieve fees for the given transaction hash.");
return;
}
const gasFee = calculateGasFeeInUnits(axelarScanStatus.fees, DEFAULT_SQUIDROUTER_GAS_ESTIMATE);
console.log(`Calculated Gas Fee in Units: ${gasFee}`);
} catch (error) {
console.error("Error calculating gas fee:", error);
}
}
main();