Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Multi-File Etherscan Verification Option #630

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 78 additions & 28 deletions src/dapp/libexec/dapp/dapp-verify-contract
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#!/usr/bin/env bash
### dapp-verify-contract -- verify contract souce on etherscan
### Usage: dapp verify-contract <path>:<contractname> <address> [constructorArgs]
### Option: --multiple for standard-json multi-file verification

set -e

[[ $ETHERSCAN_API_KEY ]] || {
cat >&2 <<.

You need an Etherscan Api Key to verify contracts.
Create one at https://etherscan.io/myapikey

Then export it with \`export ETHERSCAN_API_KEY=xxxxxxxx'

.
exit 1
}
Expand All @@ -36,16 +34,22 @@ esac

path=${1?contractname}
name=${path#*:}
contractName=$(basename "$path")
address=${2?contractaddress}

# combined-json has a sourceList field
if [[ $(jq .sourceList "$DAPP_JSON") == null ]]; then
contract=$(<"$DAPP_JSON" jq -r ".contracts[\"${path/:*/}\"][\"$name\"]")
else
else
contract=$(<"$DAPP_JSON" jq -r ".contracts[\"$path\"]")
fi
meta=$(jshon <<<"$contract" -e metadata -u)
version=$(jshon <<<"$meta" -e compiler -e version -u)
sources=$(jshon <<<"$meta" -e sources)
language=$(jshon <<<"$meta" -e language)
remappings=$(jshon <<<"$meta" -e settings -e remappings)
optimizer=$(jshon <<<"$meta" -e settings -e optimizer)
evmVersion=$(jshon <<<"meta" -e settings -e evmVersion)
file=$(jshon <<<"$meta" -e settings -e compilationTarget -k)
optimized=$(jshon <<<"$meta" -e settings -e optimizer -e enabled -u)
runs=$(jshon <<<"$meta" -e settings -e optimizer -e runs -u)
Expand Down Expand Up @@ -88,41 +92,87 @@ else
optimized=0
fi

params=(
if [[ $1 = "--multiple" ]]; then
d-xo marked this conversation as resolved.
Show resolved Hide resolved

# Standard Input JSON

inputJSON=$(cat <<-EOF
{
"language": "$language",
"sources": "$sources",
"settings": {
"remappings": "$remappings",
"optimizer": "$optimizer",
"evmVersion": "$evmVersion"
}
}
EOF
)

params=(
"module=contract" "action=verifysourcecode"
"contractname=$name" "contractaddress=$address"
"optimizationUsed=$optimized" "runs=$runs"
"contractname=$contractName" "contractaddress=$address"
"sourcecode=$inputJSON" "codeformat=solidity-standard-json-input"
"apikey=$ETHERSCAN_API_KEY"
)
)

query=$(printf "&%s" "${params[@]}")

source=$(hevm flatten --source-file "$file" --json-file "$DAPP_JSON" --dapp-root "$DAPP_ROOT")
count=0
while [ $count -lt 5 ]; do
sleep 10

source=$(cat <<.
// Verified using https://dapp.tools
response=$(curl -fsS "$ETHERSCAN_API_URL" -d "$query" \
--data-urlencode "compilerversion=$version" \
--data-urlencode "constructorArguements=$constructorArguments" -X POST)
# NOTE: the Arguements typo is in etherscan's API

$source
status=$(jshon <<<"$response" -e status -u)
guid=$(jshon <<<"$response" -e result -u)
message=$(jshon <<<"$response" -e message -u)
count=$((count + 1))

[[ $status = 1 ]] && break;
done
d-xo marked this conversation as resolved.
Show resolved Hide resolved

else

params=(
"module=contract" "action=verifysourcecode"
"contractname=$name" "contractaddress=$address"
"optimizationUsed=$optimized" "runs=$runs"
"apikey=$ETHERSCAN_API_KEY"
)

source=$(hevm flatten --source-file "$file" --json-file "$DAPP_JSON" --dapp-root "$DAPP_ROOT")

source=$(cat <<.
//Verified using https://dapp.tools
"$source"
.
)
)

query=$(printf "&%s" "${params[@]}")
query=$(printf "&%s" "${params[@]}")

count=0
while [ $count -lt 5 ]; do
sleep 10
count=0
while [ $count -lt 5 ]; do
sleep 10

response=$(curl -fsS "$ETHERSCAN_API_URL" -d "$query" \
--data-urlencode "compilerversion=$version" \
--data-urlencode "sourceCode@"<(echo "$source") \
--data-urlencode "constructorArguements=$constructorArguments" -X POST)
# NOTE: the Arguements typo is in etherscan's API
response=$(curl -fsS "$ETHERSCAN_API_URL" -d "$query" \
--data-urlencode "compilerversion=$version" \
--data-urlencode "sourceCode@"<(echo "$source") \
--data-urlencode "constructorArguements=$constructorArguments" -X POST)
# NOTE: the Arguements typo is in etherscan's API

status=$(jshon <<<"$response" -e status -u)
guid=$(jshon <<<"$response" -e result -u)
message=$(jshon <<<"$response" -e message -u)
count=$((count + 1))
status=$(jshon <<<"$response" -e status -u)
guid=$(jshon <<<"$response" -e result -u)
message=$(jshon <<<"$response" -e message -u)
count=$((count + 1))

[[ $status = 1 ]] && break;
done
[[ $status = 1 ]] && break;
done

fi

[[ $status = 0 && $message = "Contract source code already verified" ]] && {
echo >&2 "Contract source code already verified."
Expand Down