Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add redis caching support for getBalance #881

Closed
wants to merge 10 commits into from

Conversation

james-a-morris
Copy link
Contributor

This PR adds the ability for redis to be employed by the vercel edge functions.

@vercel
Copy link

vercel bot commented Oct 25, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
frontend-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 19, 2023 2:28pm
goerli-frontend-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 19, 2023 2:28pm

api/_imports.ts Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required to polyfill fetch at a global level.

);
// Return the balance
return BigNumber.from(response.data.balance);
const key = `balance_${chainId}-${account}-${token}`.toLowerCase();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our keys are in checksum case, but I don't think it would hurt to make the key case insensitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer need this endpoint.

Corollary - we can get rid of the cached coingecko endpoint as well.

api/limits.ts Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We gain the ability to go back to a single function interface with getBalance

@@ -35,6 +35,7 @@ import {
BLOCK_TAG_LAG,
} from "./_constants";
import { PoolStateResult } from "./_types";
import { kv } from "@vercel/kv";
Copy link
Contributor Author

@james-a-morris james-a-morris Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setup/destroy of this redis is fully handled by vercel. It is federated in SFO, TX, and Singapore currently.

Copy link
Contributor

@dohaki dohaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments and questions

api/_imports.ts Outdated
import * as fetch from "node-fetch";

if (!globalThis.fetch) {
(globalThis as any).fetch = fetch;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: Why do we need to polyfill this? Does the SDK depend on fetch instead of axios?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - unfortunately @vercel/kv relies on fetch. There are pros/cons to trying to adapt, but the major pro here is that Vercel handles all the setup/teardown of these connections when we use their official package.

api/_utils.ts Outdated
@@ -1,3 +1,4 @@
import "./_imports";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: The file name _imports feels not very descriptive. Maybe renaming to _polyfills is better

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea agreed we should name it to something way more intuitive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

api/_utils.ts Outdated
* @returns A promise that resolves to the BigNumber of the balance
*/
export const getCachedTokenBalance = async (
export const getBalance = async (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: It feels somewhat unintuitive to me that this function getBalance returns cached balances per default. Maybe it makes sense to rename it back to getCachedTokenBalance?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

api/_utils.ts Outdated
BLOCK_TAG_LAG // We should do this for consistency
);
await kv.set(key, balance.toString(), {
ex: 60 * 5, // 5 minutes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to set the expiry time to 5 minutes? We might also make this a parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently what we have in our balance endpoint

Comment on lines 98 to 130
const isRecipientAContract =
(await sdk.utils.isContractDeployedToAddress(
recipientAddress,
provider
)) || true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm doesn't this always return true? Maybe a leftover from manual testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was for testing - it's since resolved.

@james-a-morris james-a-morris changed the base branch from james/acx-1571-estimate-fill-gas-costs-with-non-empty-message-fields to master December 14, 2023 20:20
api/_utils.ts Outdated
getProvider(Number(chainId)),
BLOCK_TAG_LAG // We should do this for consistency
);
await kv.set(key, balance.toString(), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we try catch this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - we should be able to continue even if the cache mechanism fails.

api/suggested-fees.ts Outdated Show resolved Hide resolved
api/_utils.ts Outdated
@@ -1,3 +1,4 @@
import "./_imports";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea agreed we should name it to something way more intuitive

api/_utils.ts Outdated
* @returns A promise that resolves to the BigNumber of the balance
*/
export const getCachedTokenBalance = async (
export const getBalance = async (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor

@dohaki dohaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

@dohaki
Copy link
Contributor

dohaki commented Sep 13, 2024

Closing as this was part of the performance improvements PR #1194

@dohaki dohaki closed this Sep 13, 2024
@dohaki dohaki deleted the james/add-redis branch September 13, 2024 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants