Skip to content

Commit

Permalink
Merge pull request #275 from sunrise-stake/feature/optimisations
Browse files Browse the repository at this point in the history
Feature/optimisations
  • Loading branch information
dankelleher authored Jul 25, 2023
2 parents 6a64cf4 + ae81f1f commit 88938c2
Show file tree
Hide file tree
Showing 67 changed files with 537 additions and 1,086 deletions.
1 change: 1 addition & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@brokerloop/ttlcache": "^3.2.3",
"@civic/profile": "^0.1.5",
"@headlessui/react": "^1.7.12",
"@heroicons/react": "^2.0.13",
Expand Down
Binary file removed packages/app/public/earth_day/Leaves.png
Binary file not shown.
29 changes: 0 additions & 29 deletions packages/app/public/earth_day/drip-logo.svg

This file was deleted.

Binary file removed packages/app/public/earth_day/hero_background.png
Binary file not shown.
Binary file removed packages/app/public/earth_day/hero_picture.png
Binary file not shown.
Binary file removed packages/app/public/earth_day/nfts1.png
Binary file not shown.
Binary file removed packages/app/public/earth_day/nfts2.png
Binary file not shown.
Binary file removed packages/app/public/earth_day/nfts3.png
Binary file not shown.
Binary file removed packages/app/public/earth_day/nfts4.png
Binary file not shown.
Binary file removed packages/app/public/earth_day/nfts5.png
Binary file not shown.
Binary file removed packages/app/public/earth_day/nfts6.png
Binary file not shown.
Binary file removed packages/app/public/earth_day/tipjar.png
Binary file not shown.
9 changes: 8 additions & 1 deletion packages/app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ForestProvider } from "./common/context/forestContext";
import { HelpProvider } from "./common/context/HelpContext";
import { Routes } from "./Routes";
import { NFTsProvider } from "./common/context/NFTsContext";
import { cachedRPCFetch } from "./api/cachedRPCFetch";

require("./solana-wallet-adapter.css");

