import { u128 } from 'as-bignum' ; import { UInt128 , UInt32 } from 'as-scale-codec' ; import { DispatchClass , DispatchInfo , Pays , PostDispatchInfo , RuntimeDispatchInfo , WeightToFeeCoefficient , WeightToFeePolynomial } from 'subsembly-core' ; import { StorageEntry } from '../../frame' ; import { AccountIdType , Balance , Multiplier , SystemConfig , UncheckedExtrinsic , Weight } from '../../runtime/runtime' ; import { Payment } from './payment' ; /**
 * Storage entries for TransactionPayment module
 */ export namespace TransactionPaymentStorageEntries { description export function NextFeeMultiplier ( ) { return new StorageEntry < Multiplier > ( "TransactionPayment" , "Multiplier" ) ; } description export function TransactionByteFee ( ) { return new StorageEntry < Balance > ( "TransactionPayment" , "ByteFee" ) ; } } export class TransactionPayment { description param len param info param tip static _computeFee ( len , info , tip ) { return this . _computeFeeRaw ( len , info . weight , tip , info . paysFee ) ; } ; description param len param info param postInfo param tip static _computeActualFee ( len , info , postInfo , tip ) { return this . _computeFeeRaw ( len , postInfo . calcActualWeight ( info ) , tip , postInfo . paysFee ) ; } ; description param len param weight param tip param paysFee static _computeFeeRaw ( len , weight , tip , paysFee ) { if ( paysFee == Pays . Yes ) { let length = instantiate < Balance > ( u128 . fromU32 ( len . unwrap ( ) ) ) ; let perByte = TransactionPaymentStorageEntries . TransactionByteFee ( ) . get ( ) ; if ( perByte . unwrap ( ) == u128 . Zero ) { perByte = instantiate < Balance > ( u128 . One ) ; } let fixedLenFee = length . unwrap ( ) * perByte . unwrap ( ) ; let unadjustedWeightFee = this . _weightToFee ( weight ) ; let multiplier = instantiate < Balance > ( u128 . fromU64 ( TransactionPaymentStorageEntries . NextFeeMultiplier ( ) . get ( ) . unwrap ( ) ) ) ; const adjustedWeightFee = multiplier . unwrap ( ) * unadjustedWeightFee . unwrap ( ) ; let baseFee = this . _weightToFee ( SystemConfig . ExtrinsicBaseWeight ( ) ) ; let basedFee = baseFee . unwrap ( ) + fixedLenFee + adjustedWeightFee + tip . unwrap ( ) ; return instantiate < Balance > ( basedFee ) ; } else { return tip ; } } ; description param weight static _weightToFee ( weight ) { let cappedWeight = weight . unwrap ( ) < SystemConfig . MaximumBlockWeight ( ) . unwrap ( ) ? weight : SystemConfig . MaximumBlockWeight ( ) ; let formula = new WeightToFeePolynomial < Balance > ( [ new WeightToFeeCoefficient ( 1 , 0 , false , 1 ) ] ) ; return formula . calc ( cappedWeight ) ; } ; description param who param tip param info param len static _withdrawFee ( who , tip , info , len ) { const fee = this . _computeFee ( len , info , tip ) ; Payment . withdrawFee ( who , info , fee , tip ) ; } ; description param ext param len static _queryInfo ( ext , len ) { const dispatchInfo = new DispatchInfo < Weight > ( instantiate < Weight > ( ext . encodedLength ( ) ) , Pays . Yes , DispatchClass . Normal ) ; const partialFee = this . _computeFee ( len , dispatchInfo , instantiate < Balance > ( u128 . Zero ) ) ; return new RuntimeDispatchInfo < UInt128 , Weight > ( dispatchInfo . weight , dispatchInfo . klass , partialFee ) ; } }