Skip to content
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

🐛 Fix: Change contract import of generated file to import type {Contract} #1382

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-carpets-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tevm/runtime": patch
---

Changed the import type of Contract in generated files to include an explicit `type` modifier
5 changes: 5 additions & 0 deletions .changeset/tidy-pans-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tevm/runtime": patch
---

Added natspec comments to contract imports linking to documentation
30 changes: 26 additions & 4 deletions bundler-packages/base-bundler/src/bundler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,13 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "import { Contract } from '@tevm/contract'
"code": "import type { Contract } from '@tevm/contract'
const _abiTestContract = [] as const;
const _nameTestContract = "TestContract" as const;
/**
* TestContract Contract
* TestContract Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const TestContract: Contract<typeof _nameTestContract, typeof _abiTestContract, undefined, undefined, undefined, undefined>;",
"modules": {
Expand Down Expand Up @@ -349,11 +351,13 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "import { Contract } from '@tevm/contract'
"code": "import type { Contract } from '@tevm/contract'
const _abiTestContract = [] as const;
const _nameTestContract = "TestContract" as const;
/**
* TestContract Contract
* TestContract Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const TestContract: Contract<typeof _nameTestContract, typeof _abiTestContract, undefined, undefined, undefined, undefined>;",
"modules": {
Expand Down Expand Up @@ -438,6 +442,9 @@ describe(bundler.name, () => {
"name": "TestContract",
"humanReadableAbi": []
} as const
/**
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const TestContract = createContract(_TestContract)",
"modules": {
"module1": {
Expand Down Expand Up @@ -521,6 +528,9 @@ describe(bundler.name, () => {
"name": "TestContract",
"humanReadableAbi": []
} as const
/**
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const TestContract = createContract(_TestContract)",
"modules": {
"module1": {
Expand Down Expand Up @@ -604,6 +614,9 @@ describe(bundler.name, () => {
"name": "TestContract",
"humanReadableAbi": []
}
/**
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
module.exports.TestContract = createContract(_TestContract)",
"modules": {
"module1": {
Expand Down Expand Up @@ -687,6 +700,9 @@ describe(bundler.name, () => {
"name": "TestContract",
"humanReadableAbi": []
}
/**
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
module.exports.TestContract = createContract(_TestContract)",
"modules": {
"module1": {
Expand Down Expand Up @@ -770,6 +786,9 @@ describe(bundler.name, () => {
"name": "TestContract",
"humanReadableAbi": []
}
/**
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const TestContract = createContract(_TestContract)",
"modules": {
"module1": {
Expand Down Expand Up @@ -853,6 +872,9 @@ describe(bundler.name, () => {
"name": "TestContract",
"humanReadableAbi": []
}
/**
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const TestContract = createContract(_TestContract)",
"modules": {
"module1": {
Expand Down
4 changes: 2 additions & 2 deletions bundler-packages/runtime/src/generateRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { generateTevmBody } from './generateTevmBody.js'
const importsByModuleType = (contractPackage) => ({
contract: {
cjs: `const { createContract } = require('${contractPackage}')`,
dts: `import { Contract } from '${contractPackage}'`,
dts: `import type { Contract } from '${contractPackage}'`,
ts: `import { createContract } from '${contractPackage}'`,
mjs: `import { createContract } from '${contractPackage}'`,
},
script: {
cjs: `const { createContract } = require('${contractPackage}')`,
dts: `import { Contract } from '${contractPackage}'`,
dts: `import type { Contract } from '${contractPackage}'`,
ts: `import { createContract } from '${contractPackage}'`,
mjs: `import { createContract } from '${contractPackage}'`,
},
Expand Down
9 changes: 7 additions & 2 deletions bundler-packages/runtime/src/generateRuntime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('generateRuntime', () => {
/**
* MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
module.exports.MyContract = createContract(_MyContract)"
`)
Expand All @@ -62,11 +63,13 @@ describe('generateRuntime', () => {
it('should handle dts module type', () => {
const result = runSync(generateRuntime(artifacts, 'dts', false, '@tevm/contract'))
expect(result).toMatchInlineSnapshot(`
"import { Contract } from '@tevm/contract'
"import type { Contract } from '@tevm/contract'
const _abiMyContract = ["constructor() payable"] as const;
const _nameMyContract = "MyContract" as const;
/**
* MyContract Contract
* MyContract Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
* @notice MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
*/
Expand All @@ -87,6 +90,7 @@ describe('generateRuntime', () => {
/**
* MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const MyContract = createContract(_MyContract)"
`)
Expand All @@ -105,6 +109,7 @@ describe('generateRuntime', () => {
/**
* MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const MyContract = createContract(_MyContract)"
`)
Expand Down
8 changes: 4 additions & 4 deletions bundler-packages/runtime/src/generateTevmBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export const generateTevmBody = (artifacts, moduleType, includeBytecode) => {
if (userdoc.notice) {
natspec.unshift(` * ${userdoc.notice}`)
}
if (natspec.length) {
natspec.unshift('/**')
natspec.push(' */')
}
natspec.push(' * @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation')
natspec.unshift('/**')
natspec.push(' */')

if (moduleType === 'cjs') {
return [
`const _${contractName} = ${contract}`,
Expand Down
14 changes: 12 additions & 2 deletions bundler-packages/runtime/src/generateTevmBody.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('generateTevmBody', () => {
/**
* MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
module.exports.MyContract = createContract(_MyContract)
const _AnotherContract = {
Expand All @@ -53,6 +54,7 @@ describe('generateTevmBody', () => {
}
/**
* MyContract
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
module.exports.AnotherContract = createContract(_AnotherContract)"
`)
Expand All @@ -68,6 +70,7 @@ describe('generateTevmBody', () => {
/**
* MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const MyContract = createContract(_MyContract)
const _AnotherContract = {
Expand All @@ -76,6 +79,7 @@ describe('generateTevmBody', () => {
}
/**
* MyContract
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const AnotherContract = createContract(_AnotherContract)"
`)
Expand All @@ -91,6 +95,7 @@ describe('generateTevmBody', () => {
/**
* MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const MyContract = createContract(_MyContract)
const _AnotherContract = {
Expand All @@ -99,6 +104,7 @@ describe('generateTevmBody', () => {
} as const
/**
* MyContract
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const AnotherContract = createContract(_AnotherContract)"
`)
Expand All @@ -110,15 +116,19 @@ describe('generateTevmBody', () => {
"const _abiMyContract = [] as const;
const _nameMyContract = "MyContract" as const;
/**
* MyContract Contract
* MyContract Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
* @notice MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
*/
export const MyContract: Contract<typeof _nameMyContract, typeof _abiMyContract, undefined, undefined, undefined, undefined>;
const _abiAnotherContract = [] as const;
const _nameAnotherContract = "AnotherContract" as const;
/**
* AnotherContract Contract
* AnotherContract Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
* @notice MyContract
*/
export const AnotherContract: Contract<typeof _nameAnotherContract, typeof _abiAnotherContract, undefined, undefined, undefined, undefined>;"
Expand Down
11 changes: 5 additions & 6 deletions bundler-packages/runtime/src/generateTevmBodyDts.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ export const generateDtsBody = (artifacts, includeBytecode) => {
return [
`const _name${contractName} = ${JSON.stringify(contractName, null, 2)} as const;`,
`const _abi${contractName} = ${JSON.stringify(contract.humanReadableAbi, null, 2)} as const;`,
'// type _Address = undefined',
'// type _Bytecode = `0x${string}`',
'// type _DeployedBytecode = `0x${string}`',
'// type _Code = undefined',
'/**',
` * ${contractName} Contract+Script`,
` * ${contractName} Contract (with bytecode)`,
...natspec,
' * @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation',
' */',
`export const ${contractName}: Contract<`,
` typeof _name${contractName},`,
Expand All @@ -46,7 +43,9 @@ export const generateDtsBody = (artifacts, includeBytecode) => {
`const _abi${contractName} = ${JSON.stringify(contract.humanReadableAbi)} as const;`,
`const _name${contractName} = ${JSON.stringify(contractName)} as const;`,
'/**',
` * ${contractName} Contract`,
` * ${contractName} Contract (no bytecode)`,
` * change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode`,
' * @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation',
...natspec,
' */',
`export const ${contractName}: Contract<typeof _name${contractName}, typeof _abi${contractName}, undefined, undefined, undefined, undefined>;`,
Expand Down
12 changes: 9 additions & 3 deletions bundler-packages/runtime/src/generateTevmBodyDts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,28 @@ describe('generateDtsBody', () => {
"const _abiMyContract = ["constructor() payable"] as const;
const _nameMyContract = "MyContract" as const;
/**
* MyContract Contract
* MyContract Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
* @notice MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
*/
export const MyContract: Contract<typeof _nameMyContract, typeof _abiMyContract, undefined, undefined, undefined, undefined>;
const _abiAnotherContract = [] as const;
const _nameAnotherContract = "AnotherContract" as const;
/**
* AnotherContract Contract
* AnotherContract Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
* @notice MyContract
*/
export const AnotherContract: Contract<typeof _nameAnotherContract, typeof _abiAnotherContract, undefined, undefined, undefined, undefined>;
const _abiMissingContract = [] as const;
const _nameMissingContract = "MissingContract" as const;
/**
* MissingContract Contract
* MissingContract Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
* @notice MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
*/
Expand Down
10 changes: 7 additions & 3 deletions lsp/ts-plugin/src/decorators/getScriptSnapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,21 @@ describe(getScriptSnapshotDecorator.name, () => {
const fileName = path.join(__dirname, '../test/fixtures/HelloWorld2.sol')
const result = decorator.getScriptSnapshot(fileName)
expect((result as any).text).toMatchInlineSnapshot(`
"import { Contract } from 'tevm/contract'
"import type { Contract } from 'tevm/contract'
const _abiHelloWorld = ["function greet() pure returns (string)"] as const;
const _nameHelloWorld = "HelloWorld" as const;
/**
* HelloWorld Contract
* HelloWorld Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const HelloWorld: Contract<typeof _nameHelloWorld, typeof _abiHelloWorld, undefined, undefined, undefined, undefined>;
const _abiHelloWorld2 = ["function greet2() pure returns (string)"] as const;
const _nameHelloWorld2 = "HelloWorld2" as const;
/**
* HelloWorld2 Contract
* HelloWorld2 Contract (no bytecode)
* change file name or add file that ends in '.s.sol' extension if you wish to compile the bytecode
* @see [contract docs](https://tevm.sh/learn/contracts/) for more documentation
*/
export const HelloWorld2: Contract<typeof _nameHelloWorld2, typeof _abiHelloWorld2, undefined, undefined, undefined, undefined>;"
`)
Expand Down
Loading