Skip to content

Commit b89b22c

Browse files
initial draft for template args rules (#195)
1 parent d14462d commit b89b22c

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

contributors/TEMPLATING.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,121 @@ The special files and folders are:
176176

177177
# Things worth mentioning
178178

179+
## Rules for template args:
180+
181+
The reason for converting normal file to template is allowing customisation to that file for extension developer.
182+
This customisation can be broadly broken into:
183+
184+
1. **When extending/modifying already declared variables/objects**:
185+
186+
- Use `preConfigContent` (string) for imports and variable declarations
187+
- Use `<name>Override` (object) to extend existing variables/objects
188+
- Can reference variables defined in template and `preConfigContent` in two ways:
189+
- `$$$variableName` - When variable needs to be used without quotes (expressions/variables)
190+
- example: `{ accounts: ["$$$deployerPrivateKey"] }` -> `{ accounts: [deployerPrivateKey] }`
191+
- `\${variableName}` - When variable needs to be interpolated within a string
192+
193+
<details>
194+
<summary>
195+
196+
Example `hardhat.config.js.template.mjs`
197+
198+
</summary>
199+
200+
```typescript
201+
import { withDefaults } from "../utils";
202+
203+
const defaultConfig = {
204+
networks: {
205+
hardhat: {
206+
enabled: '$$$process.env.MAINNET_FORKING_ENABLED === "true"', // enabled: process.env.MAINNET_FORKING_ENABLED === "true"
207+
chainId: 31337,
208+
},
209+
mainnet: {
210+
url: `https://eth-mainnet.g.alchemy.com/v2/\${providerApiKey}`,
211+
accounts: ["$$$deployerPrivateKey"], // ==> accounts: [deployerPrivateKey]
212+
},
213+
},
214+
};
215+
216+
export default withDefaults(
217+
({ preConfigContent, configOverrides }) => `
218+
${preConfigContent}
219+
220+
const deployerPrivateKey =
221+
process.env.__RUNTIME_DEPLOYER_PRIVATE_KEY ?? "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
222+
223+
const config = ${stringify({ ...defaultConfig, ...configOverrides })};
224+
225+
export default config;
226+
`,
227+
{
228+
preConfigContent: "",
229+
configOverrides: {},
230+
},
231+
);
232+
233+
// In extension's args file (hardhat.config.ts.args.mjs)
234+
export const preConfigContent = `
235+
// Custom variables
236+
const CUSTOM_API_KEY = process.env.CUSTOM_API_KEY;
237+
`;
238+
239+
export const configOverrides = {
240+
networks: {
241+
hardhat: {
242+
forking: {
243+
enabled: '$$$process.env.MAINNET_FORKING_ENABLED === "false"', // (expression) enabled: process.env.MAINNET_FORKING_ENABLED === "false"
244+
blockNumber: 1234567,
245+
},
246+
},
247+
customNetwork: {
248+
url: "https://custom.network",
249+
accounts: ["$$$deployerPrivateKey"], // (variable) accounts: [deployerPrivateKey]
250+
blah: `test \${CUSTOM_API_KEY}`, // (string interpolation) blah: `test ${CUSTOM_API_KEY}`
251+
verify: {
252+
etherscan: {
253+
apiUrl: "https://api.custom-explorer.io",
254+
apiKey: "$$$etherscanApiKey",
255+
},
256+
},
257+
},
258+
},
259+
};
260+
```
261+
262+
</details>
263+
264+
2. When adding new code/logic:
265+
266+
- Use descriptive/sensible `string` arguments. Depending on level of customisation.
267+
268+
<details>
269+
<summary>
270+
271+
Example `Component.tsx.template.mjs`
272+
273+
</summary>
274+
275+
```typescript
276+
export default withDefaults(
277+
({ preConfigContent, renderContent }) => `
278+
import { Base } from './Base';
279+
${preConfigContent}
280+
281+
export const Component = () => {
282+
${renderContent}
283+
};
284+
`,
285+
{
286+
preConfigContent: "",
287+
renderContent: "",
288+
},
289+
);
290+
```
291+
292+
</details>
293+
179294
## Recommended way to handle complex arguments in templates
180295

181296
Most of the time you will use string arguments for templating, but sometimes you will need to add arrays, objects, bigints, etc. You can handle them however you want, but we're recommending to use the table below as a helper.

0 commit comments

Comments
 (0)