UNPKG

@xlink-network/xlink-sdk

Version:
421 lines (414 loc) 11.7 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/bitcoinUtils/apiHelpers/createRevealTx.ts var createRevealTx_exports = {}; __export(createRevealTx_exports, { createRevealTx: () => createRevealTx }); module.exports = __toCommonJS(createRevealTx_exports); var import_viem = require("viem"); // src/config.ts var import_network = require("@stacks/network"); var backendAPIPrefix = "https://sdk-api.xlink.network/"; var STACKS_MAINNET = new import_network.StacksMainnet({ url: "https://stacks-node-api.alexlab.co" }); var STACKS_TESTNET = new import_network.StacksMocknet({ url: "https://nakamoto-dev-api.alexlab.co" }); // src/utils/BigNumber.ts var import_big = require("big.js"); // src/utils/arrayHelpers.ts function last(ary) { return ary[ary.length - 1]; } var _concat = [].concat; var _reduce = [].reduce; function reduce(fn, initialValue, inputArray) { return _reduce.call(inputArray, fn, initialValue); } // src/utils/BigNumber.ts import_big.Big.PE = 21; import_big.Big.NE = -21; var toBig = (num) => { if (num instanceof import_big.Big) { return num; } return new import_big.Big(num); }; var fromBig = (num) => { return num; }; var BigNumber; ((BigNumber2) => { ({ roundUp: BigNumber2.roundUp, roundDown: BigNumber2.roundDown, roundHalfUp: BigNumber2.roundHalfUp, roundHalfEven: BigNumber2.roundHalfEven } = import_big.Big); let defaultRoundingMode = BigNumber2.roundHalfUp; BigNumber2.setDefaultRoundingMode = (roundingMode) => { defaultRoundingMode = roundingMode; }; BigNumber2.isBigNumber = (num) => { return num instanceof import_big.Big; }; BigNumber2.safeFrom = (value) => { try { return (0, BigNumber2.from)(value); } catch (e) { return void 0; } }; BigNumber2.from = (value) => { return fromBig(toBig(value)); }; BigNumber2.toString = (value) => { return toBig(value).toString(); }; BigNumber2.toNumber = (value) => { return toBig(value).toNumber(); }; BigNumber2.toBigInt = curry2( (options, value) => { return BigInt( (0, BigNumber2.toFixed)( { precision: 0, roundingMode: options.roundingMode ?? defaultRoundingMode }, toBig(value) ) ); } ); BigNumber2.toFixed = curry2( (options, value) => { return toBig(value).toFixed( options.precision, options.roundingMode ?? defaultRoundingMode ); } ); BigNumber2.toExponential = curry2( (options, value) => { return toBig(value).toExponential( options.precision, options.roundingMode ?? defaultRoundingMode ); } ); BigNumber2.isNegative = (value) => { return toBig(value).lt(0); }; BigNumber2.isGtZero = (value) => { return toBig(value).gt(0); }; BigNumber2.isZero = (value) => { return toBig(value).eq(0); }; BigNumber2.isEq = curry2( (value, a) => { return toBig(value).eq(toBig(a)); } ); BigNumber2.isGt = curry2( (value, a) => { return toBig(value).gt(toBig(a)); } ); BigNumber2.isGte = curry2( (value, a) => { return toBig(value).gte(toBig(a)); } ); BigNumber2.isLt = curry2( (value, a) => { return toBig(value).lt(toBig(a)); } ); BigNumber2.isLte = curry2( (value, a) => { return toBig(value).lte(toBig(a)); } ); BigNumber2.setPrecision = curry2( (options, value) => { return fromBig( toBig( toBig(value).toPrecision( options.precision, options.roundingMode ?? defaultRoundingMode ) ) ); } ); BigNumber2.getPrecision = (value) => { return toBig(value).c.length - (toBig(value).e + 1); }; BigNumber2.getIntegerLength = (value) => { return toBig(value).e + 1; }; BigNumber2.leftMoveDecimals = curry2( (distance, value) => (0, BigNumber2.moveDecimals)({ distance }, value) ); BigNumber2.rightMoveDecimals = curry2( (distance, value) => (0, BigNumber2.moveDecimals)({ distance: -distance }, value) ); BigNumber2.moveDecimals = curry2( (options, value) => { if (options.distance > 0) { return fromBig(toBig(value).div(10 ** options.distance)); } if (options.distance < 0) { return fromBig(toBig(value).mul(10 ** -options.distance)); } return (0, BigNumber2.from)(value); } ); BigNumber2.getDecimalPart = curry2( (options, value) => { const formatted = (0, BigNumber2.toFixed)( { precision: Math.min((0, BigNumber2.getPrecision)(value), options.precision), roundingMode: BigNumber2.roundDown }, value ); const [, decimals] = formatted.split("."); if (decimals == null) return void 0; return decimals; } ); BigNumber2.abs = (value) => { return fromBig(toBig(value).abs()); }; BigNumber2.neg = (value) => { return fromBig(toBig(value).neg()); }; BigNumber2.sqrt = (value) => { return fromBig(toBig(value).sqrt()); }; BigNumber2.add = curry2( (value, a) => { return fromBig(toBig(value).add(toBig(a))); } ); BigNumber2.minus = curry2( (value, a) => { return fromBig(toBig(value).minus(toBig(a))); } ); BigNumber2.mul = curry2( (value, a) => { return fromBig(toBig(value).mul(toBig(a))); } ); BigNumber2.cumulativeMul = curry2( (value, as) => { return reduce( (acc, currentRate) => [ ...acc, BigNumber2.mul(last(acc), currentRate) ], [(0, BigNumber2.from)(value)], as ); } ); BigNumber2.div = curry2( (value, a) => { return fromBig(toBig(value).div(toBig(a))); } ); BigNumber2.pow = curry2((value, a) => { return fromBig(toBig(value).pow(a)); }); BigNumber2.round = curry2( (options, value) => { return fromBig( toBig(value).round( options.precision, options.roundingMode ?? defaultRoundingMode ) ); } ); BigNumber2.toPrecision = curry2( (options, value) => { return toBig(value).toPrecision( options.precision, options.roundingMode ?? defaultRoundingMode ); } ); BigNumber2.ascend = curry2( (a, b) => (0, BigNumber2.isLt)(a, b) ? -1 : (0, BigNumber2.isGt)(a, b) ? 1 : 0 ); BigNumber2.descend = curry2( (a, b) => (0, BigNumber2.isLt)(a, b) ? 1 : (0, BigNumber2.isGt)(a, b) ? -1 : 0 ); BigNumber2.sort = (comparator, numbers) => { const _numbers = numbers.map((a) => fromBig(toBig(a))); _numbers.sort(comparator); return _numbers; }; BigNumber2.max = (numbers) => { return (0, BigNumber2.from)((0, BigNumber2.sort)(BigNumber2.descend, numbers)[0]); }; BigNumber2.min = (numbers) => { return (0, BigNumber2.from)((0, BigNumber2.sort)(BigNumber2.ascend, numbers)[0]); }; BigNumber2.clamp = (range, n) => { const [min2, max2] = range; if ((0, BigNumber2.isGte)(n, max2)) return max2; if ((0, BigNumber2.isLte)(n, min2)) return min2; return n; }; BigNumber2.sum = (numbers) => { return numbers.map((n) => fromBig(toBig(n))).reduce((acc, n) => (0, BigNumber2.add)(acc, n), BigNumber2.ZERO); }; BigNumber2.ZERO = BigNumber2.from(0); BigNumber2.ONE = BigNumber2.from(1); })(BigNumber || (BigNumber = {})); function curry2(fn) { return function(a, b) { if (arguments.length > 1) { return fn(a, b); } return (b2) => fn(a, b2); }; } // src/utils/apiHelpers.ts async function requestAPI(sdkContext, options) { const queryPairs = Object.entries(options.query ?? {}); const querystring = new URLSearchParams(queryPairs).toString(); const res = await fetch( `${backendAPIPrefix}${options.path}`.replace(/(\w)\/\//g, "$1/") + (querystring ? "?" + querystring : ""), { method: options.method, headers: { "Content-Type": "application/json", "X-Xlink-Runtime-Env": sdkContext.backendAPI.runtimeEnv }, body: JSON.stringify(options.body, (k, v) => { if (typeof v === "bigint") { return v.toString(); } if (BigNumber.isBigNumber(v)) { return BigNumber.toString(v); } return v; }) } ); return readAPIResponse(res); } var readAPIResponse = async (resp) => { if (resp.status >= 200 && resp.status < 300) { return resp.json(); } if (resp.status === 429) { throw new TooManyRequestsError(resp); } const respText = await resp.text(); let respData = null; try { respData = JSON.parse(respText); } catch { respData = respText; } const thirdPartyError = ThirdPartyRequestError.fromSerializedError(respData); if (thirdPartyError != null) { throw thirdPartyError; } throw new RequestError( { url: resp.url, status: resp.status, body: respText }, { cause: respData } ); }; var TooManyRequestsError = class extends Error { requestUrl; retryAfter; constructor(resp, options) { const _retryAfter = Number(resp.headers.get("Retry-After")); const retryAfter = Number.isNaN(_retryAfter) ? void 0 : _retryAfter; let errMsg = `Too many requests to SDK API server`; if (retryAfter != null) { errMsg += `; retry after ${retryAfter} seconds`; } super(errMsg, { ...options, cause: options?.cause ?? resp }); this.requestUrl = resp.url; this.retryAfter = retryAfter; } }; var ThirdPartyRequestError = class _ThirdPartyRequestError extends Error { constructor(serviceName, options) { super(`Request service ${serviceName} failed`, options); this.serviceName = serviceName; } isThirdPartyRequestError = true; static fromSerializedError(err) { if (!err.isThirdPartyRequestError) return; return new _ThirdPartyRequestError(err.serviceName, { cause: err.cause }); } }; var RequestError = class extends Error { constructor(response, options) { super( `Request failed: url: ${response.url} detail: ${response.body}`, options ); this.response = response; } }; // src/bitcoinUtils/apiHelpers/createRevealTx.ts async function createRevealTx(sdkContext, info) { const resp = await requestAPI(sdkContext, { path: `/2024-10-01/bitcoin/reveal-txs`, method: "POST", body: { fromChain: info.fromChain, txId: info.txId, vout: info.vout, satsAmount: info.satsAmount.toString(), orderDataHex: (0, import_viem.toHex)(info.orderData), xlinkPegInAddress: { address: info.xlinkPegInAddress.address, scriptPubKeyHex: (0, import_viem.toHex)(info.xlinkPegInAddress.scriptPubKey) } } }); return { txHex: resp.revealTxHex }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createRevealTx }); //# sourceMappingURL=createRevealTx.js.map