Skip to content

Instantly share code, notes, and snippets.

@kristjank
Created September 24, 2018 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristjank/cbbcda1eb4250e4de8e8206929cff883 to your computer and use it in GitHub Desktop.
Save kristjank/cbbcda1eb4250e4de8e8206929cff883 to your computer and use it in GitHub Desktop.
Dynamic fee selection method
/**
* Determine if transaction matches the accepted fee by delegate or max fee set by sender
* @param {Transaction} Transaction - transaction to check
* @return {Boolean} matches T/F
*/
module.exports = (transaction) => {
const transactionFee = transaction.fee.toNumber()
const staticFee = feeManager.getForTransaction(transaction)
const blockchain = container.resolvePlugin('blockchain')
const feeConstants = config.getConstants(blockchain.getLastBlock().data.height).fees
if (!feeConstants.dynamic && transactionFee !== staticFee) {
logger.debug(`Received transaction fee '${transactionFee}' for '${transaction.id}' does not match static fee of '${staticFee}'`)
return false
}
if (feeConstants.dynamic) {
const calculatedFee = dynamicFeeManager.calculateFee(config.delegates.dynamicFees.feeMultiplier, transaction)
if (transactionFee < config.delegates.dynamicFees.minAcceptableFee) {
logger.debug(`Fee declined - Received transaction "${transaction.id}" with a fee of "${transactionFee}" which is below the minimum accepted fee of "${config.delegates.dynamicFees.minAcceptableFee}" by this delegate.`)
return false
}
if (calculatedFee > transactionFee) {
logger.debug(`Fee declined - Received transaction "${transaction.id}" with a fee of "${transactionFee}" which is below the calculated fee of "${calculatedFee}".`)
return false
}
if (transactionFee > staticFee) {
logger.debug(`Fee declined - Received transaction "${transaction.id}" with a fee of "${transactionFee}" which is higher than the static fee of "${feeManager.get(transaction.type)}".`)
return false
}
logger.debug(`Transaction "${transaction.id}" accepted with fee of "${transactionFee}". The calculated fee is "${calculatedFee}".`)
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment