Skip to content

Commit

Permalink
refactor: add isVisible column to Drip List entity
Browse files Browse the repository at this point in the history
  • Loading branch information
jtourkos committed Oct 31, 2024
1 parent de3f6c9 commit 967139d
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ POSTGRES_CONNECTION_STRING=string # Required. The connection string for the data

SHOULD_START_MONITORING_UI=boolean # Optional. If true, the app will start the monitoring UI on startup.

VISIBILITY_THREDDSHOLD_BLOCK_NUMBER=number # Optional. The block number to start indexing Project and Drip List visibility data from. Defaults to 0.

### INDEXING OPTIONS

POLLING_INTERVAL=number # Optional. The interval in milliseconds to poll the blockchain for new events. Defaults to 5000.
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,6 @@ dist

# MacOS
**/.DS_Store

# IntelliJ
.idea
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"bee-queue": "^1.5.0",
"bull-arena": "^4.0.0",
"dotenv": "^16.3.1",
"dotenv-expand": "^11.0.6",
"ethers": "^6.7.1",
"express": "^4.18.2",
"get-caller-file": "^2.0.5",
Expand Down
7 changes: 5 additions & 2 deletions src/config/appSettings.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import dotenv from 'dotenv';
import dotenvExpand from 'dotenv-expand';

dotenv.config();
dotenvExpand.expand(dotenv.config());

function missingEnvVar(name: string): never {
throw new Error(`Missing ${name} in .env file.`);
}

