Skip to content

Instantly share code, notes, and snippets.

@valentinbud
Created January 28, 2022 14:57
Show Gist options
  • Save valentinbud/f71050521e60480f5baa4fb5ad7933a2 to your computer and use it in GitHub Desktop.
Save valentinbud/f71050521e60480f5baa4fb5ad7933a2 to your computer and use it in GitHub Desktop.
import os
import psycopg2
import requests
# endpoints
database_url = os.getenv("DATABASE_URL", "postgresql://blockscout@localhost/emerald")
destination_api_url = os.getenv("DESTINATION_API_URL", "http://localhost:4000/api")
# query
sql_query = """
SELECT
id,
name,
compiler_version,
optimization,
contract_source_code,
abi,
encode(address_hash::bytea, 'hex'),
inserted_at,
updated_at,
constructor_arguments,
optimization_runs,
evm_version,
external_libraries,
verified_via_sourcify,
is_vyper_contract,
partially_verified,
file_path,
is_changed_bytecode,
bytecode_checked_at
FROM
smart_contracts
WHERE
id > 19
ORDER BY id
"""
# connect to the db
conn = psycopg2.connect(database_url)
cur = conn.cursor()
cur.execute(sql_query)
row = cur.fetchone()
# process entries
while row is not None:
# extract params
id = row[0]
addressHash = row[6]
name = row[1]
compilerVersion = row[2]
optimization = str(row[3]).lower()
contractSourceCode = row[4]
constructorArguments = row[9]
evmVersion = row[11]
optimizationRuns = row[10]
externalLibraries = row[12]
# construct payload
pload = {
"module": "contract",
"action": "verify",
"addressHash": "0x{}".format(addressHash),
"name": name,
"compilerVersion": compilerVersion,
"optimization": optimization,
"contractSourceCode": contractSourceCode,
"constructorArguments": constructorArguments,
"evmVersion": evmVersion,
"optimizationRuns": optimizationRuns
}
# construct external libraries
if 0 < len(externalLibraries) <= 5:
for index in range(len(externalLibraries)):
libraries = {
"library{}Name".format(index+1): externalLibraries[index]["name"],
"library{}Address".format(index+1): externalLibraries[index]["address_hash"]
}
pload.update(libraries)
# post to api
r = requests.post(destination_api_url, data=pload)
print("{}: {}".format(id, r.json()['message']))
# fetch next row
row = cur.fetchone()
# close db connection
cur.close()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment