-
Notifications
You must be signed in to change notification settings - Fork 6
Install contracts pkg #119
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
| * into docs/contracts/ with Docusaurus-compatible frontmatter | ||
| * and category metadata. | ||
| * | ||
| * Generates the Deployed Addresses page from @yellow-org/contracts. | ||
| * | ||
| * Usage: node scripts/sync-contracts-docs.js | ||
| * | ||
| * Safe to re-run — it wipes docs/contracts/ and rebuilds from source. | ||
|
|
@@ -14,6 +16,32 @@ const ROOT = path.resolve(__dirname, '..'); | |
| const SRC = path.join(ROOT, 'vendors', 'yellow', 'docs'); | ||
| const DEST = path.join(ROOT, 'docs', 'contracts'); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // On-chain addresses from @yellow-org/contracts | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| let addresses; | ||
| try { | ||
| addresses = require('@yellow-org/contracts').addresses; | ||
| } catch { | ||
| addresses = null; | ||
| } | ||
|
|
||
| const CHAINS = { | ||
| 1: { name: 'Ethereum Mainnet', explorer: 'https://etherscan.io/address' }, | ||
| 11155111: { name: 'Sepolia Testnet', explorer: 'https://sepolia.etherscan.io/address' }, | ||
| }; | ||
|
|
||
| const ADDRESS_LABELS = { | ||
| yellowToken: 'YellowToken', | ||
| nodeRegistry: 'NodeRegistry', | ||
| appRegistry: 'AppRegistry', | ||
| governor: 'YellowGovernor', | ||
| timelock: 'TimelockController', | ||
| treasury: 'Treasury', | ||
| faucet: 'Faucet', | ||
| }; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Helpers | ||
| // --------------------------------------------------------------------------- | ||
|
|
@@ -69,7 +97,6 @@ function copyWithFrontmatter(srcFile, destFile, { title, description, sidebarPos | |
| content = content.replace(/^<!-- AUTO-GENERATED.*?-->\n*/m, ''); | ||
|
|
||
| // Escape curly braces in prose so MDX doesn't treat them as JSX expressions. | ||
| // Only escape {word} patterns outside of code blocks/fences. | ||
| content = escapeCurlyBracesOutsideCode(content); | ||
|
|
||
| const frontmatter = [ | ||
|
|
@@ -102,6 +129,66 @@ function writeCategory(dir, { label, position, link }) { | |
| fs.writeFileSync(path.join(dir, '_category_.json'), JSON.stringify(data, null, 2) + '\n'); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Address page generator | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| function generateAddressesPage() { | ||
| const lines = [ | ||
| '---', | ||
| 'title: "Deployed Addresses"', | ||
| 'description: "Mainnet and testnet contract addresses for Yellow Network."', | ||
| 'sidebar_position: 2', | ||
| 'displayed_sidebar: contractsSidebar', | ||
| '---', | ||
| '', | ||
| '', | ||
| '# Deployed Addresses', | ||
| '', | ||
| ]; | ||
|
|
||
| if (!addresses) { | ||
| lines.push('> Install `@yellow-org/contracts` and re-run `npm run sync:contracts` to populate addresses.', ''); | ||
| return lines.join('\n'); | ||
| } | ||
|
|
||
| for (const [chainIdStr, addrs] of Object.entries(addresses)) { | ||
| const chainId = Number(chainIdStr); | ||
| const chain = CHAINS[chainId]; | ||
| if (!chain) continue; | ||
|
|
||
|
Comment on lines
+155
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unknown chain IDs are silently dropped from docs.
Proposed fix- const chain = CHAINS[chainId];
- if (!chain) continue;
+ const chain = CHAINS[chainId] || { name: `Chain ${chainId}`, explorer: null };
@@
- if (addr) {
- lines.push(`| ${label} | [\`${addr}\`](${chain.explorer}/${addr}) |`);
+ if (addr) {
+ const rendered = chain.explorer ? `[\`${addr}\`](${chain.explorer}/${addr})` : `\`${addr}\``;
+ lines.push(`| ${label} | ${rendered} |`);
} else {
lines.push(`| ${label} | — |`);
}🤖 Prompt for AI Agents |
||
| lines.push(`## ${chain.name} (Chain ID: ${chainId})`, ''); | ||
| lines.push('| Contract | Address |', '|---|---|'); | ||
|
|
||
| for (const [key, label] of Object.entries(ADDRESS_LABELS)) { | ||
| const addr = addrs[key]; | ||
| if (addr) { | ||
| lines.push(`| ${label} | [\`${addr}\`](${chain.explorer}/${addr}) |`); | ||
| } else { | ||
| lines.push(`| ${label} | — |`); | ||
| } | ||
| } | ||
| lines.push(''); | ||
| } | ||
|
|
||
| lines.push( | ||
| '## Using in Code', | ||
| '', | ||
| '```ts', | ||
| 'import { addresses } from "@yellow-org/contracts";', | ||
| '', | ||
| '// Mainnet', | ||
| 'addresses[1].yellowToken;', | ||
| '', | ||
| '// Sepolia', | ||
| 'addresses[11155111].faucet;', | ||
| '```', | ||
| '', | ||
| ); | ||
|
|
||
| return lines.join('\n'); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Document mapping | ||
| // --------------------------------------------------------------------------- | ||
|
|
@@ -114,12 +201,7 @@ const DOCS = { | |
| description: 'Overview of Yellow Network smart contracts, governance, and on-chain infrastructure.', | ||
| sidebarPosition: 1, | ||
| }, | ||
| addresses: { | ||
| src: 'operations/addresses.md', | ||
| title: 'Deployed Addresses', | ||
| description: 'Mainnet and testnet contract addresses for Yellow Network.', | ||
| sidebarPosition: 2, | ||
| }, | ||
| // 'addresses' is generated from @yellow-org/contracts — not copied from vendor | ||
| faq: { | ||
| src: 'FAQ.md', | ||
| title: 'FAQ', | ||
|
|
@@ -290,7 +372,16 @@ function main() { | |
| }); | ||
| } | ||
|
|
||
| // Copy and transform each document | ||
| // Generate addresses page from @yellow-org/contracts | ||
| const addressesPage = generateAddressesPage(); | ||
| fs.writeFileSync(path.join(DEST, 'addresses.md'), addressesPage); | ||
| if (addresses) { | ||
| console.log(' generated addresses.md (from @yellow-org/contracts)'); | ||
| } else { | ||
| console.warn(' generated addresses.md (WARNING: @yellow-org/contracts not installed)'); | ||
| } | ||
|
|
||
| // Copy and transform each document from vendor | ||
| let count = 0; | ||
| const missing = []; | ||
|
|
||
|
|
@@ -315,7 +406,7 @@ function main() { | |
| } | ||
| } | ||
|
|
||
| console.log(`\nSynced ${count} docs into docs/contracts/`); | ||
| console.log(`\nSynced ${count} vendor docs + 1 generated into docs/contracts/`); | ||
| console.log('\nStructure:'); | ||
|
|
||
| // Print tree | ||
|
|
@@ -331,9 +422,7 @@ function main() { | |
| const isLast = i === entries.length - 1; | ||
| const connector = isLast ? '└── ' : '├── '; | ||
| const full = path.join(dir, entry.name); | ||
|
|
||
| if (entry.name === '_category_.json') return; // skip noise | ||
|
|
||
| console.log(`${prefix}${connector}${entry.name}`); | ||
| if (entry.isDirectory()) { | ||
| printTree(full, prefix + (isLast ? ' ' : '│ ')); | ||
|
|
||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: layer-3/docs
Length of output: 90
🏁 Script executed:
rg "@yellow-org/contracts" --type jsonRepository: layer-3/docs
Length of output: 340
🏁 Script executed:
git ls-files scripts/ | head -20Repository: layer-3/docs
Length of output: 165
🏁 Script executed:
cat -n scripts/sync-contracts-docs.js | head -50Repository: layer-3/docs
Length of output: 1947
🌐 Web query:
@yellow-org/contracts npm module exports addresses CommonJS💡 Result:
If
@yellow-org/contractsis published as ESM-only (common for “contracts artifacts” packages), then CommonJSrequire()won’t give you named exports the way you expect.How to consume it from CommonJS
Option A (recommended): use dynamic
import()from CJSNode can load ESM via
import(), and when importing CommonJS from ESM (or vice‑versa), interop often goes through a default export wrapper. (mirrors.aliyun.com)Option B:
require()and look for.defaultThis is needed when Node represents the other module system via a namespace object whose main value is on
default. (mirrors.aliyun.com)Option C: if you control the package, add conditional exports
In the package’s
package.json, publish both entry points:{ "exports": { ".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs" } } }This is the standard way to support both
importandrequire. (nodejs.org)If
require()throws “ERR_REQUIRE_ESM”That means it’s ESM-only; use Option A (dynamic
import()), or ship a CJS build + conditional exports (Option C). Node only supports requiring ESM in limited/experimental ways. (mirrors.aliyun.com)Citations:
🏁 Script executed:
Repository: layer-3/docs
Length of output: 2139
🏁 Script executed:
rg "@yellow-org/contracts" --type js -A 3 -B 3Repository: layer-3/docs
Length of output: 3341
🏁 Script executed:
Repository: layer-3/docs
Length of output: 1805
Avoid broad catch blocks—only catch missing module errors.
This catches every runtime failure and silently degrades to
addresses = null, which masks real module issues (broken exports, syntax errors, incompatible formats). CatchMODULE_NOT_FOUNDspecifically and rethrow unexpected errors so actual problems surface during development.Proposed fix
let addresses; try { addresses = require('@yellow-org/contracts').addresses; -} catch { - addresses = null; +} catch (err) { + if (err && err.code === 'MODULE_NOT_FOUND') { + addresses = null; + } else { + throw err; + } }📝 Committable suggestion
🤖 Prompt for AI Agents