How to use @polkadot/api-contract - 4 common examples

To help you get started, we’ve selected a few @polkadot/api-contract examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkadot-js / apps / packages / react-components / src / util / getContractAbi.ts View on Github external
export default function getContractAbi (address: string | null): Abi | null {
  if (!address) {
    return null;
  }

  let abi: Abi | undefined;
  const meta = getAddressMeta(address, 'contract');

  try {
    const data = meta.contract && JSON.parse(meta.contract.abi);
    abi = new Abi(registry, data);
  } catch (error) {
    // invalid address, maybe
  }

  return abi || null;
}
github polkadot-js / apps / packages / app-contracts / src / store.ts View on Github external
private addCode (json: CodeJson): void {
    try {
      const abi = json.abi ? JSON.parse(json.abi) : null;
      this.allCode[json.codeHash] = {
        json,
        contractAbi: abi
          ? new Abi(registry, abi)
          : undefined
      };

      this.emit('new-code');
    } catch (error) {
      console.error(error);
    }
  }
github polkadot-js / apps / packages / app-contracts / src / ABI.tsx View on Github external
private onChange = (u8a: Uint8Array): void => {
    const { onChange, t } = this.props;
    const json = u8aToString(u8a);
    try {
      const abi = JSON.parse(json);

      if (abi.deploy || abi.messages) {
        throw new Error(t('You are using an ABI with an outdated format. Please generate a new one.'));
      }

      const contractAbi = new Abi(registry, abi);

      this.setState({
        contractAbi,
        isAbiValid: true,
        isEmpty: false,
        isError: false
      }, (): void => onChange(json, contractAbi));
    } catch (error) {
      console.error(error);

      this.setState({
        isAbiValid: false,
        isEmpty: false,
        isError: true,
        errorText: error
      }, (): void => onChange(null, null));
github polkadot-js / apps / packages / app-contracts / src / Contracts / util.tsx View on Github external
export function getContractForAddress (api: ApiPromise, address: StringOrNull): Contract | null {
  if (!address) {
    return null;
  } else {
    const abi = getContractAbi(address);
    return abi
      ? new Contract(api, abi, address)
      : null;
  }
}

@polkadot/api-contract

Interfaces for interacting with contracts and contract ABIs

Apache-2.0
Latest version published 9 days ago

Package Health Score

87 / 100
Full package analysis

Similar packages