import { DeriveBalancesAll , DeriveFees } from '@polkadot/api-derive/types' ; import { SubmittableExtrinsic } from '@polkadot/api/submittable/types' ; import { AccountInfo } from '@polkadot/types/interfaces' ; import { compactToU8a } from '@polkadot/util' ; import BN from 'bn.js' ; const MAX_SIZE_MB = 10 ; const MAX_SIZE_BYTES = MAX_SIZE_MB * 1024 * 1024 ; const LENGTH_PUBLICKEY = 32 + 1 ; const LENGTH_SIGNATURE = 64 ; const LENGTH_ERA = 1 ; const SIGNATURE_SIZE = LENGTH_PUBLICKEY + LENGTH_SIGNATURE + LENGTH_ERA ; type Error = string ; type Errors = Error [ ] ; see https export function validateFees ( accountNonce , amount = new BN ( 0 ) , currentBalance , extrinsic , fees ) { const txLength = SIGNATURE_SIZE + compactToU8a ( accountNonce . nonce ) . length + extrinsic . encodedLength ; const allFees = fees . transactionBaseFee . add ( fees . transactionByteFee . muln ( txLength ) ) ; const isCreation = false ; const allTotal = amount . add ( allFees ) . add ( isCreation ? fees . creationFee : new BN ( 0 ) ) ; const hasAvailable = extrinsic . method . methodName === 'transfer' ? currentBalance . freeBalance . gte ( allTotal ) : true ; const isRemovable = extrinsic . method . methodName === 'transfer' ? currentBalance . votingBalance . sub ( allTotal ) . lte ( fees . existentialDeposit ) : false ; const overLimit = txLength >= MAX_SIZE_BYTES ; const errors = [ ] as Errors ; if ( ! hasAvailable ) { errors . push ( 'The selected account does not have the required balance available for this transaction.' ) ; } if ( isRemovable ) { errors . push ( 'Making this call will drop your balance below the existential balance.' ) ; } if ( overLimit ) { errors . push ( `This transaction will be rejected by the node as it is greater than the maximum size of ${ MAX_SIZE_MB }MB.` ) ; } return [ errors , allTotal , allFees ] ; }