Skip to content

Commit 415fc37

Browse files
committed
check if contract is already verified
1 parent e75025f commit 415fc37

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/routes/contract/deploy/erc20.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { erc20bytecode } from "../../../constants/bytecodes/erc20";
66
import { TransactionService } from "../../../services/transaction.service";
77
import { getBlockExplorerUrl, getContractAddressFromEvent } from "../../../utils/other";
88
import { logRequest, logStep, logError } from '../../../utils/loggingUtils';
9-
import { verifyContract } from "../../../utils/contractVerification";
9+
import { isContractVerified, verifyContract } from "../../../utils/contractVerification";
1010
import { erc20JsonInputMetadata } from "../../../constants/contractJsonInputs/erc20";
1111

1212
type ERC20DeployRequestBody = {
@@ -150,7 +150,7 @@ export async function erc20Deploy(fastify: FastifyInstance) {
150150

151151
logStep(request, 'Deploy transaction success', { txHash: receipt?.hash });
152152

153-
if (process.env.VERIFY_CONTRACT_ON_DEPLOY === 'true') {
153+
if (process.env.VERIFY_CONTRACT_ON_DEPLOY === 'true' && !isContractVerified(deployedContractAddress, chainId)) {
154154
logStep(request, 'Verifying contract', {
155155
chainId,
156156
contractAddress: deployedContractAddress,

src/utils/contractVerification.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ export const verifyContract = async (params: VerifyContractParams) => {
4242
return response;
4343
}
4444

45+
export const getContractSourceCode = async (contractAddress: string, chainId: string) => {
46+
const url = `${ETHERSCAN_V2_API}?chainid=${chainId}&module=contract&action=getsourcecode&address=${contractAddress}&apikey=${process.env.ETHERSCAN_API_KEY}`;
47+
console.log('URL', url);
48+
const response = await fetch(url, {
49+
method: 'GET',
50+
})
51+
.then(res => res.json())
52+
.catch(console.error);
53+
return response.result[0].SourceCode;
54+
}
55+
56+
export const isContractVerified = async (contractAddress: string, chainId: string) => {
57+
const sourceCode = await getContractSourceCode(contractAddress, chainId);
58+
return sourceCode !== null && sourceCode !== undefined;
59+
}
60+
4561
/**
4662
* Extracts the first source file from the sources object and returns it in the format "contract/{name.sol}:{name}".
4763
* @param sources The sources object from the contract JSON input.

0 commit comments

Comments
 (0)