Skip to content

Commit

Permalink
feat: upgrade swr and fix tests (#118)
Browse files Browse the repository at this point in the history
### Description

Fixes the local development issues by upgrading swr package and some
required changes related to this upgrade.
This pr also fixes the unit tests and sets up github actions to run test
with PRs

### Other changes

no

### Related issues

- Fixes #103
  • Loading branch information
baroooo authored Oct 11, 2024
1 parent 97a75c1 commit 4738c1d
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 78 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: CI
on: [pull_request]

jobs:
build:
name: Build and Test
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
submodules: "recursive"

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"

- name: Install deps
run: yarn install

- name: Build
run: yarn build

- name: Lint
run: yarn lint

- name: Test
run: yarn test
env:
COINMARKETCAP_KEY: ${{secrets.COINMARKETCAP_KEY}}
ETHERSCAN_API: ${{secrets.ETHERSCAN_API}}
ETHPLORER_KEY: ${{secrets.ETHPLORER_KEY}}
EXCHANGE_RATE_API: ${{secrets.EXCHANGE_RATE_API}}
SENTRY_DSN: ${{secrets.SENTRY_DSN}}
ETHEREUM_RPC_URL: ${{secrets.ETHEREUM_RPC_URL}}
CELO_NODE_RPC_URL: "https://forno.celo.org"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"react": "^18.3.1",
"react-chartjs-2": "^5.2.0",
"react-dom": "^18.3.1",
"swr": "^0.5.6",
"swr": "^2.2.5",
"tailwind-merge": "^2.5.2",
"xml2js": "^0.4.23"
},
Expand Down
13 changes: 9 additions & 4 deletions src/components/ReserveComposition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ function getPercents(holdings: HoldingsApi): SliceData[] {
}

