dethcrypto / TypeChain

šŸ”Œ TypeScript bindings for Ethereum smart contracts
MIT License
2.76k stars 361 forks source link

Failing to generate types for @account-abstraction/contracts #816

Open naddison36 opened 1 year ago

naddison36 commented 1 year ago

I'm trying to use the ERC-4337 contracts in the @account-abstraction/contracts package.

I've created a simple Hardhat project that attempts to compile and generate types for the @account-abstraction contracts. To replicate, please use the typechain branch as that is barebones. https://github.com/naddison36/abstract-accounts/tree/typechain

When I compile the project using yarn compile, I get the following error

Generating typings for: 17 artifacts in dir: src/types/typechain for target: ethers-v5
An unexpected error occurred:

SyntaxError: 'from' expected. (4:24)
  2 | /* tslint:disable */
  3 | /* eslint-disable */
> 4 | import type * as  from './..';
    |                        ^
  5 | export type {  };
  6 | export * as accountAbstraction from './@account-abstraction';
  7 | export * as openzeppelin from './@openzeppelin';
    at D (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/parser-typescript.js:1:17100)
    at LT (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/parser-typescript.js:257:10765)
    at Object.RT [as parse] (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/parser-typescript.js:257:11074)
    at Object.parse (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:7515:23)
    at coreFormat (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:8829:18)
    at formatWithCursor2 (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:9021:18)
    at /Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:38176:12
    at Object.format (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/prettier/index.js:38190:12)
    at prettierOutputTransformer (/Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/typechain/src/codegen/outputTransformers/prettier.ts:8:19)
    at /Users/nicholasaddison/Documents/workspaces/abstract-accounts/node_modules/typechain/src/typechain/io.ts:22:33 {
  loc: { start: { line: 4, column: 24 } },
  codeFrame: '\x1B[0m \x1B[90m 2 |\x1B[39m \x1B[90m/* tslint:disable */\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 3 |\x1B[39m \x1B[90m/* eslint-disable */\x1B[39m\x1B[0m\n' +
    "\x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 4 |\x1B[39m \x1B[36mimport\x1B[39m type \x1B[33m*\x1B[39m \x1B[36mas\x1B[39m  \x1B[36mfrom\x1B[39m \x1B[32m'./..'\x1B[39m\x1B[33m;\x1B[39m\x1B[0m\n" +
    '\x1B[0m \x1B[90m   |\x1B[39m                        \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 5 |\x1B[39m \x1B[36mexport\x1B[39m type {  }\x1B[33m;\x1B[39m\x1B[0m\n' +
    "\x1B[0m \x1B[90m 6 |\x1B[39m \x1B[36mexport\x1B[39m \x1B[33m*\x1B[39m \x1B[36mas\x1B[39m accountAbstraction \x1B[36mfrom\x1B[39m \x1B[32m'./@account-abstraction'\x1B[39m\x1B[33m;\x1B[39m\x1B[0m\n" +
    "\x1B[0m \x1B[90m 7 |\x1B[39m \x1B[36mexport\x1B[39m \x1B[33m*\x1B[39m \x1B[36mas\x1B[39m openzeppelin \x1B[36mfrom\x1B[39m \x1B[32m'./@openzeppelin'\x1B[39m\x1B[33m;\x1B[39m\x1B[0m"
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Interestingly, types are created in the src/types and src/types/types folders. If I just use the Hardhat typechain plug-in defaults, types are created in typechain-types and the project root.

Screenshot 2023-03-15 at 10 54 28 am

I've kept the installed packages to a minimum to try and isolate the problem

npm ls
abstract-accounts@0.0.1 /Users/nicholasaddison/Documents/workspaces/abstract-accounts
ā”œā”€ā”€ @account-abstraction/contracts@0.5.0
ā”œā”€ā”€ @nomiclabs/hardhat-ethers@2.2.2
ā”œā”€ā”€ @openzeppelin/contracts@4.8.2
ā”œā”€ā”€ @typechain/ethers-v5@10.2.0
ā”œā”€ā”€ @typechain/hardhat@6.1.5
ā”œā”€ā”€ @types/node@18.15.3
ā”œā”€ā”€ defender-relay-client@1.39.0
ā”œā”€ā”€ ethers@5.7.2
ā”œā”€ā”€ hardhat@2.13.0
ā”œā”€ā”€ ts-generator@0.1.1
ā”œā”€ā”€ ts-node@10.9.1
ā”œā”€ā”€ typechain@8.1.1
ā””ā”€ā”€ typescript@4.9.5

The typechain config in Hardhat is

typechain: {
      outDir: "src/types/typechain",
      target: "ethers-v5",
  },
andrevmatos commented 1 year ago

This seems to be caused by Typechain normalizing files/directories names by simply removing special symbols; in this case, it's importing from "./..", which after normalized, becomes "". We got a similar issue, but with version-named folders:

import type * as 047 from './0.4.7';

047 is the naive normalization of the path, but isn't a valid identifier, and prettier is rightfully complaining. Whatever function is generating those alias import names, should make sure they're both unique and valid.

naddison36 commented 1 year ago

Thanks for commenting @andrevmatos.

The contracts in @account-abstraction/contracts do use relative paths but so does openzeppelin-contracts which does work with Typechain when imported to a project.

Screenshot 2023-03-21 at 1 53 17 pm

I'll try stripping out the contracts in @account-abstraction/contracts to see if I can isolate which contracts are causing the problem.