Skip to content

Commit

Permalink
lint and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKurt committed Sep 6, 2024
1 parent 64900ba commit 7f9e17f
Showing 1 changed file with 80 additions and 75 deletions.
155 changes: 80 additions & 75 deletions src/http/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,97 +108,102 @@ export const createHttpApi = (config: HttpApiConfig): HttpApi => {
});

app.post("/index", (req, res) => {
const reindex = async () => {
try {
const { chainId, address, timestamp, signature } = req.body as {
chainId: string;
address: string;
timestamp: number;
signature: `0x${string}`;
};

if (!chainId || !config.indexedChains) {
return res.status(400).send("chainId is required");
}

try {
const isAuthenticated = await recoverEthereumAddress({
address,
timestamp,
signature,
});

config.logger.info(
`Reindexing chain ${chainId} requested by ${address} at ${timestamp}`
);
const reindex = async () => {
if (!chainId || !config.indexedChains) {
return res.status(400).send("chainId is required");
}

if (isAuthenticated) {
await config.db.deleteChainData(Number(chainId));
try {
const isAuthenticated = await recoverEthereumAddress({
address,
timestamp,
signature,
});

const filteredIndexedChains = config.indexedChains.filter(
(chain) =>
(chain as { context: { chainId: number } }).context.chainId ===
Number(chainId)
config.logger.info(
`Reindexing chain ${chainId} requested by ${address} at ${timestamp}`
);

if (filteredIndexedChains.length === 0) {
config.logger.error(`Chain ${chainId} not found`);
return res.status(400).send("chain not found");
if (isAuthenticated) {
await config.db.deleteChainData(Number(chainId));

const filteredIndexedChains = config.indexedChains.filter(
(chain) =>
(chain as { context: { chainId: number } }).context.chainId ===
Number(chainId)
);

if (filteredIndexedChains.length === 0) {
config.logger.error(`Chain ${chainId} not found`);
return res.status(400).send("chain not found");
}

const filteredChains = config.chains.filter(
(chain) => chain.id === Number(chainId)
);

if (filteredChains.length === 0) {
config.logger.error(`Chain ${chainId} not found`);
return res.status(400).send("chain not found");
}

const chain = filteredChains[0];
const indexedChain = filteredIndexedChains[0];

chain.subscriptions.forEach((subscription) => {
indexedChain.unsubscribeFromContract({
address: subscription.address,
});

const contractName = subscription.contractName;
const subscriptionFromBlock =
subscription.fromBlock === undefined
? undefined
: BigInt(subscription.fromBlock);

indexedChain.subscribeToContract({
contract: contractName,
address: subscription.address,
fromBlock: subscriptionFromBlock || BigInt(0),
});
});
} else {
config.logger.error(
`Reindexing chain ${chainId} requested by ${address} at ${timestamp} failed authentication`
);
return res.status(401).send("Authentication failed");
}

const filteredChains = config.chains.filter(
(chain) => chain.id === Number(chainId)
} catch {
config.logger.error(
`Reindexing chain ${chainId} requested by ${address} at ${timestamp} failed with error`
);
return res.status(500).send("An error occurred");
}
};

if (filteredChains.length === 0) {
config.logger.error(`Chain ${chainId} not found`);
return res.status(400).send("chain not found");
}

const chain = filteredChains[0];
const indexedChain = filteredIndexedChains[0];

chain.subscriptions.forEach((subscription) => {
indexedChain.unsubscribeFromContract({
address: subscription.address,
});

const contractName = subscription.contractName;
const subscriptionFromBlock =
subscription.fromBlock === undefined
? undefined
: BigInt(subscription.fromBlock);

indexedChain.subscribeToContract({
contract: contractName,
address: subscription.address,
fromBlock: subscriptionFromBlock || BigInt(0),
});
});
} else {
reindex()
.then(() => {
config.logger.info(`Reindexing of chain ${chainId} finished`);
res.send("Reindexing finished");
})
.catch(() => {
config.logger.error(
`Reindexing chain ${chainId} requested by ${address} at ${timestamp} failed authentication`
`Reindexing of chain ${chainId} failed with error`
);
return res.status(401).send("Authentication failed");
}
} catch (error) {
config.logger.error(
`Reindexing chain ${chainId} requested by ${address} at ${timestamp} failed with error: ${error}`
);
return res.status(500).send("An error occurred");
}
};

reindex()
.then(() => {
config.logger.info(`Reindexing of chain ${req.body.chainId} finished`);
res.send("Reindexing finished");
})
.catch(() => {
config.logger.error(
`Reindexing of chain ${req.body.chainId} failed with error`
);
res.status(500).send("An error occurred");
});
res.status(500).send("An error occurred");
});
} catch {
config.logger.error(`Reindexing failed with error`);
res.status(500).send("An error occurred");
}
});

app.use("/api/v1", api);
Expand Down

0 comments on commit 7f9e17f

Please sign in to comment.