|
| 1 | +import type { EthereumProvider } from "hardhat/types"; |
| 2 | +import type { ChainConfig } from "../types"; |
| 3 | + |
| 4 | +import { HARDHAT_NETWORK_NAME } from "hardhat/plugins"; |
| 5 | + |
| 6 | +import { |
| 7 | + ChainConfigNotFoundError, |
| 8 | + HardhatNetworkNotSupportedError, |
| 9 | +} from "./errors"; |
| 10 | +import { ValidationResponse } from "./utilities"; |
| 11 | +import { builtinChains } from "./blockscout.chain-config"; |
| 12 | + |
| 13 | +import { Etherscan } from "./etherscan"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Blockscout verification provider for verifying smart contracts. |
| 17 | + */ |
| 18 | +export class Blockscout { |
| 19 | + private _etherscan: Etherscan; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a new instance of the Blockscout verification provider. |
| 23 | + * @param apiUrl - The Blockscout API URL, e.g. https://eth.blockscout.com/api. |
| 24 | + * @param browserUrl - The Blockscout browser URL, e.g. https://eth.blockscout.com. |
| 25 | + */ |
| 26 | + constructor(public apiUrl: string, public browserUrl: string) { |
| 27 | + this._etherscan = new Etherscan("api_key", apiUrl, browserUrl); |
| 28 | + } |
| 29 | + |
| 30 | + public static async getCurrentChainConfig( |
| 31 | + networkName: string, |
| 32 | + ethereumProvider: EthereumProvider, |
| 33 | + customChains: ChainConfig[] |
| 34 | + ): Promise<ChainConfig> { |
| 35 | + const currentChainId = parseInt( |
| 36 | + await ethereumProvider.send("eth_chainId"), |
| 37 | + 16 |
| 38 | + ); |
| 39 | + |
| 40 | + const currentChainConfig = [ |
| 41 | + // custom chains has higher precedence than builtin chains |
| 42 | + ...[...customChains].reverse(), // the last entry has higher precedence |
| 43 | + ...builtinChains, |
| 44 | + ].find(({ chainId }) => chainId === currentChainId); |
| 45 | + |
| 46 | + if (currentChainConfig === undefined) { |
| 47 | + if (networkName === HARDHAT_NETWORK_NAME) { |
| 48 | + throw new HardhatNetworkNotSupportedError(); |
| 49 | + } |
| 50 | + |
| 51 | + throw new ChainConfigNotFoundError(currentChainId); |
| 52 | + } |
| 53 | + |
| 54 | + return currentChainConfig; |
| 55 | + } |
| 56 | + |
| 57 | + public static fromChainConfig(chainConfig: ChainConfig): Blockscout { |
| 58 | + const apiUrl = chainConfig.urls.apiURL; |
| 59 | + const browserUrl = chainConfig.urls.browserURL.trim().replace(/\/$/, ""); |
| 60 | + |
| 61 | + return new Blockscout(apiUrl, browserUrl); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Check if a smart contract is verified on Blockscout. |
| 66 | + * @link https://docs.blockscout.com/for-users/api/rpc-endpoints/contract#get-contract-source-code-for-a-verified-contract |
| 67 | + * @param address - The address of the smart contract. |
| 68 | + * @returns True if the contract is verified, false otherwise. |
| 69 | + * @throws {NetworkRequestError} if there is an error on the request. |
| 70 | + * @throws {ContractVerificationInvalidStatusCodeError} if the API returns an invalid status code. |
| 71 | + */ |
| 72 | + public async isVerified(address: string): Promise<boolean> { |
| 73 | + return this._etherscan.isVerified(address); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Verify a smart contract on Blockscout. |
| 78 | + * @link https://docs.blockscout.com/for-users/api/rpc-endpoints/contract#verify-a-contract-with-standard-input-json-file |
| 79 | + * @param contractAddress - The address of the smart contract to verify. |
| 80 | + * @param sourceCode - The source code of the smart contract. |
| 81 | + * @param contractName - The name of the smart contract, e.g. "contracts/Sample.sol:MyContract" |
| 82 | + * @param compilerVersion - The version of the Solidity compiler used, e.g. `v0.8.19+commit.7dd6d404` |
| 83 | + * @returns A promise that resolves to an `BlockscoutResponse` object. |
| 84 | + * @throws {NetworkRequestError} if there is an error on the request. |
| 85 | + * @throws {ContractVerificationInvalidStatusCodeError} if the API returns an invalid status code. |
| 86 | + * @throws {ContractVerificationMissingBytecodeError} if the bytecode is not found on the block explorer. |
| 87 | + * @throws {ContractAlreadyVerifiedError} if the contract is already verified. |
| 88 | + * @throws {HardhatVerifyError} if the response status is not OK. |
| 89 | + */ |
| 90 | + public async verify( |
| 91 | + contractAddress: string, |
| 92 | + sourceCode: string, |
| 93 | + contractName: string, |
| 94 | + compilerVersion: string |
| 95 | + ): Promise<BlockscoutResponse> { |
| 96 | + const etherscanResponse = await this._etherscan.verify( |
| 97 | + contractAddress, |
| 98 | + sourceCode, |
| 99 | + contractName, |
| 100 | + compilerVersion, |
| 101 | + "" |
| 102 | + ); |
| 103 | + |
| 104 | + return new BlockscoutResponse( |
| 105 | + etherscanResponse.status, |
| 106 | + etherscanResponse.message |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get the verification status of a smart contract from Blockscout. |
| 112 | + * This method performs polling of the verification status if it's pending. |
| 113 | + * @link https://docs.blockscout.com/for-users/api/rpc-endpoints/contract#return-status-of-a-verification-attempt |
| 114 | + * @param guid - The verification GUID to check. |
| 115 | + * @returns A promise that resolves to an `BlockscoutResponse` object. |
| 116 | + * @throws {NetworkRequestError} if there is an error on the request. |
| 117 | + * @throws {ContractStatusPollingInvalidStatusCodeError} if the API returns an invalid status code. |
| 118 | + * @throws {ContractStatusPollingResponseNotOkError} if the response status is not OK. |
| 119 | + */ |
| 120 | + public async getVerificationStatus( |
| 121 | + guid: string |
| 122 | + ): Promise<BlockscoutResponse> { |
| 123 | + const etherscanResponse = await this._etherscan.getVerificationStatus(guid); |
| 124 | + |
| 125 | + return new BlockscoutResponse( |
| 126 | + etherscanResponse.status, |
| 127 | + etherscanResponse.message |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Get the Blockscout URL for viewing a contract's details. |
| 133 | + * @param address - The address of the smart contract. |
| 134 | + * @returns The URL to view the contract on Blockscout's website. |
| 135 | + */ |
| 136 | + public getContractUrl(address: string): string { |
| 137 | + return `${this.browserUrl}/address/${address}#code`; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +class BlockscoutResponse implements ValidationResponse { |
| 142 | + public readonly status: number; |
| 143 | + public readonly message: string; |
| 144 | + |
| 145 | + constructor(status: number, message: string) { |
| 146 | + this.status = status; |
| 147 | + this.message = message; |
| 148 | + } |
| 149 | + |
| 150 | + public isPending() { |
| 151 | + return this.message === "Pending in queue"; |
| 152 | + } |
| 153 | + |
| 154 | + public isFailure() { |
| 155 | + return this.message === "Fail - Unable to verify"; |
| 156 | + } |
| 157 | + |
| 158 | + public isSuccess() { |
| 159 | + return this.message === "Pass - Verified"; |
| 160 | + } |
| 161 | + |
| 162 | + public isAlreadyVerified() { |
| 163 | + return ( |
| 164 | + // returned by blockscout |
| 165 | + this.message.startsWith("Smart-contract already verified") || |
| 166 | + // returned by etherscan |
| 167 | + this.message.startsWith("Contract source code already verified") || |
| 168 | + this.message.startsWith("Already Verified") |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + public isOk() { |
| 173 | + return this.status === 1; |
| 174 | + } |
| 175 | +} |
0 commit comments