Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #110 from factchain/fix_mongodb
Browse files Browse the repository at this point in the history
[backend] close mongodb connection
  • Loading branch information
YBadiss authored Mar 27, 2024
2 parents fd90901 + b515f01 commit 2989dd8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 60 deletions.
68 changes: 38 additions & 30 deletions fc-community-backend/apps/api/src/factchain-core/web2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class DBConnector implements NoteReader {
private _config: Config;
private _network: NetworkConfig;
private _fcCommunity: ethers.Contract;
private _mongoClient: MongoClient;
private _mainWallet: ethers.Signer;
private _provider: ethers.AbstractProvider;

Expand All @@ -29,49 +28,58 @@ export class DBConnector implements NoteReader {
config.NOTE_FINALISER_PKEY,
this._provider,
);
this._fcCommunity = new ethers.Contract(
this._network.MAIN_CONTRACT_ADDRESS,
FC_COMMUNITY_JSON_ABI,
this._mainWallet,
);
}

getClient = () => {
const uri = `mongodb+srv://${this._config.MONGO_USER}:${this._config.MONGO_PASSWORD}@${this._config.MONGO_CLUSTER}/?w=majority&appName=${this._config.MONGO_APP_NAME}`;
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
this._mongoClient = new MongoClient(uri, {
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
this._fcCommunity = new ethers.Contract(
this._network.MAIN_CONTRACT_ADDRESS,
FC_COMMUNITY_JSON_ABI,
this._mainWallet,
);
}
return client;
};

getEvents = async (
eventName: FactchainEventName,
from: number,
to: number,
): Promise<FactchainEvent[]> => {
const db = this._mongoClient.db("fc-community");
const collection = db.collection("events");
const cursor = collection.find({
networkName: this._network.INFRA_RPC_URL.includes("sepolia")
? "Ethereum Sepolia"
: "Base Mainnet",
eventName: eventName,
blockTimestamp: {
$gte: from,
$lte: to,
},
});
const documents = await cursor.toArray();
const events: FactchainEvent[] = documents.map((document) => ({
networkName: document.networkName,
contractAddress: document.contractAddress,
eventName: document.eventName,
blockTimestamp: document.blockTimestamp,
blockNumber: document.blockNumber,
eventArgs: document.eventArgs,
}));
return events;
const client = this.getClient();
try {
const db = client.db("fc-community");
const collection = db.collection("events");
const cursor = collection.find({
networkName: this._network.INFRA_RPC_URL.includes("sepolia")
? "Ethereum Sepolia"
: "Base Mainnet",
eventName: eventName,
blockTimestamp: {
$gte: from,
$lte: to,
},
});
const documents = await cursor.toArray();
const events: FactchainEvent[] = documents.map((document) => ({
networkName: document.networkName,
contractAddress: document.contractAddress,
eventName: document.eventName,
blockTimestamp: document.blockTimestamp,
blockNumber: document.blockNumber,
eventArgs: document.eventArgs,
}));
return events;
} finally {
await client.close();
}
};

// TODO: directly retrieve Note from mongodb
Expand Down
69 changes: 39 additions & 30 deletions fc-community-backend/apps/shared/src/web2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class DBConnector implements NoteReader {
private _config: Config;
private _network: NetworkConfig;
private _fcCommunity: ethers.Contract;
private _mongoClient: MongoClient;
private _mainWallet: ethers.Signer;
private _provider: ethers.AbstractProvider;

Expand All @@ -29,49 +28,59 @@ export class DBConnector implements NoteReader {
config.NOTE_FINALISER_PKEY,
this._provider,
);

this._fcCommunity = new ethers.Contract(
this._network.MAIN_CONTRACT_ADDRESS,
FC_COMMUNITY_JSON_ABI,
this._mainWallet,
);
}

getClient = () => {
const uri = `mongodb+srv://${this._config.MONGO_USER}:${this._config.MONGO_PASSWORD}@${this._config.MONGO_CLUSTER}/?w=majority&appName=${this._config.MONGO_APP_NAME}`;
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
this._mongoClient = new MongoClient(uri, {
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
this._fcCommunity = new ethers.Contract(
this._network.MAIN_CONTRACT_ADDRESS,
FC_COMMUNITY_JSON_ABI,
this._mainWallet,
);
}
return client;
};

getEvents = async (
eventName: FactchainEventName,
from: number,
to: number,
): Promise<FactchainEvent[]> => {
const db = this._mongoClient.db("fc-community");
const collection = db.collection("events");
const cursor = collection.find({
networkName: this._network.INFRA_RPC_URL.includes("sepolia")
? "Ethereum Sepolia"
: "Base Mainnet",
eventName: eventName,
blockTimestamp: {
$gte: from,
$lte: to,
},
});
const documents = await cursor.toArray();
const events: FactchainEvent[] = documents.map((document) => ({
networkName: document.networkName,
contractAddress: document.contractAddress,
eventName: document.eventName,
blockTimestamp: document.blockTimestamp,
blockNumber: document.blockNumber,
eventArgs: document.eventArgs,
}));
return events;
const client = this.getClient();
try {
const db = client.db("fc-community");
const collection = db.collection("events");
const cursor = collection.find({
networkName: this._network.INFRA_RPC_URL.includes("sepolia")
? "Ethereum Sepolia"
: "Base Mainnet",
eventName: eventName,
blockTimestamp: {
$gte: from,
$lte: to,
},
});
const documents = await cursor.toArray();
const events: FactchainEvent[] = documents.map((document) => ({
networkName: document.networkName,
contractAddress: document.contractAddress,
eventName: document.eventName,
blockTimestamp: document.blockTimestamp,
blockNumber: document.blockNumber,
eventArgs: document.eventArgs,
}));
return events;
} finally {
await client.close();
}
};

// TODO: directly retrieve Note from mongodb
Expand Down

0 comments on commit 2989dd8

Please sign in to comment.