fix: use static tranches data

This commit is contained in:
Dexter 2022-05-24 12:16:53 +01:00
parent 103793c6f6
commit 0367c6bd7f
2 changed files with 68 additions and 14 deletions

View File

@ -7,20 +7,24 @@ import {
useAppState, useAppState,
} from '../contexts/app-state/app-state-context'; } from '../contexts/app-state/app-state-context';
import { BigNumber } from '../lib/bignumber'; import { BigNumber } from '../lib/bignumber';
import { useTranches } from './use-tranches';
export const useGetUserTrancheBalances = ( export const useGetUserTrancheBalances = (
address: string, address: string,
vesting: VegaVesting vesting: VegaVesting
) => { ) => {
const { appDispatch } = useAppState(); const { appDispatch } = useAppState();
const { tranches } = useTranches();
return React.useCallback(async () => { return React.useCallback(async () => {
appDispatch({ appDispatch({
type: AppStateActionType.SET_TRANCHE_ERROR, type: AppStateActionType.SET_TRANCHE_ERROR,
error: null, error: null,
}); });
try { try {
const tranches = await vesting.getAllTranches(); if (!tranches) {
const userTranches = tranches.filter((t) => return;
}
const userTranches = tranches?.filter((t) =>
t.users.some( t.users.some(
({ address: a }) => ({ address: a }) =>
a && address && a.toLowerCase() === address.toLowerCase() a && address && a.toLowerCase() === address.toLowerCase()

View File

@ -1,29 +1,79 @@
import type { Networks, Tranche } from '@vegaprotocol/smart-contracts-sdk';
import React from 'react'; import React from 'react';
import * as Sentry from '@sentry/react'; import { APP_ENV } from '../config';
import type { Tranche } from '@vegaprotocol/smart-contracts-sdk';
import { useContracts } from '../contexts/contracts/contracts-context'; import { BigNumber } from '../lib/bignumber';
const TRANCHES_URLS: { [N in Networks]: string } = {
MAINNET: 'https://static.vega.xyz/assets/mainnet-tranches.json',
TESTNET: 'https://static.vega.xyz/assets/testnet-tranches.json',
STAGNET: 'https://static.vega.xyz/assets/stagnet1-tranches.json',
STAGNET2: 'https://static.vega.xyz/assets/stagnet2-tranches.json',
DEVNET: 'https://static.vega.xyz/assets/devnet-tranches.json',
CUSTOM: '',
};
export function useTranches() { export function useTranches() {
const { vesting } = useContracts();
const [tranches, setTranches] = React.useState<Tranche[] | null>(null); const [tranches, setTranches] = React.useState<Tranche[] | null>(null);
const [error, setError] = React.useState<string | null>(null); const [error, setError] = React.useState<Error | null>(null);
const [loading, setLoading] = React.useState<boolean>(false);
const url = React.useMemo(() => TRANCHES_URLS[APP_ENV], []);
React.useEffect(() => { React.useEffect(() => {
const run = async () => { const run = async () => {
try { try {
const res = await vesting.getAllTranches(); setLoading(true);
setTranches(res); const res = await fetch(url);
} catch (err) { if (!res.ok) {
Sentry.captureException(err); throw new Error(res.statusText);
setError((err as Error).message); }
const tranchesJson = await res.json();
const processedTrances = tranchesJson
.map((t: Tranche) => ({
...t,
tranche_start: new Date(t.tranche_start),
tranche_end: new Date(t.tranche_end),
total_added: new BigNumber(t.total_added),
total_removed: new BigNumber(t.total_removed),
locked_amount: new BigNumber(t.locked_amount),
deposits: t.deposits.map((d) => ({
...d,
amount: new BigNumber(d.amount),
})),
withdrawals: t.withdrawals.map((w) => ({
...w,
amount: new BigNumber(w.amount),
})),
users: t.users.map((u) => ({
...u,
// @ts-ignore - types are incorrect in the SDK lib
deposits: u.deposits.map((d) => ({
...d,
amount: new BigNumber(d.amount),
})),
// @ts-ignore - types are incorrect in the SDK lib
withdrawals: u.withdrawals.map((w) => ({
...w,
amount: new BigNumber(w.amount),
})),
total_tokens: new BigNumber(u.total_tokens),
withdrawn_tokens: new BigNumber(u.withdrawn_tokens),
remaining_tokens: new BigNumber(u.remaining_tokens),
})),
}))
.sort((a: Tranche, b: Tranche) => a.tranche_id - b.tranche_id);
setTranches(processedTrances);
} catch (e) {
setError(e as Error);
} finally {
setLoading(false);
} }
}; };
run(); run();
}, [vesting]); }, [setTranches, url]);
return { return {
tranches, tranches,
loading,
error, error,
}; };
} }