Skip to content

Commit

Permalink
add category to protocol config
Browse files Browse the repository at this point in the history
  • Loading branch information
Manh Cao committed Nov 20, 2024
1 parent 0a1885e commit e872a8f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
7 changes: 7 additions & 0 deletions const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const DEFAULT_PROTOCOL_CATEGORY = 'others';
const PROTOCOL_CATEGORIES = ['money market', 'vault', 'liquid locker'];

module.exports = {
DEFAULT_PROTOCOL_CATEGORY,
PROTOCOL_CATEGORIES,
}
31 changes: 24 additions & 7 deletions merge-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const yaml = require("js-yaml");
const {DEFAULT_PROTOCOL_CATEGORY} = require("./const");

async function run() {
const protocolsPath = path.join(__dirname, 'protocols');
Expand Down Expand Up @@ -51,19 +52,35 @@ async function run() {
}

function formatProtocolConfig(config) {
const {metadata} = config;
const {id, name, icon, category, metadata} = config;
const {pt, yt, lp} = metadata;
lowercaseAddressOfMetadata(pt);
lowercaseAddressOfMetadata(yt);
lowercaseAddressOfMetadata(lp);

return config;
return {
id,
name,
icon,
category: category ?? DEFAULT_PROTOCOL_CATEGORY,
metadata: {
pt: formatMetadataAssets(pt),
yt: formatMetadataAssets(yt),
lp: formatMetadataAssets(lp),
},
};
}

function lowercaseAddressOfMetadata(assets) {
function formatMetadataAssets(assets) {
const result = [];
for (const asset of (assets ?? [])) {
asset.address = asset.address.toLowerCase();
const {chainId, address, integrationUrl, description} = asset;
result.push({
chainId,
address: address.toLowerCase(),
integrationUrl,
description,
})
}

return result;
}

function createMD5(filePath) {
Expand Down
16 changes: 14 additions & 2 deletions validate-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const {PROTOCOL_CATEGORIES} = require("./const");

const LIMIT_ICON_KB_SIZE = 20;
const BUFFER_LIMIT_ICON_KB_SIZE = LIMIT_ICON_KB_SIZE + 1;
Expand All @@ -15,6 +16,16 @@ function isKebabCase(str) {
return kebabCaseRegex.test(str);
}

function validateCategory(protocol, category) {
if (category === undefined) {
return;
}

if (!mustBeNonEmptyString(category) || !PROTOCOL_CATEGORIES.includes(category)) {
throw new Error(`protocol ${protocol}: invalid field 'category', category must be one of the values (${PROTOCOL_CATEGORIES.join(', ')}) or left unset`);
}
}

async function main() {
const CHANGED_PROTOCOLS = process.env.CHANGED_PROTOCOLS;
const GET_ASSET_LIST_URL = process.env.GET_ASSET_LIST_URL;
Expand Down Expand Up @@ -81,12 +92,14 @@ function validateConfig(protocol, assetMap) {
throw new Error(`protocol ${protocol}: config is not an object`);
}

const {name, icon, metadata} = protocolConfig;
const {name, icon, metadata, category} = protocolConfig;

if (!mustBeNonEmptyString(name)) {
throw new Error(`protocol ${protocol}: invalid field 'name'`);
}

validateCategory(protocol, category);

if (!mustBeNonEmptyString(icon)) {
throw new Error(`protocol ${protocol}: invalid field 'icon'`);
}
Expand All @@ -113,7 +126,6 @@ function validateConfig(protocol, assetMap) {
throw new Error(`protocol ${protocol}: icon size must be less than ${LIMIT_ICON_KB_SIZE}KB file`);
}


const {pt, yt, lp} = metadata;
checkMetadataField(pt, protocol, 'pt', ptMap);
checkMetadataField(yt, protocol, 'yt', ytMap);
Expand Down

0 comments on commit e872a8f

Please sign in to comment.