Skip to content

Commit

Permalink
[OTE-134] update compliance_status table in compliance provider Round…
Browse files Browse the repository at this point in the history
…table task (#1043)
  • Loading branch information
dydxwill authored Feb 6, 2024
1 parent 30d3608 commit 84d9d00
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 74 deletions.
8 changes: 8 additions & 0 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ComplianceReason,
ComplianceStatus,
ComplianceStatusCreateObject,
ComplianceStatusUpsertObject,
FillCreateObject,
FillType,
FundingIndexUpdatesCreateObject,
Expand Down Expand Up @@ -610,6 +611,13 @@ export const noncompliantStatusData: ComplianceStatusCreateObject = {
updatedAt: createdDateTime.plus(1).toISO(),
};

export const noncompliantStatusUpsertData: ComplianceStatusUpsertObject = {
address: blockedAddress,
status: ComplianceStatus.BLOCKED,
reason: ComplianceReason.SANCTIONED_GEO,
updatedAt: createdDateTime.toISO(),
};

// ========= Trading Reward Data ==========

export const defaultTradingReward: TradingRewardCreateObject = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { ComplianceStatus, ComplianceStatusFromDatabase } from '../../src/types';
import {
ComplianceStatus,
ComplianceStatusFromDatabase,
ComplianceDataColumns,
Ordering,
ComplianceStatusUpsertObject,
} from '../../src/types';
import * as ComplianceStatusTable from '../../src/stores/compliance-status-table';
import { clearData, migrate, teardown } from '../../src/helpers/db-helpers';
import { compliantStatusData, defaultAddress, noncompliantStatusData } from '../helpers/constants';
import {
compliantStatusData,
defaultAddress,
noncompliantStatusData,
noncompliantStatusUpsertData,
} from '../helpers/constants';
import { DateTime } from 'luxon';

describe('Compliance status store', () => {
Expand Down Expand Up @@ -130,28 +141,23 @@ describe('Compliance status store', () => {
});

it('Successfully upserts a new compliance status', async () => {
await ComplianceStatusTable.upsert(noncompliantStatusData);
await ComplianceStatusTable.upsert(noncompliantStatusUpsertData);

const complianceStatus: ComplianceStatusFromDatabase[] = await ComplianceStatusTable.findAll(
{},
[],
{ readReplica: true },
);
expect(complianceStatus.length).toEqual(1);
expect(complianceStatus[0]).toEqual(noncompliantStatusData);
expect(complianceStatus[0]).toEqual(expect.objectContaining(noncompliantStatusUpsertData));
});

it('Successfully upserts an existing compliance status', async () => {
await ComplianceStatusTable.upsert(noncompliantStatusData);

const updatedTime: string = DateTime.fromISO(
noncompliantStatusData.createdAt!,
).plus({ minutes: 10 }).toUTC().toISO();
await ComplianceStatusTable.upsert(noncompliantStatusUpsertData);

await ComplianceStatusTable.upsert({
...noncompliantStatusData,
...noncompliantStatusUpsertData,
status: ComplianceStatus.CLOSE_ONLY,
updatedAt: updatedTime,
});

const updatedComplianceStatus: ComplianceStatusFromDatabase[] = await
Expand All @@ -163,64 +169,61 @@ describe('Compliance status store', () => {
{ readReplica: true },
);
expect(updatedComplianceStatus.length).toEqual(1);
expect(updatedComplianceStatus[0]).toEqual({
...noncompliantStatusData,
status: ComplianceStatus.CLOSE_ONLY,
updatedAt: updatedTime,
});
expect(updatedComplianceStatus[0]).toEqual(
expect.objectContaining({
...noncompliantStatusUpsertData,
status: ComplianceStatus.CLOSE_ONLY,
}),
);
});

it('Successfully bulk upserts compliance status', async () => {
await ComplianceStatusTable.create(noncompliantStatusData);

const updatedTime1: string = DateTime.fromISO(
noncompliantStatusData.createdAt!,
).plus({ minutes: 10 }).toUTC().toISO();
const updatedTime2: string = DateTime.fromISO(
noncompliantStatusData.createdAt!,
).plus({ minutes: 20 }).toUTC().toISO();
const compliantUpsertStatusData: ComplianceStatusUpsertObject = {
address: defaultAddress,
status: ComplianceStatus.COMPLIANT,
updatedAt: DateTime.utc().toISO(),
};
await ComplianceStatusTable.create(noncompliantStatusUpsertData);
const otherAddress: string = '0x123456789abcdef';

await ComplianceStatusTable.bulkUpsert(
[
compliantStatusData,
compliantUpsertStatusData,
{
...noncompliantStatusData,
...noncompliantStatusUpsertData,
status: ComplianceStatus.FIRST_STRIKE,
updatedAt: updatedTime1,
},
{
...noncompliantStatusData,
...noncompliantStatusUpsertData,
address: otherAddress,
status: ComplianceStatus.COMPLIANT,
updatedAt: updatedTime2,
},
],
);

const complianceStatus = await ComplianceStatusTable.findAll(
{},
[],
{ readReplica: true },
{
orderBy: [[ComplianceDataColumns.address, Ordering.DESC]],
},
);

expect(complianceStatus.length).toEqual(3);
expect(complianceStatus).toEqual(expect.arrayContaining([
{
...compliantStatusData,
expect.objectContaining({
...compliantUpsertStatusData,
reason: null,
},
{
...noncompliantStatusData,
}),
expect.objectContaining({
...noncompliantStatusUpsertData,
status: ComplianceStatus.FIRST_STRIKE,
updatedAt: updatedTime1,
},
{
...noncompliantStatusData,
}),
expect.objectContaining({
...noncompliantStatusUpsertData,
address: otherAddress,
status: ComplianceStatus.COMPLIANT,
updatedAt: updatedTime2,
},
}),
]));
});
});
23 changes: 13 additions & 10 deletions indexer/packages/postgres/src/stores/compliance-status-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ import {
import Transaction from '../helpers/transaction';
import ComplianceStatusModel from '../models/compliance-status-model';
import {
ComplianceStatusColumns,
ComplianceStatusCreateObject,
ComplianceStatusFromDatabase,
ComplianceStatusQueryConfig,
ComplianceStatusUpdateObject,
ComplianceStatusUpsertObject,
Options,
Ordering,
QueryableField,
QueryConfig,
} from '../types';
import {
ComplianceStatusColumns,
ComplianceStatusCreateObject,
ComplianceStatusUpdateObject,
} from '../types/compliance-status-types';

export async function findAll(
{
Expand Down Expand Up @@ -118,7 +117,7 @@ export async function update(
}

export async function upsert(
complianceStatusToUpsert: ComplianceStatusCreateObject,
complianceStatusToUpsert: ComplianceStatusUpsertObject,
options: Options = { txId: undefined },
): Promise<ComplianceStatusFromDatabase> {
const complianceStatus: ComplianceStatusFromDatabase | undefined = await
Expand All @@ -142,28 +141,32 @@ export async function upsert(
}

export async function bulkUpsert(
complianceStatusObjects: ComplianceStatusCreateObject[],
complianceStatusObjects: ComplianceStatusUpsertObject[],
options: Options = { txId: undefined },
): Promise<void> {
if (complianceStatusObjects.length === 0) {
return;
}

complianceStatusObjects.forEach(
(complianceStatusObject: ComplianceStatusCreateObject) => verifyAllInjectableVariables(
(complianceStatusObject: ComplianceStatusUpsertObject) => verifyAllInjectableVariables(
Object.values(complianceStatusObject),
),
);

const columns: ComplianceStatusColumns[] = Object.values(ComplianceStatusColumns);
const columns: ComplianceStatusColumns[] = [
ComplianceStatusColumns.address,
ComplianceStatusColumns.status,
ComplianceStatusColumns.reason,
ComplianceStatusColumns.updatedAt,
];
const rows: string[] = setBulkRowsForUpdate<ComplianceStatusColumns>({
objectArray: complianceStatusObjects,
columns,
stringColumns: [
ComplianceStatusColumns.address,
],
timestampColumns: [
ComplianceStatusColumns.createdAt,
ComplianceStatusColumns.updatedAt,
],
enumColumns: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export interface ComplianceStatusCreateObject {
updatedAt?: IsoString;
}

export interface ComplianceStatusUpsertObject {
address: string;
status: ComplianceStatus;
reason?: ComplianceReason;
updatedAt: IsoString;
}

export interface ComplianceStatusUpdateObject {
address: string;
status?: ComplianceStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ describe('update-close-only-status', () => {
`${config.SERVICE_NAME}.num_stale_close_only_updated.count`,
0,
);
expect(stats.gauge).toHaveBeenCalledWith(
`${config.SERVICE_NAME}.num_stale_close_only.count`,
0,
);
});

it('updates CLOSE_ONLY addresses older than 7 days to BLOCKED', async () => {
Expand Down Expand Up @@ -95,9 +91,5 @@ describe('update-close-only-status', () => {
`${config.SERVICE_NAME}.num_stale_close_only_updated.count`,
1,
);
expect(stats.gauge).toHaveBeenCalledWith(
`${config.SERVICE_NAME}.num_stale_close_only.count`,
1,
);
});
});
Loading

0 comments on commit 84d9d00

Please sign in to comment.