Skip to content

Commit

Permalink
Merge pull request #1560 from sphinx-labs/pate/chu-616-investigate-wh…
Browse files Browse the repository at this point in the history
…y-etherscan-verification-failed-for-calnix-on

fix(core): Always attempt to verify contracts on Etherscan even if API says it is verified
  • Loading branch information
RPate97 authored Mar 15, 2024
2 parents 5f01a16 + e36f793 commit 7919415
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/clean-chairs-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sphinx-labs/plugins': patch
'@sphinx-labs/core': patch
---

Always attempt to verify contract even if Etherscan claims it is verified
41 changes: 30 additions & 11 deletions packages/core/src/etherscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,28 @@ export const attemptVerification = async (

const contractURL = etherscan.getContractUrl(address)

const isVerified = await etherscan.isVerified(address)
if (isVerified) {
console.log(
`The contract ${address} has already been verified on Etherscan:\n${contractURL}`
)
return { success: true }
}

let guid: string
// We wrap this in a try/catch because this call may throw an error if the contract was recently
// deployed and hasn't propogated to Etherscan's backend yet.
/**
* We wrap this in a try/catch because this call may throw an error if the contract was recently
* deployed and hasn't propogated to Etherscan's backend yet.
*
* An error may also occur if the contract is already verified which can happen in two scenarios:
* - We're retrying verification and this contract was succesfully verified in a previous attempt.
* - The contract was verified by some mechanism implemented by the Blockexplorer such as automatic
* linking via source code matching.
*
* You might wonder why we do not check if a contract is already verified before attempting to verify
* it here. The reason is that we've found that the `etherscan.isVerified()` function will sometimes
* return true for contracts have been verified through source code matching. However, the source code
* won't always appear in the Etherscan UI. We've found that in this situation if we simply attempt to
* verify the contract, the source code will appear in the Etherscan UI.
* See this repo to replicate that issue: https://github.com/sphinx-labs/etherscan_verification_bug
*
* It's useful to note that when attempting to verify a contract on Etherscan, an error may be thrown
* if the contract is already verified. So to handle that, we call `etherscan.isVerified()`in the catch
* block below and then return { success: true } if the contract is already verified according to that
* function.
*/
try {
const response = await etherscan.verify(
address,
Expand All @@ -226,7 +237,15 @@ export const attemptVerification = async (
)
guid = response.message
} catch (err) {
return { success: false, message: err.message }
const verified = await etherscan.isVerified(address)
if (verified) {
console.log(
`The contract ${address} has already been verified on Etherscan:\n${contractURL}`
)
return { success: true }
} else {
return { success: false, message: err.message }
}
}

const networkName = fetchNameForNetwork(BigInt(chainId))
Expand Down

0 comments on commit 7919415

Please sign in to comment.