All files oracleClient.ts

54.54% Statements 6/11
50% Branches 2/4
50% Functions 2/4
54.54% Lines 6/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 1071x                                                     1x                             5x                                                                                                                   3x 1x   2x      
import { BaseClient } from './baseClient';
 
export interface Nonce {
  nonce: string;
}
 
export interface Announcement {
  announcementId: string;
  eventId: string;
  offerId: string;
  announcement: string;
  attestation?: string;
  cso: boolean;
  startDate: number;
  endDate: number;
  nonces: Nonce[];
  quantity?: number;
  outcome?: string;
}
 
export interface AnnouncementResponse {
  announcements: Announcement[];
}
 
/**
 * Oracle client
 */
export class OracleClient extends BaseClient {
  /**
   * getAnnouncements
   *
   * Get all announcements
   *
   * @returns
   */
  public getAnnouncements(
    offerId?: string,
    startDate?: number,
    endDate?: number,
    page?: number,
    limit?: number,
  ): Promise<AnnouncementResponse> {
    return this.get('/announcements', {
      offerId,
      startDate,
      endDate,
      page,
      limit,
    });
  }
 
  /**
   * getAnnouncementByEventId
   *
   * Get Announcement By Event ID
   *
   * @param {string} eventId the eventId for the DLC OracleAnnouncement
   *                         https://github.com/discreetlogcontracts/dlcspecs/blob/master/Messaging.md#oracle_event
   * @returns {Announcement}
   */
  public getAnnouncementByEventId(eventId: string): Promise<Announcement> {
    return this.get(`/announcements/${eventId}`);
  }
 
  /**
   * getAnnouncementByAnnouncementId
   *
   * Get Announcement By Annoucement ID
   *
   * @param {string} announcementId the announcementId for the DLC OracleAnnouncement
   *                         https://github.com/discreetlogcontracts/dlcspecs/blob/master/Messaging.md#oracle_event
   * @returns {Announcement}
   */
  public async getAnnouncementByAnnouncementId(
    announcementId: string,
  ): Promise<Announcement> {
    const response = await this.get(`/announcements/`, { announcementId });
    if (response.announcements.length === 0) {
      throw Error(`Announcement ${announcementId} not found`);
    }
 
    return response.announcements[0];
  }
 
  /**
   * postAnnouncementQuantity
   *
   * Specify quantity of funds entered for a particular announcement
   * Note: this function does not modify the DLC OracleAnnouncement
   *       itself but rather the Oracle internal DB
   *
   * @param {string} eventId the eventId for the DLC OracleAnnouncement
   *                         https://github.com/discreetlogcontracts/dlcspecs/blob/master/Messaging.md#oracle_event
   * @param {number} quantity the quantity entered in SATS
   * @returns {Announcement} return modified announcement
   */
  public postAnnouncementQuantity(
    eventId: string,
    quantity: number,
  ): Promise<Announcement> {
    if (!this.opts.apiKey)
      throw Error('API Key required for postAnnouncementQuantity');
 
    return this.post(`/announcements/${eventId}/quantity`, { quantity });
  }
}