@xlink-network/xlink-sdk
Version:
468 lines (466 loc) • 22.9 kB
JavaScript
import {
bridgeInfoFromBitcoin
} from "./chunk-B5H2K2TI.mjs";
import {
bridgeInfoFromEVM
} from "./chunk-JQD6DHL4.mjs";
import {
bridgeInfoFromStacks
} from "./chunk-KXUQUA42.mjs";
import {
bridgeFromStacks,
supportedRoutes as supportedRoutes3
} from "./chunk-52RRFT5Y.mjs";
import {
estimateBridgeTransactionFromBitcoin
} from "./chunk-TCX5G7F5.mjs";
import {
claimTimeLockedAssetsFromEVM,
getTimeLockedAssetsFromEVM
} from "./chunk-P5KNCVD4.mjs";
import {
bridgeFromBitcoin,
supportedRoutes
} from "./chunk-66DYX3RO.mjs";
import {
defaultEvmClients
} from "./chunk-4IBMNMIU.mjs";
import {
getBTCPegInAddress
} from "./chunk-LZ7NVYPA.mjs";
import {
bridgeFromEVM,
supportedRoutes as supportedRoutes2
} from "./chunk-JQWEO2ZM.mjs";
import {
getEVMContractCallInfo,
getEVMToken,
getEVMTokenContractInfo
} from "./chunk-UJDCORME.mjs";
import {
getBRC20SupportedRoutes,
getRunesSupportedRoutes
} from "./chunk-BNOOAGFD.mjs";
import {
TooManyRequestsError
} from "./chunk-3OBVOHLZ.mjs";
import {
getStacksToken,
getStacksTokenContractInfo
} from "./chunk-JJNS722O.mjs";
import {
PublicEVMContractType,
evmNativeCurrencyAddress
} from "./chunk-FZQUIYHC.mjs";
import {
nativeCurrencyAddress
} from "./chunk-ZXKZ4Z37.mjs";
import {
TooFrequentlyError
} from "./chunk-KTUPNUM5.mjs";
import {
KnownChainId
} from "./chunk-GZDQGY7B.mjs";
// src/XLinkSDK.ts
var defaultConfig = {
evm: {
cacheOnChainConfig: true
}
};
var XLinkSDK = class {
static defaultConfig(options) {
defaultConfig = options;
}
sdkContext;
constructor(options = {}) {
const cacheEVMOnChainConfig = options.evm?.cacheOnChainConfig ?? defaultConfig.evm?.cacheOnChainConfig;
this.sdkContext = {
backendAPI: {
...options.__experimental?.backendAPI,
runtimeEnv: options.__experimental?.backendAPI?.runtimeEnv ?? "prod"
},
brc20: {
routesConfigCache: /* @__PURE__ */ new Map()
},
runes: {
routesConfigCache: /* @__PURE__ */ new Map()
},
evm: {
onChainConfigCache: cacheEVMOnChainConfig ? /* @__PURE__ */ new Map() : void 0,
viemClients: {
...defaultEvmClients,
...options.evm?.viemClients
}
}
};
}
/**
* This function retrieves the list of supported routes for token transfers between blockchain
* networks, filtered based on optional conditions. It aggregates the results from different
* blockchain networks (Stacks, EVM, Bitcoin) to return a list of possible routes.
* @param conditions - An optional object containing the conditions for filtering the supported routes:
* - `fromChain?: ChainId` - The ID of the source blockchain (optional).
* - `toChain?: ChainId` - The ID of the destination blockchain (optional).
* - `fromToken?: TokenId` - The ID of the token being transferred from the source blockchain (optional).
* - `toToken?: TokenId` - The ID of the token expected on the destination blockchain (optional).
*
* @returns A promise that resolves with an array of `KnownRoute` objects, each representing a
* possible route for the token transfer.
*/
async getSupportedRoutes(conditions) {
const promises = [
supportedRoutes3,
supportedRoutes2,
supportedRoutes
].map(async (rules) => rules.getSupportedRoutes(this.sdkContext, conditions));
return (await Promise.all(promises)).flat();
}
async isSupportedRoute(route) {
const checkingResult = await Promise.all(
[
supportedRoutes3,
supportedRoutes2,
supportedRoutes
].map(
(rule) => rule.checkRouteValid(this.sdkContext, route).then(() => true).catch(() => false)
)
);
return checkingResult.some((r) => r);
}
stacksAddressFromStacksToken = stacksAddressFromStacksToken;
stacksAddressToStacksToken = stacksAddressToStacksToken;
/**
* This function provides detailed information about token transfers from the Stacks network to other supported
* blockchain networks, including Bitcoin and EVM-compatible chains. It verifies the validity of the transfer
* route and retrieves bridge information based on the destination chain and tokens.
* @param input - An object containing the input parameters required for retrieving bridge information:
* - `fromChain: ChainId` - The ID of the source blockchain (Stacks in this case).
* - `toChain: ChainId` - The ID of the destination blockchain (Bitcoin, EVM-compatible chains, etc.).
* - `fromToken: TokenId` - The token being transferred from the Stacks network.
* - `toToken: TokenId` - The token expected on the destination chain.
* - `amount: SDKNumber` - The amount of tokens involved in the transfer.
*
* @returns A promise that resolves with an object containing detailed information about the token transfer, including:
* - `fromChain: KnownChainId.KnownChain` - The source blockchain.
* - `fromToken: KnownTokenId.KnownToken` - The token being transferred from the Stacks network.
* - `toChain: KnownChainId.KnownChain` - The destination blockchain.
* - `toToken: KnownTokenId.KnownToken` - The token expected on the destination chain.
* - `fromAmount: SDKNumber` - The amount of tokens being transferred.
* - `toAmount: SDKNumber` - The amount of tokens expected on the destination chain after the transfer.
* - `feeAmount: SDKNumber` - The fee amount deducted during the transfer.
* @throws UnsupportedBridgeRouteError - If the provided route between the source and destination
* chains or tokens is unsupported.
*/
bridgeInfoFromStacks(input) {
return bridgeInfoFromStacks(this.sdkContext, input);
}
/**
* This function facilitates the transfer of tokens from the Stacks network to other supported blockchain
* networks, including Bitcoin and EVM-compatible chains. It validates the route and calls the appropriate
* bridging function based on the destination chain and tokens involved.
* @param input - An object containing the input parameters required for the bridging operation:
* - `fromChain: ChainId` - The ID of the source blockchain.
* - `toChain: ChainId` - The ID of the destination blockchain (Bitcoin, EVM, etc.).
* - `fromToken: TokenId` - The token being transferred from the source chain.
* - `toToken: TokenId` - The token expected on the destination chain.
* - `fromAddress: string` - The sender's address on the source chain.
* - `toAddress: string` - The recipient's address on the destination blockchain.
* - `amount: SDKNumber` - The amount of tokens to transfer.
* - `sendTransaction` - // Implementation for sending transaction from Stacks mainnet.
*
* @returns A promise that resolves with the transaction ID (`txid`) of the bridging operation.
* @throws UnsupportedBridgeRouteError - If the provided route between the source and destination
* chains or tokens is unsupported.
*/
bridgeFromStacks(input) {
return bridgeFromStacks(this.sdkContext, input);
}
/**
* This function retrieves the contract address of a specific type of contract
* (e.g., a bridge endpoint) on a given EVM-compatible blockchain.
* @param chain - The ID of the EVM-compatible blockchain where the contract is deployed.
* @param contractType - The type of contract (e.g., `PublicEVMContractType.BridgeEndpoint`)
* for which the address is to be retrieved.
*
* @returns A promise that resolves with the contract address of the specified type, or
* `undefined` if the chain is not EVM-compatible or if the address cannot be retrieved.
*/
async getEVMContractAddress(chain, contractType) {
if (!KnownChainId.isEVMChain(chain)) return;
const info = await getEVMContractCallInfo(this.sdkContext, chain);
if (contractType === PublicEVMContractType.BridgeEndpoint) {
return info?.bridgeEndpointContractAddress;
}
return;
}
/**
* This function retrieves the contract address of a specific token on a given EVM-compatible blockchain.
* @param chain - The ID of the EVM-compatible blockchain where the token contract is deployed.
* @param token - The specific token ID for which the contract address is to be retrieved.
*
* @returns A promise that resolves with the contract address of the token, or `undefined` if the
* chain is not EVM-compatible or if the contract address cannot be retrieved.
*/
async evmAddressFromEVMToken(chain, token) {
if (!KnownChainId.isEVMChain(chain)) return;
const info = await getEVMTokenContractInfo(this.sdkContext, chain, token);
const addr = info?.tokenContractAddress;
if (addr === nativeCurrencyAddress) {
return evmNativeCurrencyAddress;
}
return addr;
}
/**
* This function maps a given contract address on an EVM-compatible blockchain to its corresponding known token ID.
* @param chain - The ID of the EVM-compatible blockchain where the contract is deployed.
* @param address - The contract address on the EVM-compatible blockchain.
*
* @returns A promise that resolves with the known token ID corresponding to the provided contract
* address, or `undefined` if the token ID cannot be found or if the chain is not EVM-compatible.
*/
async evmAddressToEVMToken(chain, address) {
if (!KnownChainId.isEVMChain(chain)) return;
if (address === evmNativeCurrencyAddress) {
address = nativeCurrencyAddress;
}
return getEVMToken(this.sdkContext, chain, address);
}
/**
* This function provides detailed information about token transfers from an EVM-compatible blockchain to other supported
* blockchain networks, including Stacks, Bitcoin, and other EVM-compatible chains. It verifies the validity of the transfer
* route and retrieves bridge information based on the destination chain and tokens.
* @param input - An object containing the input parameters required for retrieving bridge information:
* - `fromChain: ChainId` - The ID of the source blockchain (Stacks in this case).
* - `toChain: ChainId` - The ID of the destination blockchain (Bitcoin, EVM-compatible chains, etc.).
* - `fromToken: TokenId` - The token being transferred from the Stacks network.
* - `toToken: TokenId` - The token expected on the destination chain.
* - `amount: SDKNumber` - The amount of tokens involved in the transfer.
*
* @returns A promise that resolves with an object containing detailed information about the token transfer, including:
* - `fromChain: KnownChainId.KnownChain` - The source blockchain.
* - `fromToken: KnownTokenId.KnownToken` - The token being transferred from the Stacks network.
* - `toChain: KnownChainId.KnownChain` - The destination blockchain.
* - `toToken: KnownTokenId.KnownToken` - The token expected on the destination chain.
* - `fromAmount: SDKNumber` - The amount of tokens being transferred.
* - `toAmount: SDKNumber` - The amount of tokens expected on the destination chain after the transfer.
* - `feeAmount: SDKNumber` - The fee amount deducted during the transfer.
* @throws UnsupportedBridgeRouteError - If the provided route between the source and destination
* chains or tokens is unsupported.
*/
bridgeInfoFromEVM(input) {
return bridgeInfoFromEVM(this.sdkContext, input);
}
/**
* This function facilitates the transfer of tokens from an EVM-compatible blockchain to other supported
* blockchain networks, including Stacks, Bitcoin, and other EVM-compatible chains. It validates the
* route and calls the appropriate bridging function based on the destination chain and tokens involved.
* @param input - An object containing the input parameters required for the bridging operation:
* - `fromChain: ChainId` - The ID of the source blockchain (an EVM-compatible chain in this case).
* - `toChain: ChainId` - The ID of the destination blockchain (Stacks, Bitcoin, or another EVM-compatible chain).
* - `fromToken: TokenId` - The token being transferred from the EVM-compatible blockchain.
* - `toToken: TokenId` - The token expected on the destination chain.
* - `fromAddress: string` - The sender's address on the source chain.
* - `toAddress: string` - The recipient's address on the destination blockchain.
* - `toAddressScriptPubKey?: Uint8Array` - The script public key for the `toAddress`, required when the destination is a Bitcoin chain.
* - `amount: SDKNumber` - The amount of tokens to transfer.
* - `sendTransaction` - // Implementation for sending transaction from EVM chain.
*
* @returns A promise that resolves with the transaction hash (`txHash`) of the bridging operation.
* @throws UnsupportedBridgeRouteError - If the provided route between the source and destination
* chains or tokens is unsupported.
*/
bridgeFromEVM(input) {
return bridgeFromEVM(this.sdkContext, input);
}
/**
* This function retrieves a list of time-locked assets for a given wallet address across multiple EVM-compatible
* blockchain networks. It queries smart contracts to find and return information about the assets that are
* currently locked in time-based agreements.
* @param input - An object containing the input parameters required for retrieving time-locked assets:
* - `walletAddress: EVMAddress` - The address of the wallet for which to retrieve the time-locked assets.
* - `chains: KnownChainId.EVMChain[]` - An array of EVM-compatible blockchains to query for locked assets.
*
* @returns A promise that resolves with an object containing a list of time-locked assets:
* - `assets: TimeLockedAsset[]` - An array of objects, each representing a time-locked asset, including:
* - `id: string` - The unique identifier of the time-locked agreement.
* - `chain: KnownChainId.EVMChain` - The blockchain where the asset is locked.
* - `token: KnownTokenId.EVMToken` - The token that is locked.
* - `amount: SDKNumber` - The amount of the token that is locked.
* - `releaseTime: Date` - The time when the asset is scheduled to be released.
*
* @throws UnsupportedChainError - If any of the provided EVM chains is unsupported or invalid.
*/
getTimeLockedAssetsFromEVM(input) {
return getTimeLockedAssetsFromEVM(this.sdkContext, input);
}
/**
* This function facilitates the claiming of time-locked assets on EVM-compatible chains. It uses smart
* contract functions to execute the release of locked assets based on predefined conditions.
* @param input - An object containing the input parameters required for claiming time-locked assets:
* - `chain: KnownChainId.EVMChain` - The ID of the EVM-compatible blockchain where the assets are locked.
* - `lockedAssetIds: string[]` - An array of IDs representing the locked assets to be claimed.
* - `sendTransaction` - // Implementation for sending transaction from EVM chain.
*
* @returns A promise that resolves with the transaction hash (`txHash`) of the claiming operation, or `undefined` if the operation fails.
* @throws UnsupportedChainError - If the provided EVM chain is unsupported or invalid.
*/
claimTimeLockedAssetsFromEVM(input) {
return claimTimeLockedAssetsFromEVM(this.sdkContext, input);
}
async bitcoinReceiverAddress(fromChain, toChain) {
if (!KnownChainId.isBitcoinChain(fromChain)) return;
if (!KnownChainId.isKnownChain(toChain)) return;
return getBTCPegInAddress(fromChain, toChain);
}
/**
* This function provides detailed information about token transfers from the Bitcoin network to other supported
* blockchain networks, including Stacks and EVM-compatible chains. It verifies the validity of the transfer route
* and retrieves bridge information based on the destination chain.
* @param info - An object containing the input parameters required for retrieving bridge information:
* - `fromChain: ChainId` - The ID of the source blockchain.
* - `toChain: ChainId` - The ID of the destination blockchain (Stacks, EVM, etc.).
* - `amount: SDKNumber` - The amount of tokens involved in the transfer.
*
* @returns A promise that resolves with an object containing detailed information about the token transfer, including:
* - `fromChain: KnownChainId.KnownChain` - The source blockchain.
* - `fromToken: KnownTokenId.KnownToken` - The token being transferred from the Bitcoin network.
* - `toChain: KnownChainId.KnownChain` - The destination blockchain.
* - `toToken: KnownTokenId.KnownToken` - The token expected on the destination chain.
* - `fromAmount: SDKNumber` - The amount of tokens being transferred.
* - `toAmount: SDKNumber` - The amount of tokens expected on the destination chain after the transfer.
* - `feeAmount: SDKNumber` - The fee amount deducted during the transfer.
* @throws UnsupportedBridgeRouteError - If the provided route between the source and destination
* chains is unsupported.
*/
bridgeInfoFromBitcoin(input) {
return bridgeInfoFromBitcoin(this.sdkContext, input).catch((err) => {
if (err instanceof TooManyRequestsError) {
throw new TooFrequentlyError(
["bridgeInfoFromBitcoin"],
err.retryAfter,
{
cause: err
}
);
}
throw err;
});
}
/**
* This function estimates the transaction fee and vSize for move or swap tokens from the Bitcoin network
* to other supported blockchain networks, including Stacks and EVM-compatible chains.
* @param input - An object containing the input parameters required for estimating the transaction:
* - `fromChain: ChainId` - The ID of the source blockchain (Bitcoin in this case).
* - `toChain: ChainId` - The ID of the destination blockchain (Stacks, EVM-compatible chains, etc.).
* - `fromToken: TokenId` - The token being transferred from the Bitcoin network.
* - `toToken: TokenId` - The token expected on the destination chain.
* - `fromAddress: string` - The source address on the Bitcoin network.
* - `fromAddressScriptPubKey: Uint8Array` - The script public key of the source address.
* - `toAddress: string` - The destination address on the target blockchain.
* - `amount: SDKNumber` - The amount of tokens to be transferred.
* - `networkFeeRate: bigint` - The fee rate for the Bitcoin network.
* - `reselectSpendableUTXOs` - // Implementation for reselect UTXOs.
*
* @returns A promise that resolves with an object containing the estimated transaction details:
* - `fee: SDKNumber` - The estimated transaction fee.
* - `estimatedVSize: SDKNumber` - The estimated vSize of the transaction.
* @throws UnsupportedBridgeRouteError - If the provided route between the source and destination
* chains or tokens is unsupported.
*/
estimateBridgeTransactionFromBitcoin(input) {
return estimateBridgeTransactionFromBitcoin(this.sdkContext, input).catch(
(err) => {
if (err instanceof TooManyRequestsError) {
throw new TooFrequentlyError(
["estimateBridgeTransactionFromBitcoin"],
err.retryAfter,
{
cause: err
}
);
}
throw err;
}
);
}
/**
* This function facilitates the transfer of tokens from the Bitcoin network to other supported
* blockchain networks. It checks the validity of the route and then calls the appropriate
* bridging function based on the destination chain.
* @param input - An object containing the input parameters required for the bridging operation:
* - `fromChain: ChainId` - The ID of the source blockchain.
* - `toChain: ChainId` - The ID of the destination blockchain (Stacks, EVM, etc.).
* - `fromToken: TokenId` - The token being transferred from the Bitcoin network.
* - `toToken: TokenId` - The token expected on the destination chain.
* - `fromAddress: string` - The source address on the Bitcoin network.
* - `fromAddressScriptPubKey: Uint8Array` - The script public key corresponding to the `fromAddress`.
* - `toAddress: string` - The recipient's address on the destination blockchain.
* - `amount: SDKNumber` - The amount of tokens to transfer.
* - `networkFeeRate: bigint` - The network fee rate to be used for the transaction.
* - `reselectSpendableUTXOs: ReselectSpendableUTXOsFn`.
* - `signPsbt: BridgeFromBitcoinInput_signPsbtFn`.
*
* @returns A promise that resolves with the transaction ID (`tx`) of the bridging operation.
* @throws UnsupportedBridgeRouteError - If the provided route between the source and destination
* chains or tokens is unsupported.
*/
bridgeFromBitcoin(input) {
return bridgeFromBitcoin(this.sdkContext, input).catch((err) => {
if (err instanceof TooManyRequestsError) {
throw new TooFrequentlyError(["bridgeFromBitcoin"], err.retryAfter, {
cause: err
});
}
throw err;
});
}
brc20TickFromBRC20Token(chain, token) {
return brc20TickFromBRC20Token(this.sdkContext, chain, token);
}
brc20TickToBRC20Token(chain, tick) {
return brc20TickToBRC20Token(this.sdkContext, chain, tick);
}
runesIdFromRunesToken(chain, token) {
return runesIdFromRunesToken(this.sdkContext, chain, token);
}
runesIdToRunesToken(chain, id) {
return runesIdToRunesToken(this.sdkContext, chain, id);
}
};
async function stacksAddressFromStacksToken(chain, token) {
if (!KnownChainId.isStacksChain(chain)) return;
const info = await getStacksTokenContractInfo(chain, token);
if (info == null) return;
return {
deployerAddress: info.deployerAddress,
contractName: info.contractName
};
}
async function stacksAddressToStacksToken(chain, address) {
if (!KnownChainId.isStacksChain(chain)) return;
return getStacksToken(chain, address);
}
async function brc20TickFromBRC20Token(sdkContext, chain, token) {
if (!KnownChainId.isBRC20Chain(chain)) return;
const routes = await getBRC20SupportedRoutes(sdkContext, chain);
return routes.find((r) => r.brc20Token === token)?.brc20Tick;
}
async function brc20TickToBRC20Token(sdkContext, chain, tick) {
if (!KnownChainId.isBRC20Chain(chain)) return;
const routes = await getBRC20SupportedRoutes(sdkContext, chain);
return routes.find((r) => r.brc20Tick === tick)?.brc20Token;
}
async function runesIdFromRunesToken(sdkContext, chain, token) {
if (!KnownChainId.isRunesChain(chain)) return;
const routes = await getRunesSupportedRoutes(sdkContext, chain);
return routes.find((r) => r.runesToken === token)?.runesId;
}
async function runesIdToRunesToken(sdkContext, chain, runesId) {
if (!KnownChainId.isRunesChain(chain)) return;
const routes = await getRunesSupportedRoutes(sdkContext, chain);
return routes.find((r) => r.runesId === runesId)?.runesToken;
}
export {
XLinkSDK
};
//# sourceMappingURL=chunk-QM7SUNEN.mjs.map