UNPKG

@xlink-network/xlink-sdk

Version:
163 lines (161 loc) 4.87 kB
import { toCorrespondingStacksToken } from "./chunk-COUTRDKA.mjs"; import { contractAssignedChainIdFromKnownChain } from "./chunk-RGOLYR47.mjs"; import { addressToBuffer, executeReadonlyCallXLINK, getStacksContractCallInfo, getStacksTokenContractInfo } from "./chunk-JJNS722O.mjs"; import { getTerminatingStacksTokenContractAddress } from "./chunk-6UKYXMCX.mjs"; import { decodeHex } from "./chunk-UZKY7HXV.mjs"; import { UnsupportedBridgeRouteError } from "./chunk-KTUPNUM5.mjs"; import { KnownChainId, KnownTokenId } from "./chunk-GZDQGY7B.mjs"; import { checkNever } from "./chunk-E5Y3FGR6.mjs"; import { hasLength } from "./chunk-PRLWYSC3.mjs"; // src/stacksUtils/createBridgeOrder.ts import { callReadOnlyFunction } from "@stacks/transactions"; import { unwrapResponse } from "clarity-codegen"; async function createBridgeOrder_BitcoinToStacks(info) { let data; const contractCallInfo = getStacksContractCallInfo( info.toChain, "btc-peg-in-endpoint-v2-05" ); if (contractCallInfo == null) { throw new UnsupportedBridgeRouteError( info.fromChain, info.toChain, KnownTokenId.Bitcoin.BTC, info.toToken ); } const { swapRoute, toStacksAddress /*, swapSlippedAmount = 0n */ } = info; const executeOptions = { deployerAddress: contractCallInfo.deployerAddress, callReadOnlyFunction: (callOptions) => callReadOnlyFunction({ ...callOptions, network: contractCallInfo.network }) }; const targetTokenContractInfo = getStacksTokenContractInfo( info.toChain, info.toToken ); if (targetTokenContractInfo == null) { return void 0; } if (hasLength(swapRoute, 0)) { data = await executeReadonlyCallXLINK( contractCallInfo.contractName, "create-order-cross-or-fail", { order: { from: info.fromBitcoinScriptPubKey, to: addressToBuffer(info.toChain, toStacksAddress), "chain-id": void 0, token: `${targetTokenContractInfo.deployerAddress}.${targetTokenContractInfo.contractName}`, "token-out": `${targetTokenContractInfo.deployerAddress}.${targetTokenContractInfo.contractName}` } }, executeOptions ).then(unwrapResponse); } else { checkNever(swapRoute); } return { terminatingStacksToken: targetTokenContractInfo, data }; } async function createBridgeOrder_BitcoinToEVM(info) { const contractCallInfo = getStacksContractCallInfo( info.fromChain === KnownChainId.Bitcoin.Mainnet ? KnownChainId.Stacks.Mainnet : KnownChainId.Stacks.Testnet, "btc-peg-in-endpoint-v2-05" ); if (contractCallInfo == null) { throw new UnsupportedBridgeRouteError( info.fromChain, info.toChain, KnownTokenId.Bitcoin.BTC, info.toToken ); } const { swapRoute, toEVMAddress /*, swapSlippedAmount = 0n */ } = info; const executeOptions = { deployerAddress: contractCallInfo.deployerAddress, callReadOnlyFunction: (callOptions) => callReadOnlyFunction({ ...callOptions, network: contractCallInfo.network }) }; const targetChainId = contractAssignedChainIdFromKnownChain(info.toChain); const swappedStacksToken = await toCorrespondingStacksToken(info.toToken); if (swappedStacksToken == null) return void 0; const swappedStacksTokenAddress = getStacksTokenContractInfo( contractCallInfo.network.isMainnet() ? KnownChainId.Stacks.Mainnet : KnownChainId.Stacks.Testnet, swappedStacksToken ); if (swappedStacksTokenAddress == null) return void 0; const terminatingStacksTokenAddress = getTerminatingStacksTokenContractAddress({ fromChain: contractCallInfo.network.isMainnet() ? KnownChainId.Stacks.Mainnet : KnownChainId.Stacks.Testnet, fromToken: swappedStacksToken, toChain: info.toChain, toToken: info.toToken }) ?? swappedStacksTokenAddress; let data; if (hasLength(swapRoute, 0)) { data = await executeReadonlyCallXLINK( contractCallInfo.contractName, "create-order-cross-or-fail", { order: { from: info.fromBitcoinScriptPubKey, to: decodeHex(toEVMAddress), "chain-id": targetChainId, token: `${swappedStacksTokenAddress.deployerAddress}.${swappedStacksTokenAddress.contractName}`, "token-out": `${terminatingStacksTokenAddress.deployerAddress}.${terminatingStacksTokenAddress.contractName}` } }, executeOptions ).then(unwrapResponse); } else { checkNever(swapRoute); } return { terminatingStacksToken: { deployerAddress: terminatingStacksTokenAddress.deployerAddress, contractName: terminatingStacksTokenAddress.contractName }, data }; } export { createBridgeOrder_BitcoinToStacks, createBridgeOrder_BitcoinToEVM }; //# sourceMappingURL=chunk-VBBYJHRM.mjs.map