p-sdk-wallet
Version:
A comprehensive wallet SDK for React Native (pwc), supporting multi-chain and multi-account features.
124 lines (123 loc) • 5.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NETWORK_GAS_CONFIG = exports.GAS_CONFIG = void 0;
exports.setGlobalGasConfig = setGlobalGasConfig;
exports.setGlobalNetworkGasConfig = setGlobalNetworkGasConfig;
exports.clearGlobalGasConfigs = clearGlobalGasConfigs;
exports.getGasConfig = getGasConfig;
exports.getNetworkGasConfig = getNetworkGasConfig;
exports.calculateOptimalGasPrice = calculateOptimalGasPrice;
/**
* Gas configuration constants to avoid hardcoded values
*/
exports.GAS_CONFIG = {
/** Default gas limit for ERC-20 token transfers */
ERC20_TRANSFER: 65000,
/** Default gas limit for native token transfers */
NATIVE_TRANSFER: 21000,
/** Default gas limit for token approvals */
TOKEN_APPROVAL: 46000,
/** Default gas limit for contract interactions */
CONTRACT_INTERACTION: 100000,
/** Maximum gas limit for safety */
MAX_GAS_LIMIT: 500000,
/** Gas multiplier for batch operations */
BATCH_MULTIPLIER: 1.1,
/** Priority fee (tip) in wei for EIP-1559 transactions */
DEFAULT_PRIORITY_FEE: 1500000000n, // 1.5 gwei
/** Base fee buffer for EIP-1559 transactions */
BASE_FEE_BUFFER: 1.2,
};
// Global gas config registry for mobile app overrides
let GLOBAL_GAS_CONFIG = {};
let GLOBAL_NETWORK_GAS_CONFIG = {};
/**
* Set global gas configuration from mobile app (highest priority)
*/
function setGlobalGasConfig(config) {
GLOBAL_GAS_CONFIG = { ...GLOBAL_GAS_CONFIG, ...config };
}
/**
* Set global network gas configuration from mobile app (highest priority)
*/
function setGlobalNetworkGasConfig(config) {
GLOBAL_NETWORK_GAS_CONFIG = { ...GLOBAL_NETWORK_GAS_CONFIG, ...config };
}
/**
* Clear all global gas configurations (reset to defaults)
*/
function clearGlobalGasConfigs() {
GLOBAL_GAS_CONFIG = {};
GLOBAL_NETWORK_GAS_CONFIG = {};
}
/**
* Get merged gas config (Mobile App > Env > SDK Defaults)
*/
function getGasConfig() {
return {
...exports.GAS_CONFIG,
...GLOBAL_GAS_CONFIG,
// Environment variables override
ERC20_TRANSFER: parseInt(process.env.GAS_ERC20_TRANSFER || GLOBAL_GAS_CONFIG.ERC20_TRANSFER?.toString() || exports.GAS_CONFIG.ERC20_TRANSFER.toString()),
NATIVE_TRANSFER: parseInt(process.env.GAS_NATIVE_TRANSFER || GLOBAL_GAS_CONFIG.NATIVE_TRANSFER?.toString() || exports.GAS_CONFIG.NATIVE_TRANSFER.toString()),
TOKEN_APPROVAL: parseInt(process.env.GAS_TOKEN_APPROVAL || GLOBAL_GAS_CONFIG.TOKEN_APPROVAL?.toString() || exports.GAS_CONFIG.TOKEN_APPROVAL.toString()),
CONTRACT_INTERACTION: parseInt(process.env.GAS_CONTRACT_INTERACTION || GLOBAL_GAS_CONFIG.CONTRACT_INTERACTION?.toString() || exports.GAS_CONFIG.CONTRACT_INTERACTION.toString()),
MAX_GAS_LIMIT: parseInt(process.env.GAS_MAX_LIMIT || GLOBAL_GAS_CONFIG.MAX_GAS_LIMIT?.toString() || exports.GAS_CONFIG.MAX_GAS_LIMIT.toString()),
BATCH_MULTIPLIER: parseFloat(process.env.GAS_BATCH_MULTIPLIER || GLOBAL_GAS_CONFIG.BATCH_MULTIPLIER?.toString() || exports.GAS_CONFIG.BATCH_MULTIPLIER.toString()),
DEFAULT_PRIORITY_FEE: BigInt(process.env.GAS_DEFAULT_PRIORITY_FEE || GLOBAL_GAS_CONFIG.DEFAULT_PRIORITY_FEE?.toString() || exports.GAS_CONFIG.DEFAULT_PRIORITY_FEE.toString()),
BASE_FEE_BUFFER: parseFloat(process.env.GAS_BASE_FEE_BUFFER || GLOBAL_GAS_CONFIG.BASE_FEE_BUFFER?.toString() || exports.GAS_CONFIG.BASE_FEE_BUFFER.toString()),
};
}
/**
* Network-specific gas configurations
*/
exports.NETWORK_GAS_CONFIG = {
'1': {
maxFeePerGas: 50000000000n, // 50 gwei
maxPriorityFeePerGas: 2000000000n, // 2 gwei
},
'56': {
maxFeePerGas: 5000000000n, // 5 gwei
maxPriorityFeePerGas: 1000000000n, // 1 gwei
},
'137': {
maxFeePerGas: 30000000000n, // 30 gwei
maxPriorityFeePerGas: 1000000000n, // 1 gwei
},
'42161': {
maxFeePerGas: 100000000n, // 0.1 gwei
maxPriorityFeePerGas: 100000000n, // 0.1 gwei
},
'10': {
maxFeePerGas: 1000000000n, // 1 gwei
maxPriorityFeePerGas: 100000000n, // 0.1 gwei
},
'8453': {
maxFeePerGas: 1000000000n, // 1 gwei
maxPriorityFeePerGas: 100000000n, // 0.1 gwei
},
};
/**
* Get gas configuration for a specific network with priority: Mobile App > Env > SDK Defaults
*/
function getNetworkGasConfig(chainId) {
const defaultConfig = exports.NETWORK_GAS_CONFIG[chainId] || {
maxFeePerGas: 20000000000n, // 20 gwei default
maxPriorityFeePerGas: 1000000000n, // 1 gwei default
};
const override = GLOBAL_NETWORK_GAS_CONFIG[chainId] || {};
return { ...defaultConfig, ...override };
}
/**
* Calculate optimal gas price for EIP-1559 transactions using dynamic config
*/
function calculateOptimalGasPrice(baseFee, chainId) {
const networkConfig = getNetworkGasConfig(chainId);
const gasConfig = getGasConfig();
const priorityFee = networkConfig.maxPriorityFeePerGas;
const maxFeePerGas = baseFee * BigInt(Math.ceil(gasConfig.BASE_FEE_BUFFER * 100)) / BigInt(100) + priorityFee;
return {
maxFeePerGas: maxFeePerGas > networkConfig.maxFeePerGas ? networkConfig.maxFeePerGas : maxFeePerGas,
maxPriorityFeePerGas: priorityFee,
};
}