Skip to content

Commit

Permalink
Fixes for weight calculation
Browse files Browse the repository at this point in the history
Not yet functional. Some rounding differences are still present.
  • Loading branch information
athei committed May 27, 2020
1 parent 5b3b643 commit 017fc30
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/ApiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,18 @@ export default class ApiHandler {
}
}

const fixed128 = new BN(10).pow(new BN(18))
const perBill = new BN(1e9);
const perByteFee = api.consts.transactionPayment.transactionByteFee.mul(perBill);
const extrinsicBaseWeight = api.consts.system.extrinsicBaseWeight.mul(perBill);
const perByteFee = api.consts.transactionPayment.transactionByteFee;
const extrinsicBaseWeight = api.consts.system.extrinsicBaseWeight;
// Here we assume that `weightToFee` Vec only has 1 element, which is of
// degree 1. FIXME Implement polynomial.
const coefficients = api.consts.transactionPayment.weightToFee[0];
const coeffFrac = coefficients.coeffFrac;
const coeffInt = coefficients.coeffInteger.mul(perBill);
const coeffInt = coefficients.coeffInteger;

// bn.js values must be < 0x20000000000000, cannot divide by 1e18 [Ref: bn.js:128]
const targetedFeeAdjustment = (await api.query.transactionPayment.nextFeeMultiplier.at(parentHash)).div(perBill); // FIXME This might round the number
const targetedFeeAdjustment = await api.query.transactionPayment.nextFeeMultiplier.at(parentHash);

// TODO: We assume that the polynomial has one positive term of degree one.
// const degree = coefficients.degree;
Expand Down Expand Up @@ -189,22 +190,21 @@ export default class ApiHandler {
}

// Note: we use the same variable names as in Rust
const len = block.extrinsics[idx].encodedLength;
const lenFee = perByteFee.muln(len);
const lenFee = perByteFee.muln(block.extrinsics[idx].encodedLength);

// weight_to_fee calculation
// FIXME Implement polynomial.
const weight = weightInfo.weight;
const weightedCoeffFrac = weight.mul(coeffFrac);
const weightedCoeffFrac = weight.mul(coeffFrac).div(perBill);
const weightedCoeffInt = weight.mul(coeffInt);
const unadjustedWeightFee = weightedCoeffInt.add(weightedCoeffFrac); // In Perbill
const unadjustedWeightFee = weightedCoeffInt.add(weightedCoeffFrac);

const adjustableFee = lenFee.add(unadjustedWeightFee);
const adjustedFee = targetedFeeAdjustment.mul(adjustableFee);
const adjustedFee = adjustableFee.add(targetedFeeAdjustment.mul(adjustableFee).div(fixed128));

// weight_to_fee calculation
// FIXME Implement polynomial.
const baseFeeFrac = extrinsicBaseWeight.mul(coeffFrac);
const baseFeeFrac = extrinsicBaseWeight.mul(coeffFrac).div(perBill);
const baseFeeInt = extrinsicBaseWeight.mul(coeffInt);
const baseFee = baseFeeInt.add(baseFeeFrac); // In Perbill

Expand All @@ -213,7 +213,7 @@ export default class ApiHandler {
extrinsics[idx].info = api.createType('RuntimeDispatchInfo', {
weight,
class: weightInfo.class,
partialFee: partialFee.div(perBill) // We were in Perbill-land, now back to integer
partialFee: partialFee
});
} catch (err) {
console.error(err);
Expand Down

0 comments on commit 017fc30

Please sign in to comment.