@lifestonelabs/tokensales 1.0.0 → 1.0.2

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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ # Changelog
2
+ ______________________________________________________________
3
+ ## [1.0.2] - 2026-01-30
4
+ ### Added
5
+ - `listAvailableTickets` function to list available tickets for purchase from an active TokenSale
6
+ - Support for `@electrum-cash/protocol` in addition to `@electrum-cash/network` for provider initialization
7
+
8
+ ### Changed
9
+ - Updated README to clarify that library doesn't require `@electrum-cash/network` directly (only needed via CashScript)
10
+ ______________________________________________________________
11
+ ## [1.0.1] - 2026-01-29
12
+ ### Added
13
+ - TokenSales cashscript contract for reference `src/artifacts/TokenSales.cash`
14
+ ______________________________________________________________
15
+ ## [1.0.0] - Initial Release
16
+ ### Added
17
+ - `buyNFT` - Purchase a NFT from a TokenSale
18
+ - `modifyTokenSale` - Modify TokenSale parameters (prices, endSale)
19
+ - `burn` - Burn a TokenSale NFT and all associated NFTs
20
+ - `addSatoshis` - Add satoshis to a minting NFT in users wallet
21
+ - `createListing` - Create a new TokenSale listing
22
+ - Utility functions: `parseTicketMasterCommitment`, `formatBCH`, `isTicketMaster`, `isTicket`
23
+ - Contract address helpers: `getContractAddress`, `getTokenAddress`, `getContractAddresses`
24
+ - WalletConnect signing helper: `createSignTransaction`
package/README.md CHANGED
@@ -22,8 +22,9 @@ import {
22
22
  createSignTransaction,
23
23
  getContractAddresses
24
24
  } from 'tokensales';
25
- import { Contract } from 'cashscript';
26
25
  import { ElectrumNetworkProvider } from '@electrum-cash/network';
26
+ // OR use the high-level protocol package:
27
+ // import { ElectrumNetworkProvider } from '@electrum-cash/protocol';
27
28
  import { SignClient } from '@walletconnect/sign-client';
28
29
 
29
30
  // Initialize provider
@@ -31,6 +32,9 @@ const provider = new ElectrumNetworkProvider('mainnet');
31
32
 
32
33
  // Note: The contract artifact is included in the library and compiled automatically
33
34
  // Functions return transaction objects that you can sign with your preferred method
35
+ //
36
+ // The library accepts any ElectrumNetworkProvider instance compatible with CashScript.
37
+ // You can use either @electrum-cash/network (low-level) or @electrum-cash/protocol (high-level).
34
38
  ```
35
39
 
36
40
  ### Purchase a Ticket
@@ -126,12 +130,27 @@ const wcTransactionObj = await createListing({
126
130
  priceType2: 2000n,
127
131
  priceType3: 0n,
128
132
  endSale: 950000,
129
- adminPubKeyHash: '0000000000000000000000000000000000000000' // 40 hex chars = 20bytes
133
+ adminPubKeyHash: '0000000000000000000000000000000000000000' // TokenSale creator/owners pubkeyhash. Sales revenue and admin capabilities (e.g. modify, burn) are controlled by this address. 40 hex chars = 20bytes
130
134
  });
131
135
 
132
136
  // Sign and broadcast transaction (see buyNFT example above)
133
137
  ```
134
138
 
139
+ ### List Available Tickets
140
+
141
+ ```typescript
142
+ import { listAvailableTickets } from 'tokensales';
143
+
144
+ const availableTickets = await listAvailableTickets({
145
+ electrumProvider: provider,
146
+ categoryID: 'your-token-category-id' // Hex string of the token category
147
+ });
148
+
149
+ // Returns array of ticket UTXOs that are available for purchase
150
+ // Throws error if no active TokenSale found or sale has ended
151
+ console.log(`Found ${availableTickets.length} available tickets`);
152
+ ```
153
+
135
154
  ## API Reference
