@xlink-network/xlink-sdk
Version:
89 lines (87 loc) • 2.45 kB
JavaScript
import {
getBTCPegInAddress
} from "./chunk-LZ7NVYPA.mjs";
import {
calculateFee
} from "./chunk-STX72JIW.mjs";
import {
requestAPI
} from "./chunk-3OBVOHLZ.mjs";
import {
decodeHex
} from "./chunk-UZKY7HXV.mjs";
import {
UnsupportedBridgeRouteError
} from "./chunk-KTUPNUM5.mjs";
import {
KnownTokenId
} from "./chunk-GZDQGY7B.mjs";
import {
BigNumber
} from "./chunk-KHTJNOQE.mjs";
// src/bitcoinUtils/apiHelpers/createBitcoinPegInRecipients.ts
import { estimateInputVSizeAfterSign } from "@c4/btc-utils";
import { toHex } from "viem";
var REVEAL_TX_OUTPUT_AMOUNT = 546n;
async function createBitcoinPegInRecipients(sdkContext, info) {
const pegInAddress = await getBTCPegInAddress(info.fromChain, info.toChain);
if (pegInAddress == null) {
throw new UnsupportedBridgeRouteError(
info.fromChain,
info.toChain,
KnownTokenId.Bitcoin.BTC
);
}
const resp = await requestAPI(sdkContext, {
path: `/2024-10-01/bitcoin/peg-in-recipients`,
method: "POST",
body: {
fromChain: info.fromChain,
toChain: info.toChain,
fromToken: info.fromToken,
toToken: info.toToken,
fromAddress: {
address: info.fromAddress.address,
scriptPubKeyHex: toHex(info.fromAddress.scriptPubKey)
},
toAddress: info.toAddress,
fromAmount: BigNumber.toString(info.fromAmount),
orderDataHex: toHex(info.orderData)
}
});
const recipient = resp.addresses.find(
(address) => address.type === "commitOrder"
);
const revealInputSize = estimateInputVSizeAfterSign({
addressType: "p2tr-leafScript",
tapLeafScriptArgumentByteLengths: [
/* signature */
64
],
tapLeafScriptByteLength: recipient.tapScriptHex.length / 2,
controlBlockByteLength: 33
});
const { fee: newTxFee } = await calculateFee({
recipientAddressScriptPubKeys: [pegInAddress.scriptPubKey],
opReturnData: [],
selectedUTXOs: [],
feeRate: info.feeRate,
extraSize: revealInputSize.inputSize + revealInputSize.witnessDataSize / 4
});
const revealUTXOSatsAmount = BigNumber.sum([
newTxFee,
REVEAL_TX_OUTPUT_AMOUNT
]);
return {
address: recipient.address,
scriptPubKey: decodeHex(recipient.scriptPubKeyHex),
satsAmount: BigNumber.toBigInt(
{ roundingMode: BigNumber.roundUp },
revealUTXOSatsAmount
)
};
}
export {
createBitcoinPegInRecipients
};
//# sourceMappingURL=chunk-PDADMSDC.mjs.map