util.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2017-2020 @polkadot/app-contracts authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { ContractABIFn, ContractABIMessage } from '@polkadot/api-contract/types';
  5. import { StringOrNull } from '@polkadot/react-components/types';
  6. import React from 'react';
  7. import { ApiPromise } from '@polkadot/api';
  8. import { PromiseContract as Contract } from '@polkadot/api-contract';
  9. import { getContractAbi } from '@polkadot/react-components/util';
  10. import MessageSignature from '../shared/MessageSignature';
  11. export function findCallMethod (callContract: Contract | null, callMethodIndex = 0): ContractABIMessage | null {
  12. const message = callContract && callContract.abi.abi.contract.messages[callMethodIndex];
  13. return message || null;
  14. }
  15. export function getContractMethodFn (callContract: Contract | null, callMethodIndex: number | null): ContractABIFn | null {
  16. const fn = callContract && callContract.abi && callMethodIndex !== null && callContract.abi.messages[callMethodIndex];
  17. return fn || null;
  18. }
  19. export function getContractForAddress (api: ApiPromise, address: StringOrNull): Contract | null {
  20. if (!address) {
  21. return null;
  22. } else {
  23. const abi = getContractAbi(address);
  24. return abi
  25. ? new Contract(api, abi, address)
  26. : null;
  27. }
  28. }
  29. export function getCallMessageOptions (callContract: Contract | null): any[] {
  30. return callContract
  31. ? callContract.messages.map(({ def: message, def: { name }, index }): { key: string; text: React.ReactNode; value: string } => {
  32. return {
  33. key: name,
  34. text: (
  35. <MessageSignature message={message} />
  36. ),
  37. value: `${index}`
  38. };
  39. })
  40. : [];
  41. }