-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[Typechain] Investigate how to support the attach method #6442
Merged
ChristopherDedominici
merged 8 commits into
v-next
from
add-attach-support-in-hh-typechain
Mar 7, 2025
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1bffc5f
add logic to support attach method
ChristopherDedominici 89f95f9
Create small-buckets-ring.md
ChristopherDedominici b105a26
update tests
ChristopherDedominici b9c24a4
Merge branch 'add-attach-support-in-hh-typechain' of github.com:Nomic…
ChristopherDedominici bab4d41
Merge branch 'v-next' into add-attach-support-in-hh-typechain
ChristopherDedominici 6983ad1
Merge branch 'v-next' into add-attach-support-in-hh-typechain
ChristopherDedominici 287102a
add comment with example of factory file structure
ChristopherDedominici 8f2403d
Merge branch 'add-attach-support-in-hh-typechain' of github.com:Nomic…
ChristopherDedominici 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,5 @@ | ||
--- | ||
"@nomicfoundation/hardhat-typechain": patch | ||
--- | ||
|
||
Added support for the `attach` method in `hardhat-typechain`. |
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 |
---|---|---|
|
@@ -110,6 +110,8 @@ function addCompiledFilesTransformerIfAbsent( | |
'declare module "@nomicfoundation/hardhat-ethers/types"', | ||
); | ||
|
||
modifiedContent = addSupportForAttachMethod(modifiedContent); | ||
|
||
return modifiedContent; | ||
}; | ||
|
||
|
@@ -140,3 +142,37 @@ export function addJsExtensionsIfNeeded(content: string): string { | |
`import ${imports} from ${quote}${path}/index.js${quote};`, | ||
); | ||
} | ||
|
||
function addSupportForAttachMethod(modifiedContent: string): string { | ||
galargh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const pattern = /class\s+(\w+)__factory/; // Pattern to find the contract name in factory files | ||
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. Just a note for other reviewers because I checked it myself, |
||
const match = modifiedContent.match(pattern); | ||
galargh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (match === null) { | ||
// File is not a factory file, so there is no need to modify it | ||
return modifiedContent; | ||
} | ||
|
||
const contractName = match[1]; | ||
|
||
// Insert the "attach" snippet right before the "connect" method | ||
const insertPoint = modifiedContent.lastIndexOf("static connect("); | ||
galargh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const attachMethod = ` | ||
override attach(address: string | Addressable): ${contractName} { | ||
return super.attach(address) as ${contractName}; | ||
} | ||
`; | ||
|
||
modifiedContent = | ||
modifiedContent.slice(0, insertPoint) + | ||
attachMethod + | ||
modifiedContent.slice(insertPoint); | ||
|
||
// Import the "Addressable" type as it is required by the "attach" method | ||
modifiedContent = modifiedContent.replace( | ||
"/* eslint-disable */", | ||
galargh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'/* eslint-disable */\nimport type { Addressable } from "ethers";', | ||
); | ||
|
||
return modifiedContent; | ||
} |
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.
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.
As with the other logic in this file, since
Typechain
is no longer maintained, we cannot open PRs to the repository, so we need to find a workaround.The logic behind this code is:
ContractFactory
attach
methodethers
import to support theattach
methodThere 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.
We discussed this possibility in the design doc but discarded it due to several maintenance complications