All files / src fetch.ts

35.13% Statements 13/37
0% Branches 0/8
33.33% Functions 1/3
37.14% Lines 13/35

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 9527x             27x   27x         209x 209x 209x 209x 209x 209x 209x       209x                                                                   27x                                                     27x                  
import {
  PublicKey,
  Field,
  Mina,
  fetchAccount,
  checkZkappTransaction,
} from "o1js";
import { sleep } from "./mina";
 
export async function fetchMinaAccount(params: {
  publicKey: string | PublicKey;
  tokenId?: string | Field | undefined;
  force?: boolean;
}) {
  const { publicKey, tokenId, force } = params;
  const timeout = 1000 * 60 * 10; // 10 minutes
  const startTime = Date.now();
  let result = { account: undefined };
  while (Date.now() - startTime < timeout) {
    try {
      const result = await fetchAccount({
        publicKey,
        tokenId,
      });
      return result;
      /*
      if (result.account !== undefined) return result;
      if (force !== true) return result;
      console.log(
        "Cannot fetch account",
        typeof publicKey === "string" ? publicKey : publicKey.toBase58(),
        result
      );
      */
    } catch (error: any) {
      if (force === true) console.log("Error in fetchAccount:", error);
      else {
        console.log(
          "fetchMinaAccount error",
          typeof publicKey === "string" ? publicKey : publicKey.toBase58(),
          tokenId?.toString(),
          force,
          error
        );
        return result;
      }
    }
    await sleep(1000 * 10);
  }
  console.log(
    "fetchMinaAccount timeout",
    typeof publicKey === "string" ? publicKey : publicKey.toBase58(),
    tokenId?.toString(),
    force
  );
  return result;
}
 
export async function fetchMinaActions(
  publicKey: PublicKey,
  fromActionState: Field,
  endActionState?: Field
) {
  const timeout = 1000 * 60 * 600; // 10 hours
  const startTime = Date.now();
  while (Date.now() - startTime < timeout) {
    try {
      let actions = await Mina.fetchActions(publicKey, {
        fromActionState,
        endActionState,
      });
      if (Array.isArray(actions)) return actions;
      else console.log("Cannot fetch actions - wrong format");
    } catch (error: any) {
      console.log(
        "Error in fetchMinaActions",
        error.toString().substring(0, 300)
      );
    }
    await sleep(1000 * 60 * 2);
  }
  console.log("Timeout in fetchMinaActions");
  return undefined;
}
 
export async function checkMinaZkappTransaction(hash: string) {
  try {
    const result = await checkZkappTransaction(hash);
    return result;
  } catch (error) {
    console.error("Error in checkZkappTransaction:", error);
    return { success: false };
  }
}