@chorus-one/ethereum
Version:
All-in-one toolkit for building staking dApps on Ethereum network
44 lines (43 loc) • 1.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWithdrawalQueueLength = exports.getWithdrawalQueue = void 0;
const viem_1 = require("viem");
// https://eips.ethereum.org/EIPS/eip-7002#fee-calculation
const getWithdrawalQueue = async (ethPublicClient, config) => {
const length = await (0, exports.getWithdrawalQueueLength)(ethPublicClient, config);
const fee = getRequiredFee(config.consolidationRequestFeeAddition, length, config.minConsolidationRequestFee);
return { length, fee };
};
exports.getWithdrawalQueue = getWithdrawalQueue;
const getWithdrawalQueueLength = async (ethPublicClient, config) => {
let queueLengthHex;
try {
queueLengthHex = await ethPublicClient.getStorageAt({
address: config.withdrawalContractAddress,
slot: (0, viem_1.toHex)(config.excessWithdrawalRequestsStorageSlot)
});
if (!queueLengthHex) {
throw new Error('Unable to get withdrawal queue length');
}
if (queueLengthHex === config.excessInhibitor) {
throw new Error('Withdrawal queue is disabled');
}
}
catch (error) {
console.error(error);
queueLengthHex = '0x0';
}
return BigInt(queueLengthHex);
};
exports.getWithdrawalQueueLength = getWithdrawalQueueLength;
const getRequiredFee = (factor, queueLength, denominator) => {
let i = 1n;
let output = 0n;
let numeratorAccum = factor * denominator;
while (numeratorAccum > 0n) {
output = output + numeratorAccum;
numeratorAccum = (numeratorAccum * queueLength) / (i * denominator);
i = i + 1n;
}
return output / denominator;
};