vega-frontend-monorepo/apps/token/src/routes/staking/associate/wallet-associate.tsx
botond 3a27172e04
feat(#175): ui-toolkit links (#453)
* feat: add enviromnemt provider to the ui-toolkit

* chore: replace etherscan links

* chore: wrap trading app into environment provider

* chore: move env provider to react-helpers and wrap every app

* chore: remove more env variables from libs and replace them with the env hook

* fix: lint

* fix: update readme with correct formatting command

* fix: warnings for web3 hook

* fix: wrap warning in conditional, print message only when env keys are missing

* fix: incorrect condition on deposit manager fauceting param

Co-authored-by: Matthew Russell <mattrussell36@gmail.com>

* fix: cleanup token app ethereum config

* chore: add better error handling to the useEnvironment hook

* fix: lint

* fix: formatting

* fix: more lint

* fix: throw error if required env variables are missing

* fix: remove default eth chain id

* fix: add back etherscan testid to withdrawals links

* fix: imports

* fix: try using babel jest for smart contracts test transpilation

* fix: uniform ts syntax

* chore: set resolveJsonModule in base tsconfig

* fix: add missing data-ids for etherscan links

Co-authored-by: Matthew Russell <mattrussell36@gmail.com>
2022-05-31 17:30:02 -07:00

116 lines
3.2 KiB
TypeScript

import React from 'react';
import { useTranslation } from 'react-i18next';
import { TokenInput } from '../../../components/token-input';
import {
AppStateActionType,
useAppState,
} from '../../../contexts/app-state/app-state-context';
import { useContracts } from '../../../contexts/contracts/contracts-context';
import { TxState } from '../../../hooks/transaction-reducer';
import { useTransaction } from '../../../hooks/use-transaction';
import { BigNumber } from '../../../lib/bignumber';
import { AssociateInfo } from './associate-info';
import type { VegaKeyExtended } from '@vegaprotocol/wallet';
import { useEnvironment } from '@vegaprotocol/react-helpers';
export const WalletAssociate = ({
perform,
vegaKey,
amount,
setAmount,
address,
}: {
perform: () => void;
amount: string;
setAmount: React.Dispatch<React.SetStateAction<string>>;
vegaKey: VegaKeyExtended;
address: string;
}) => {
const { ADDRESSES } = useEnvironment();
const { t } = useTranslation();
const {
appDispatch,
appState: { walletBalance, allowance, walletAssociatedBalance },
} = useAppState();
const { token } = useContracts();
const {
state: approveState,
perform: approve,
dispatch: approveDispatch,
} = useTransaction(() => token.approve(ADDRESSES.stakingBridge));
// Once they have approved deposits then we need to refresh their allowance
React.useEffect(() => {
const run = async () => {
if (approveState.txState === TxState.Complete) {
const allowance = await token.allowance(
address,
ADDRESSES.stakingBridge
);
appDispatch({
type: AppStateActionType.SET_ALLOWANCE,
allowance,
});
}
};
run();
}, [
address,
appDispatch,
approveState.txState,
token,
ADDRESSES.stakingBridge,
]);
let pageContent = null;
if (
walletBalance.isEqualTo('0') &&
new BigNumber(walletAssociatedBalance || 0).isEqualTo('0')
) {
pageContent = (
<div className="text-intent-danger">
{t(
'You have no VEGA tokens in your connected wallet. You will need to buy some VEGA tokens from an exchange in order to stake using this method.'
)}
</div>
);
} else if (
walletBalance.isEqualTo('0') &&
!new BigNumber(walletAssociatedBalance || 0).isEqualTo('0')
) {
pageContent = (
<div className="text-intent-danger">
{t(
'All VEGA tokens in the connected wallet is already associated with a Vega wallet/key'
)}
</div>
);
} else {
pageContent = (
<>
<AssociateInfo pubKey={vegaKey.pub} />
<TokenInput
approveText={t('Approve VEGA tokens for staking on Vega')}
submitText={t('Associate VEGA Tokens with key')}
requireApproval={true}
allowance={allowance}
approve={approve}
perform={perform}
amount={amount}
setAmount={setAmount}
maximum={walletBalance}
approveTxState={approveState}
approveTxDispatch={approveDispatch}
currency={t('VEGA Tokens')}
/>
</>
);
}
return <section data-testid="wallet-associate">{pageContent}</section>;
};