@xlink-network/xlink-sdk
Version:
147 lines (145 loc) • 4.38 kB
JavaScript
import {
prepareBitcoinTransaction,
supportedRoutes
} from "./chunk-66DYX3RO.mjs";
import {
createBridgeOrder_BitcoinToEVM,
createBridgeOrder_BitcoinToStacks
} from "./chunk-VBBYJHRM.mjs";
import {
getBTCPegInAddress
} from "./chunk-LZ7NVYPA.mjs";
import {
numberToStacksContractNumber
} from "./chunk-JJNS722O.mjs";
import {
toSDKNumberOrUndefined
} from "./chunk-FZQUIYHC.mjs";
import {
UnsupportedBridgeRouteError
} from "./chunk-KTUPNUM5.mjs";
import {
KnownChainId,
KnownTokenId
} from "./chunk-GZDQGY7B.mjs";
import {
assertExclude,
checkNever
} from "./chunk-E5Y3FGR6.mjs";
// src/xlinkSdkUtils/estimateBridgeTransactionFromBitcoin.ts
async function estimateBridgeTransactionFromBitcoin(ctx, info) {
const route = await supportedRoutes.checkRouteValid(ctx, info);
if (KnownChainId.isBitcoinChain(route.fromChain)) {
if (KnownChainId.isStacksChain(route.toChain)) {
if (KnownTokenId.isBitcoinToken(route.fromToken) && KnownTokenId.isStacksToken(route.toToken)) {
return estimateFromBitcoin_toStacks(ctx, {
...info,
fromChain: route.fromChain,
toChain: route.toChain,
fromToken: route.fromToken,
toToken: route.toToken
});
}
} else if (KnownChainId.isEVMChain(route.toChain)) {
if (KnownTokenId.isBitcoinToken(route.fromToken) && KnownTokenId.isEVMToken(route.toToken)) {
return estimateFromBitcoin_toEVM(ctx, {
...info,
fromChain: route.fromChain,
toChain: route.toChain,
fromToken: route.fromToken,
toToken: route.toToken
});
}
} else if (KnownChainId.isBRC20Chain(route.toChain) || KnownChainId.isRunesChain(route.toChain)) {
assertExclude(route.toChain, assertExclude.i());
assertExclude(route.toChain, assertExclude.i());
} else {
assertExclude(route.toChain, assertExclude.i());
checkNever(route);
}
} else {
assertExclude(route.fromChain, assertExclude.i());
assertExclude(route.fromChain, assertExclude.i());
checkNever(route);
}
throw new UnsupportedBridgeRouteError(
info.fromChain,
info.toChain,
info.fromToken,
info.toToken
);
}
async function estimateFromBitcoin_toStacks(sdkContext, info) {
const pegInAddress = getBTCPegInAddress(info.fromChain, info.toChain);
if (pegInAddress == null) {
throw new UnsupportedBridgeRouteError(
info.fromChain,
info.toChain,
KnownTokenId.Bitcoin.BTC
);
}
const createdOrder = await createBridgeOrder_BitcoinToStacks({
fromChain: info.fromChain,
fromBitcoinScriptPubKey: info.fromAddressScriptPubKey,
toChain: info.toChain,
toToken: info.toToken,
toStacksAddress: info.toAddress,
swapSlippedAmount: numberToStacksContractNumber(info.amount),
swapRoute: []
});
if (createdOrder == null) {
throw new UnsupportedBridgeRouteError(
info.fromChain,
info.toChain,
KnownTokenId.Bitcoin.BTC
);
}
const resp = await prepareBitcoinTransaction(sdkContext, {
...info,
orderData: createdOrder.data,
pegInAddress
});
return {
fee: toSDKNumberOrUndefined(resp.fee),
estimatedVSize: toSDKNumberOrUndefined(resp.estimatedVSize)
};
}
async function estimateFromBitcoin_toEVM(sdkContext, info) {
const pegInAddress = getBTCPegInAddress(info.fromChain, info.toChain);
if (pegInAddress == null) {
throw new UnsupportedBridgeRouteError(
info.fromChain,
info.toChain,
KnownTokenId.Bitcoin.BTC
);
}
const createdOrder = await createBridgeOrder_BitcoinToEVM({
fromChain: info.fromChain,
toChain: info.toChain,
toToken: info.toToken,
fromBitcoinScriptPubKey: info.fromAddressScriptPubKey,
toEVMAddress: info.toAddress,
swapSlippedAmount: numberToStacksContractNumber(info.amount),
swapRoute: []
});
if (createdOrder == null) {
throw new UnsupportedBridgeRouteError(
info.fromChain,
info.toChain,
KnownTokenId.Bitcoin.BTC
);
}
const resp = await prepareBitcoinTransaction(sdkContext, {
...info,
orderData: createdOrder.data,
pegInAddress
});
return {
fee: toSDKNumberOrUndefined(resp.fee),
estimatedVSize: toSDKNumberOrUndefined(resp.estimatedVSize)
};
}
export {
estimateBridgeTransactionFromBitcoin
};
//# sourceMappingURL=chunk-TCX5G7F5.mjs.map