refactor: use fetch rather than custom implementation
This commit is contained in:
parent
2f1d0cf224
commit
82d62ce426
@ -1,3 +1,4 @@
|
|||||||
|
import { useFetch } from '@vegaprotocol/react-helpers';
|
||||||
import type { Networks, Tranche } from '@vegaprotocol/smart-contracts-sdk';
|
import type { Networks, Tranche } from '@vegaprotocol/smart-contracts-sdk';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { APP_ENV } from '../config';
|
import { APP_ENV } from '../config';
|
||||||
@ -15,61 +16,45 @@ const TRANCHES_URLS: { [N in Networks]: string } = {
|
|||||||
|
|
||||||
export function useTranches() {
|
export function useTranches() {
|
||||||
const [tranches, setTranches] = React.useState<Tranche[] | null>(null);
|
const [tranches, setTranches] = React.useState<Tranche[] | 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], []);
|
const url = React.useMemo(() => TRANCHES_URLS[APP_ENV], []);
|
||||||
React.useEffect(() => {
|
const {
|
||||||
const run = async () => {
|
state: { data, loading, error },
|
||||||
try {
|
} = useFetch<Tranche[] | null>(url);
|
||||||
setLoading(true);
|
const processedTrances = data
|
||||||
const res = await fetch(url);
|
?.map((t) => ({
|
||||||
if (!res.ok) {
|
...t,
|
||||||
throw new Error(res.statusText);
|
tranche_start: new Date(t.tranche_start),
|
||||||
}
|
tranche_end: new Date(t.tranche_end),
|
||||||
const tranchesJson = await res.json();
|
total_added: new BigNumber(t.total_added),
|
||||||
const processedTrances = tranchesJson
|
total_removed: new BigNumber(t.total_removed),
|
||||||
.map((t: Tranche) => ({
|
locked_amount: new BigNumber(t.locked_amount),
|
||||||
...t,
|
deposits: t.deposits.map((d) => ({
|
||||||
tranche_start: new Date(t.tranche_start),
|
...d,
|
||||||
tranche_end: new Date(t.tranche_end),
|
amount: new BigNumber(d.amount),
|
||||||
total_added: new BigNumber(t.total_added),
|
})),
|
||||||
total_removed: new BigNumber(t.total_removed),
|
withdrawals: t.withdrawals.map((w) => ({
|
||||||
locked_amount: new BigNumber(t.locked_amount),
|
...w,
|
||||||
deposits: t.deposits.map((d) => ({
|
amount: new BigNumber(w.amount),
|
||||||
...d,
|
})),
|
||||||
amount: new BigNumber(d.amount),
|
users: t.users.map((u) => ({
|
||||||
})),
|
...u,
|
||||||
withdrawals: t.withdrawals.map((w) => ({
|
// @ts-ignore - types are incorrect in the SDK lib
|
||||||
...w,
|
deposits: u.deposits.map((d) => ({
|
||||||
amount: new BigNumber(w.amount),
|
...d,
|
||||||
})),
|
amount: new BigNumber(d.amount),
|
||||||
users: t.users.map((u) => ({
|
})),
|
||||||
...u,
|
// @ts-ignore - types are incorrect in the SDK lib
|
||||||
// @ts-ignore - types are incorrect in the SDK lib
|
withdrawals: u.withdrawals.map((w) => ({
|
||||||
deposits: u.deposits.map((d) => ({
|
...w,
|
||||||
...d,
|
amount: new BigNumber(w.amount),
|
||||||
amount: new BigNumber(d.amount),
|
})),
|
||||||
})),
|
total_tokens: new BigNumber(u.total_tokens),
|
||||||
// @ts-ignore - types are incorrect in the SDK lib
|
withdrawn_tokens: new BigNumber(u.withdrawn_tokens),
|
||||||
withdrawals: u.withdrawals.map((w) => ({
|
remaining_tokens: new BigNumber(u.remaining_tokens),
|
||||||
...w,
|
})),
|
||||||
amount: new BigNumber(w.amount),
|
}))
|
||||||
})),
|
.sort((a: Tranche, b: Tranche) => a.tranche_id - b.tranche_id);
|
||||||
total_tokens: new BigNumber(u.total_tokens),
|
setTranches(processedTrances ? processedTrances : null);
|
||||||
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();
|
|
||||||
}, [setTranches, url]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
tranches,
|
tranches,
|
||||||
|
Loading…
Reference in New Issue
Block a user