@bestinslot/wallet-kit 0.9.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import { FeePolicy } from '../lib/fee-policy';
1
2
  import { SwapFees } from '../lib/swap-reporting';
2
3
  import { BISSwapWalletInfo } from './store';
3
4
  /**
@@ -100,13 +101,18 @@ export interface SwapInfo {
100
101
  factory_address: string;
101
102
  wbtc_address: string;
102
103
  wbtc_handler_address: string;
104
+ fee_policy: FeePolicy | null;
103
105
  }
104
106
  /**
105
- * Fetches the swap's deployment info — the factory, WBTC and WBTC handler addresses — for the current network, by making an API call to the swap backend. The result is fetched once per network and cached.
107
+ * Fetches the swap's deployment info (the factory, WBTC and WBTC handler addresses, and the
108
+ * backend-owned swap fee policy) for the current network, by making an API call to the swap
109
+ * backend. The result is cached per network for 60 seconds; a response without a fee policy (an
110
+ * older backend) is returned but not cached, so the policy is picked up once the backend is
111
+ * upgraded.
106
112
  *
107
113
  * This does not require a connected wallet or a smart wallet.
108
114
  *
109
- * @returns {Promise<SwapInfo>} A promise that resolves to a SwapInfo object containing the factory address, the WBTC token address, and the WBTC handler address.
115
+ * @returns {Promise<SwapInfo>} A promise that resolves to a SwapInfo object containing the factory address, the WBTC token address, the WBTC handler address, and the fee policy (`null` if the backend does not serve one).
110
116
  */
111
117
  export declare function getSwapInfo(): Promise<SwapInfo>;
112
118
  /**
@@ -122,11 +128,12 @@ export interface SwapBalance {
122
128
  ticker: string;
123
129
  decimals: number;
124
130
  is_lp: boolean;
125
- price_sats: number;
131
+ price_sats: number | null;
126
132
  reserve_token_amt?: string;
127
133
  token_decimals?: number;
128
134
  reserve_btc_amt?: string;
129
135
  btc_decimals?: number;
136
+ quote_token?: string;
130
137
  lp_total_supply?: string;
131
138
  }
132
139
  /**
@@ -205,11 +212,13 @@ export interface PairInfo {
205
212
  price: number;
206
213
  price_change_24h: number;
207
214
  price_change_7d: number;
208
- volume_24h: bigint;
209
- volume_7d: bigint;
215
+ volume_24h: bigint | null;
216
+ volume_7d: bigint | null;
210
217
  lp_fee_tier: number;
211
- tvl: bigint;
212
- apr: number;
218
+ tvl: bigint | null;
219
+ apr: number | null;
220
+ price_quote_token: string;
221
+ exposure_sats: bigint;
213
222
  }
214
223
  export interface ListPairsResponse {
215
224
  page: number;
@@ -223,7 +232,7 @@ export interface ListPairsResponse {
223
232
  * This does not require a connected wallet or a smart wallet, so it can be used to populate a market table or pair selector before the user connects.
224
233
  *
225
234
  * @param params (Optional) An object containing the sort order (defaults to 'tvl_desc'), the page to fetch (defaults to 1), and the number of pairs per page (defaults to 20, max 100).
226
- * @returns {Promise<ListPairsResponse>} A promise that resolves to an object containing the current page, page size, total number of pairs, and an array of PairInfo objects. Each pair includes both token addresses and symbols, price, 24h/7d price change, 24h/7d volume, LP fee tier, TVL, and APR. The volumes and TVL are returned as strings from the API and converted to bigint in this function.
235
+ * @returns {Promise<ListPairsResponse>} A promise that resolves to an object containing the current page, page size, total number of pairs, and an array of PairInfo objects. Each pair includes both token addresses and symbols, price and the token it is quoted in, 24h/7d price change, 24h/7d volume, LP fee tier, TVL, APR, and BTC exposure. The volumes, TVL and exposure are returned as strings from the API and converted to bigint in this function; volume, TVL and APR are `null` for a token/token pair below the backend's BTC-exposure threshold.
227
236
  */
