Skip to content

Commit

Permalink
add validate asset address (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Manh Cao <[email protected]>
  • Loading branch information
manh-pendle and Manh Cao authored Sep 26, 2024
1 parent 54ab68d commit 4f9b817
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/validate-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ jobs:

- name: Validate JSON files
run: |
export BASE_SHA=${{ github.event.pull_request.base.sha }}
git fetch origin main
export BASE_SHA=$(git rev-parse origin/main)
export HEAD_SHA=$(git rev-parse HEAD)
echo $BASE_SHA
echo $HEAD_SHA
git diff --quiet $BASE_SHA $HEAD_SHA -- config.json || (echo "file config.json must not change" && exit 1)
CHANGED_PROTOCOLS=$(git diff --name-only $BASE_SHA $HEAD_SHA -- 'protocols/*' | grep '^protocols/' | xargs -L1 dirname | sed 's|protocols/||' | sort -u)
CHANGED_PROTOCOLS=$CHANGED_PROTOCOLS node validate-config.js
CHANGED_PROTOCOLS=$CHANGED_PROTOCOLS GET_ASSET_LIST_URL=${{ secrets.GET_ASSET_LIST_URL }} node validate-config.js
57 changes: 50 additions & 7 deletions validate-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,31 @@ function isKebabCase(str) {

async function main() {
const CHANGED_PROTOCOLS = process.env.CHANGED_PROTOCOLS;
const GET_ASSET_LIST_URL = process.env.GET_ASSET_LIST_URL;

if (!CHANGED_PROTOCOLS) {
console.log('No changed protocols');
return;
}

if (!GET_ASSET_LIST_URL) {
throw new Error('GET_ASSET_LIST_URL is missing');
}

const assetMap = await getAssetList(GET_ASSET_LIST_URL);

const protocols = CHANGED_PROTOCOLS.split('\n');

console.log('Currently validating protocols:', protocols);

protocols.forEach((protocol) => validateConfig(protocol));
protocols.forEach((protocol) => validateConfig(protocol, assetMap));

console.log('Everything is fine.....')
}

function validateConfig(protocol) {
function validateConfig(protocol, assetMap) {
const {ptMap, ytMap, lpMap} = assetMap;

if (!isKebabCase(protocol)) {
throw new Error(`protocol ${protocol}: protocol name must be in kebab-case`);
}
Expand Down Expand Up @@ -87,16 +96,16 @@ function validateConfig(protocol) {
}

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

function mustBeNonEmptyString(str) {
return typeof str === 'string' && str.trim() !== '';
}

function checkMetadataField(data, protocol, field) {
function checkMetadataField(data, protocol, field, assetMap) {
if (data === null || data === undefined) {
return;
}
Expand All @@ -114,7 +123,11 @@ function checkMetadataField(data, protocol, field) {
}

if (!mustBeNonEmptyString(address) || !isValidEthereumAddress(address)) {
throw new Error(`protocol ${protocol}: metadata ${field} invalid 'address' field at index ${index}`);
throw new Error(`protocol ${protocol}: metadata ${field} address is not a valid ethereum address at index ${index}`);
}

if (!((`${chainId}-${address}`.toLowerCase()) in assetMap)) {
throw new Error(`protocol ${protocol}: metadata ${field} address not found in pendle ${field} list at index ${index}`);
}

if (!mustBeNonEmptyString(description)) {
Expand All @@ -127,4 +140,34 @@ function checkMetadataField(data, protocol, field) {
}
}

async function getAssetList(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`response from getting asset list is not okay: ${response}`);
}

const { data } = await response.json();

const ptMap = {};
const ytMap = {};
const lpMap = {};

for (const chainData of data) {
const {chainId, markets, pts, yts} = chainData;
if (pts) {
pts.map((pt) => ptMap[`${chainId}-${pt}`] = true)
}

if (yts) {
yts.map((yt) => ytMap[`${chainId}-${yt}`] = true)
}

if (markets) {
markets.map((market) => lpMap[`${chainId}-${market}`] = true)
}
}

return {ptMap, ytMap, lpMap};
}

void main()

0 comments on commit 4f9b817

Please sign in to comment.