Expand All @@ -48,7 +49,13 @@ const App: FC = () => {

return (
<>
<ConnectionProvider endpoint={endpoint}>
<ConnectionProvider
endpoint={endpoint}
config={{
fetch: cachedRPCFetch,
commitment: "confirmed",
}}
>
<WalletProvider wallets={wallets} autoConnect>
<WalletModalProvider>
<SunriseProvider>
Expand Down
19 changes: 0 additions & 19 deletions packages/app/src/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Link, useLocation } from "react-router-dom";
import { Layout } from "./common/partials/Layout";
import { debounce } from "./common/utils";
import { useHelp } from "./common/context/HelpContext";
import { TipjarApp } from "./tipjar/TipjarApp";

export enum AppRoute {
Connect = "/connect", // not a route at present TODO fix
Expand All @@ -24,7 +23,6 @@ export enum AppRoute {
Hub = "/",
Lock = "/lock",
Stake = "/stake",
TipJar = "/earthday",
}

export const Routes: FC = () => {
Expand All @@ -36,7 +34,6 @@ export const Routes: FC = () => {
locking: useRef<null | HTMLDivElement>(null),
lost: useRef<null | HTMLDivElement>(null),
staking: useRef<null | HTMLDivElement>(null),
tipjar: useRef<null | HTMLDivElement>(null),
};
const [currentRouteApp, setCurrentRouteApp] =
useState<null | MutableRefObject<null | HTMLDivElement>>(null);
Expand Down Expand Up @@ -109,16 +106,6 @@ export const Routes: FC = () => {
setCurrentHelpRoute(AppRoute.Stake);
},
},
{
path: AppRoute.TipJar,
onMatch: () => {
appRefs.tipjar.current?.scrollIntoView({
behavior: "smooth",
});
setCurrentRouteApp(appRefs.tipjar);
setCurrentHelpRoute(AppRoute.TipJar);
},
},
{
path: "/*",
onMatch: () => {
Expand Down Expand Up @@ -155,12 +142,6 @@ export const Routes: FC = () => {
ref={appRefs.staking}
active={currentRouteApp === appRefs.staking}
/>
<TipjarApp
id="tipjar-app"
className="App TipjarApp"
ref={appRefs.tipjar}
active={currentRouteApp === appRefs.tipjar}
/>
<div
id="lost-app"
className="App LostApp flex flex-col min-h-screen justify-center items-center"
Expand Down
45 changes: 45 additions & 0 deletions packages/app/src/api/cachedRPCFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { TTLCache } from "@brokerloop/ttlcache";

const cache = new TTLCache<string, Response>({
ttl: 3000,
max: 10,
clock: Date,
});

const CACHEABLE_RPC_METHODS = [
"getBalance",
"getAccountInfo",
"getEpochInfo",
"getTokenAccountBalance",
"getTokenSupply",
];

const cacheableRequest = (options: RequestInit): boolean => {
const parsedBody = JSON.parse(options.body as string);
return CACHEABLE_RPC_METHODS.includes(parsedBody.method);
};

const requestToKey = (url: RequestInfo | URL, options: RequestInit): string => {
const parsedBody = JSON.parse(options.body as string);
delete parsedBody.id;
return `${JSON.stringify(url)}${JSON.stringify(parsedBody)}`;
};

export const cachedRPCFetch: typeof fetch = async (url, options) => {
if (options && cacheableRequest(options)) {
const cacheKey = requestToKey(url, options);
const cachedResponse = cache.get(cacheKey);
if (cachedResponse) {
console.log("cache hit");
return cachedResponse.clone();
} else {
console.log("cache miss - key: ", cacheKey);
}
}
const response = await fetch(url, options);
if (response.ok && options && cacheableRequest(options)) {
const cacheKey = requestToKey(url, options);
cache.set(cacheKey, response.clone());
}
return response;
};
36 changes: 36 additions & 0 deletions packages/app/src/api/db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {
type GetNeighboursResponse,
type Mint,
type MintResponse,
type MongoResponse,
type NeighbourEntry,
type RawGetNeighboursResponse,
type RawNeighbourEntry,
type Transfer,
type TransferResponse,
} from "./types";
Expand All @@ -16,6 +20,7 @@ const STUBS = {
sender: sendingStub,
recipient: receiptStub,
};
const GET_NEIGHBOURS_URL = process.env.REACT_APP_GET_NEIGHBOURS_URL ?? "";
const MONGODB_API_URL = process.env.REACT_APP_MONGODB_API_URL ?? "";
const MONGODB_READ_TOKEN = process.env.REACT_APP_MONGODB_READ_TOKEN ?? "";
const buildTransferRecord = (transfer: TransferResponse): Transfer => ({
Expand All @@ -30,6 +35,37 @@ const buildMintRecord = (mint: MintResponse): Mint => ({
sender: mint.sender !== undefined ? new PublicKey(mint.sender) : undefined,
amount: mint.amount,
});

const fromRawNeighbourEntry = (
rawEntry: RawNeighbourEntry
): NeighbourEntry => ({
...rawEntry,
address: new PublicKey(rawEntry.address),
start: new Date(rawEntry.start),
end: new Date(rawEntry.end),
sender: new PublicKey(rawEntry.sender),
recipient: new PublicKey(rawEntry.recipient),
});

export const getNeighbours = async (
address: PublicKey,
depth: number
): Promise<GetNeighboursResponse> =>
fetch(`${GET_NEIGHBOURS_URL}/${address.toBase58()}?depth=${depth}`)
.then(async (resp) => resp.json() as Promise<RawGetNeighboursResponse>)
.then((rawResponse) => ({
firstTransfer: new Date(rawResponse.firstTransfer),
lastTransfer: new Date(rawResponse.lastTransfer),
neighbours: {
senderResult: rawResponse.neighbours.senderResult.map(
fromRawNeighbourEntry
),
recipientResult: rawResponse.neighbours.recipientResult.map(
fromRawNeighbourEntry
),
},
}));

const getDBData = async <T>(
collection: "mints" | "transfers",
types: Array<"recipient" | "sender">,
Expand Down
Loading

0 comments on commit 88938c2

Please sign in to comment.