-
Notifications
You must be signed in to change notification settings - Fork 370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: generate presetNetwork file from server #5410
Draft
originalix
wants to merge
9
commits into
x
Choose a base branch
from
chore/preset-network
base: x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a87a046
chore: generate presetNetwork from server
originalix 8302fbb
feat: sort by impl
originalix 6314645
fix: market explorer url
originalix cf7b35b
chore: update preset network
originalix 54efd11
Merge branch 'x' into chore/preset-network
originalix b9d8d27
Merge branch 'x' of github.com:OneKeyHQ/app-monorepo into chore/prese…
originalix 41df1a5
chore: lint
originalix 9006009
Merge branch 'chore/preset-network' of github.com:OneKeyHQ/app-monore…
originalix ca96b02
chore: generate getNetworkIdMaps
originalix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
const axios = require('axios'); | ||
const fs = require('fs'); | ||
const { exec } = require('child_process'); | ||
|
||
const API_URL = 'https://wallet.onekeytest.com/wallet/v1/network/list/all'; | ||
const OUTPUT_FILE = 'packages/shared/src/config/presetNetworks.ts'; | ||
|
||
function sanitizeShortcode(shortcode) { | ||
return shortcode.replace(/[-_]/g, ''); | ||
} | ||
|
||
function stringifyWithSingleQuotes(obj, space = 2) { | ||
const json = JSON.stringify(obj, null, space); | ||
return json.replace(/"([^"]+)":/g, "'$1':").replace(/"/g, "'"); | ||
} | ||
|
||
async function fetchNetworks() { | ||
try { | ||
if (fs.existsSync(OUTPUT_FILE)) { | ||
fs.unlinkSync(OUTPUT_FILE); | ||
console.log(`Deleted old ${OUTPUT_FILE}`); | ||
} | ||
|
||
const response = await axios.get(API_URL); | ||
const networks = response.data.data; | ||
|
||
if (!Array.isArray(networks) || !networks.length) { | ||
console.error('Networks is empty'); | ||
return; | ||
} | ||
|
||
console.log('====>>>networks: ', networks); | ||
|
||
let fileContent = `/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable spellcheck/spell-checker */ | ||
import { memoFn } from '@onekeyhq/shared/src/utils/cacheUtils'; | ||
import type { IServerNetwork } from '@onekeyhq/shared/types'; | ||
import { ENetworkStatus } from '@onekeyhq/shared/types'; | ||
|
||
import platformEnv from '../platformEnv'; | ||
|
||
// dangerNetwork represents a virtual network | ||
export const dangerAllNetworkRepresent: IServerNetwork = { | ||
'chainId': '0', | ||
'code': 'onekeyall', | ||
'decimals': 0, | ||
'id': 'onekeyall--0', | ||
'impl': 'onekeyall', | ||
'isTestnet': false, | ||
'isAllNetworks': true, | ||
'logoURI': 'https://uni.onekey-asset.com/static/logo/chain_selector_logo.png', | ||
'name': 'All Networks', | ||
'shortcode': 'onekeyall', | ||
'shortname': 'onekeyall', | ||
'symbol': 'ALL NETWORKS', | ||
'feeMeta': { | ||
'code': '', | ||
'decimals': 0, | ||
'symbol': '0', | ||
}, | ||
'defaultEnabled': true, | ||
'status': ENetworkStatus.LISTED, | ||
};\n | ||
`; | ||
|
||
// Group networks by impl | ||
const networkGroups = {}; | ||
networks.forEach((network) => { | ||
const { impl, shortcode } = network; | ||
if (!shortcode) return; | ||
|
||
if (!networkGroups[impl]) { | ||
networkGroups[impl] = []; | ||
} | ||
networkGroups[impl].push(network); | ||
}); | ||
|
||
const networkMap = new Map(); | ||
const networkDeclarations = []; | ||
|
||
Object.keys(networkGroups) | ||
.sort() | ||
.forEach((impl) => { | ||
if (networkDeclarations.length > 0) { | ||
networkDeclarations.push(''); | ||
} | ||
networkDeclarations.push(`// ${impl.toUpperCase()} networks`); | ||
|
||
networkGroups[impl].forEach((network) => { | ||
const { shortcode } = network; | ||
if (!shortcode) return; | ||
|
||
const sanitizedShortcode = sanitizeShortcode(shortcode); | ||
if (network.status === 'LISTED') { | ||
network.status = 'ENetworkStatus.LISTED'; | ||
} else { | ||
network.status = 'ENetworkStatus.TRASH'; | ||
} | ||
|
||
const networkString = stringifyWithSingleQuotes(network).replace( | ||
/'(ENetworkStatus\.(LISTED|TRASH))'/g, | ||
'$1', | ||
); | ||
|
||
const networkDeclaration = `const ${sanitizedShortcode}: IServerNetwork = ${networkString};`; | ||
|
||
networkDeclarations.push(networkDeclaration); | ||
networkMap.set(sanitizedShortcode, network); | ||
}); | ||
}); | ||
|
||
fileContent += networkDeclarations.join('\n'); | ||
|
||
fileContent += ` | ||
|
||
const chainsOnlyEnabledInDev: IServerNetwork[] = [ | ||
// Add your dev-only chains here | ||
]; | ||
|
||
export const presetNetworksMap = { | ||
${Array.from(networkMap.keys()).join(',\n ')} | ||
}; | ||
|
||
export const getPresetNetworks = memoFn((): IServerNetwork[] => [ | ||
dangerAllNetworkRepresent, | ||
${Array.from(networkMap.keys()).join(',\n ')}, | ||
...(platformEnv.isDev ? chainsOnlyEnabledInDev : []), | ||
]); | ||
`; | ||
fs.writeFileSync(OUTPUT_FILE, fileContent); | ||
console.log(`${OUTPUT_FILE} has been generated successfully.`); | ||
|
||
// Run ESLint fix command | ||
exec(`npx eslint --fix ${OUTPUT_FILE}`, (error, stdout, stderr) => { | ||
if (error) { | ||
console.error(`Error running ESLint: ${error}`); | ||
return; | ||
} | ||
console.log(`ESLint fix completed for ${OUTPUT_FILE}`); | ||
}); | ||
} catch (error) { | ||
console.error('Error fetching networks:', error.message); | ||
} | ||
} | ||
|
||
fetchNetworks(); |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getNetworkIdsMap 也可以在预生成好,减少运行时计算量