dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
79 lines • 3.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GasEstimator = void 0;
const ethers_1 = require("ethers");
class GasEstimator {
/**
* Estimates gas for upgrade transactions using TypeChain contracts
*/
static async failSafeEstimateUpgradeGas(cuts, upgradeService, functionName, additionalArgs = []) {
try {
const cleanedCuts = cuts.map(cut => ({
moduleAddress: cut.facetAddress,
action: cut.action,
functionSelectors: cut.functionSelectors
}));
const args = [
cleanedCuts,
ethers_1.ethers.constants.AddressZero,
'0x'
];
if (functionName === 'versionedUpgrade') {
args.push(...additionalArgs);
}
try {
// First attempt: Try normal estimation with high gas limit
const overrides = {
gasLimit: ethers_1.ethers.BigNumber.from("500000000") // 500M gas
};
const gasEstimate = await upgradeService.estimateGas[functionName](...args, overrides);
return gasEstimate.mul(120).div(100); // Add 20% buffer
}
catch (estimateError) {
// console.log("Initial gas estimation failed, using fallback method...");
// Fallback: Try to simulate the transaction using callStatic
await upgradeService.callStatic[functionName](...args, {
gasLimit: ethers_1.ethers.BigNumber.from("500000000")
});
// If simulation succeeds, return a conservative gas estimate for SKALE
return ethers_1.ethers.BigNumber.from("30000000"); // 30M gas as fallback
}
}
catch (error) {
console.error('Gas estimation error:', error);
// Last resort: Return a very high fixed gas value
console.log("All estimation methods failed, using fixed gas limit");
return ethers_1.ethers.BigNumber.from("100000000"); // 100M gas as last resort
}
}
static async estimateUpgradeGas(cuts, upgradeService, functionName, additionalArgs = []) {
try {
// Clean the cuts structure to match contract expectations
const cleanedCuts = cuts.map(cut => ({
moduleAddress: cut.facetAddress,
action: cut.action,
functionSelectors: cut.functionSelectors
}));
// Prepare arguments based on function name
const args = [
cleanedCuts,
ethers_1.ethers.constants.AddressZero,
'0x' // init calldata
];
// Add any additional arguments for versionedUpgrade
if (functionName === 'versionedUpgrade') {
args.push(...additionalArgs);
}
// Estimate gas
const gasEstimate = await upgradeService.estimateGas[functionName](...args);
// Add 20% buffer for safety
return gasEstimate.mul(120).div(100);
}
catch (error) {
console.error('Gas estimation error:', error);
throw error;
}
}
}
exports.GasEstimator = GasEstimator;
//# sourceMappingURL=gasEstimation.js.map