以太坊一些常用的计算方式 §
计算下一个区块的baseFee的方式 §
export const calcNextBlockBaseFee = (curBlock) => {
const baseFee = curBlock.baseFeePerGas; //取得当前块的 base gas 40.075439165
const gasUsed = curBlock.gasUsed; // 取得当前块的 gasUsed 27438201
const targetGasUsed = curBlock.gasLimit.div(2); // 当前块的 gasLimit 的一半 30,000,000/2
const delta = gasUsed.sub(targetGasUsed); // 27438201 - 15000000 12438201
const newBaseFee = baseFee.add(
baseFee.mul(delta).div(targetGasUsed).div(ethers.BigNumber.from(8))
);
// newBaseFee = baseFee+ (baseFee * del)/(8 * targetGaseUsed)
// 40.075439165 + (40.075439165 * 12438201 )/(8 * 15000000)
// Add 0-9 wei so it becomes a different hash each time
// newBaseFee 很接近下一个的区块的baseFee了
const rand = Math.floor(Math.random() * 10); // 增加一个随机值
return newBaseFee.add(rand);
};