const appSettings = {
network: process.env.NETWORK || missingEnvVar('NETWORK is not set.'),
network: process.env.NETWORK || missingEnvVar('NETWORK.'),
primaryRpcUrl:
process.env.PRIMARY_RPC_URL || missingEnvVar('PRIMARY_RPC_URL is not set.'),
fallbackRpcUrl: process.env.FALLBACK_RPC_URL,
Expand All @@ -25,6 +26,8 @@ const appSettings = {
shouldStartMonitoringUI:
(process.env.SHOULD_START_MONITORING_UI as unknown as string) === 'true',
cacheInvalidationEndpoint: process.env.CACHE_INVALIDATION_ENDPOINT,
visibilityThresholdBlockNumber:
Number(process.env.VISIBILITY_THRESHOLD_BLOCK_NUMBER) || 0,
} as const;

export default appSettings;
2 changes: 2 additions & 0 deletions src/core/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ declare global {
POSTGRES_CONNECTION_STRING: string;
SHOULD_START_MONITORING_UI: boolean;
SHOULD_PROCESS_PAST_EVENTS: boolean;
CACHE_INVALIDATION_ENDPOINT: string;
VISIBILITY_THRESHOLD_BLOCK_NUMBER: number;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export default class AccountMetadataEmittedEventHandler extends EventHandlerBase
transaction,
ipfsHash,
blockTimestamp,
blockNumber,
);
} else {
logManager.appendLog(
Expand Down Expand Up @@ -153,10 +154,6 @@ export default class AccountMetadataEmittedEventHandler extends EventHandlerBase
}

private _isEmittedByTheDripsApp(key: string): boolean {
if (key === DRIPS_APP_USER_METADATA_KEY) {
return true;
}

return false;
return key === DRIPS_APP_USER_METADATA_KEY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ import unreachableError from '../../../utils/unreachableError';
import validateSplitsReceivers from '../splitsValidator';
import getUserAddress from '../../../utils/getAccountAddress';
import { AddressDriverSplitReceiverType } from '../../../models/AddressDriverSplitReceiverModel';
import appSettings from '../../../config/appSettings';

export default async function handleDripListMetadata(
logManager: LogManager,
dripListId: DripListId,
transaction: Transaction,
ipfsHash: IpfsHash,
blockTimestamp: Date,
blockNumber: number,
) {
const dripList = await DripListModel.findByPk(dripListId, {
transaction,
Expand Down Expand Up @@ -80,7 +82,13 @@ export default async function handleDripListMetadata(

await validateDripListMetadata(dripList, metadata);

await updateDripListMetadata(dripList, logManager, transaction, metadata);
await updateDripListMetadata(
dripList,
logManager,
transaction,
metadata,
blockNumber,
);

await createDbEntriesForDripListSplits(
dripListId,
Expand All @@ -96,6 +104,7 @@ async function updateDripListMetadata(
logManager: LogManager,
transaction: Transaction,
metadata: AnyVersion<typeof nftDriverAccountMetadataParser>,
blockNumber: number,
): Promise<void> {
dripList.isValid = true;
dripList.name = metadata.name ?? null;
Expand All @@ -106,6 +115,15 @@ async function updateDripListMetadata(
? (metadata.latestVotingRoundId as UUID) || null
: null;

if (
blockNumber > appSettings.visibilityThresholdBlockNumber &&
'isVisible' in metadata
) {
dripList.isVisible = metadata.isVisible;
} else {
dripList.isVisible = true;
}

logManager.appendUpdateLog(dripList, DripListModel, dripList.id);

await dripList.save({ transaction });
Expand Down
18 changes: 15 additions & 3 deletions src/eventHandlers/TransferEventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ZeroAddress } from 'ethers';
import type { TransferEvent } from '../../contracts/CURRENT_NETWORK/NftDriver';
import EventHandlerBase from '../events/EventHandlerBase';
import LogManager from '../core/LogManager';
Expand All @@ -6,6 +7,7 @@ import type EventHandlerRequest from '../events/EventHandlerRequest';
import { DripListModel, TransferEventModel } from '../models';
import { dbConnection } from '../db/database';
import { isLatestEvent } from '../utils/eventUtils';
import appSettings from '../config/appSettings';

export default class TransferEventHandler extends EventHandlerBase<'Transfer(address,address,uint256)'> {
public eventSignatures = ['Transfer(address,address,uint256)' as const];
Expand Down Expand Up @@ -60,6 +62,8 @@ export default class TransferEventHandler extends EventHandlerBase<'Transfer(add
`${transferEvent.transactionHash}-${transferEvent.logIndex}`,
);

const { visibilityThresholdBlockNumber } = appSettings;

// This must be the only place a Drip List is created.
const [dripList, isDripListCreated] = await DripListModel.findOrCreate({
transaction,
Expand All @@ -74,6 +78,11 @@ export default class TransferEventHandler extends EventHandlerBase<'Transfer(add
ownerAddress: to,
ownerAccountId: await calcAccountId(to),
previousOwnerAddress: from,

isVisible:
blockNumber > visibilityThresholdBlockNumber
? from === ZeroAddress // If it's a mint, then the Drip List will be visible. If it's a real transfer, then it's not.
: true, // If the block number is less than the visibility threshold, then the Drip List is visible by default.
},
});

Expand All @@ -86,7 +95,7 @@ export default class TransferEventHandler extends EventHandlerBase<'Transfer(add
}

// Here, the Drip List already exists.
// Only if the event is the latest (in the DB), we process the metadata.
// Only if the event is the latest (in the DB), we process its data.
// After all events are processed, the Drip List will be updated with the latest values.
if (
!(await isLatestEvent(
Expand All @@ -109,6 +118,9 @@ export default class TransferEventHandler extends EventHandlerBase<'Transfer(add
dripList.previousOwnerAddress = from;
dripList.ownerAccountId = await calcAccountId(to);

// This is real transfer. The Drip List should not be visible unless the block number is less than the visibility threshold.
dripList.isVisible = blockNumber < visibilityThresholdBlockNumber;

logManager
.appendIsLatestEventLog()
.appendUpdateLog(dripList, DripListModel, dripList.id);
Expand All @@ -126,8 +138,8 @@ export default class TransferEventHandler extends EventHandlerBase<'Transfer(add
const { args, blockTimestamp } = context;
const [from, to, tokenId] = args;

super.afterHandle({
args: [tokenId, calcAccountId(from), calcAccountId(to)],
await super.afterHandle({
args: [tokenId, await calcAccountId(from), await calcAccountId(to)],
blockTimestamp,
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/metadata/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { repoDriverAccountMetadataSchemaV3 } from './repo-driver/v3';
import { repoDriverAccountMetadataSchemaV4 } from './repo-driver/v4';
import { nftDriverAccountMetadataSchemaV4 } from './nft-driver/v4';
import { repoDriverAccountMetadataSchemaV5 } from './repo-driver/v5';
import { nftDriverAccountMetadataSchemaV5 } from './nft-driver/v5';

export const nftDriverAccountMetadataParser = createVersionedParser([
nftDriverAccountMetadataSchemaV5.parse,
nftDriverAccountMetadataSchemaV4.parse,
nftDriverAccountMetadataSchemaV3.parse,
nftDriverAccountMetadataSchemaV2.parse,
Expand Down
7 changes: 7 additions & 0 deletions src/metadata/schemas/nft-driver/v5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod';
import { nftDriverAccountMetadataSchemaV4 } from './v4';

export const nftDriverAccountMetadataSchemaV5 =
nftDriverAccountMetadataSchemaV4.extend({
isVisible: z.boolean(),
});
5 changes: 5 additions & 0 deletions src/models/DripListModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class DripListModel extends Model<
public declare ownerAccountId: AccountId;
public declare previousOwnerAddress: AddressLike;
public declare latestVotingRoundId: UUID | null;
public declare isVisible: boolean | null;

public static initialize(sequelize: Sequelize): void {
this.init(
Expand Down Expand Up @@ -62,6 +63,10 @@ export default class DripListModel extends Model<
type: DataTypes.STRING,
allowNull: false,
},
isVisible: {
type: DataTypes.BOOLEAN,
allowNull: true,
},
},
{
sequelize,
Expand Down

0 comments on commit 967139d

Please sign in to comment.