solotto 1.0.8 → 1.0.9

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/README.md CHANGED
@@ -431,7 +431,7 @@ const ticket = await lottery.GetTicket(authority, lotteryId, ticketNumber);
431
431
 
432
432
  #### GetTickets
433
433
 
434
- Fetches all tickets for a lottery, optionally filtered by buyer.
434
+ Fetches all tickets for a lottery, optionally filtered by buyer and/or grouped by owner.
435
435
 
436
436
  ```js
437
437
  // Get all tickets
@@ -439,6 +439,9 @@ const allTickets = await lottery.GetTickets(authority, lotteryId);
439
439
 
440
440
  // Get tickets for a specific buyer
441
441
  const myTickets = await lottery.GetTickets(authority, lotteryId, buyer);
442
+
443
+ // Get all tickets grouped by owner
444
+ const grouped = await lottery.GetTickets(authority, lotteryId, false, true);
442
445
  ```
443
446
 
444
447
  | Parameter | Type | Default | Description |
@@ -446,8 +449,9 @@ const myTickets = await lottery.GetTickets(authority, lotteryId, buyer);
446
449
  | `authority` | `{publicKey}` | — | The lottery authority. |
447
450
  | `lotteryId` | `Number` | — | The lottery ID. |
448
451
  | `buyer` | `{publicKey} \| false` | `false` | Optional buyer to filter by. |
452
+ | `group` | `Boolean` | `false` | If `true`, groups tickets by owner. |
449
453
 
450
- **Returns:**
454
+ **Returns (ungrouped):**
451
455
 
452
456
  ```js
453
457
  {
@@ -468,6 +472,28 @@ const myTickets = await lottery.GetTickets(authority, lotteryId, buyer);
468
472
  }
469
473
  ```
470
474
 
475
+ **Returns (grouped, `group = true`):**
476
+
477
+ ```js
478
+ {
479
+ lotteryId: 1,
480
+ lotteryAddress: "Pubkey...",
481
+ lotteryAuth: "Pubkey...",
482
+ buyer: "All",
483
+ tickets: [
484
+ {
485
+ owner: "Pubkey...",
486
+ ticketCount: 3,
487
+ tickets: [
488
+ { owner: "Pubkey...", lottery: "Pubkey...", ticketReceipt: "Pubkey...", ticketNumber: 42, ticketPda: "Pubkey..." },
489
+ // ...
490
+ ],
491
+ },
492
+ // ... one entry per unique owner
493
+ ],
494
+ }
495
+ ```
496
+
471
497
  ---
472
498
 
473
499
  #### WatchDraw
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solotto",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Solana lottery client",
5
5
  "type": "module",
6
6
  "types": "./solotto.d.ts",
package/solotto.d.ts CHANGED
@@ -107,14 +107,23 @@ declare module "solotto" {
107
107
  ticketPda: string;
108
108
  }
109
109
 
110
+ interface GroupedTicketOwner {
111
+ /** Owner wallet public key. */
112
+ owner: string;
113
+ /** Number of tickets owned. */
114
+ ticketCount: number;
115
+ /** Array of ticket objects for this owner. */
116
+ tickets: TicketListItem[];
117
+ }
118
+
110
119
  interface TicketListResult {
111
120
  lotteryId: number;
112
121
  lotteryAddress: string;
113
122
  lotteryAuth: string;
114
123
  /** The buyer's public key, or `"All"` if unfiltered. */
115
124
  buyer: string;
116
- /** Tickets sorted descending by ticket number. */
117
- tickets: TicketListItem[];
125
+ /** Tickets sorted descending by ticket number, or grouped by owner when `group = true`. */
126
+ tickets: TicketListItem[] | GroupedTicketOwner[];
118
127
  }
119
128
 
120
129
  // ── WatchDraw Events ──────────────────────────────────────────────────
@@ -283,11 +292,12 @@ declare module "solotto" {
283
292
  ticket: number
284
293
  ): Promise<TicketInfo>;
285
294
 
286
- /** Fetch all tickets for a lottery, optionally filtered by buyer. */
295
+ /** Fetch all tickets for a lottery, optionally filtered by buyer and/or grouped by owner. */
287
296
  GetTickets(
288
297
  authority: HasPublicKey,
289
298
  lotteryId: number,
290
- buyer?: HasPublicKey | false
299
+ buyer?: HasPublicKey | false,
300
+ group?: boolean
291
301
  ): Promise<TicketListResult>;
292
302
 
293
303
  /**
package/solotto.js CHANGED
@@ -429,12 +429,7 @@ class Lottery extends EventEmitter {
429
429
  * @param {Number} lotteryId - Lottery Id
430
430
  * @param {PublicKey} buyer - Ticket Buyer Optional
431
431
  */
432
- async GetTickets(authority, lotteryId, buyer = false) {
433
- async function numberToBase58(num, byteLength = 8) {
434
- const buffer = Buffer.alloc(byteLength);
435
- buffer.writeBigUInt64LE(BigInt(num), 0);
436
- return bs58.encode(buffer);
437
- }
432
+ async GetTickets(authority, lotteryId, buyer = false, group = false) {
438
433
  const [lotteryPDA] = await this.DeriveLotteryPDA(authority.publicKey, lotteryId);
439
434
  const filters = [];
440
435
  filters.push({dataSize: 104});
@@ -446,8 +441,7 @@ class Lottery extends EventEmitter {
446
441
  while(i < data_.length){
447
442
  const data = data_[i];
448
443
  const newTicket = {};
449
- const account = await this.connection.getAccountInfo(data.pubkey);
450
- const decoded = this.TICKET_STATE.decode(account.data);
444
+ const decoded = this.TICKET_STATE.decode(data.account.data);
451
445
  newTicket.owner = new PublicKey(decoded.owner).toString();
452
446
  newTicket.lottery = new PublicKey(decoded.lottery).toString();
453
447
  newTicket.ticketReceipt = new PublicKey(decoded.ticketReceipt).toString();
@@ -457,6 +451,24 @@ class Lottery extends EventEmitter {
457
451
  i++;
458
452
  }
459
453
  tickets.sort((a, b) => b.ticketNumber - a.ticketNumber);
454
+
455
+ let processedTickets = tickets;
456
+ if(group){
457
+ const grouped = {};
458
+ tickets.forEach(ticket => {
459
+ if(!grouped[ticket.owner]){
460
+ grouped[ticket.owner] = {
461
+ owner: ticket.owner,
462
+ ticketCount: 0,
463
+ tickets: []
464
+ };
465
+ }
466
+ grouped[ticket.owner].tickets.push(ticket);
467
+ grouped[ticket.owner].ticketCount++;
468
+ });
469
+ processedTickets = Object.values(grouped);
470
+ }
471
+
460
472
  let _buyer_ = "All";
461
473
  if(buyer){_buyer_ = buyer.publicKey.toString();}
462
474
  return {
@@ -464,7 +476,7 @@ class Lottery extends EventEmitter {
464
476
  lotteryAddress: lotteryPDA.toString(),
465
477
  lotteryAuth: authority.publicKey.toString(),
466
478
  buyer: _buyer_,
467
- tickets: tickets,
479
+ tickets: processedTickets,
468
480
  };
469
481
  }
470
482