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
Show file tree
Hide file tree
Changes from 21 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
42 changes: 22 additions & 20 deletions src/dapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,30 +234,31 @@ To deploy a contract, you can use `dapp create`:
dapp create Dapptutorial [<constructorArgs>] [<options>]
```

The `--verify` flag verifies the contract on etherscan (requires `ETHERSCAN_API_KEY`).
The `--verify` and `--verify-multiple` flags verify the contract/s on etherscan (requires `ETHERSCAN_API_KEY`).

## Configuration

The commands of `dapp` can be customized with environment variables or flags.
These variables can be set at the prompt or in a `.dapprc` file.

| Variable | Default | Synopsis |
|----------------------------|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|
| `DAPP_SRC` | `src` | Project Solidity source directory |
| `DAPP_LIB` | `lib` | Directory for installed Dapp packages |
| `DAPP_OUT` | `out` | Directory for compilation artifacts |
| `DAPP_ROOT` | `.` | Root directory of compilation |
| `DAPP_SOLC_VERSION` | n/a | Solidity compiler version to use |
| `DAPP_SOLC` | n/a | solc binary to use |
| `DAPP_VERBOSE` | n/a | Produce more `dapp test` output |
| `DAPP_LIBRARIES` | automatically deployed | Library addresses to link to |
| `DAPP_SKIP_BUILD` | n/a | Avoid compiling this time |
| `DAPP_LINK_TEST_LIBRARIES` | `1` when testing; else `0` | Compile with libraries |
| `DAPP_VERIFY_CONTRACT` | `yes` | Attempt Etherscan verification |
| `DAPP_STANDARD_JSON` | $(dapp mk-standard-json) | [Solidity compilation options](https://docs.soliditylang.org/en/latest/using-the-compiler.html#compiler-input-and-output-json-description) |
| `DAPP_REMAPPINGS` | $(dapp remappings) | [Solidity remappings](https://docs.soliditylang.org/en/latest/using-the-compiler.html#path-remapping) |
| `DAPP_BUILD_OPTIMIZE` | no | Activate Solidity optimizer |
| `DAPP_BUILD_OPTIMIZE_RUNS` | 200 | Set the optimizer runs |
| Variable | Default | Synopsis |
|---------------------------------|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
| `DAPP_SRC` | `src` | Project Solidity source directory |
| `DAPP_LIB` | `lib` | Directory for installed Dapp packages |
| `DAPP_OUT` | `out` | Directory for compilation artifacts |
| `DAPP_ROOT` | `.` | Root directory of compilation |
| `DAPP_SOLC_VERSION` | n/a | Solidity compiler version to use |
| `DAPP_SOLC` | n/a | solc binary to use |
| `DAPP_VERBOSE` | n/a | Produce more `dapp test` output |
| `DAPP_LIBRARIES` | automatically deployed | Library addresses to link to |
| `DAPP_SKIP_BUILD` | n/a | Avoid compiling this time |
| `DAPP_LINK_TEST_LIBRARIES` | `1` when testing; else `0` | Compile with libraries |
| `DAPP_VERIFY_CONTRACT` | `yes` | Attempt Etherscan verification |
| `DAPP_VERIFY_CONTRACT_MULTIPLE` | `yes` | Attempt Etherscan multifile verification |
| `DAPP_STANDARD_JSON` | $(dapp mk-standard-json) | [Solidity compilation options](https://docs.soliditylang.org/en/latest/using-the-compiler.html#compiler-input-and-output-json-description) |
| `DAPP_REMAPPINGS` | $(dapp remappings) | [Solidity remappings](https://docs.soliditylang.org/en/latest/using-the-compiler.html#path-remapping) |
| `DAPP_BUILD_OPTIMIZE` | no | Activate Solidity optimizer |
| `DAPP_BUILD_OPTIMIZE_RUNS` | 200 | Set the optimizer runs |

A global (always loaded) config file is located in `~/.dapprc`.
A local `.dapprc` can also be defined in your project's root, which overrides variables in the global config.
Expand Down Expand Up @@ -421,7 +422,7 @@ for key bindings for navigation.
dapp-create -- deploy a compiled contract (--verify on Etherscan)
Usage: dapp create <contractname> or
dapp create <path>:<contractname>
Add --verify and export your ETHERSCAN_API_KEY to auto-verify on Etherscan
Add --verify or --verify-multiple and export your ETHERSCAN_API_KEY to auto-verify on Etherscan


### `dapp address`
Expand Down Expand Up @@ -468,7 +469,7 @@ Requires `ETHERSCAN_API_KEY` to be set.

`seth chain` will be used to determine on which network the contract is to be verified.

Automatically run when the `--verify` flag is passed to `dapp create`.
Automatically run when the `--verify` or `--verify-multiple` flag is passed to `dapp create`.

### `dapp mk-standard-json`

Expand All @@ -481,3 +482,4 @@ The following environment variables can be used to override settings:
- `DAPP_BUILD_OPTIMIZE`
- `DAPP_BUILD_OPTIMIZE_RUNS`
- `DAPP_LIBRARIES`
- `DAPP_VERIFY_CONTRACT_MULTIPLE`
3 changes: 3 additions & 0 deletions src/dapp/libexec/dapp/dapp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
###
### Deployment options:
### verify verify contract on etherscan
### verify-multiple multifile verification on etherscan
###
### Contract verifying options:
### async don't wait for confirmation
Expand Down Expand Up @@ -81,6 +82,7 @@ smtdebug print the SMT queries produced by hevm

Deployment options:
verify verify contract on etherscan
verify-multiple multifile verification on etherscan

Contract verifying options:
async don't wait for confirmation
Expand Down Expand Up @@ -161,6 +163,7 @@ while [[ $1 ]]; do
export HEVM_RPC=yes;;

--verify) export DAPP_VERIFY_CONTRACT=yes;;
--verify-multiple) export DAPP_VERIFY_CONTRACT_MULTIPLE=yes;;

--async) export DAPP_ASYNC=yes;;

Expand Down
2 changes: 1 addition & 1 deletion src/dapp/libexec/dapp/dapp-create
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bin=$(jq .evm.bytecode.object -r <<< "$contract")
type=$(seth --abi-constructor <<< "$abi")
address=$(set -x; seth send --create "$bin" "${type/constructor/${1#*:}}" "${@:2}")

[[ $DAPP_VERIFY_CONTRACT ]] && {
[[ $DAPP_VERIFY_CONTRACT || $DAPP_VERIFY_CONTRACT_MULTIPLE ]] && {
echo >&2 "Verifying contract at $address"
sleep 5 # give etherscan some time to process the block
dapp verify-contract "$path":"${1#*:}" "$address" "${@:2}"
Expand Down
16 changes: 13 additions & 3 deletions src/dapp/libexec/dapp/dapp-mk-standard-json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ for root, dirnames, filenames in os.walk(src):
files.append(os.path.join(root, filename))

tmpljson["sources"]={}
for f in files:
tmpljson["sources"][f]={}
tmpljson["sources"][f]["urls"] = [f]

if os.getenv('DAPP_VERIFY_CONTRACT_MULTIPLE') == "yes":
for f in files:
if f.endswith(".t.sol"):
continue
else:
tmpljson["sources"][f]={}
content = open(f, "r");
tmpljson["sources"][f]["content"] = [content.read()]
naszam marked this conversation as resolved.
Show resolved Hide resolved
else:
for f in files:
tmpljson["sources"][f]={}
tmpljson["sources"][f]["urls"] = [f]

if os.getenv('DAPP_REMAPPINGS') is None or os.getenv('DAPP_REMAPPINGS') == "":
tmpljson["settings"].pop("remappings", None)
Expand Down
57 changes: 39 additions & 18 deletions src/dapp/libexec/dapp/dapp-verify-contract
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ 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,12 +33,13 @@ 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)
Expand Down Expand Up @@ -88,33 +86,56 @@ else
optimized=0
fi

params=(
if [[ $DAPP_VERIFY_CONTRACT_MULTIPLE == yes ]]; then

# Standard Input JSON

inputJSON=$(dapp mk-standard-json)

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"
)
)

else

source=$(hevm flatten --source-file "$file" --json-file "$DAPP_JSON" --dapp-root "$DAPP_ROOT")
params=(
"module=contract" "action=verifysourcecode"
"contractname=$name" "contractaddress=$address"
"optimizationUsed=$optimized" "runs=$runs"
"apikey=$ETHERSCAN_API_KEY"
)

source=$(cat <<.
// Verified using https://dapp.tools
source=$(hevm flatten --source-file "$file" --json-file "$DAPP_JSON" --dapp-root "$DAPP_ROOT")

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

fi

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

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
if [[ $DAPP_VERIFY_CONTRACT_MULTIPLE == yes ]]; then
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
else
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
fi

status=$(jshon <<<"$response" -e status -u)
guid=$(jshon <<<"$response" -e result -u)
Expand Down