Skip to content

Commit

Permalink
refactor(lint): rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
sam committed Mar 26, 2021
1 parent a50eae1 commit 6b57c51
Show file tree
Hide file tree
Showing 18 changed files with 60,604 additions and 57,131 deletions.
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.github/
api/
.prettierrc.js
.kodiak.toml
img/
renovate.json
7 changes: 5 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "yearn-tokenlist-api",
"version": "1.0.2",
"main": "index.js",
"files": [ "scripts/*", "src/*"],
"files": [
"scripts/*",
"src/*"
],
"repository": {
"type": "git",
"url": "https://github.com/sambacha/yearn-finance-tokenlist.git",
Expand All @@ -19,6 +22,7 @@
"author": "sam bacha <sam@freighttrust.com>",
"license": "MIT",
"devDependencies": {
"@0xsequence/collectible-lists": "1.1.0",
"@nomiclabs/hardhat-ethers": "2.0.2",
"@openzeppelin/contracts": "3.4.1",
"@types/fs-extra": "9.0.9",
Expand All @@ -27,7 +31,6 @@
"@types/node": "14.14.35",
"@types/node-fetch": "2.5.8",
"@uniswap/token-lists": "1.0.0-beta.19",
"@0xsequence/collectible-lists": "1.1.0",
"ajv": "6.12.6",
"ajv-formats": "1.5.1",
"cli-progress": "3.9.0",
Expand Down
162 changes: 84 additions & 78 deletions api/scripts/erc1155-mainnet.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import * as fs from "fs"
import * as ethers from 'ethers'
import fetch from "node-fetch"
import { nextVersion, schema, VersionUpgrade, YearnList, YearnInfo } from '@0xsequence/collectible-lists'
import { getEnvConfig } from "../src/utils"
const Ajv = require("ajv")
const isEqual = require("lodash.isequal")
import * as fs from 'fs';
import * as ethers from 'ethers';
import fetch from 'node-fetch';
import {
nextVersion,
schema,
VersionUpgrade,
YearnList,
YearnInfo,
} from '@0xsequence/collectible-lists';
import { getEnvConfig } from '../src/utils';
const Ajv = require('ajv');
const isEqual = require('lodash.isequal');
const cliProgress = require('cli-progress');

// Loading jsons
const erc1155json = require("@openzeppelin/contracts/build/contracts/ERC1155.json")
const erc1155Dump: TokenDump[] = require("../src/data/erc1155_dune_dump_2021_01_25.json")
const erc1155: YearnList = require("../../index/mainnet/erc1155.json")
const erc1155json = require('@openzeppelin/contracts/build/contracts/ERC1155.json');
const erc1155Dump: TokenDump[] = require('../src/data/erc1155_dune_dump_2021_01_25.json');
const erc1155: YearnList = require('../../index/mainnet/erc1155.json');

// Build ERC-1155 list
// 1. Load crv dump from Dune analytics
// 2. Query contract info via opensea API
// 3. Build list according to @0xsequence/collectible-lists

// List to fetch
const ERC1155_LIST_PATH = "../index/mainnet/erc1155.json"
const config = getEnvConfig()
const provider = new ethers.providers.InfuraProvider('mainnet', config['INFURA_API_KEY'])
const ERC1155_LIST_PATH = '../index/mainnet/erc1155.json';
const config = getEnvConfig();
const provider = new ethers.providers.InfuraProvider('mainnet', config['INFURA_API_KEY']);

interface TokenDump {
name: string;
Expand All @@ -30,128 +36,128 @@ interface TokenDump {

// Building list
const main = async () => {

// Create token information array
let newYearnList: YearnInfo[] = []
const erc1155Contracts: string[] = [...new Set([...erc1155Dump.map(t => t.address)])]
const errors: any = []
let newYearnList: YearnInfo[] = [];
const erc1155Contracts: string[] = [...new Set([...erc1155Dump.map((t) => t.address)])];
const errors: any = [];

// Progress bar init
console.log('Building ERC-1155 mainnet list')
console.log('Building ERC-1155 mainnet list');
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
progressBar.start(erc1155Contracts.length, 0)
for (let i= 0; i < erc1155Contracts.length; i++) {
let resp
progressBar.start(erc1155Contracts.length, 0);
for (let i = 0; i < erc1155Contracts.length; i++) {
let resp;
try {
resp = await fetch('https://api.opensea.io/api/v1/asset_contract/' + erc1155Contracts[i])
resp = await fetch('https://api.opensea.io/api/v1/asset_contract/' + erc1155Contracts[i]);
} catch (err) {
console.log(err)
console.log(err);
}
while (resp && resp.status == 429) {
await new Promise(r => setTimeout(r, 5000));
await new Promise((r) => setTimeout(r, 5000));
try {
resp = await fetch('https://api.opensea.io/api/v1/asset_contract/' + erc1155Contracts[i])
resp = await fetch('https://api.opensea.io/api/v1/asset_contract/' + erc1155Contracts[i]);
} catch (err) {
console.log(err)
console.log(err);
}
}
if (!resp || !resp.ok) {
errors.push({
id: i,
address: erc1155Contracts[i],
resp: !resp ? null : resp.status + ' - ' + resp.statusText
})
progressBar.update(i+1)
continue
resp: !resp ? null : resp.status + ' - ' + resp.statusText,
});
progressBar.update(i + 1);
continue;
}
const info = await resp.json()
const info = await resp.json();

// Query symbol on contract if couldn't find it
let validSymbol
if (!info.symbol || info.symbol === "") {
const erc1155contract = new ethers.Contract(erc1155Contracts[i], erc1155json.abi, provider)
let validSymbol;
if (!info.symbol || info.symbol === '') {
const erc1155contract = new ethers.Contract(erc1155Contracts[i], erc1155json.abi, provider);
try {
validSymbol = await erc1155contract.symbol()
validSymbol = await erc1155contract.symbol();
} catch {
validSymbol = ""
validSymbol = '';
}
} else {
validSymbol = info.symbol
validSymbol = info.symbol;
}

// Force some basic validation so they are compatible with schema
validSymbol = validSymbol.length <= 20 ? validSymbol : validSymbol.slice(0,20)
const validName = !info.name || info.name.length <= 64 ? info.name : info.name.slice(0,64)
const validDescription = !info.description || info.description.length <= 1000 ? info.description : info.description.slice(0,997) + '...'
validSymbol = validSymbol.length <= 20 ? validSymbol : validSymbol.slice(0, 20);
const validName = !info.name || info.name.length <= 64 ? info.name : info.name.slice(0, 64);
const validDescription =
!info.description || info.description.length <= 1000
? info.description
: info.description.slice(0, 997) + '...';

// Append token to list
newYearnList.push({
chainId: 1,
address: erc1155Contracts[i],
name: validName,
standard: "erc1155",
symbol: validSymbol === "" ? null : validSymbol,
logoURI: !info.image_url || info.image_url === "" ? null : info.image_url,
standard: 'erc1155',
symbol: validSymbol === '' ? null : validSymbol,
logoURI: !info.image_url || info.image_url === '' ? null : info.image_url,
extensions: {
"link": !info.external_link || info.external_link === "" ? null : info.external_link,
"description": !validDescription || validDescription === "" ? null : validDescription
}
})
link: !info.external_link || info.external_link === '' ? null : info.external_link,
description: !validDescription || validDescription === '' ? null : validDescription,
},
});

progressBar.update(i+1)
progressBar.update(i + 1);
}
progressBar.stop()
progressBar.stop();

// Print contracts that were ignored and why
if (errors.length > 0) {
console.log('Contracts ignored')
console.log(errors)
console.log('\n')
console.log('Contracts ignored');
console.log(errors);
console.log('\n');
}

// Validate the list fetched against current YearnList schema1
const ajv = new Ajv()
const validateList = ajv.compile(schema)
const ajv = new Ajv();
const validateList = ajv.compile(schema);

// Update token list version
// Increment minor version when tokens are added
// Increment patch version when tokens already on the list have details changed
const newErc1155List = {
...erc1155,
timestamp: (new Date()).toISOString(),
timestamp: new Date().toISOString(),
tokens: newYearnList,
version: nextVersion(erc1155.version, newYearnList.length > erc1155.tokens.length ? VersionUpgrade.MINOR : VersionUpgrade.PATCH)
}
version: nextVersion(
erc1155.version,
newYearnList.length > erc1155.tokens.length ? VersionUpgrade.MINOR : VersionUpgrade.PATCH,
),
};

// Validate list against schema
if (!validateList(newErc1155List)) {
console.log("New list has invalid schema: ")
console.log(validateList.errors)
console.log('New list has invalid schema: ');
console.log(validateList.errors);
//throw Error("^^^")
}

// Check whether list changed or not (except version)
if (isEqual(newErc1155List.tokens, erc1155.tokens)) {
console.log("List is already up-to-date")
return
}
console.log('List is already up-to-date');
return;
}

// Store latest erc-1155 tokens list
fs.writeFile(
ERC1155_LIST_PATH,
JSON.stringify(newErc1155List),
{ flag: "w+" },
function (err) {
if (err) throw err
console.log("ERC-1155 Mainnet List Updated")
}
)
}
fs.writeFile(ERC1155_LIST_PATH, JSON.stringify(newErc1155List), { flag: 'w+' }, function (err) {
if (err) throw err;
console.log('ERC-1155 Mainnet List Updated');
});
};

main()
.then(() => {
console.log("Finished")
console.log('Finished');
})
.catch((error) => {
console.error(error)
})
console.error(error);
});
Loading

0 comments on commit 6b57c51

Please sign in to comment.