types#ContractDocument TypeScript Examples

The following examples show how to use types#ContractDocument. 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: contract.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export async function updateContract(
  db: Database,
  address: string,
  { name, tags }: Partial<ContractDocument>,
  savePair = true
): Promise<string> {
  try {
    const contract = await getContractCollection(db).findOne({ address });

    if (contract) {
      if (name) contract.name = name;
      if (tags) contract.tags = tags;

      savePair &&
        keyring.saveContract(address, {
          ...(keyring.getContract(address)?.meta || {}),
          name,
          tags,
        });

      const id = await contract.save();

      return id;
    }

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

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

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

  return collection;
}
Example #3
Source File: useTopContracts.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function useTopContracts(): DbQuery<ContractDocument[]> {
  const { db } = useDatabase();

  const query = useCallback((): Promise<ContractDocument[] | null> => {
    return findTopContracts(db);
  }, [db]);

  return useDbQuery(query);
}
Example #4
Source File: contract.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export async function findTopContracts(db: Database): Promise<ContractDocument[]> {
  return getContractCollection(db).find({}).toArray();
}
Example #5
Source File: contract.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export async function findContractByAddress(
  db: Database,
  address: string
): Promise<ContractDocument | null> {
  return (await getContractCollection(db).findOne({ address })) || null;
}
Example #6
Source File: contract.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export async function createContract(
  db: Database,
  owner: PrivateKey | null,
  {
    abi,
    address,
    codeHash,
    creator,
    date = new Date().toISOString(),
    name,
    tags = [],
  }: Partial<ContractDocument>,
  savePair = true
): Promise<ContractDocument> {
  try {
    if (!abi || !address || !codeHash || !creator || !name) {
      return Promise.reject(new Error('Missing required fields'));
    }

    if (await getContractCollection(db).findOne({ address })) {
      return Promise.reject(new Error('Contract already exists'));
    }

    const exists = await getCodeBundleCollection(db).findOne({ codeHash });

    if (!exists) {
      await createCodeBundle(db, owner, {
        abi,
        codeHash,
        creator,
        name,
        owner: publicKeyHex(owner),
        tags: [],
        date,
        instances: 1,
      });
    } else {
      exists.instances += 1;

      await exists.save();
    }

    const newContract = getContractCollection(db).create({
      abi,
      address,
      codeHash,
      creator,
      name,
      owner: publicKeyHex(owner),
      tags,
      date,
    });

    savePair && keyring.saveContract(address, { name, tags, abi });

    await newContract.save();

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

    return Promise.reject(e);
  }
}
Example #7
Source File: dropdown.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export function createContractOptions(data: ContractDocument[]): DropdownOption<string>[] {
  return data.map(({ name, address }) => ({
    label: name,
    value: address,
  }));
}