All files / src/api api.ts

20% Statements 13/65
9.3% Branches 4/43
18.18% Functions 2/11
20.63% Lines 13/63

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 3826x 6x   6x   6x             6x                 2x 2x                         2x             2x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 2x             2x 2x 2x                                  
import axios from "axios";
import { sleep } from "../mina";
 
import config from "../config";
import { MinaNFTCommitData } from "../update";
const { MINNFTAPIAUTH, MINNFTAPI } = config;
 
/**
* API class for interacting with the serverless api
* @property jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
* @property endpoint The endpoint of the serverless api
*/
export class api {
  jwt: string;
  endpoint: string;
 
  /**
    * Constructor for the API class
    * @param jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
    */
  constructor(jwt: string) {
    this.jwt = jwt;
    this.endpoint = MINNFTAPI;
  }
 
  /**
    * Gets the address (publicKey) of the NFT using serverless api call
    * @param name The name of the NFT
    */
  public async lookupName(name: string): Promise<{
    success: boolean;
    error?: string;
    address?: string;
    reason?: string;
  }> {
    const result = await this.apiHub("lookupName", {
      transactions: [],
      developer: "@dfst",
      name: "lookupName",
      task: "lookupName",
      args: [name],
    });
    return {
      success: result.success,
      error: result.error,
      address: result.data === "error" ? undefined : result.data,
      reason: result.data === "error" ? "not found" : undefined,
    };
  }
 
  /**
    * Reserves the name of the NFT using serverless api call
    * @param data The data for the reserveName call
    * @param data.name The name of the NFT
    * @param data.publicKey The public key of the NFT
    */
  public async reserveName(data: { name: string; publicKey: string }): Promise<{
    success: boolean;
    error?: string;
    price: object;
    isReserved: boolean;
    signature?: string;
    reason?: string;
  }> {
    const result = await this.apiHub("reserveName", {
      transactions: [],
      developer: "@dfst",
      name: "reserveName",
      task: "reserveName",
      args: [data.name, data.publicKey],
    });
    const reserved =
      result.data === undefined ? { success: false } : result.data;
    const price: object = reserved.price ? JSON.parse(reserved.price) : {};
    return {
      success: result.success,
      error: result.error,
      price: price,
      isReserved: reserved.success ?? false,
      signature: reserved.signature,
      reason: reserved.reason ?? reserved.toString(),
    };
  }
 
  /**
    * Index the NFT using serverless api call
    * The NFT mint transaction should be included in the block before calling this function
    * otherwise it will fail and return isIndexed : false
    * @param data The data for the indexName call
    * @param data.name The name of the NFT
    */
  public async indexName(data: { name: string }): Promise<{
    success: boolean;
    isIndexed: boolean;
    error?: string;
    reason?: string;
  }> {
    const result = await this.apiHub("indexName", {
      transactions: [],
      developer: "@dfst",
      name: "indexName",
      task: "indexName",
      args: [data.name],
    });
    const isIndexed = result?.data?.success ?? false;
    return {
      success: result.success,
      isIndexed,
      error: result.error ?? "",
      reason: result?.data?.reason ?? "",
    };
  }
 
  /**
   * Mints a new NFT using serverless api call
   * @param data the data for the mint call
   * @param data.uri the uri of the metadata
   * @param data.signature the signature returned by the reserveName call
   * @param data.privateKey the private key of the address where NFT should be minted
   * @param data.useArweave true if the metadata should be uploaded to the Arweave, default is IPFS
   * @returns { success: boolean, error?: string, jobId?: string }
   * where jonId is the jobId of the minting transaction
   */
  public async mint(data: {
    uri: string;
    signature: string;
    privateKey: string;
    useArweave?: boolean;
  }): Promise<{
    success: boolean;
    error?: string;
    jobId?: string;
  }> {
    const result = await this.apiHub("mint_v3", {
      transactions: [data.uri],
      developer: "@dfst",
      name: "nft",
      task: "mint",
      args: [
        data.signature,
        data.privateKey,
        (data.useArweave ?? false).toString(),
      ],
    });
    return { success: result.success, jobId: result.data, error: result.error };
  }
 
  /**
   * Creates a new post for existing NFT using serverless api call
   * @param data the data for the post call
   * @param data.commitData the commit data
   * @param data.ownerPublicKey the owner's public key
   * @param data.nftName the name of the NFT
   * @param data.postName the name of the post
   * @returns { success: boolean, error?: string, jobId?: string }
   * where jonId is the jobId of the minting transaction
   */
  public async post(data: {
    commitData: MinaNFTCommitData;
    ownerPublicKey: string;
    nftName: string;
    postName: string;
  }): Promise<{
    success: boolean;
    error?: string;
    jobId?: string;
  }> {
    const result = await this.apiHub("post_v3", {
      transactions: data.commitData.transactions,
      developer: "@dfst",
      name: "post",
      task: "mint",
      args: [
        data.commitData.signature,
        data.commitData.address,
        data.commitData.update,
        data.ownerPublicKey,
        data.nftName,
        data.postName,
      ],
    });
    return { success: result.success, jobId: result.data, error: result.error };
  }
 
