import { Runestone } from '@saturnbtcio/ordinals-lib'; import { IdentifiableLiquidityPool } from '@saturnbtcio/pool-serde-sdk'; import { DEFAULT_ARCH_P2TR_INPUT, DEFAULT_P2TR_OUTPUT, InputType, OutputType, TransactionSizeCalculator, } from '@saturnbtcio/psbt'; import { selectBestShardsToRemoveFrom, UpdateLiquidityBy } from './shards'; import { MempoolInfoMap } from '../providers/bitcoin.provider'; export const getCalculatorForRuneToBtcTx = ( userRuneInputType: InputType, userBtcOutputType: OutputType, pool: IdentifiableLiquidityPool, amountOut: bigint, mempoolInfo: MempoolInfoMap, ) => { // Choose which shards to use. const txSizeCalculator = new TransactionSizeCalculator(); // Fee payer txSizeCalculator.addInput(DEFAULT_ARCH_P2TR_INPUT); // Fee payer output txSizeCalculator.addOutput(DEFAULT_P2TR_OUTPUT); // User input for rune txSizeCalculator.addInput(userRuneInputType); // User output for BTC txSizeCalculator.addOutput(userBtcOutputType); // 1 op_return const runestone = Runestone.default(); runestone.pointer = 2; const enciphered = runestone.encipher(); // Somehow in segwit the program is not correctly calculating the final size of the transaction // and it's missing it for 1 byte (4 wu). const newArray = new Uint8Array(enciphered.length + 1); newArray.set(enciphered); // Additional byte to have some offset in the calculations newArray[enciphered.length] = 0x00; txSizeCalculator.addOutput({ script: newArray, amount: 0n, }); // 1 rune input/output from the shard with the smallest amount of runes txSizeCalculator.addOutput(DEFAULT_P2TR_OUTPUT); txSizeCalculator.addInput(DEFAULT_ARCH_P2TR_INPUT); const shardsToUse = selectBestShardsToRemoveFrom( pool, amountOut, UpdateLiquidityBy.BtcAmount, mempoolInfo, ); // 1 account utxo per shard + 1 btc utxo per shard for (let i = 0; i < shardsToUse.length; i++) { txSizeCalculator.addInput(DEFAULT_ARCH_P2TR_INPUT); txSizeCalculator.addInput(DEFAULT_ARCH_P2TR_INPUT); } // 1 output account utxos (LP) + 1 btc output per shard for (let i = 0; i < shardsToUse.length * 2; i++) { txSizeCalculator.addOutput(DEFAULT_P2TR_OUTPUT); } return txSizeCalculator; };