S4: EtherscanAPI로 Verify & publish 하는법

S4: EtherscanAPI로 Verify & publish 하는법

POST맨으로 인증하기

우선 Remix로 helloworld라는 컨트랙트를 배포하여 ropsten 네트워크에 올렸다.

ropsten 네트워크에 올라간 컨트랙트 주소를 확인한뒤 해당 주소에 etherscan에서 contract 부분에 verify & publish를 할 것이다.

data: {

apikey: $('#apikey').val(), // 이더스캔 본인 API키

module: 'contract', //Do not change

action: 'verifysourcecode', //Do not change

contractaddress: $('#contractaddress').val(), // verify할 컨트랙트 주소

sourceCode: $('#sourceCode').val(), // verify할 컨트랙트의 소스

codeformat: $('#codeformat').val(), // 코드 포맷

contractname: $('#contractname').val(), // 컨트랙트 이름

compilerversion: $('#compilerversion').val(), // 솔리디티 컴파일 버전

optimizationUsed: $('#optimizationUsed').val(), //0 = No Optimization, 1 = Optimization used (applicable when codeformat=solidity-single-file)

runs: 200, //set to 200 as default unless otherwise (applicable when codeformat=solidity-single-file)

constructorArguements: $('#constructorArguements').val(), //if applicable

evmversion: $('#evmVersion').val(), //evm버전 기본값은 default

licenseType:$('#licenseType').val(), //f라이센스 타입

EtherscanApi문서에서는 위와 같은 바디를 전송해줘야 한다고 한다.

post맨으로 body를 보낼때 content-type을 x-www-form-urlencoded로 보내주어야한다.

테스트하면서 이미 Verify했기 때문에 정상적으로 요청했고 이미 Verify됐다고한다.

로컬에서 코드로 작성하여 인증하기

const axios = require('axios'); async function test() { const sourceCode2 =`// SPDX-License-Identifier: MIT pragma solidity 0.8.7; contract helloWorld { function renderHelloWorld () public pure returns (string memory greeting){ greeting = 'Hello World!'; } }` //컨트랙트 내 소스코드를 그대로 붙여넣기 위해 백틱으로 감싸서 변수로 선언하였다 const headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*/*' } //x-www-form-urlencoded 형식으로 바디를 보내기위해 headers에 선언하였다. const params = new URLSearchParams(); params.append('apikey','1EMM8FN84K2627TEGTYW3Q'); params.append('module','contract'); params.append('action','verifysourcecode'); params.append('contractaddress','0x142AE4a1B0A594CBD98525ce0CA4BB0C869ae95b'); params.append('sourceCode',sourceCode2); params.append('codeformat','solidity-single-file'); params.append('contractname','helloWorld'); params.append('compilerversion','v0.8.7+commit.e28d00a7'); params.append('optimizationUsed', 0); params.append('runs', 200); params.append('evmversion', 'default'); params.append('licenseType', 3); //axios바디를 보낼때 json이 아닌 x-www-form-urlencoded 형식을 쓰기위해서는 URLSearchParams를 사용하여 params에 키-값을 담아 보내야한다. await axios.post("https://api-ropsten.etherscan.io/api", // { // apikey: '1EMM8FN84K2627TEGBD1GJNH7G6MZTYW3Q', //A valid API-Key is required // module: 'contract', //Do not change // action: 'verifysourcecode', //Do not change // contractaddress: '0x25405948c08663f7386B1205c06B407398cb0723', //Contract Address starts with 0x... // sourceCode: sourceCode2, //Contract Source Code (Flattened if necessary) // codeformat: 'solidity-single-file', //solidity-single-file (default) or solidity-standard-json-input (for std-input-json-format support // contractname: 'helloWorld', //ContractName (if codeformat=solidity-standard-json-input, then enter contractname as ex: erc20.sol:erc20) // compilerversion: 'v0.8.7+commit.e28d00a7', // see https://ropsten.etherscan.io/solcversions for list of support versions // optimizationUsed: 0, //0 = No Optimization, 1 = Optimization used (applicable when codeformat=solidity-single-file) // runs: 200, //set to 200 as default unless otherwise (applicable when codeformat=solidity-single-file) // evmversion: 'default', //leave blank for compiler default, homestead, tangerineWhistle, spuriousDragon, byzantium, constantinople, petersburg, istanbul (applicable when codeformat=solidity-single-file) // licenseType: 3, //Valid codes 1-12 where 1=No License .. 12=Apache 2.0, see https://ropsten.etherscan.io/contract-license-types // } //해당 방식으로 전송하면 json형식으로 전달되어 apikey를 포함한 데이터들이 전송되지 않는다. params //x-www-form-urlencoded를 위한 바디전송 ,{headers}).then((result)=>{ console.log(result); }).catch((error)=>{ console.log('error!!',error) }) } test();

문제점

axios로 post보낼때 바디를 {key : value} 형태로 넣어 보내니 headers로 'Content-Type': 'application/x-www-form-urlencoded'를 선언하여도 해당 content-type으로 보낼수 없다는것을 알게 되었고

axios 러닝 가이드에 따르면

javascript 객체는 JSON으로 직렬화를 따르므로 x-www-form-urlencoded 를 사용할려면 위 옵션을 써야했다.

해결

from http://mybc.tistory.com/92 by ccl(A) rewrite - 2021-12-09 12:01:39