types#CodeBundleDocument TypeScript Examples

The following examples show how to use types#CodeBundleDocument. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: codeBundle.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export async function findTopCodeBundles(
  db: Database,
  excludeOwnedBy?: PrivateKey | null,
  limit?: number
): Promise<(CodeBundleDocument & { instances: number })[]> {
  try {
    const codeBundles = (
      await getCodeBundleCollection(db)
        .find(excludeOwnedBy ? { owner: { $ne: publicKeyHex(excludeOwnedBy) } } : {})
        .toArray()
    ).slice(0, limit ? limit : undefined);

    return Promise.all(
      codeBundles.map(async codeBundle => {
        const instances = (
          await getContractCollection(db).find({ codeHash: codeBundle.codeHash }).toArray()
        ).length;

        return {
          ...(codeBundle as CodeBundleDocument),
          instances,
        };
      })
    );
  } catch (e) {
    console.error(e);

    return Promise.reject(e);
  }
}
Example #2
Source File: codeBundle.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export async function findOwnedCodeBundles(
  db: Database,
  identity: PrivateKey | null,
  limit = 2
): Promise<CodeBundleDocument[]> {
  try {
    const user = await findUser(db, identity);

    if (!user) {
      return [];
    }

    return (await getCodeBundleCollection(db).find({ owner: user.publicKey }).toArray()).slice(
      0,
      limit ? limit : undefined
    );
  } catch (e) {
    console.error(e);

    return Promise.reject(e);
  }
}
Example #3
Source File: codeBundle.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export async function searchForCodeBundle(
  db: Database,
  fragment: string
): Promise<CodeBundleDocument[] | null> {
  if (!fragment || fragment === '') {
    return null;
  }

  const matches = await db.dexie
    .table<CodeBundleDocument>('CodeBundle')
    .filter(({ name, codeHash }) => {
      const regex = new RegExp(fragment);

      return regex.test(name) || regex.test(codeHash);
    })
    .limit(10)
    .toArray();

  return matches;
}
Example #4
Source File: codeBundle.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export async function updateCodeBundle(
  db: Database,
  id: string,
  { abi, name, tags }: Partial<CodeBundleDocument>
): Promise<string> {
  try {
    const codeBundle = await getCodeBundleCollection(db).findOne({ id });

    if (codeBundle) {
      if (name) codeBundle.name = name;
      if (tags) codeBundle.tags = tags;
      if (abi) codeBundle.abi = abi;

      const id = await codeBundle.save();

      return id;
    }

    return Promise.reject(new Error('Code does not exist'));
  } catch (e) {
    console.error(e);

    return Promise.reject(e);
  }
}
Example #5
Source File: util.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function getCodeBundleCollection(db: Database): Collection<CodeBundleDocument> {
  const collection = db.collection<CodeBundleDocument>('CodeBundle');

  if (!collection) throw new Error('Code Bundle collection not found.');

  return collection;
}
Example #6
Source File: AvailableCodeBundles.tsx    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function AvailableCodeBundles() {
  const { api } = useApi();
  const { data } = useAvailableCodeBundles(PAGE_SIZE * 2);
  const [codes, setCodes] = useState<CodeBundleDocument[]>([]);

  useEffect(() => {
    data &&
      filterOnChainCode(api, data)
        .then(codes => setCodes(codes))
        .catch(console.error);
  }, [api, data]);

  if (!data || data.length === 0) {
    return null;
  }

  return (
    <>
      {codes.length > 0 && (
        <div className="text-sm py-4 text-center dark:text-gray-500">
          Or choose from a code hash below
        </div>
      )}
      <List items={codes} label="Uploaded Contract Code" />
    </>
  );
}
Example #7
Source File: useAvailableCodeBundles.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function useAvailableCodeBundles(limit = 1): DbQuery<CodeBundleDocument[]> {
  const { db, identity } = useDatabase();

  const query = useCallback(async (): Promise<CodeBundleDocument[]> => {
    return (await findOwnedCodeBundles(db, identity, limit)).sort(byDate);
  }, [db, identity, limit]);

  return useDbQuery(query);
}
Example #8
Source File: useCodeBundleSearch.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function useCodeBundleSearch(fragment: string) {
  const { db } = useDatabase();
  const { api } = useApi();

  const query = useCallback(async (): Promise<CodeBundleDocument[]> => {
    const searchResults = await searchForCodeBundle(db, fragment);
    return await filterOnChainCode(api, searchResults || []);
  }, [api, db, fragment]);

  return useDbQuery(query);
}
Example #9
Source File: index.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export async function filterOnChainCode(api: ApiPromise, items: CodeBundleDocument[]) {
  const codes: CodeBundleDocument[] = [];
  for (const item of items) {
    const isOnChain = await checkOnChainCode(api, item.codeHash);
    isOnChain && codes.push(item);
  }
  return codes;
}
Example #10
Source File: codeBundle.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export async function findCodeBundleByHash(
  db: Database,
  codeHash: string
): Promise<CodeBundleDocument | null> {
  return (await getCodeBundleCollection(db).findOne({ codeHash })) || null;
}
Example #11
Source File: codeBundle.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export async function findCodeBundleById(
  db: Database,
  id: string
): Promise<CodeBundleDocument | null> {
  return (await getCodeBundleCollection(db).findOne({ id })) || null;
}
Example #12
Source File: codeBundle.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export async function createCodeBundle(
  db: Database,
  owner: PrivateKey | null,
  {
    abi,
    codeHash,
    creator,
    id = getNewCodeBundleId(),
    instances = 1,
    name,
    tags = [],
    date = new Date().toISOString(),
  }: Partial<CodeBundleDocument>
): Promise<CodeBundleDocument> {
  try {
    if (!creator) {
      return Promise.reject(new Error('Missing creator address'));
    }

    if (!codeHash || !name) {
      return Promise.reject(new Error('Missing codeHash or name'));
    }

    const newCode = getCodeBundleCollection(db).create({
      abi,
      codeHash,
      creator,
      id,
      name,
      owner: publicKeyHex(owner),
      tags,
      date,
      instances,
    });

    await newCode.save();

    return Promise.resolve(newCode);
  } catch (e) {
    console.error(e);

    return Promise.reject(e);
  }
}
Example #13
Source File: useAvailableCodeBundles.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
function byDate(a: CodeBundleDocument, b: CodeBundleDocument): number {
  return compareDesc(parseISO(a.date), parseISO(b.date));
}