Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 59 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from './types';
import {notifyDiscord} from './notifyDiscord';
import {getAllProjectsAndSubProjectSortedByCreationDate} from './queries/getAllProjectsSortedByCreationDate';
import {getAllOrcidAccountsWithSplits} from './queries/getAllOrcidAccountsWithSplits';

const MAX_CYCLES = 1000;
const SCRIPT_ITERATIONS = 3;
Expand Down Expand Up @@ -81,10 +82,12 @@ async function main(): Promise<void> {

const dripListsResult = await processDripLists(db, tokens);
const projectsResult = await processProjects(db, tokens);
const orcidResult = await processOrcidAccounts(db, tokens);

allWriteOperations.push(
...dripListsResult.writeOperations,
...projectsResult.writeOperations,
...orcidResult.writeOperations,
);

console.log(
Expand Down Expand Up @@ -155,11 +158,7 @@ async function processDripLists(
continue; // Skip to the next drip list
}

const splitsReceivers = await getCurrentSplitsReceivers(
db,
dripListId,
'dripList',
);
const splitsReceivers = await getCurrentSplitsReceivers(db, dripListId);

for (const token of tokens) {
const result = await processToken(
Expand Down Expand Up @@ -192,11 +191,7 @@ async function processProjects(
continue; // Skip to the next project
}

const splitsReceivers = await getCurrentSplitsReceivers(
db,
projectId,
'project',
);
const splitsReceivers = await getCurrentSplitsReceivers(db, projectId);

const weightsAreCorrect = await checkTotalWeight(
splitsReceivers,
Expand Down Expand Up @@ -231,11 +226,63 @@ async function processProjects(
return {writeOperations};
}

async function processOrcidAccounts(
db: Client,
tokens: OxString[],
): Promise<ProcessingResult> {
console.log('\nProcessing ORCID accounts...');
const writeOperations: WriteOperation[] = [];

const orcidRows = await getAllOrcidAccountsWithSplits(db);

console.log(`Found ${orcidRows.length} ORCID accounts to process`);

for (const row of orcidRows) {
const accountIdStr = row.accountId.toString();

if (appSettings.accountIdsToSkip.includes(accountIdStr)) {
console.log(
`Skipping ORCID Account ${accountIdStr} as per ACCOUNT_IDS_TO_SKIP.`,
);
continue;
}

// Validate that ORCID account splits 100% to owner
if (row.receiverAccountId !== row.ownerAccountId || row.weight !== 1_000_000) {
const message = `ORCID Validation Error: Account ${accountIdStr} doesn't split 100% to owner. Expected receiver: ${row.ownerAccountId}, got: ${row.receiverAccountId}. Expected weight: 1000000, got: ${row.weight}`;
Comment thread
jtourkos marked this conversation as resolved.
console.warn(message);
await notifyDiscord(`⚠️ ${message}`);
continue;
}

// Create splits receiver configuration (100% to owner)
const splitsReceivers: SplitsReceiver[] = [
{
accountId: row.receiverAccountId,
weight: row.weight,
},
];

for (const token of tokens) {
const result = await processToken(
row.accountId,
token,
splitsReceivers,
'orcidAccount',
);
writeOperations.push(...result.writeOperations);
}
}

console.log('Completed processing ORCID accounts');
return {writeOperations};
}

async function processToken(
accountId: bigint,
token: string,
splitsReceivers: SplitsReceiver[],
type: 'dripList' | 'project',
type: 'dripList' | 'project' | 'orcidAccount',
): Promise<ProcessingResult> {
const writeOperations: WriteOperation[] = [];
const entityDescription = `${type} ${accountId}`;
Expand Down Expand Up @@ -284,7 +331,7 @@ async function processToken(
}
}

const splittable = await dripsReadContract({
const splittable = await dripsReadContract({
functionName: 'splittable',
args: [accountId, token as OxString],
});
Expand Down
49 changes: 49 additions & 0 deletions src/queries/getAllOrcidAccountsWithSplits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {Client} from 'pg';
import appSettings from '../appSettings';
import type {OxString} from '../types';

const {
network: {name: dbSchema},
} = appSettings;

export type OrcidAccountRow = {
accountId: bigint;
ownerAddress: OxString;
ownerAccountId: bigint;
isLinked: boolean;
identityType: string;
receiverAccountId: bigint;
weight: number;
};

export async function getAllOrcidAccountsWithSplits(
db: Client,
): Promise<OrcidAccountRow[]> {
const query = `
SELECT
li.account_id as "accountId",
li.owner_address as "ownerAddress",
li.owner_account_id as "ownerAccountId",
li.is_linked as "isLinked",
li.identity_type as "identityType",
sr.receiver_account_id as "receiverAccountId",
sr.weight as "weight"
FROM ${dbSchema}.linked_identities li
INNER JOIN ${dbSchema}.splits_receivers sr ON li.account_id = sr.sender_account_id
WHERE li.identity_type = 'orcid'
AND li.is_linked = true
ORDER BY li.account_id
`;

const result = await db.query(query);

return result.rows.map(row => ({
accountId: BigInt(row.accountId),
ownerAddress: row.ownerAddress as OxString,
ownerAccountId: BigInt(row.ownerAccountId),
isLinked: row.isLinked,
identityType: row.identityType,
receiverAccountId: BigInt(row.receiverAccountId),
weight: row.weight,
}));
}
19 changes: 8 additions & 11 deletions src/queries/getCurrentSplitsReceivers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,25 @@ const {
} = appSettings;

type SplitRow = {
fundeeAccountId?: string;
fundeeDripListId?: string;
fundeeProjectId?: string;
weight: bigint;
receiver_account_id: string;
weight: number;
};

export default async function getCurrentSplitsReceivers(
db: Client,
accountId: string,
type: 'dripList' | 'project',
): Promise<SplitsReceiver[]> {
const {rows: splits} = await db.query<SplitRow>({
text: `SELECT * FROM "${dbSchema}"."splits_receivers" WHERE sender_account_id = $1`,
text: `SELECT receiver_account_id, weight FROM "${dbSchema}"."splits_receivers" WHERE sender_account_id = $1`,
values: [accountId],
});

const splitsReceivers = sortSplitsReceivers([
...splits.map(({fundeeProjectId, weight}) => ({
accountId: BigInt(fundeeProjectId!),
weight: Number(weight),
const splitsReceivers = sortSplitsReceivers(
splits.map(row => ({
accountId: BigInt(row.receiver_account_id),
weight: row.weight,
})),
]);
);

return splitsReceivers;
}
Expand Down