228
237
  export declare function listPairs(params?: ListPairsRequest): Promise<ListPairsResponse>;
229
238
  export interface GetPairVolumeRequest {
@@ -237,7 +246,7 @@ export interface GetPairVolumeResponse {
237
246
  token_b_address: string;
238
247
  token_b_symbol: string;
239
248
  period_days: number;
240
- total_volume_wbtc: string;
249
+ total_volume_wbtc: string | null;
241
250
  total_trades: number;
242
251
  start_time: string;
243
252
  end_time: string;
@@ -246,7 +255,7 @@ export interface GetPairVolumeResponse {
246
255
  * Fetches the trading volume and number of trades for a specific token pair over a given number of days by making an API call to the swap backend.
247
256
  *
248
257
  * @param params An object containing the pair address and the number of days to look back for volume data.
249
- * @returns {Promise<GetPairVolumeResponse>} A promise that resolves to an object containing the pair address, token addresses and symbols, period in days, total volume in WBTC, total number of trades, and the start and end time of the period. The total volume is returned as a string from the API and can be converted to bigint if needed.
258
+ * @returns {Promise<GetPairVolumeResponse>} A promise that resolves to an object containing the pair address, token addresses and symbols, period in days, total volume in WBTC, total number of trades, and the start and end time of the period. The total volume is returned as a string from the API and can be converted to bigint if needed; it is `null` for a token/token pair below the backend's BTC-exposure threshold.
250
259
  */
251
260
  export declare function getPairVolumeOverDays(params: GetPairVolumeRequest): Promise<GetPairVolumeResponse>;
252
261
  export interface GetKlinesRequest {
@@ -272,6 +281,7 @@ export interface GetKlinesResponse {
272
281
  token_a_symbol: string;
273
282
  token_b_address: string;
274
283
  token_b_symbol: string;
284
+ price_quote_token: string | null;
275
285
  interval: string;
276
286
  klines: Kline[];
277
287
  }
@@ -279,7 +289,7 @@ export interface GetKlinesResponse {
279
289
  * Fetches the historical price and volume data (klines) for a specific token pair and time interval by making an API call to the swap backend.
280
290
  *
281
291
  * @param params An object containing the pair address, desired time interval for the klines, limit on the number of klines to fetch (max 1000), and optional start and end timestamps to filter the klines.
282
- * @returns {Promise<GetKlinesResponse>} A promise that resolves to an object containing the pair address, token addresses and symbols, interval, and an array of kline data. Each kline includes open time, close time, open price, high price, low price, close price, volume in WBTC, and number of trades. The volume is returned as a string from the API and can be converted to bigint if needed.
292
+ * @returns {Promise<GetKlinesResponse>} A promise that resolves to an object containing the pair address, token addresses and symbols, the token prices are quoted in (`price_quote_token`), interval, and an array of kline data. Each kline includes open time, close time, open price, high price, low price, close price, volume in WBTC, and number of trades. For a token/token pair the prices are the raw base/quote ratio (quote-token base units per whole base token), with no BTC conversion. The volume is returned as a string from the API and can be converted to bigint if needed.
283
293
  */
284
294
  export declare function getKlines(params: GetKlinesRequest): Promise<GetKlinesResponse>;
285
295
  export interface GetTvlHistoryRequest {
@@ -289,7 +299,7 @@ export interface GetTvlHistoryRequest {
289
299
  export interface TvlPoint {
290
300
  timestamp: number;
291
301
  block_height: number;
292
- tvl: string;
302
+ tvl: string | null;
293
303
  }
294
304
  export interface GetTvlHistoryResponse {
295
305
  pair_address: string;
@@ -304,10 +314,10 @@ export interface GetTvlHistoryResponse {
304
314
  /**
305
315
  * Fetches a daily Total Value Locked (TVL) series for a token pair from the swap backend, for charting TVL over time.
306
316
  *
307
- * TVL is valued in WBTC sats (2 x the WBTC-side reserve). Points are one per day, oldest first, each carrying the pair's reserves as of that day (carried forward across days with no on-chain change). A pair with no WBTC side reports `wbtc_side: ''` and a zero series.
317
+ * TVL is valued in WBTC sats: 2 x the WBTC-side reserve for a WBTC pair. A token/token pair (`wbtc_side: ''`) is valued through each token's direct BTC pool as of that day, and its point is `tvl: null` on days its BTC exposure is below the backend's threshold. Points are one per day, oldest first, each carrying the pair's reserves as of that day (carried forward across days with no on-chain change).
308
318
  *
309
319
  * @param params An object with the pair address and the number of days to look back (max 365; the backend clamps out-of-range values).
310
- * @returns {Promise<GetTvlHistoryResponse>} A promise that resolves to the pair address, token addresses and symbols, which side is WBTC, the period in days, and the ascending array of `{ timestamp, block_height, tvl }` points. Each `tvl` is a string of sats and can be converted to bigint if needed.
320
+ * @returns {Promise<GetTvlHistoryResponse>} A promise that resolves to the pair address, token addresses and symbols, which side is WBTC, the period in days, and the ascending array of `{ timestamp, block_height, tvl }` points. Each `tvl` is a string of sats (or `null`, see above) and can be converted to bigint if needed.
311
321
  */
312
322
  export declare function getTvlHistory(params: GetTvlHistoryRequest): Promise<GetTvlHistoryResponse>;
313
323
  export interface GetActivityOfPairRequest {
@@ -517,6 +527,19 @@ interface RemoveLiquidityOrderResponse {
517
527
  * @returns A promise that resolves to an object indicating the success of the remove liquidity order request.
518
528
  */
519
529
  export declare function prepareAndSendRemoveLiquidityOrder(token1Addr: string, token2Addr: string, lpAmt: bigint, amt1: bigint, amt2: bigint, slippageBPS: bigint): Promise<RemoveLiquidityOrderResponse>;
530
+ /**
531
+ * The protocol-fee split the swap backend requires for a swap, from its
532
+ * `fee_policy` (see `swapFeeBps`). Exported for tests; not part of the public API.
533
+ *
534
+ * @param token1Addr The input token address (for both exact-input and exact-output swaps).
535
+ * @param token2Addr The output token address.
536
+ * @returns The fee bps on the input (`token1FeeBps`) and output (`token2FeeBps`) legs.
537
+ * @throws {Error} `MISSING_FEE_POLICY_ERROR` if the backend does not serve a fee policy.
538
+ */
539
+ export declare function getSwapFeesBps(token1Addr: string, token2Addr: string): Promise<{
540
+ token1FeeBps: bigint;
541
+ token2FeeBps: bigint;
542
+ }>;
520
543
  /**
521
544
  * Calculates the expected output amount, quoted price, and price impact for a token swap operation. The function retrieves the necessary swap information and connected wallet details, then uses a proxy to check the current balances and reserves of the tokens in the liquidity pool. It also requests the miner fee for swapping and gets the fee basis points for the tokens involved in the swap. Finally, it calls the swapRequest function to get the expected results based on the input parameters.
522
545
  *
@@ -524,7 +547,7 @@ export declare function prepareAndSendRemoveLiquidityOrder(token1Addr: string, t
524
547
  * @param tokenOutAddr The address of the token being swapped to.
525
548
  * @param amtIn The amount of the input token to be swapped, represented as a bigint.
526
549
  *
527
- * @returns A promise that resolves to an object containing the expected output amount of the token being swapped to, the quoted price for the swap, the price impact in basis points, and the fee breakdown (see `SwapFees` — `amount_out` is net of the pool fee only, with the rest charged on top).
550
+ * @returns A promise that resolves to an object containing the expected output amount of the token being swapped to, the quoted price for the swap (sats per whole token when either side is WBTC; for a token/token swap, output-token base units per whole input token, see `quotedPrice`), the price impact in basis points, and the fee breakdown (see `SwapFees`: `amount_out` is net of the pool fee only, with the rest charged on top).
528
551
  */
529
552
  export declare function getSwapResult(tokenInAddr: string, tokenOutAddr: string, amtIn: bigint): Promise<{
530
553
  amount_out: bigint;
@@ -557,7 +580,7 @@ export declare function prepareAndSendSwapOrder(token1Addr: string, token2Addr:
557
580
  * @param tokenOutAddr The address of the token being swapped to.
558
581
  * @param amtOut The amount of the output token expected from the swap, represented as a bigint.
559
582
  *
560
- * @returns A promise that resolves to an object containing the expected input amount of the token being swapped from, the quoted price for the swap, the price impact in basis points, and the fee breakdown (see `SwapFees` — `amount_in` covers the pool fee only, with the rest charged on top).
583
+ * @returns A promise that resolves to an object containing the expected input amount of the token being swapped from, the quoted price for the swap (sats per whole token when either side is WBTC; for a token/token swap, output-token base units per whole input token, see `quotedPrice`), the price impact in basis points, and the fee breakdown (see `SwapFees`: `amount_in` covers the pool fee only, with the rest charged on top).
561
584
  */
562
585
  export declare function getSwap2Result(tokenInAddr: string, tokenOutAddr: string, amtOut: bigint): Promise<{
563
586
  amount_in: bigint;
@@ -1,5 +1,29 @@
1
1
  import { BISProvider, SignFunction } from '../provider/api';
2
2
  import { BISSession, BISWallet, BISWalletProvider, BISWalletPurpose } from '../types/common';
3
+ /**
4
+ * Thrown before signing when the account active in the wallet extension differs from the
5
+ * connected session (the user switched accounts inside the wallet after connecting). The stored
6
+ * session is cleared when this is thrown; the app should prompt the user to reconnect.
7
+ */
8
+ export declare class WalletAccountChangedError extends Error {
9
+ readonly expectedAddress: string;
10
+ readonly accounts: string[];
11
+ readonly code = "WALLET_ACCOUNT_CHANGED";
12
+ /**
13
+ * @param expectedAddress The address of the connected session.
14
+ * @param accounts The addresses the wallet reports as active.
15
+ */
16
+ constructor(expectedAddress: string, accounts: string[]);
17
+ }
18
+ /**
19
+ * Emitted when the wallet's active account no longer matches the connected session. The stored
20
+ * session has already been cleared when listeners run.
21
+ */
22
+ export interface AccountChangeEvent {
23
+ provider: BISWalletProvider;
24
+ previousAddresses: string[];
25
+ accounts: string[];
26
+ }
3
27
  /**
4
28
  * Registry of wallet providers, populated per build via `registerProvider`. The
5
29
  * browser build registers the extension wallets; the server build registers only
@@ -24,6 +48,15 @@ export declare function registerProvider(name: BISWalletProvider, provider: BISP
24
48
  * @throws An error if the provider is not supported or if there is an error while fetching the wallets, with a message indicating the reason for the failure.
25
49
  */
26
50
  export declare function getWallets(provider: BISWalletProvider): Promise<BISSession>;
51
+ /**
52
+ * Subscribes to account switches made inside the connected wallet extension (supported by
53
+ * Unisat and OKX). When the wallet's active account stops matching the session, the kit clears
54
+ * the stored session and calls the listener so the app can prompt a reconnect.
55
+ *
56
+ * @param listener Called with the provider, the session's previous addresses and the wallet's new accounts.
57
+ * @returns An unsubscribe function that removes the listener.
58
+ */
59
+ export declare function subscribeToAccountChanges(listener: (event: AccountChangeEvent) => void): () => void;
27
60
  /**
28
61
  * Returns the currently connected ordinals wallet, or undefined if no ordinals wallet is connected. The function retrieves the wallet information from local storage and checks for a wallet with the purpose of 'ordinals'. If such a wallet is found, it is returned; otherwise, the function returns undefined.
29
62
  *
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The swap protocol-fee policy served by the swap backend (`/get_swap_info` →
3
+ * `fee_policy`).
4
+ */
5
+ export interface FeePolicy {
6
+ /** Total protocol fee on a swap, in basis points. */
7
+ swap_fee_bps: number;
8
+ /** Lowercased token addresses that take the whole fee, highest priority first. WBTC is always first. */
9
+ priority_tokens: string[];
10
+ /** Minimum BTC exposure (sats) for a token/token pair's volume/TVL to be reported. A decimal string. */
11
+ min_btc_exposure_sats: string;
12
+ }
13
+ export declare const MISSING_FEE_POLICY_ERROR: string;
14
+ /**
15
+ * Validates and normalises the raw `fee_policy` object from `/get_swap_info`.
16
+ * Priority addresses are lowercased.
17
+ *
18
+ * @param raw The `fee_policy` value exactly as the backend returned it.
19
+ * @returns The validated policy.
20
+ * @throws {Error} `MISSING_FEE_POLICY_ERROR` when `raw` is null/undefined (backend too old),
21
+ * or an `Invalid fee_policy…` error when a field is malformed.
22
+ */
23
+ export declare function parseFeePolicy(raw: unknown): FeePolicy;
24
+ /**
25
+ * The protocol-fee split for a swap. `token1` is always the input token, for both
26
+ * exact-input and exact-output swaps.
27
+ *
28
+ * If either side is a priority token, the higher-priority side (lower index in
29
+ * `priority_tokens`) pays the full rate and the other side pays 0. Otherwise the
30
+ * rate is split, with the odd bp on the input side: input `ceil(rate/2)`, output
31
+ * `floor(rate/2)`. Addresses are compared lowercased.
32
+ *
33
+ * @param tokenIn The input token address.
34
+ * @param tokenOut The output token address.
35
+ * @param policy The backend's fee policy.
36
+ * @returns The fee bps on the input (`token1FeeBps`) and output (`token2FeeBps`) legs.
37
+ */
38
+ export declare function swapFeeBps(tokenIn: string, tokenOut: string, policy: FeePolicy): {
39
+ token1FeeBps: bigint;
40
+ token2FeeBps: bigint;
41
+ };
@@ -78,3 +78,25 @@ export interface SwapFees {
78
78
  * @returns The fee breakdown for the quote.
79
79
  */
80
80
  export declare function buildSwapFees(amountIn: bigint, amountOut: bigint, token1FeeBps: bigint, token2FeeBps: bigint, btcFee: bigint): SwapFees;
81
+ /**
82
+ * The `quoted_price` that the swap quote functions report. The price is floored
83
+ * to hundredths in bigint arithmetic, then converted to a `number`; above
84
+ * `Number.MAX_SAFE_INTEGER` hundredths (common for token/token quotes) that
85
+ * conversion rounds to the nearest representable value. Its unit depends on the
86
+ * direction:
87
+ *
88
+ * - WBTC in: sats per whole output token.
89
+ * - Any other input, WBTC out included: output-token base units per whole input
90
+ * token. With WBTC out that is sats per whole input token. For a token/token swap
91
+ * the output is **not** scaled by its decimals, so 1 XYZ in for 2 ABC out (18
92
+ * decimals) quotes `2e18`.
93
+ *
94
+ * @param tokenInAddress The input token address.
95
+ * @param wbtcAddress The WBTC token address.
96
+ * @param amountIn The input amount, in input-token base units.
97
+ * @param amountOut The output amount, in output-token base units.
98
+ * @param decimalsIn The input token's decimals.
99
+ * @param decimalsOut The output token's decimals.
100
+ * @returns The quoted price.
101
+ */
102
+ export declare function quotedPrice(tokenInAddress: string, wbtcAddress: string, amountIn: bigint, amountOut: bigint, decimalsIn: number, decimalsOut: number): number;
@@ -48,6 +48,22 @@ export interface BISProvider {
48
48
  * @returns A promise that resolves to an array of BISWallet objects, each representing a wallet associated with the provider. This allows developers to access the wallets available through the provider and perform various operations with them. If there is an error in retrieving the wallets, the promise will be rejected with an appropriate error message.
49
49
  */
50
50
  getWallets: () => Promise<BISWallet[]>;
51
+ /**
52
+ * Returns the addresses of the account currently active in the wallet, without prompting the
53
+ * user. Implemented only by providers that expose this silently; the kit compares the result
54
+ * with the stored session before signing to detect an account switch made inside the wallet.
55
+ *
56
+ * @returns The active account's addresses, or an empty array when the wallet cannot tell (e.g. it is locked).
57
+ */
58
+ getAccounts?: () => Promise<string[]>;
59
+ /**
60
+ * Subscribes to account switches made inside the wallet. Implemented only by providers that
61
+ * emit such an event.
62
+ *
63
+ * @param handler Called with the newly active account's addresses (empty when the wallet cannot tell).
64
+ * @returns An unsubscribe function.
65
+ */
66
+ onAccountsChanged?: (handler: (accounts: string[]) => void) => () => void;
51
67
  /**
52
68
  * Signs a message using the provider. The signMessage method takes a message as a string and returns a promise that resolves to the signature of the message as a hexadecimal string. This method allows developers to sign messages using the wallets associated with the provider, enabling functionalities such as authentication or transaction signing. If there is an error in signing the message, the promise will be rejected with an appropriate error message.
53
69
  *
@@ -14,6 +14,9 @@ declare global {
14
14
 
15
15
  interface UniSat {
16
16
  requestAccounts: () => Promise<string[]>
17
+ getAccounts: () => Promise<string[]>
18
+ on: (event: 'accountsChanged', handler: (accounts: string[]) => void) => void
19
+ removeListener: (event: 'accountsChanged', handler: (accounts: string[]) => void) => void
17
20
  getNetwork: () => Promise<'livenet' | 'testnet'>
18
21
  switchNetwork: (network: string) => Promise<void>
19
22
  signMessage: (message: string, type?: 'ecdsa' | 'bip322-simple') => Promise<string>
package/dist/vue.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { ref as s, onScopeDispose as i, watch as u } from "vue";
2
- import { d as r, w as c, i as w } from "./mint-BXVHw4CQ.js";
3
- function n() {
2
+ import { d as r, R as c, i as a } from "./mint-Dm67SjcM.js";
3
+ function w() {
4
4
  const e = s(r()), t = c((o) => {
5
5
  e.value !== o && (e.value = o);
6
6
  });
7
7
  return i(t, !0), u(e, (o) => {
8
- o !== r() && w(o);
8
+ o !== r() && a(o);
9
9
  }), e;
10
10
  }
11
11
  export {
12
- n as useNetwork
12
+ w as useNetwork
13
13
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bestinslot/wallet-kit",
3
3
  "type": "module",
4
- "version": "0.9.1",
4
+ "version": "0.11.0",
5
5
  "private": false,
6
6
  "description": "A lightweight JavaScript library for connecting Bitcoin wallets with a unified API.",
7
7
  "license": "MIT",
@@ -92,8 +92,10 @@
92
92
  "dotenv": "^17.3.1",
93
93
  "eslint": "^9.39.2",
94
94
  "eslint-plugin-jsdoc": "^62.6.1",
95
+ "husky": "9.1.7",
95
96
  "jsdom": "^29.1.1",
96
- "prettier": "^3.8.1",
97
+ "lint-staged": "16.4.0",
98
+ "prettier": "3.9.9",
97
99
  "react": "^19.2.7",
98
100
  "react-dom": "^19.2.7",
99
101
  "solc": "^0.8.33",
@@ -113,13 +115,18 @@
113
115
  "publishConfig": {
114
116
  "access": "public"
115
117
  },
118
+ "lint-staged": {
119
+ "*": "prettier --write --ignore-unknown"
120
+ },
116
121
  "scripts": {
117
122
  "dev": "vite",
118
123
  "build": "vue-tsc -b && vite build",
119
124
  "build:debug": "vue-tsc -b && vite build --minify=false",
120
125
  "preview": "vite preview",
121
126
  "lint": "eslint",
122
- "lint:fix": "prettier --write . && eslint --fix",
127
+ "lint:fix": "eslint --fix && prettier --write .",
128
+ "format": "prettier --write .",
129
+ "format:check": "prettier --check .",
123
130
  "test": "vitest run",
124
131
  "release": "node scripts/release.mjs",
125
132
  "release:tag": "node scripts/release.mjs --tag",