Skip to content

Commit

Permalink
Optimize query to prevent notification from mongo db (#106)
Browse files Browse the repository at this point in the history
* Optimize query to prevent notification from mongo db

* Remove unused code

* Remove log

* Remove unused code
  • Loading branch information
KarishmaBothara authored Dec 1, 2023
1 parent 5856bb5 commit 5701e88
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 91 deletions.
34 changes: 33 additions & 1 deletion components/erc20TransfersForAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { usePages } from "@/libs/hooks";
import { usePagination } from "@/libs/stores";
import { formatAddress } from "@/libs/utils";

import { getContractDataForAddress } from "../libs/evm-api";
import {
InOutLabel,
LoadingBlock,
Expand All @@ -23,7 +24,7 @@ const PaginationTable = "accountErc20Transfers";
export default function Erc20TransfersForAddress({ walletAddress }) {
const { pages, currentPage } = usePagination(PaginationTable);

const query = useQuery(
const queryTxs = useQuery(
["erc20_transfers", walletAddress, currentPage],
() => {
return getERC20TransferForAddress(walletAddress, currentPage);
Expand All @@ -32,6 +33,37 @@ export default function Erc20TransfersForAddress({ walletAddress }) {
refetchInterval: 15_000,
}
);
const queryContractDetails = useQuery(
["evm_transactions_contract_data", walletAddress],
() => {
return getContractDataForAddress(walletAddress);
},
{
refetchInterval: 85_000,
}
);
const contractData = queryContractDetails?.data?.contractData;
const queryTxsData = queryTxs?.data?.docs;
const dataDocs = queryTxsData
? queryTxsData.map((tx) => {
const fromContract = contractData?.find(
(data) => data.address === tx.from
);
const toContract = contractData?.find((data) => data.address === tx.to);
if (fromContract) {
return { ...tx, fromContract: [fromContract] };
} else if (toContract) {
return { ...tx, toContract: [toContract] };
}
return tx;
})
: null;
const query = dataDocs
? {
...queryTxs,
data: { docs: dataDocs, totalPages: queryTxs.data.totalPages },
}
: queryTxs;
usePages(PaginationTable, query?.data?.totalPages);

return (
Expand Down
34 changes: 33 additions & 1 deletion components/erc721TransfersForAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { usePages } from "@/libs/hooks";
import { usePagination } from "@/libs/stores";
import { formatAddress } from "@/libs/utils";

import { getContractDataForAddress } from "../libs/evm-api";
import {
InOutLabel,
LoadingBlock,
Expand All @@ -23,7 +24,7 @@ const PaginationTable = "accountErc721Transfers";
export default function Erc721TransfersForAddress({ walletAddress }) {
const { pages, currentPage } = usePagination(PaginationTable);

const query = useQuery(
const queryTxs = useQuery(
["erc721_transfers", walletAddress, currentPage],
() => {
return getERC721TransferForAddress(walletAddress, currentPage);
Expand All @@ -32,6 +33,37 @@ export default function Erc721TransfersForAddress({ walletAddress }) {
refetchInterval: 15_000,
}
);
const queryContractDetails = useQuery(
["evm_transactions_contract_data", walletAddress],
() => {
return getContractDataForAddress(walletAddress);
},
{
refetchInterval: 85_000,
}
);
const contractData = queryContractDetails?.data?.contractData;
const queryTxsData = queryTxs?.data?.docs;
const dataDocs = queryTxsData
? queryTxsData.map((tx) => {
const fromContract = contractData?.find(
(data) => data.address === tx.from
);
const toContract = contractData?.find((data) => data.address === tx.to);
if (fromContract) {
return { ...tx, fromContract: [fromContract] };
} else if (toContract) {
return { ...tx, toContract: [toContract] };
}
return tx;
})
: null;
const query = dataDocs
? {
...queryTxs,
data: { docs: dataDocs, totalPages: queryTxs.data.totalPages },
}
: queryTxs;
usePages(PaginationTable, query?.data?.totalPages);

return (
Expand Down
35 changes: 34 additions & 1 deletion components/evmTransactionsForAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { getTransactionsForAddress } from "@/libs/evm-api";
import { usePages } from "@/libs/hooks";
import { usePagination } from "@/libs/stores";

import { getContractDataForAddress } from "../libs/evm-api";

const PaginationTable = "accountEvmTransactions";

export default function EvmTransactionsForAddress({ walletAddress }) {
const { pages, currentPage } = usePagination(PaginationTable);

const query = useQuery(
const queryTxs = useQuery(
["evm_transactions", walletAddress, currentPage],
() => {
return getTransactionsForAddress(walletAddress, currentPage);
Expand All @@ -20,6 +22,37 @@ export default function EvmTransactionsForAddress({ walletAddress }) {
refetchInterval: 15_000,
}
);
const queryContractDetails = useQuery(
["evm_transactions_contract_data", walletAddress],
() => {
return getContractDataForAddress(walletAddress);
},
{
refetchInterval: 85_000,
}
);
const contractData = queryContractDetails?.data?.contractData;
const queryTxsData = queryTxs?.data?.docs;
const dataDocs = queryTxsData
? queryTxsData.map((tx) => {
const fromContract = contractData?.find(
(data) => data.address === tx.from
);
const toContract = contractData?.find((data) => data.address === tx.to);
if (fromContract) {
return { ...tx, fromContract: [fromContract] };
} else if (toContract) {
return { ...tx, toContract: [toContract] };
}
return tx;
})
: null;
const query = dataDocs
? {
...queryTxs,
data: { docs: dataDocs, totalPages: queryTxs.data.totalPages },
}
: queryTxs;
usePages(PaginationTable, query?.data?.totalPages);

return (
Expand Down
4 changes: 4 additions & 0 deletions libs/evm-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export const getTransactionsForAddress = async (address, page) => {
return await fetchEvmData("getTransactionsForAddress", { address, page });
};

export const getContractDataForAddress = async (address) => {
return await fetchEvmData("getContractDataForAddress", { address });
};

export const getERC721TransferForAddress = async (address, page) => {
return await fetchEvmData("getERC721TransferForAddress", { address, page });
};
Expand Down
48 changes: 48 additions & 0 deletions pages/api/evm/getContractDataForAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { utils as ethers } from "ethers";
import type { NextApiRequest, NextApiResponse } from "next";

import { ContractData } from "@/libs/providers";
import { fetchMongoData, formatMongoData } from "@/libs/utils";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
let address: string = req.query?.address as string;

if (!ethers.isAddress(address)) throw { message: "Invalid address" };
address = ethers.getAddress(address);

const $match = {
$or: [{ address: address }, { contractCreator: address }],
};

const mongoData = await fetchMongoData(
"action/aggregate",
"Contractaddresses",
{
pipeline: [
{
$match,
},
{
$project: {
_id: false,
bytecode: true,
address: true,
contractCreator: true,
deploymentTransactionHash: true,
},
},
],
}
);
const contractData = mongoData?.documents as ContractData[];

return res.status(200).json({ isContract: true, contractData });
// return res.json(formatMongoData(agg, meta, page, limit));
} catch (err: any) {
res.status(500).json({ error: err.message });
}
}
24 changes: 0 additions & 24 deletions pages/api/evm/getERC20TransferForAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,6 @@ export default async function handler(
creates: true,
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "parsedLogs.address",
foreignField: "address",
as: "parsedLogs.contractData",
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "parsedLogs.args.from",
foreignField: "address",
as: "fromContract",
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "parsedLogs.args.to",
foreignField: "address",
as: "toContract",
},
},
],
}),
fetchMongoData("action/aggregate", "Transactions", {
Expand Down
24 changes: 0 additions & 24 deletions pages/api/evm/getERC721TransferForAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,6 @@ export default async function handler(
creates: true,
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "parsedLogs.address",
foreignField: "address",
as: "parsedLogs.contractData",
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "fromContract",
foreignField: "address",
as: "fromContract",
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "toContract",
foreignField: "address",
as: "toContract",
},
},
],
}),
fetchMongoData("action/aggregate", "Transactions", {
Expand Down
16 changes: 0 additions & 16 deletions pages/api/evm/getEVMTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@ export default async function handler(
status: true,
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "from",
foreignField: "address",
as: "fromContract",
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "to",
foreignField: "address",
as: "toContract",
},
},
],
}),
fetchMongoData("action/aggregate", "Transactions", {
Expand Down
57 changes: 33 additions & 24 deletions pages/api/evm/getTransactionsForAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,39 @@ export default async function handler(
value: true,
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "contractAddress",
foreignField: "address",
as: "parsedLogs.contractData",
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "from",
foreignField: "address",
as: "fromContract",
},
},
{
$lookup: {
from: "Contractaddresses",
localField: "to",
foreignField: "address",
as: "toContract",
},
},
//{
// $lookup:
// {
// from: <collection to join>,
// localField: <field from the input documents>,
// foreignField: <field from the documents of the "from" collection>,
// as: <output array field>
// }
// }
// {
// $lookup: {
// from: "Contractaddresses",
// localField: "contractAddress",
// foreignField: "address",
// as: "parsedLogs.contractData",
// },
// },
// {
// $lookup: {
// from: "Contractaddresses",
// localField: "from",
// foreignField: "address",
// as: "fromContract",
// },
// },
// {
// $lookup: {
// from: "Contractaddresses",
// localField: "to",
// foreignField: "address",
// as: "toContract",
// },
// },
],
}),
fetchMongoData("action/aggregate", "Transactions", {
Expand Down

0 comments on commit 5701e88

Please sign in to comment.