dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
88 lines • 3.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EtherscanVerifier = void 0;
const axios_1 = __importDefault(require("axios"));
const logger_1 = require("../logsAndMetrics/core/logger");
const ethers_1 = require("ethers");
class EtherscanVerifier {
constructor(apiKey) {
this.baseUrls = {
1: 'https://api.etherscan.io/api',
3: 'https://api-ropsten.etherscan.io/api',
4: 'https://api-rinkeby.etherscan.io/api',
5: 'https://api-goerli.etherscan.io/api',
42: 'https://api-kovan.etherscan.io/api',
137: 'https://api.polygonscan.com/api',
80001: 'https://api-testnet.polygonscan.com/api',
// Add more networks as needed
};
this.apiKey = apiKey;
}
async verify(input) {
const baseUrl = this.baseUrls[input.network.chainId];
if (!baseUrl) {
throw new Error(`Unsupported network: ${input.network.chainId}`);
}
try {
const response = await axios_1.default.post(baseUrl, {
apikey: this.apiKey,
module: 'contract',
action: 'verifysourcecode',
contractaddress: input.address,
sourceCode: input.sourceCode,
contractname: input.contractName,
compilerversion: `v${input.compiler.version}`,
optimizationUsed: input.compiler.optimization ? '1' : '0',
runs: input.compiler.runs,
constructorArguements: this.encodeConstructorArgs(input.constructorArgs),
});
if (response.data.status !== '1') {
throw new Error(`Verification submission failed: ${response.data.message}`);
}
return response.data.result; // GUID for checking status
}
catch (error) {
logger_1.Logger.error('Etherscan verification submission failed:', error);
throw error;
}
}
async checkStatus(verificationId) {
try {
const response = await axios_1.default.get(this.baseUrls[1], {
params: {
apikey: this.apiKey,
module: 'contract',
action: 'checkverifystatus',
guid: verificationId,
},
});
if (response.data.result === 'Pending') {
return 'pending';
}
return response.data.status === '1' ? 'success' : 'failed';
}
catch (error) {
logger_1.Logger.error('Failed to check verification status:', error);
throw error;
}
}
getSupportedNetworks() {
return Object.keys(this.baseUrls).map(Number);
}
encodeConstructorArgs(args) {
// Remove 0x prefix and concatenate
return args
.map(arg => {
if (typeof arg === 'string' && arg.startsWith('0x')) {
return arg.slice(2);
}
return ethers_1.ethers.utils.defaultAbiCoder.encode(['string'], [arg]).slice(2);
})
.join('');
}
}
exports.EtherscanVerifier = EtherscanVerifier;
//# sourceMappingURL=etherscan.js.map