-
Notifications
You must be signed in to change notification settings - Fork 43
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
api/_imports.ts
Outdated
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
api/account-balance.ts
Outdated
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ( |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
api/suggested-fees.ts
Outdated
const isRecipientAContract = | ||
(await sdk.utils.isContractDeployedToAddress( | ||
recipientAddress, | ||
provider | ||
)) || true; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
f7a8b88
to
7d49221
Compare
api/_utils.ts
Outdated
getProvider(Number(chainId)), | ||
BLOCK_TAG_LAG // We should do this for consistency | ||
); | ||
await kv.set(key, balance.toString(), { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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/_utils.ts
Outdated
@@ -1,3 +1,4 @@ | |||
import "./_imports"; |
There was a problem hiding this comment.
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 ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍
Closing as this was part of the performance improvements PR #1194 |
This PR adds the ability for redis to be employed by the vercel edge functions.