toFee method
Converts the fee history to FeeHistorical with priority fee levels.
Implementation
FeeHistorical toFee() {
FeeHistorical toPriority(List<_FeeHistorical> priorities, BigInt baseFee) {
BigInt avg(List<BigInt> arr) {
final sum = arr.reduce((a, v) => a + v);
return sum ~/ BigInt.from(arr.length);
}
final slow = avg(priorities.map((b) {
return b.priorityFeePerGas[0];
}).toList());
final average =
avg(priorities.map((b) => b.priorityFeePerGas[1]).toList());
final fast = avg(priorities.map((b) => b.priorityFeePerGas[2]).toList());
return FeeHistorical(
slow: slow, high: fast, normal: average, baseFee: baseFee);
}
final minLength = [gasUsedRatio.length, baseFeePerGas.length, reward.length]
.reduce((min, current) => current < min ? current : min);
final historical = List<_FeeHistorical>.generate(
minLength,
(index) => _FeeHistorical(
baseFeePerGas: baseFeePerGas[index],
gasUsedRatio: gasUsedRatio[index],
priorityFeePerGas: reward[index]));
return toPriority(historical, baseFeePerGas.last);
}