-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
163 changed files
with
482,392 additions
and
472,492 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,166 @@ | ||
import { getAssetLists } from '@chain-registry/utils'; | ||
import { assets, ibc } from 'chain-registry'; | ||
import { writeFileSync } from 'fs'; | ||
import { assets, chains, ibc } from 'chain-registry'; | ||
import { rmSync, writeFileSync } from 'fs'; | ||
import { mkdirpSync } from 'mkdirp'; | ||
import path from 'path'; | ||
|
||
const NON_COSMOS_NETWORK_TYPE = 'noncosmos'; | ||
|
||
const chainNetworkMap = {}; | ||
const networkChainMap = {}; | ||
|
||
chains.forEach((chain) => { | ||
chainNetworkMap[chain.chain_name] = chain.network_type; | ||
}); | ||
|
||
const asset_lists = assets.reduce((m, { chain_name }) => { | ||
return [...m, ...getAssetLists(chain_name, ibc, assets)]; | ||
}, []); | ||
|
||
const write = (file, json, TypeName, isArray = false) => { | ||
const SRC_ROOT = `${__dirname}/../src`; | ||
rmSync(SRC_ROOT, { recursive: true, force: true }); | ||
|
||
const getValidVarName = (varName) => { | ||
if (!/^[a-zA-Z_$]/.test(varName)) { | ||
return `_${varName}`; | ||
} | ||
|
||
return varName; | ||
}; | ||
|
||
const writeNetworkIndex = (filePath, networkObj) => { | ||
writeFileSync( | ||
filePath, | ||
`${Object.keys(networkObj) | ||
.map((chain_name) => { | ||
return `export * as ${getValidVarName( | ||
chain_name | ||
)} from './${chain_name}'`; | ||
}) | ||
.filter(Boolean) | ||
.join(';\n')}` | ||
); | ||
}; | ||
|
||
const writeNetworkAssets = (filePath, networkObj) => { | ||
const validChain = []; | ||
const importStat = Object.keys(networkObj) | ||
.map((chain_name) => { | ||
validChain.push(chain_name); | ||
return `import _${chain_name} from './${chain_name}'`; | ||
}) | ||
.filter(Boolean) | ||
.join(';\n'); | ||
|
||
if (!validChain.length) { | ||
return false; | ||
} | ||
|
||
writeFileSync( | ||
filePath, | ||
`import { AssetList } from '@chain-registry/types'; | ||
${importStat} | ||
const assets: AssetList[] = [${validChain | ||
.map((chain_name) => { | ||
return `_${chain_name}`; | ||
}) | ||
.join(',')}]; | ||
export default assets; | ||
` | ||
); | ||
|
||
return true; | ||
}; | ||
|
||
const write = (filePath, json, TypeName, isArray = false) => { | ||
const strfy = JSON.stringify(json, null, 2); | ||
const exportType = isArray ? TypeName + '[]' : TypeName; | ||
writeFileSync( | ||
`${__dirname}/../src/${file}.ts`, | ||
filePath, | ||
`import { ${TypeName} } from '@chain-registry/types'; | ||
const ${file}: ${exportType} = ${strfy}; | ||
export default ${file}; | ||
const assets: ${exportType} = ${strfy}; | ||
export default assets; | ||
` | ||
); | ||
}; | ||
|
||
write(`asset_lists`, asset_lists, 'AssetList', true); | ||
const writeRootAssets = (filePath, obj) => { | ||
const validNetwork = []; | ||
const importStat = Object.keys(obj) | ||
.map((network_type) => { | ||
validNetwork.push(network_type); | ||
return `import _${network_type} from './${network_type}/assets'`; | ||
}) | ||
.filter(Boolean) | ||
.join(';\n'); | ||
|
||
if (!validNetwork.length) { | ||
return false; | ||
} | ||
|
||
writeFileSync( | ||
filePath, | ||
`import { AssetList } from '@chain-registry/types'; | ||
${importStat} | ||
const assets: AssetList[] = [${validNetwork | ||
.map((network_type) => { | ||
return `..._${network_type}`; | ||
}) | ||
.join(',')}]; | ||
export default assets; | ||
` | ||
); | ||
|
||
return true; | ||
}; | ||
|
||
const writeRootIndex = (filePath, obj) => { | ||
let imports = Object.keys(obj) | ||
.map((network_type) => { | ||
return `export * from './${network_type}'`; | ||
}) | ||
.filter(Boolean) | ||
.join(';\n'); | ||
|
||
imports = `${imports}; export * from './asset_lists';`; | ||
|
||
writeFileSync(filePath, `${imports}`); | ||
}; | ||
|
||
asset_lists.forEach((list) => { | ||
const network_type = | ||
chainNetworkMap[list.chain_name] ?? NON_COSMOS_NETWORK_TYPE; | ||
|
||
if (!networkChainMap[network_type]) { | ||
networkChainMap[network_type] = {}; | ||
} | ||
networkChainMap[network_type][list.chain_name] = true; | ||
const networkFolder = path.join(SRC_ROOT, network_type); | ||
|
||
mkdirpSync(networkFolder); | ||
|
||
const filePath = path.join(networkFolder, `${list.chain_name}.ts`); | ||
write(filePath, list, 'AssetList', false); | ||
}); | ||
|
||
Object.keys(networkChainMap).forEach((network_type) => { | ||
const networkFolder = path.join(SRC_ROOT, network_type); | ||
|
||
const assetsFilePath = path.join(networkFolder, 'assets.ts'); | ||
writeNetworkAssets(assetsFilePath, networkChainMap[network_type]); | ||
|
||
const indexFilePath = path.join(networkFolder, 'index.ts'); | ||
writeNetworkIndex(indexFilePath, networkChainMap[network_type]); | ||
}); | ||
|
||
const assetsRootFilePath = path.join(SRC_ROOT, 'asset_lists.ts'); | ||
writeRootAssets(assetsRootFilePath, networkChainMap); | ||
|
||
const indexRootFilePath = path.join(SRC_ROOT, 'index.ts'); | ||
writeRootIndex(indexRootFilePath, networkChainMap); |
Oops, something went wrong.