136
155
 
137
156
  ### Functions
@@ -141,13 +160,12 @@ const wcTransactionObj = await createListing({
141
160
  Purchase a ticket NFT from a TokenSale.
142
161
 
143
162
  **Parameters:**
144
- - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance
163
+ - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance (from `@electrum-cash/network` or `@electrum-cash/protocol`)
145
164
  - `usersAddress: string` - User's Bitcoin Cash address
146
165
  - `ticketMasterUtxo: Utxo` - TokenSale UTXO
147
166
  - `ticketUtxo: Utxo` - Ticket UTXO to purchase
148
167
  - `ticketType: '01' | '02' | '03'` - Ticket type
149
168
  - `isAdmin: boolean` - Whether user is admin (uses minting NFT)
150
- - `contractAddress?: string` - Optional contract address override (defaults to v4)
151
169
 
152
170
  **Returns:** `Promise<WalletConnectTransactionObject>` - Transaction object ready for signing
153
171
 
@@ -156,7 +174,7 @@ Purchase a ticket NFT from a TokenSale.
156
174
  Modify TokenSale parameters (prices, endSale).
157
175
 
158
176
  **Parameters:**
159
- - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance
177
+ - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance (from `@electrum-cash/network` or `@electrum-cash/protocol`)
160
178
  - `usersAddress: string` - User's Bitcoin Cash address
161
179
  - `tokenSaleUtxo: Utxo` - TokenSale UTXO to modify
162
180
  - `userMintingUtxo: Utxo` - User's minting NFT
@@ -164,7 +182,6 @@ Modify TokenSale parameters (prices, endSale).
164
182
  - `priceType2: number | bigint` - Price for type 2 tickets
165
183
  - `priceType3: number | bigint` - Price for type 3 tickets
166
184
  - `endSale: number` - Block height when sale ends
167
- - `contractAddress?: string` - Optional contract address override (defaults to v4)
168
185
 
169
186
  **Returns:** `Promise<WalletConnectTransactionObject>` - Transaction object ready for signing
170
187
 
@@ -173,12 +190,11 @@ Modify TokenSale parameters (prices, endSale).
173
190
  Burn a TokenSale NFT and all associated tickets.
174
191
 
175
192
  **Parameters:**
176
- - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance
193
+ - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance (from `@electrum-cash/network` or `@electrum-cash/protocol`)
177
194
  - `usersAddress: string` - User's Bitcoin Cash address
178
195
  - `ticketMasterUtxo: Utxo` - TokenSale UTXO to burn (note: parameter name kept for backward compatibility)
179
196
  - `userMintingNFT: Utxo` - User's minting NFT
180
197
  - `tickets: Utxo[]` - Array of ticket UTXOs to burn
181
- - `contractAddress?: string` - Optional contract address override (defaults to v4)
182
198
 
183
199
  **Returns:** `Promise<WalletConnectTransactionObject>` - Transaction object ready for signing
184
200
 
@@ -187,7 +203,7 @@ Burn a TokenSale NFT and all associated tickets.
187
203
  Add satoshis to a minting NFT.
188
204
 
189
205
  **Parameters:**
190
- - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance
206
+ - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance (from `@electrum-cash/network` or `@electrum-cash/protocol`)
191
207
  - `usersAddress: string` - User's Bitcoin Cash address
192
208
  - `userMintingNFT: Utxo` - Minting NFT to add satoshis to
193
209
  - `additionalSatoshis: bigint` - Amount of satoshis to add
@@ -199,7 +215,7 @@ Add satoshis to a minting NFT.
199
215
  Create a new TokenSale listing.
200
216
 
201
217
  **Parameters:**
202
- - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance
218
+ - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance (from `@electrum-cash/network` or `@electrum-cash/protocol`)
203
219
  - `usersAddress: string` - User's Bitcoin Cash address
204
220
  - `userMintingNFT: Utxo` - User's minting NFT
205
221
  - `priceType1: bigint` - Price for type 1 tickets
@@ -207,11 +223,24 @@ Create a new TokenSale listing.
207
223
  - `priceType3: bigint` - Price for type 3 tickets
208
224
  - `endSale: number` - Block height when sale ends
209
225
  - `adminPubKeyHash: string` - Admin public key hash (40 hex chars)
210
- - `contractAddress?: string` - Optional contract address override (defaults to v4)
211
- - `tokenAddress?: string` - Optional token address override (defaults to v4)
212
226
 
213
227
  **Returns:** `Promise<WalletConnectTransactionObject>` - Transaction object ready for signing
214
228
 
229
+ #### `listAvailableTickets(params: ListAvailableTicketsParams): Promise<Utxo[]>`
230
+
231
+ List available tickets for purchase from an active TokenSale.
232
+
233
+ **Parameters:**
234
+ - `electrumProvider: ElectrumNetworkProvider` - Electrum provider instance (from `@electrum-cash/network` or `@electrum-cash/protocol`)
235
+ - `categoryID: string` - Token category ID (hex string) to filter tickets
236
+
237
+ **Returns:** `Promise<Utxo[]>` - Array of ticket UTXOs available for purchase
238
+
239
+ **Throws:**
240
+ - Error if no TokenSale found for the category
241
+ - Error if TokenSale has ended (current block height >= endSale block)
242
+ - Error if TokenSale commitment cannot be parsed
243
+
215
244
  ### Utilities
216
245
 
217
246
  #### `compileContract(provider: ElectrumNetworkProvider, addressType?: 'p2sh32' | 'p2sh20'): Contract`
@@ -0,0 +1,10 @@
1
+ import { Utxo } from 'cashscript';
2
+ import { ListAvailableTicketsParams } from '../types';
3
+ /**
4
+ * List available tickets for purchase from a TokenSale
5
+ * @param params - ListAvailableTicketsParams object containing all required parameters
6
+ * @returns Promise resolving to array of ticket UTXOs that are available for purchase
7
+ * @throws Error if validation fails or TokenSale is not found/active
8
+ */
9
+ export declare function listAvailableTickets(params: ListAvailableTicketsParams): Promise<Utxo[]>;
10
+ //# sourceMappingURL=listAvailableTickets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"listAvailableTickets.d.ts","sourceRoot":"","sources":["../../src/functions/listAvailableTickets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAA2B,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAItD;;;;;GAKG;AACH,wBAAsB,oBAAoB,CAAC,MAAM,EAAE,0BAA0B,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAqE9F"}
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export { modifyTokenSale } from './functions/modifyTokenSale';
7
7
  export { burn } from './functions/burn';
8
8
  export { addSatoshis } from './functions/addSatoshis';
9
9
  export { createListing } from './functions/createListing';
10
+ export { listAvailableTickets } from './functions/listAvailableTickets';
10
11
  export { parseTicketMasterCommitment, formatBCH, isTicketMaster, isTicket } from './utils/ticketMaster';
11
12
  export { default as toTokenAddress } from './utils/toTokenAddress';
12
13
  export { ensureAddressPrefix, hexToUint8Array, toLittleEndianHexString } from './utils';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE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AS,CAAC;AAExF,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAGhE,cAAc,SAAS,CAAC;AAGxB,OAAO,EAAE,0BAA0B,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAGjH,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/index.esm.js CHANGED
@@ -1453,5 +1453,61 @@ function getContractAddresses() {
1453
1453
  };
1454
1454
  }
1455
1455
 
1456
- export { AddressTicketMaster, AddressTicketMasterToken, DEFAULT_CONTRACT_ADDRESSES, TICKET_LEVEL_DESCRIPTIONS, addSatoshis, burn, buyNFT, createListing, createSignTransaction, ensureAddressPrefix, formatBCH, getContractAddress, getContractAddresses, getTokenAddress, hexToUint8Array, isTicket, isTicketMaster, modifyTokenSale, parseTicketMasterCommitment, toLittleEndianHexString, toTokenAddress };
1456
+ /**
1457
+ * List available tickets for purchase from a TokenSale
1458
+ * @param params - ListAvailableTicketsParams object containing all required parameters
1459
+ * @returns Promise resolving to array of ticket UTXOs that are available for purchase
1460
+ * @throws Error if validation fails or TokenSale is not found/active
1461
+ */
1462
+ async function listAvailableTickets(params) {
1463
+ const { electrumProvider, categoryID } = params;
1464
+ if (!electrumProvider) {
1465
+ throw new Error('Electrum provider not available');
1466
+ }
1467
+ if (!categoryID || categoryID.length === 0) {
1468
+ throw new Error('Category ID is required');
1469
+ }
1470
+ // Get the contract token address
1471
+ const contractTokenAddress = getTokenAddress();
1472
+ // Get all UTXOs from the contract token address
1473
+ const contractUtxos = await electrumProvider.getUtxos(contractTokenAddress);
1474
+ // Filter UTXOs to only those matching the categoryID
1475
+ const categoryUtxos = contractUtxos.filter((utxo) => utxo?.token?.category === categoryID);
1476
+ if (categoryUtxos.length === 0) {
1477
+ return []; // No UTXOs found for this category
1478
+ }
1479
+ // Find the TokenSale minting NFT (capability === 'minting')
1480
+ const tokenSaleUtxo = categoryUtxos.find((utxo) => isTicketMaster(utxo));
1481
+ if (!tokenSaleUtxo) {
1482
+ throw new Error(`No active TokenSale found for category ${categoryID}`);
1483
+ }
1484
+ // Parse the TokenSale commitment to get endSale block height
1485
+ const commitment = tokenSaleUtxo.token?.nft?.commitment || '';
1486
+ const tokenSaleData = parseTicketMasterCommitment(commitment);
1487
+ if (!tokenSaleData) {
1488
+ throw new Error('Failed to parse TokenSale commitment');
1489
+ }
1490
+ // Get current block height
1491
+ // ElectrumNetworkProvider may expose getBlockHeight directly, or we need to use performRequest
1492
+ let currentBlockHeight;
1493
+ if (typeof electrumProvider.getBlockHeight === 'function') {
1494
+ currentBlockHeight = await electrumProvider.getBlockHeight();
1495
+ }
1496
+ else if (typeof electrumProvider.performRequest === 'function') {
1497
+ const header = await electrumProvider.performRequest('blockchain.headers.subscribe');
1498
+ currentBlockHeight = header?.height || header;
1499
+ }
1500
+ else {
1501
+ throw new Error('Unable to get block height from provider');
1502
+ }
1503
+ // Check if sale is active (currentBlockHeight < endSale)
1504
+ if (currentBlockHeight >= tokenSaleData.endSale) {
1505
+ throw new Error(`TokenSale has ended. Current block: ${currentBlockHeight}, End sale block: ${tokenSaleData.endSale}`);
1506
+ }
1507
+ // Filter to only ticket UTXOs (capability === 'none')
1508
+ const availableTickets = categoryUtxos.filter((utxo) => isTicket(utxo, categoryID));
1509
+ return availableTickets;
1510
+ }
1511
+
1512
+ export { AddressTicketMaster, AddressTicketMasterToken, DEFAULT_CONTRACT_ADDRESSES, TICKET_LEVEL_DESCRIPTIONS, addSatoshis, burn, buyNFT, createListing, createSignTransaction, ensureAddressPrefix, formatBCH, getContractAddress, getContractAddresses, getTokenAddress, hexToUint8Array, isTicket, isTicketMaster, listAvailableTickets, modifyTokenSale, parseTicketMasterCommitment, toLittleEndianHexString, toTokenAddress };
1457
1513
  //# sourceMappingURL=index.esm.js.map