export const ReserveComposition = () => {
const { data, isLoadingCelo, isLoadingOther } = useHoldings();
const percentages = getPercents(data);
const { data, error, isLoadingCelo, isLoadingOther } = useHoldings();

if (isLoadingCelo || isLoadingOther) {
return <ReserveCompositionSkeleton assets={[...data.otherAssets]} />;
if (isLoadingCelo || isLoadingOther || error) {
return (
<ReserveCompositionSkeleton
assets={[...data.otherAssets, data.celo.custody]}
/>
);
}

const percentages = getPercents(data);

return (
<div>
<Heading className="mb-4 lg:hidden">
Expand Down
47 changes: 18 additions & 29 deletions src/hooks/useHoldings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMemo } from "react";
import { Tokens } from "src/service/Data";
import { HoldingsApi } from "src/service/holdings";
import { fetcher } from "src/utils/fetcher";
Expand Down Expand Up @@ -46,36 +45,26 @@ export default function useHoldings(): {
isLoadingCelo: boolean;
isLoadingOther: boolean;
} {
const celoHoldings = useSWR<Pick<HoldingsApi, "celo">>(
"/api/holdings/celo",
fetcher,
{
initialData: { celo: INITAL_DATA.celo },
revalidateOnMount: true,
},
);
const otherHoldings = useSWR<Pick<HoldingsApi, "otherAssets">>(
"/api/holdings/other",
fetcher,
{
initialData: { otherAssets: INITAL_DATA.otherAssets },
revalidateOnMount: true,
},
);
const error = celoHoldings.error || otherHoldings.error;
const {
data: celoHoldings,
error: celoError,
isLoading: isLoadingCelo,
} = useSWR<Pick<HoldingsApi, "celo">>("/api/holdings/celo", fetcher, {
fallbackData: { celo: INITAL_DATA.celo },
revalidateOnMount: true,
});
const {
data: otherHoldings,
error: otherError,
isLoading: isLoadingOther,
} = useSWR<Pick<HoldingsApi, "otherAssets">>("/api/holdings/other", fetcher, {
fallbackData: { otherAssets: INITAL_DATA.otherAssets },
revalidateOnMount: true,
});
const error = celoError || otherError;
//TODO: Refactor holdings data return to avoid conditional chaining & undefined values
// See: https://github.com/mento-protocol/reserve-site/issues/107
const data: HoldingsApi = { ...celoHoldings.data, ...otherHoldings.data };

const isLoadingCelo = useMemo(() => {
return (
data?.celo?.frozen?.updated === 0 || data?.celo?.unfrozen?.updated === 0
);
}, [data?.celo?.frozen?.updated, data?.celo?.unfrozen?.updated]);

const isLoadingOther = useMemo(() => {
return data?.otherAssets?.findIndex((coin) => coin.updated === 0) !== -1;
}, [data.otherAssets]);
const data: HoldingsApi = { ...celoHoldings, ...otherHoldings };

return { data, error, isLoadingCelo, isLoadingOther };
}
10 changes: 3 additions & 7 deletions src/hooks/useReserveTotals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ import useSWR from "swr";
import { sumTotalHoldings } from "@/helpers/holdings";

export const useReserveTotals = () => {
const stables = useSWR<StableValueTokensAPI>(
const { data: stables, isLoading } = useSWR<StableValueTokensAPI>(
"/api/stable-value-tokens",
fetcher,
);
const holdings = useHoldings();

const isLoading = useMemo(() => {
return !holdings.data || !stables.data;
}, [holdings.data, stables.data]);

const outstanding: number | null = useMemo(() => {
return stables?.data?.totalStableValueInUSD || null;
}, [stables.data]);
return stables?.totalStableValueInUSD || null;
}, [stables]);

const totalReserveValue = useMemo(() => {
return holdings.data ? sumTotalHoldings(holdings.data) : 1;
Expand Down
11 changes: 3 additions & 8 deletions src/hooks/useStableTokens.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import StableValueTokensAPI from "@/interfaces/stable-value-tokens";
import { SECOND } from "@/utils/TIME";
import { fetcher } from "@/utils/fetcher";
import { useMemo } from "react";
import useSWR from "swr";

export const useStableTokens = () => {
Expand All @@ -13,12 +12,12 @@ export const useStableTokens = () => {
updated: 0,
} as const;

const { data, error } = useSWR<StableValueTokensAPI>(
const { data, error, isLoading } = useSWR<StableValueTokensAPI>(
"/api/stable-value-tokens",
fetcher,
{
refreshInterval: SECOND * 10,
initialData: {
fallbackData: {
totalStableValueInUSD: 0,
tokens: [
{ ...initialOtherToken, token: "cUSD" },
Expand All @@ -32,9 +31,5 @@ export const useStableTokens = () => {
},
);

const isLoading = useMemo(() => {
return data?.tokens?.findIndex((coin) => coin.updated === 0) !== -1;
}, [data?.tokens]);

return { stables: data?.tokens, isLoading, error };
return { stables: data.tokens, isLoading, error };
};
34 changes: 21 additions & 13 deletions src/hooks/useTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,31 @@ const EMPTY_TARGETS: Allocation[] = [
];

export default function useTargets() {
const stablesData = useSWR<StableValueTokensAPI>(
"/api/stable-value-tokens",
fetcher,
{
shouldRetryOnError: true,
},
);

const holdingsApi = useHoldings();

if (stablesData.error || holdingsApi.error || !stablesData.data) {
const {
data: stableTokensData,
error: stableTokensError,
isLoading: stableTokensLoading,
} = useSWR<StableValueTokensAPI>("/api/stable-value-tokens", fetcher, {
shouldRetryOnError: true,
});

const {
data: holdingsData,
error: holdingsError,
isLoadingCelo,
isLoadingOther,
} = useHoldings();

const isLoading = stableTokensLoading || isLoadingCelo || isLoadingOther;
const error = stableTokensError || holdingsError;

if (error || isLoading || !stableTokensData || !holdingsData) {
return { data: EMPTY_TARGETS, isLoading: true };
}

const allocationData = calculateTargetAllocation(
stablesData.data,
getTotalReserveUSD(holdingsApi.data),
stableTokensData,
getTotalReserveUSD(holdingsData),
);

return {
Expand Down
8 changes: 8 additions & 0 deletions src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ export const handlers = [
);
}

if (symbol === "CELO") {
return res(
ctx.json({
data: { base: "CGLD", currency: "USD", amount: "3.9619" },
}),
);
}

return res(
ctx.json({
status: {
Expand Down
4 changes: 2 additions & 2 deletions src/providers/Celo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ describe("getCeloPrice", () => {
const balance = await getCELOPrice();
expect(balance).toEqual({
hasError: false,
source: "celoNode",
source: "coinbase",
time: 1587686400000,
value: 3.892,
value: 3.9619,
});
});
});
Expand Down
31 changes: 17 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2866,7 +2866,7 @@ __metadata:
react-chartjs-2: ^5.2.0
react-dom: ^18.3.1
sass: ^1.78.0
swr: ^0.5.6
swr: ^2.2.5
tailwind-merge: ^2.5.2
tailwindcss: ^3.4.10
ts-jest: ^29.2.5
Expand Down Expand Up @@ -6196,13 +6196,6 @@ __metadata:
languageName: node
linkType: hard

"dequal@npm:2.0.2":
version: 2.0.2
resolution: "dequal@npm:2.0.2"
checksum: 86c7a2c59f7b0797ed397c74b5fcdb744e48fc19440b70ad6ac59f57550a96b0faef3f1cfd5760ec5e6d3f7cb101f634f1f80db4e727b1dc8389bf62d977c0a0
languageName: node
linkType: hard

"dequal@npm:^2.0.3":
version: 2.0.3
resolution: "dequal@npm:2.0.3"
Expand Down Expand Up @@ -13684,14 +13677,15 @@ __metadata:
languageName: node
linkType: hard

"swr@npm:^0.5.6":
version: 0.5.7
resolution: "swr@npm:0.5.7"
"swr@npm:^2.2.5":
version: 2.2.5
resolution: "swr@npm:2.2.5"
dependencies:
dequal: 2.0.2
client-only: ^0.0.1
use-sync-external-store: ^1.2.0
peerDependencies:
react: ^16.11.0 || ^17.0.0
checksum: 1bbc7fb107565cdfa2d15d5e14cd51cac9e4087ba08d4557aedf69552186ec4eb57aeb9dc6e3021e23b2ad3638b56e392b86f95518a000842797d10e89f2405e
react: ^16.11.0 || ^17.0.0 || ^18.0.0
checksum: c6e6a5bd254951b22e5fd0930a95c7f79b5d0657f803c41ba1542cd6376623fb70b1895049d54ddde26da63b91951ae9d62a06772f82be28c1014d421e5b7aa9
languageName: node
linkType: hard

Expand Down Expand Up @@ -14443,6 +14437,15 @@ __metadata:
languageName: node
linkType: hard

"use-sync-external-store@npm:^1.2.0":
version: 1.2.2
resolution: "use-sync-external-store@npm:1.2.2"
peerDependencies:
react: ^16.8.0 || ^17.0.0 || ^18.0.0
checksum: fe07c071c4da3645f112c38c0e57beb479a8838616ff4e92598256ecce527f2888c08febc7f9b2f0ce2f0e18540ba3cde41eb2035e4fafcb4f52955037098a81
languageName: node
linkType: hard

"utf-8-validate@npm:^5.0.2":
version: 5.0.4
resolution: "utf-8-validate@npm:5.0.4"
Expand Down

0 comments on commit 4738c1d

Please sign in to comment.