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 #105 from factchain/add_ts_to_note
Browse files Browse the repository at this point in the history
[backend] add createdAt timestamp to Note
  • Loading branch information
Haypierre authored Mar 21, 2024
2 parents abfc07d + 5ba8db8 commit f6bc48f
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 38 deletions.
2 changes: 1 addition & 1 deletion fc-community-backend/apps/api/scripts/finaliseNotes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NoteService } from "../dist/factchain-core/noteService.js";
import { config } from "../dist/factchain-core/env.js";
import { getNetworkConfig } from "../dist/factchain-core/networks/config.js"

const LOOKBACK_DAYS = parseInt(process.argv[2]) || 2;
const LOOKBACK_DAYS = parseInt(config.LOOKBACK_DAYS);
const MINIMUM_RATING = parseInt(config.MINIMUM_RATING);

const fc = new FactChainBackend(config, getNetworkConfig("ETHEREUM_SEPOLIA"));
Expand Down
27 changes: 14 additions & 13 deletions fc-community-backend/apps/api/src/factchain-core/networks/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export interface NetworkConfig {
NFT_CONTRACT_ADDRESS: string;
SFT_CONTRACT_ADDRESS: string;
X_CONTRACT_ADDRESS?: string;

}

export interface NetworkConfigs {
Expand All @@ -16,21 +15,23 @@ export interface NetworkConfigs {

const networks: NetworkConfigs = {
BASE_MAINNET: {
INFRA_RPC_URL: "<OVERRIDE ME>",
MAIN_CONTRACT_ADDRESS: "0xde31FB31adeB0a97E34aCf7EF4e21Ad585F667f7",
NFT_CONTRACT_ADDRESS: "0x8885DC14732AE2ADd64d8C6581E722F0A88aA3Da",
SFT_CONTRACT_ADDRESS: "0x77840A1815f4F62a4cCCA3aBA3566fB8ff0b10D0"
INFRA_RPC_URL: "<OVERRIDE ME>",
MAIN_CONTRACT_ADDRESS: "0xde31FB31adeB0a97E34aCf7EF4e21Ad585F667f7",
NFT_CONTRACT_ADDRESS: "0x8885DC14732AE2ADd64d8C6581E722F0A88aA3Da",
SFT_CONTRACT_ADDRESS: "0x77840A1815f4F62a4cCCA3aBA3566fB8ff0b10D0",
},
ETHEREUM_SEPOLIA: {
INFRA_RPC_URL: "<OVERRIDE ME>",
MAIN_CONTRACT_ADDRESS: "0x3b5946b3bd79c2B211E49c3149872f1d66223AE7",
NFT_CONTRACT_ADDRESS: "0x5818764B4272f4eCff170216abE99D36c0c41622",
SFT_CONTRACT_ADDRESS: "0xF9408EB2C2219E28aEFB32035c49d491880650A2",
X_CONTRACT_ADDRESS: "0xaC51f5E2664aa966c678Dc935E0d853d3495A48C"
}
INFRA_RPC_URL: "<OVERRIDE ME>",
MAIN_CONTRACT_ADDRESS: "0x3b5946b3bd79c2B211E49c3149872f1d66223AE7",
NFT_CONTRACT_ADDRESS: "0x5818764B4272f4eCff170216abE99D36c0c41622",
SFT_CONTRACT_ADDRESS: "0xF9408EB2C2219E28aEFB32035c49d491880650A2",
X_CONTRACT_ADDRESS: "0xaC51f5E2664aa966c678Dc935E0d853d3495A48C",
},
};

export function getNetworkConfig(networkKey: keyof NetworkConfigs): NetworkConfig {
export function getNetworkConfig(
networkKey: keyof NetworkConfigs,
): NetworkConfig {
// default to Sepolia to avoid extension breaking change
// TODO: throw BadRequest when extension is updated
// Only replace the networkKey if it is not set.
Expand All @@ -40,4 +41,4 @@ export function getNetworkConfig(networkKey: keyof NetworkConfigs): NetworkConfi
console.log(`using network ${updatedNetworkKey}`, network);
network.INFRA_RPC_URL = config[`${updatedNetworkKey}_INFRA_RPC_URL`];
return network;
}
}
8 changes: 7 additions & 1 deletion fc-community-backend/apps/api/src/factchain-core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export type Rating = {
export type Note = {
postUrl: string;
creatorAddress: string;
// TODO: createdAt shouldn't be optional
createdAt?: number;
content?: string;
finalRating?: number;
ratings?: Array<number>;
Expand Down Expand Up @@ -71,7 +73,11 @@ export type FactChainEvent =
| "NoteFinalised";

export interface NoteReader {
getNote: (postUrl: string, creator: string) => Promise<Note>;
getNote: (
postUrl: string,
creator: string,
timestamp?: number,
) => Promise<Note>;
getNotes: (
predicate: (postUrl: string, creator: string) => boolean,
lookBackDays: number,
Expand Down
21 changes: 17 additions & 4 deletions fc-community-backend/apps/api/src/factchain-core/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,23 @@ export class FactChainBackend implements NoteReader, NoteWriter {
return eventLogs;
};

getNote = async (postUrl: string, creator: string): Promise<Note> => {
getNote = async (
postUrl: string,
creator: string,
timestamp?: number,
): Promise<Note> => {
// TODO: timestamp shouldn't be optional
const result = await this._fcCommunity.communityNotes(postUrl, creator);
return {
const note: Note = {
postUrl: result[0],
content: result[1],
creatorAddress: result[2],
finalRating: parseInt(result[3]),
};
if (timestamp !== undefined) {
note.createdAt = timestamp;
}
return note;
};

getNoteRaters = async (
Expand Down Expand Up @@ -133,8 +142,12 @@ export class FactChainBackend implements NoteReader, NoteWriter {
predicate(e.args[0], e.args[1]),
);
return Promise.all(
relatedEvents.map((event) =>
this.getNote(event.args[0], event.args[1]),
relatedEvents.map(async (event) =>
this.getNote(
event.args[0],
event.args[1],
(await event.getBlock()).timestamp,
),
),
);
});
Expand Down
2 changes: 1 addition & 1 deletion fc-community-backend/apps/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ console.log(figlet.textSync("FactChain"));
program.version("1.0.0").description("FactChain command line");

program
.command("events <eventType>")
.command("get-events <eventType>")
.description("get a list of events")
.option("-f, --from <number>", "Start block", parseInt, 0)
.option("-t, --to <number>", "End block", parseInt)
Expand Down
27 changes: 14 additions & 13 deletions fc-community-backend/apps/shared/src/networks/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export interface NetworkConfig {
NFT_CONTRACT_ADDRESS: string;
SFT_CONTRACT_ADDRESS: string;
X_CONTRACT_ADDRESS?: string;

}

export interface NetworkConfigs {
Expand All @@ -16,21 +15,23 @@ export interface NetworkConfigs {

const networks: NetworkConfigs = {
BASE_MAINNET: {
INFRA_RPC_URL: "<OVERRIDE ME>",
MAIN_CONTRACT_ADDRESS: "0xde31FB31adeB0a97E34aCf7EF4e21Ad585F667f7",
NFT_CONTRACT_ADDRESS: "0x8885DC14732AE2ADd64d8C6581E722F0A88aA3Da",
SFT_CONTRACT_ADDRESS: "0x77840A1815f4F62a4cCCA3aBA3566fB8ff0b10D0"
INFRA_RPC_URL: "<OVERRIDE ME>",
MAIN_CONTRACT_ADDRESS: "0xde31FB31adeB0a97E34aCf7EF4e21Ad585F667f7",
NFT_CONTRACT_ADDRESS: "0x8885DC14732AE2ADd64d8C6581E722F0A88aA3Da",
SFT_CONTRACT_ADDRESS: "0x77840A1815f4F62a4cCCA3aBA3566fB8ff0b10D0",
},
ETHEREUM_SEPOLIA: {
INFRA_RPC_URL: "<OVERRIDE ME>",
MAIN_CONTRACT_ADDRESS: "0x3b5946b3bd79c2B211E49c3149872f1d66223AE7",
NFT_CONTRACT_ADDRESS: "0x5818764B4272f4eCff170216abE99D36c0c41622",
SFT_CONTRACT_ADDRESS: "0xF9408EB2C2219E28aEFB32035c49d491880650A2",
X_CONTRACT_ADDRESS: "0xaC51f5E2664aa966c678Dc935E0d853d3495A48C"
}
INFRA_RPC_URL: "<OVERRIDE ME>",
MAIN_CONTRACT_ADDRESS: "0x3b5946b3bd79c2B211E49c3149872f1d66223AE7",
NFT_CONTRACT_ADDRESS: "0x5818764B4272f4eCff170216abE99D36c0c41622",
SFT_CONTRACT_ADDRESS: "0xF9408EB2C2219E28aEFB32035c49d491880650A2",
X_CONTRACT_ADDRESS: "0xaC51f5E2664aa966c678Dc935E0d853d3495A48C",
},
};

export function getNetworkConfig(networkKey: keyof NetworkConfigs): NetworkConfig {
export function getNetworkConfig(
networkKey: keyof NetworkConfigs,
): NetworkConfig {
// default to Sepolia to avoid extension breaking change
// TODO: throw BadRequest when extension is updated
// Only replace the networkKey if it is not set.
Expand All @@ -40,4 +41,4 @@ export function getNetworkConfig(networkKey: keyof NetworkConfigs): NetworkConfi
console.log(`using network ${updatedNetworkKey}`, network);
network.INFRA_RPC_URL = config[`${updatedNetworkKey}_INFRA_RPC_URL`];
return network;
}
}
8 changes: 7 additions & 1 deletion fc-community-backend/apps/shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export type Rating = {
export type Note = {
postUrl: string;
creatorAddress: string;
// TODO: createdAt shouldn't be optional
createdAt?: number;
content?: string;
finalRating?: number;
ratings?: Array<number>;
Expand Down Expand Up @@ -71,7 +73,11 @@ export type FactChainEvent =
| "NoteFinalised";

export interface NoteReader {
getNote: (postUrl: string, creator: string) => Promise<Note>;
getNote: (
postUrl: string,
creator: string,
timestamp?: number,
) => Promise<Note>;
getNotes: (
predicate: (postUrl: string, creator: string) => boolean,
lookBackDays: number,
Expand Down
21 changes: 17 additions & 4 deletions fc-community-backend/apps/shared/src/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,23 @@ export class FactChainBackend implements NoteReader, NoteWriter {
return eventLogs;
};

getNote = async (postUrl: string, creator: string): Promise<Note> => {
getNote = async (
postUrl: string,
creator: string,
timestamp?: number,
): Promise<Note> => {
// TODO: timestamp shouldn't be optional
const result = await this._fcCommunity.communityNotes(postUrl, creator);
return {
const note: Note = {
postUrl: result[0],
content: result[1],
creatorAddress: result[2],
finalRating: parseInt(result[3]),
};
if (timestamp !== undefined) {
note.createdAt = timestamp;
}
return note;
};

getNoteRaters = async (
Expand Down Expand Up @@ -133,8 +142,12 @@ export class FactChainBackend implements NoteReader, NoteWriter {
predicate(e.args[0], e.args[1]),
);
return Promise.all(
relatedEvents.map((event) =>
this.getNote(event.args[0], event.args[1]),
relatedEvents.map(async (event) =>
this.getNote(
event.args[0],
event.args[1],
(await event.getBlock()).timestamp,
),
),
);
});
Expand Down

0 comments on commit f6bc48f

Please sign in to comment.