  /** 
    * Starts a new job for the proof calculation using serverless api call
    * The developer and name should correspond to the BackupPlugin of the API
    * All other parameters should correspond to the parameters of the BackupPlugin
    * @param data the data for the proof call
    * @param data.transactions the transactions
    * @param data.developer the developer
    * @param data.name the name of the job
    * @param data.task the task of the job
    * @param data.args the arguments of the job
    * @returns { success: boolean, error?: string, jobId?: string }
    * where jonId is the jobId of the job
    */
  public async proof(data: {
    transactions: string[];
    developer: string;
    name: string;
    task: string;
    args: string[];
  }): Promise<{
    success: boolean;
    error?: string;
    jobId?: string;
  }> {
    const result = await this.apiHub("proof", data);
    if (result.data === "error")
      return {
        success: false,
        error: result.error,
      };
    else
      return {
        success: result.success,
        jobId: result.data,
        error: result.error,
      };
  }
 
  /** 
    * Gets the result of the job using serverless api call
    * @param data the data for the jobResult call
    * @param data.jobId the jobId of the job
    * @returns { success: boolean, error?: string, result?: any }
    * where result is the result of the job
    * if the job is not finished yet, the result will be undefined
    * if the job failed, the result will be undefined and error will be set
    * if the job is finished, the result will be set and error will be undefined
    * if the job is not found, the result will be undefined and error will be set
    */
  public async jobResult(data: { jobId: string }): Promise<{
    success: boolean;
    error?: string;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    result?: any;
  }> {
    const result = await this.apiHub("jobResult", data);
    if (this.isError(result.data))
      return {
        success: false,
        error: result.error,
        result: result.data,
      };
    else
      return {
        success: result.success,
        error: result.error,
        result: result.data,
      };
  }
 
  /**
    * Gets the billing report for the jobs sent using JWT
    * @returns { success: boolean, error?: string, result?: any }
    * where result is the billing report
    */
  public async queryBilling(): Promise<{
    success: boolean;
    error?: string;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    result?: any;
  }> {
    const result = await this.apiHub("queryBilling", {});
    if (this.isError(result.data))
      return {
        success: false,
        error: result.error,
        result: result.data,
      };
    else
      return {
        success: result.success,
        error: result.error,
        result: result.data,
      };
  }
 
  /**
    * Waits for the job to finish
    * @param data the data for the waitForJobResult call
    * @param data.jobId the jobId of the job
    * @param data.maxAttempts the maximum number of attempts, default is 360 (2 hours)
    * @param data.interval the interval between attempts, default is 20000 (20 seconds)
    * @param data.maxErrors the maximum number of network errors, default is 10
    * @returns { success: boolean, error?: string, result?: any }
    * where result is the result of the job
    */
  public async waitForJobResult(data: {
    jobId: string;
    maxAttempts?: number;
    interval?: number;
    maxErrors?: number;
  }): Promise<{
    success: boolean;
    error?: string;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    result?: any;
  }> {
    const maxAttempts = data?.maxAttempts ?? 360; // 2 hours
    const interval = data?.interval ?? 20000;
    const maxErrors = data?.maxErrors ?? 10;
    const errorDelay = 30000; // 30 seconds
    let attempts = 0;
    let errors = 0;
    while (attempts < maxAttempts) {
      const result = await this.apiHub("jobResult", data);
      if (result.success === false) {
        errors++;
        Iif (errors > maxErrors) {
          return {
            success: false,
            error: "Too many network errors",
            result: undefined,
          };
        }
        await sleep(errorDelay * errors);
      } else {
        if (this.isError(result.data))
          return {
            success: false,
            error: result.error,
            result: result.data,
          };
        else Iif (result.data?.result !== undefined) {
          return {
            success: result.success,
            error: result.error,
            result: result.data,
          };
        }
        await sleep(interval);
      }
      attempts++;
    }
    return {
      success: false,
      error: "Timeout",
      result: undefined,
    };
  }
 
  /** 
    * Calls the serverless API
    * @param command the command of the API
    * @param data the data of the API
    * */
  private async apiHub(
    command: string,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    data: any
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
  ): Promise<{ success: boolean; data?: any; error?: any }> {
    const apiData = {
      auth: MINNFTAPIAUTH,
      command: command,
      jwtToken: this.jwt,
      data: data,
    };
 
    try {
      const response = await axios.post(this.endpoint, apiData);
      return { success: true, data: response.data };
    } catch (error) {
      console.error("catch api", error);
      return { success: false, error: error };
    }
  }
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private isError(data: any): boolean {
    Iif (data === "error") return true;
    Iif (data?.jobStatus === "failed") return true;
    Iif (typeof data === "string" && data.toLowerCase().startsWith("error"))
      return true;
    return false;
  }
}