Claim.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2017-2019 @polkadot/app-123code 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 { Option } from '@polkadot/types';
  5. import { BalanceOf, EthereumAddress } from '@polkadot/types/interfaces';
  6. import { I18nProps } from '@polkadot/react-components/types';
  7. import React, { useContext, useEffect, useState } from 'react';
  8. import styled from 'styled-components';
  9. import { ApiContext } from '@polkadot/react-api';
  10. import { Button, Card } from '@polkadot/react-components';
  11. import { formatBalance } from '@polkadot/util';
  12. import translate from './translate';
  13. import { addrToChecksum } from './util';
  14. interface Props extends I18nProps {
  15. button: React.ReactNode;
  16. ethereumAddress: EthereumAddress | null;
  17. }
  18. function Claim ({ button, className, ethereumAddress, t }: Props): React.ReactElement<Props> | null {
  19. const { api } = useContext(ApiContext);
  20. const [claimValue, setClaimValue] = useState<BalanceOf | null>(null);
  21. const [claimAddress, setClaimAddress] = useState<EthereumAddress | null>(null);
  22. const [isBusy, setIsBusy] = useState(false);
  23. const _fetchClaim = (address: EthereumAddress): void => {
  24. setIsBusy(true);
  25. api.query.claims
  26. .claims<Option<BalanceOf>>(address)
  27. .then((claim): void => {
  28. setClaimValue(claim.unwrapOr(null));
  29. setIsBusy(false);
  30. })
  31. .catch((): void => setIsBusy(false));
  32. };
  33. useEffect((): void => {
  34. if (ethereumAddress !== claimAddress) {
  35. setClaimAddress(ethereumAddress);
  36. ethereumAddress && _fetchClaim(ethereumAddress);
  37. }
  38. }, [ethereumAddress]);
  39. if (isBusy || !claimAddress) {
  40. return null;
  41. }
  42. const hasClaim = claimValue && claimValue.gten(0);
  43. return (
  44. <Card
  45. isError={!hasClaim}
  46. isSuccess={!!hasClaim}
  47. >
  48. <div className={className}>
  49. {t('Your Ethereum account')}
  50. <h3>{addrToChecksum(claimAddress.toString())}</h3>
  51. {hasClaim && claimValue
  52. ? (
  53. <>
  54. {t('has a valid claim for')}
  55. <h2>{formatBalance(claimValue)}</h2>
  56. <Button.Group>{button}</Button.Group>
  57. </>
  58. )
  59. : (
  60. <>
  61. {t('does not appear to have a valid claim. Please double check that you have signed the transaction correctly on the correct ETH account.')}
  62. </>
  63. )}
  64. </div>
  65. </Card>
  66. );
  67. }
  68. export default translate(
  69. styled(Claim)`
  70. font-size: 1.15rem;
  71. display: flex;
  72. flex-direction: column;
  73. justify-content: center;
  74. min-height: 12rem;
  75. align-items: center;
  76. margin: 0 1rem;
  77. h3 {
  78. font-family: monospace;
  79. font-size: 1.5rem;
  80. max-width: 100%;
  81. margin: 0.5rem;
  82. overflow: hidden;
  83. text-overflow: ellipsis;
  84. white-space: nowrap;
  85. }
  86. h2 {
  87. margin: 0.5rem 0 2rem;
  88. font-family: monospace;
  89. font-size: 2.5rem;
  90. font-weight: 200;
  91. }
  92. `
  93. );