Skip to content

Commit

Permalink
rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jtgi committed Jul 12, 2024
1 parent 9faa177 commit fe0b447
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 7 additions & 0 deletions app/lib/bullish.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export const openRankLimiter = new Bottleneck({
connection: bottleneckConnection,
});

export const nodeRpcLimiter = new Bottleneck({
maxConcurrent: 10,
datastore: "ioredis",
clearDatastore: false,
connection: bottleneckConnection,
});

export const webhookQueue = new Queue("webhookQueue", {
connection,
});
Expand Down
11 changes: 6 additions & 5 deletions app/lib/validations.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { db } from "./db.server";
import { Cast, CastId } from "@neynar/nodejs-sdk/build/neynar-api/v2";
import axios from "axios";
import { UnrecoverableError } from "bullmq";
import { openRankLimiter } from "./bullish.server";
import { nodeRpcLimiter, openRankLimiter } from "./bullish.server";

export type RuleDefinition = {
name: RuleName;
Expand Down Expand Up @@ -1890,7 +1890,7 @@ export async function holdsErc721(args: CheckFunctionArgs) {
isOwner = await getSetCache({
key: `erc721-owner:${contractAddress}:${tokenId}`,
get: async () => {
const owner = await contract.read.ownerOf([BigInt(tokenId)]);
const owner = await nodeRpcLimiter.schedule(() => contract.read.ownerOf([BigInt(tokenId)]));
return [cast.author.custody_address, ...cast.author.verifications].some(
(address) => address.toLowerCase() === owner.toLowerCase()
);
Expand All @@ -1901,7 +1901,7 @@ export async function holdsErc721(args: CheckFunctionArgs) {
for (const address of [cast.author.custody_address, ...cast.author.verifications]) {
const balance = await getSetCache({
key: `erc721-balance:${contractAddress}:${address}`,
get: () => contract.read.balanceOf([getAddress(address)]),
get: () => nodeRpcLimiter.schedule(() => contract.read.balanceOf([getAddress(address)])),
ttlSeconds: 60 * 60 * 2,
});

Expand Down Expand Up @@ -1997,7 +1997,8 @@ export async function holdsErc1155(args: CheckFunctionArgs) {
for (const address of [cast.author.custody_address, ...cast.author.verifications]) {
const balance = await getSetCache({
key: `erc1155-${contractAddress}-${address}-${tokenId}`,
get: () => contract.read.balanceOf([getAddress(address), BigInt(tokenId)]),
get: () =>
nodeRpcLimiter.schedule(() => contract.read.balanceOf([getAddress(address), BigInt(tokenId)])),
ttlSeconds: 60 * 60 * 2,
});

Expand Down Expand Up @@ -2068,7 +2069,7 @@ export async function verifyErc20Balance({
});

const balances = (await Promise.all(
wallets.map((add) => contract.read.balanceOf([getAddress(add)]))
wallets.map((add) => nodeRpcLimiter.schedule(() => contract.read.balanceOf([getAddress(add)])))
)) as bigint[];
const decimals = await contract.read.decimals();
const minBalanceBigInt = parseUnits(minBalanceRequired ?? "0", decimals);
Expand Down
11 changes: 10 additions & 1 deletion app/lib/viem.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import { arbitrum, base, mainnet, zora, optimism } from "viem/chains";

const mainnetClient = createPublicClient({
chain: mainnet,
transport: http(`https://mainnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`),
transport: fallback(
[
http(`https://mainnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`),
http(`https://eth-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`),
],
{
retryCount: 3,
retryDelay: 2000,
}
),
});

const optimismClient = createPublicClient({
Expand Down

0 comments on commit fe0b447

Please sign in to comment.