@gotake/gotake-sdk
Version:
SDK for interacting with GoTake blockchain contracts
95 lines (94 loc) • 4.12 kB
JavaScript
import { ethers } from 'ethers';
import { BaseWrapper } from './base-wrapper.js';
/**
* Base class for all contract wrappers that handle transactions
* Extends BaseWrapper with gas handling capabilities
*/
export class BaseTransactionWrapper extends BaseWrapper {
/**
* Create transaction wrapper instance
* @param provider Provider instance
* @param signer Signer instance
*/
constructor(provider, signer) {
super(provider, signer);
}
/**
* Get gas price data based on current network conditions and user configuration
* @param config Optional gas configuration
* @returns Gas price data including baseFeePerGas, maxFeePerGas, and maxPriorityFeePerGas
*/
async getGasPriceData(config) {
// Default multipliers
const gasPriceMultiplier = (config === null || config === void 0 ? void 0 : config.gasPriceMultiplier) || 1.2;
const priorityFeeMultiplier = (config === null || config === void 0 ? void 0 : config.priorityFeeMultiplier) || 1.5;
try {
// Get latest block
const latestBlock = await this._provider.getBlock('latest');
let baseFeePerGas;
// Get baseFeePerGas if available (EIP-1559 networks)
if (latestBlock.baseFeePerGas) {
baseFeePerGas = latestBlock.baseFeePerGas;
// Try to get network's suggested priority fee
let maxPriorityFeePerGas;
try {
const feeData = await this._provider.getFeeData();
maxPriorityFeePerGas = feeData.maxPriorityFeePerGas || ethers.utils.parseUnits("1", "gwei");
}
catch (e) {
// Default priority fee if not available
maxPriorityFeePerGas = ethers.utils.parseUnits("1", "gwei");
}
// Apply multipliers
const adjustedPriorityFee = maxPriorityFeePerGas.mul(Math.floor(priorityFeeMultiplier * 100)).div(100);
// Calculate maxFeePerGas = baseFeePerGas * multiplier + maxPriorityFeePerGas
const adjustedMaxFee = baseFeePerGas
.mul(Math.floor(gasPriceMultiplier * 100))
.div(100)
.add(adjustedPriorityFee);
return {
baseFeePerGas,
maxFeePerGas: adjustedMaxFee,
maxPriorityFeePerGas: adjustedPriorityFee
};
}
else {
// For non-EIP-1559 networks
const gasPrice = await this._provider.getGasPrice();
// Apply multiplier to gas price
const adjustedGasPrice = gasPrice.mul(Math.floor(gasPriceMultiplier * 100)).div(100);
return {
maxFeePerGas: adjustedGasPrice,
};
}
}
catch (error) {
console.warn("Failed to get gas price data:", error);
return {}; // Return empty object, let ethers.js handle defaults
}
}
/**
* Create transaction overrides object from gas configuration
* @param gasConfig Optional gas configuration
* @returns Transaction overrides object for ethers.js
*/
createOverrides(gasConfig) {
if (!gasConfig)
return {};
const overrides = {};
// Convert BigNumber objects to strings/numbers to avoid version conflicts
if (gasConfig.gasLimit) {
const gasLimit = ethers.BigNumber.from(gasConfig.gasLimit);
overrides.gasLimit = gasLimit.toString();
}
if (gasConfig.maxFeePerGas) {
const maxFeePerGas = ethers.BigNumber.from(gasConfig.maxFeePerGas);
overrides.maxFeePerGas = maxFeePerGas.toString();
}
if (gasConfig.maxPriorityFeePerGas) {
const maxPriorityFeePerGas = ethers.BigNumber.from(gasConfig.maxPriorityFeePerGas);
overrides.maxPriorityFeePerGas = maxPriorityFeePerGas.toString();
}
return overrides;
}
}