vega-frontend-monorepo/apps/token/src/routes/staking/associate/hooks.ts
Dexter Edwards c70a273e0b
feat: 0.54.0 (#949)
* chore: remove stagnet2, add stagnet3

* fix: types for transaction order submission (#932)

* test: add night runs again (#917)

Co-authored-by: Rado <rado@vegaprotocol.io>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Feat/893 anchor per network param (#907)

* feat: highlight network param targeted by anchor

* Feat/893: Highlighted key-value row colours

* Feat/893: Removed unneeded important bangs

Co-authored-by: sam-keen <samuel.kleinmann@gmail.com>

* Fix/687: Jump to block input should not allow negative block numbers or 0 (#918)

* Fix/687: Jump to block input should not allow negative block numbers

* Fix/687: Jump to block input should have a minimum of 1

* fix: text and theme icon colors (#920)

* Fix/eager connection (#923)

* fix: eager connect

* chore: add comment for eslint disable

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Task/Remove vegawallet service api client (#916)

* chore: remove generated vegawallet client code and implement in rest connector

* feat: add zod validation

* feat: handle specific auth/token delete case

* feat: make withdraw dialog match vega tx dialog

* fix: response stub to be right shape, add content type to requests

* chore: revert unrelated classname change

* feat: [console-lite] - fixes in order margin calcs (#891)

* feat: [console-lite] - fixes in order margin calcs

* feat: [console-lite] - fixes in order margin calcs - fix decimals

* feat: [console-lite] - fixes in order margin calcs - add unit tests

* feat: [console-lite] - fixes in order margin calcs - improve cals

* feat: [console-lite] - fixes in order margin calcs - improve cals

* feat: [console-lite] - fixes in order margin calcs - more improvements

Co-authored-by: maciek <maciek@vegaprotocol.io>

* fix: make check link unclickable while loading (#928)

* fix: make check link unclickable while loading

* fix: format

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* fix/904: Validators sorting (#913)

* fix(790): gap between orderbook headers and tabs (#929)

* feat: show correct reward type (#930)

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Task/remove vegawallet service (#926)

* feat: improve error handling

* chore: lint

* fix: cypress test incorrect assertion

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update tranches

Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* fix: order transaction types

Co-authored-by: Radosław Szpiech <szpiechrados@gmail.com>
Co-authored-by: Rado <rado@vegaprotocol.io>
Co-authored-by: mattrussell36 <mattrussell36@users.noreply.github.com>
Co-authored-by: Edd <edd@vega.xyz>
Co-authored-by: sam-keen <samuel.kleinmann@gmail.com>
Co-authored-by: Sam Keen <samuel@vegaprotocol.io>
Co-authored-by: Matthew Russell <mattrussell36@gmail.com>
Co-authored-by: macqbat <kubat.maciek@gmail.com>
Co-authored-by: maciek <maciek@vegaprotocol.io>
Co-authored-by: asiaznik <artur.siaznik@gmail.com>
Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>

* test: fix test import

* chore: resolve bad merge files

* chore: begin enum changes

* test: fix the majority of the tests

* test: fix tests

* style: lint

* fix: update enums

* fix: adjusted tests to updated enums

* ci: bump capsule

* fix: temporarily skip failing staking tests

* ci: revert capsule changes

* test: fix tests for stats

* chore: merge few changes and api updates to stagnet3 updates branch (#1100)

* chore: merge few changes and api updates to stagnet3 updates branch

* chore: [console-lite] - fast fix in unit test

Co-authored-by: maciek <maciek@vegaprotocol.io>

* test: fix broken test

* chore: change to use the new nodes rather than load balancer

* style: formatting

* style: formatting

* chore: fix env file

* fix: don't assume GQL URL path

* fix: skip positions  query if there is no part

* fix: simple trading app test

Co-authored-by: botond <105208209+notbot00@users.noreply.github.com>
Co-authored-by: Radosław Szpiech <szpiechrados@gmail.com>
Co-authored-by: Rado <rado@vegaprotocol.io>
Co-authored-by: mattrussell36 <mattrussell36@users.noreply.github.com>
Co-authored-by: Edd <edd@vega.xyz>
Co-authored-by: sam-keen <samuel.kleinmann@gmail.com>
Co-authored-by: Sam Keen <samuel@vegaprotocol.io>
Co-authored-by: Matthew Russell <mattrussell36@gmail.com>
Co-authored-by: macqbat <kubat.maciek@gmail.com>
Co-authored-by: maciek <maciek@vegaprotocol.io>
Co-authored-by: asiaznik <artur.siaznik@gmail.com>
Co-authored-by: Joe <joe@vega.xyz>
Co-authored-by: m.ray <16125548+MadalinaRaicu@users.noreply.github.com>
2022-08-22 15:50:13 -07:00

134 lines
3.7 KiB
TypeScript

import { gql, useApolloClient } from '@apollo/client';
import * as Sentry from '@sentry/react';
import React from 'react';
import { StakingMethod } from '../../../components/staking-method-radio';
import { useContracts } from '../../../contexts/contracts/contracts-context';
import { TxState } from '../../../hooks/transaction-reducer';
import { useGetAssociationBreakdown } from '../../../hooks/use-get-association-breakdown';
import { useRefreshBalances } from '../../../hooks/use-refresh-balances';
import { useTransaction } from '../../../hooks/use-transaction';
import type {
PartyStakeLinkings,
PartyStakeLinkings_party_stake_linkings,
PartyStakeLinkingsVariables,
} from './__generated__/PartyStakeLinkings';
import { useAppState } from '../../../contexts/app-state/app-state-context';
import { removeDecimal } from '@vegaprotocol/react-helpers';
import { StakeLinkingStatus } from '@vegaprotocol/types';
export const useAddStake = (
address: string,
amount: string,
vegaKey: string,
stakingMethod: StakingMethod | null,
confirmations: number
) => {
const { staking, vesting } = useContracts();
const {
appState: { decimals },
} = useAppState();
const contractAdd = useTransaction(
() => vesting.stake_tokens(removeDecimal(amount, decimals), vegaKey),
confirmations
);
const walletAdd = useTransaction(
() => staking.stake(removeDecimal(amount, decimals), vegaKey),
confirmations
);
const refreshBalances = useRefreshBalances(address);
const getAssociationBreakdown = useGetAssociationBreakdown(
address,
staking,
vesting
);
React.useEffect(() => {
if (
walletAdd.state.txState === TxState.Complete ||
contractAdd.state.txState === TxState.Complete
) {
refreshBalances();
getAssociationBreakdown();
}
}, [
contractAdd.state.txState,
refreshBalances,
walletAdd.state.txState,
getAssociationBreakdown,
]);
return React.useMemo(() => {
if (stakingMethod === StakingMethod.Contract) {
return contractAdd;
} else {
return walletAdd;
}
}, [contractAdd, stakingMethod, walletAdd]);
};
const PARTY_STAKE_LINKINGS = gql`
query PartyStakeLinkings($partyId: ID!) {
party(id: $partyId) {
id
stake {
linkings {
id
txHash
status
}
}
}
}
`;
export const usePollForStakeLinking = (
partyId: string,
txHash: string | null
) => {
const client = useApolloClient();
const [linking, setLinking] =
React.useState<PartyStakeLinkings_party_stake_linkings | null>(null);
// Query for linkings under current connected party (vega key)
React.useEffect(() => {
const interval = setInterval(() => {
if (!txHash || !partyId) return;
client
.query<PartyStakeLinkings, PartyStakeLinkingsVariables>({
query: PARTY_STAKE_LINKINGS,
variables: { partyId },
// 'network-only' doesn't work here. no-cache just means its network only plus
// the result is not stored in the cache
fetchPolicy: 'no-cache',
})
.then((res) => {
const linkings = res.data?.party?.stake.linkings;
if (!linkings?.length) return;
const matchingLinking = linkings?.find((l) => {
return (
l.txHash === txHash &&
l.status === StakeLinkingStatus.STATUS_ACCEPTED
);
});
if (matchingLinking) {
setLinking(matchingLinking);
clearInterval(interval);
}
})
.catch((err) => {
Sentry.captureException(err);
clearInterval(interval);
});
}, 1000);
return () => clearInterval(interval);
}, [partyId, client, txHash]);
return linking;
};