@chorus-one/ethereum
Version:
All-in-one toolkit for building staking dApps on Ethereum network
39 lines (38 loc) • 1.42 kB
JavaScript
import { toHex } from 'viem';
// https://eips.ethereum.org/EIPS/eip-7002#fee-calculation
export const getWithdrawalQueue = async (ethPublicClient, config) => {
const length = await getWithdrawalQueueLength(ethPublicClient, config);
const fee = getRequiredFee(config.consolidationRequestFeeAddition, length, config.minConsolidationRequestFee);
return { length, fee };
};
export const getWithdrawalQueueLength = async (ethPublicClient, config) => {
let queueLengthHex;
try {
queueLengthHex = await ethPublicClient.getStorageAt({
address: config.withdrawalContractAddress,
slot: 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);
};
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;
};