Skip to content

Commit

Permalink
introduce delegateeRegistrationRequestToMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
shazarre committed Oct 2, 2024
1 parent 5336615 commit c6b12a0
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 15 deletions.
50 changes: 50 additions & 0 deletions src/features/delegation/delegateeMetadata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { delegateeRegistrationRequestToMetadata } from 'src/features/delegation/delegateeMetadata';
import { getValidRequest } from 'src/test/delegatee-registration-utils';
import { expect, it } from 'vitest';

it('converts registration request to valid metadata', async () => {
const request = await getValidRequest();
const date = new Date(1727882822000);

expect(delegateeRegistrationRequestToMetadata(request, date)).toMatchInlineSnapshot(`
{
"address": "0x6A5DD51Da29914e8659b9CC354B414f30c7692c4",
"date": "2024-10-02",
"description": "Delegatee description",
"interests": [
"blockchain",
"NFTs",
],
"links": {
"twitter": "https://example.com/x",
"website": "https://example.com",
},
"logoUri": "/logos/delegatees/0x6A5DD51Da29914e8659b9CC354B414f30c7692c4.jpg",
"name": "Delegatee name",
}
`);
});

it('converts registration request without website to valid metadata', async () => {
const request = await getValidRequest();
const date = new Date(1727882822000);

delete request.websiteUrl;

expect(delegateeRegistrationRequestToMetadata(request, date)).toMatchInlineSnapshot(`
{
"address": "0x6A5DD51Da29914e8659b9CC354B414f30c7692c4",
"date": "2024-10-02",
"description": "Delegatee description",
"interests": [
"blockchain",
"NFTs",
],
"links": {
"twitter": "https://example.com/x",
},
"logoUri": "/logos/delegatees/0x6A5DD51Da29914e8659b9CC354B414f30c7692c4.jpg",
"name": "Delegatee name",
}
`);
});
34 changes: 33 additions & 1 deletion src/features/delegation/delegateeMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import path from 'path';
import DelegateeJsonData from 'src/config/delegates.json';
import { DelegateeMetadata, DelegateeMetadataMapSchema } from 'src/features/delegation/types';
import { SocialLinks } from 'src/config/types';
import {
DelegateeMetadata,
DelegateeMetadataMapSchema,
RegisterDelegateRequest,
} from 'src/features/delegation/types';
import { logger } from 'src/utils/logger';

let cachedMetadata: AddressTo<DelegateeMetadata>;
Expand All @@ -11,6 +17,32 @@ export function getDelegateeMetadata(): AddressTo<DelegateeMetadata> {
return cachedMetadata;
}

export function delegateeRegistrationRequestToMetadata(
request: RegisterDelegateRequest,
date: Date,
): DelegateeMetadata {
const logoUri = `/logos/delegatees/${request.address}${path.extname(request.image!.name)}`;
const links: SocialLinks = {};

if (request.websiteUrl) {
links['website'] = request.websiteUrl;
}

if (request.twitterUrl) {
links['twitter'] = request.twitterUrl;
}

return {
name: request.name,
address: request.address,
logoUri,
date: date.toISOString().split('T')[0],
links,
interests: request.interests.split(',').map((i) => i.trim()),
description: request.description.trim(),
};
}

function parseDelegateeMetadata(): AddressTo<DelegateeMetadata> {
try {
return DelegateeMetadataMapSchema.parse(DelegateeJsonData);
Expand Down
17 changes: 3 additions & 14 deletions src/features/delegation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { App } from 'octokit';
import path from 'path';
import { fornoRpcUrl } from 'src/config/config';
import { Addresses } from 'src/config/contracts';
import { DelegateeMetadata, RegisterDelegateRequest } from 'src/features/delegation/types';
import { delegateeRegistrationRequestToMetadata } from 'src/features/delegation/delegateeMetadata';
import { RegisterDelegateRequest } from 'src/features/delegation/types';
import { createPublicClient, http } from 'viem';
import { celo } from 'viem/chains';

Expand Down Expand Up @@ -41,19 +42,7 @@ export async function createDelegationPR(request: RegisterDelegateRequest) {
const branchName = `json-${request.address}-${Date.now()}`;
const metadataPath = `delegatees/${request.address}.json`;
const imagePath = `public/logos/delegatees/${request.address}${path.extname(request.image!.name)}`;
const delegateeMetadata: DelegateeMetadata = {
name: request.name,
address: request.address,
logoUri: imagePath,
// TODO fix the date format
date: Date.now().toString(),
links: {
website: request.websiteUrl,
twitter: request.twitterUrl,
},
interests: request.interests.split(',').map((i) => i.trim()),
description: request.description.trim(),
};
const delegateeMetadata = delegateeRegistrationRequestToMetadata(request, new Date());

const newRefResponse = await octokit.rest.git.createRef({
owner: GITHUB_REPO_OWNER,
Expand Down

0 comments on commit c6b12a0

Please sign in to comment.