@helios-lang/ledger-conway
Advanced tools
Comparing version 0.2.12 to 0.2.13
{ | ||
"name": "@helios-lang/ledger-conway", | ||
"version": "0.2.12", | ||
"version": "0.2.13", | ||
"description": "Cardano ledger types for Conway era", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -36,8 +36,10 @@ import { Tx as BabbageTx, TxInput } from "@helios-lang/ledger-babbage" | ||
const refScriptSize = calcRefScriptsSize( | ||
const refScriptsSize = calcRefScriptsSize( | ||
this.body.inputs, | ||
this.body.refInputs | ||
) | ||
const refScriptsFee = | ||
BigInt(helper.refScriptsFeePerByte) * BigInt(refScriptSize) | ||
const refScriptsFee = calcRefScriptsFee( | ||
refScriptsSize, | ||
helper.refScriptsFeePerByte | ||
) | ||
@@ -49,2 +51,32 @@ return sizeFee + exFee + refScriptsFee | ||
/** | ||
* @param {bigint} size | ||
* @param {number} feePerByte | ||
* @param {bigint} growthIncrement | ||
* @param {number} growthFactor | ||
* @returns {bigint} - a lovelace value | ||
*/ | ||
export function calcRefScriptsFee( | ||
size, | ||
feePerByte, | ||
growthIncrement = 25600n, | ||
growthFactor = 1.2 | ||
) { | ||
let multiplier = 1.0 | ||
let fee = 0n | ||
while (size > growthIncrement) { | ||
fee += BigInt( | ||
Math.floor(Number(growthIncrement) * multiplier * feePerByte) | ||
) | ||
size -= growthIncrement | ||
multiplier *= growthFactor | ||
} | ||
fee += BigInt(Math.floor(Number(size) * multiplier * feePerByte)) | ||
return fee | ||
} | ||
/** | ||
* Calculates the total size of reference scripts in unique inputs | ||
* @param {TxInput[]} inputs | ||
@@ -55,5 +87,13 @@ * @param {Option<TxInput[]>} refInputs | ||
export function calcRefScriptsSize(inputs, refInputs) { | ||
const refScriptSize = inputs | ||
.concat(refInputs ?? []) | ||
.reduce((prev, txInput) => { | ||
/** | ||
* @type {Record<string, TxInput>} | ||
*/ | ||
const uniqueInputs = {} | ||
inputs.concat(refInputs ?? []).forEach((input) => { | ||
uniqueInputs[input.id.toString()] = input | ||
}) | ||
const refScriptSize = Object.values(uniqueInputs).reduce( | ||
(prev, txInput) => { | ||
if (txInput.output.refScript) { | ||
@@ -64,5 +104,7 @@ return prev + BigInt(txInput.output.refScript.toCbor().length) | ||
} | ||
}, 0n) | ||
}, | ||
0n | ||
) | ||
return refScriptSize | ||
} |
/** | ||
* @param {bigint} size | ||
* @param {number} feePerByte | ||
* @param {bigint} growthIncrement | ||
* @param {number} growthFactor | ||
* @returns {bigint} - a lovelace value | ||
*/ | ||
export function calcRefScriptsFee(size: bigint, feePerByte: number, growthIncrement?: bigint, growthFactor?: number): bigint; | ||
/** | ||
* Calculates the total size of reference scripts in unique inputs | ||
* @param {TxInput[]} inputs | ||
@@ -3,0 +12,0 @@ * @param {Option<TxInput[]>} refInputs |
Sorry, the diff of this file is not supported yet
47760
1062