2021-10-17 10:01:20 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use ethers_core::abi::{Abi, Address};
|
|
|
|
|
|
|
|
use crate::{Client, Response, Result};
|
|
|
|
|
2021-10-17 10:01:20 +00:00
|
|
|
/// Arguments for verifying contracts
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
|
|
pub struct VerifyContract {
|
|
|
|
pub address: Address,
|
|
|
|
pub source: String,
|
|
|
|
#[serde(rename = "codeformat")]
|
|
|
|
pub code_format: CodeFormat,
|
|
|
|
/// if codeformat=solidity-standard-json-input, then expected as
|
|
|
|
/// `erc20.sol:erc20`
|
|
|
|
#[serde(rename = "contractname", skip_serializing_if = "Option::is_none")]
|
|
|
|
pub contract_name: Option<String>,
|
|
|
|
#[serde(rename = "compilerversion")]
|
|
|
|
pub compiler_version: String,
|
|
|
|
/// applicable when codeformat=solidity-single-file
|
|
|
|
#[serde(rename = "optimizationUsed", skip_serializing_if = "Option::is_none")]
|
|
|
|
optimization_used: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub runs: Option<String>,
|
|
|
|
/// NOTE: there is a typo in the etherscan API `constructorArguements`
|
2021-10-29 12:29:35 +00:00
|
|
|
#[serde(rename = "constructorArguements", skip_serializing_if = "Option::is_none")]
|
2021-10-17 10:01:20 +00:00
|
|
|
pub constructor_arguments: Option<String>,
|
|
|
|
#[serde(rename = "evmversion")]
|
|
|
|
pub evm_version: Option<String>,
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub other: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VerifyContract {
|
|
|
|
pub fn new(address: Address, source: String, compiler_version: String) -> Self {
|
|
|
|
Self {
|
|
|
|
address,
|
|
|
|
source,
|
|
|
|
code_format: Default::default(),
|
|
|
|
contract_name: None,
|
|
|
|
compiler_version,
|
|
|
|
optimization_used: None,
|
|
|
|
runs: None,
|
|
|
|
constructor_arguments: None,
|
|
|
|
evm_version: None,
|
|
|
|
other: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contract_name(mut self, name: impl Into<String>) -> Self {
|
|
|
|
self.contract_name = Some(name.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn runs(mut self, runs: u32) -> Self {
|
|
|
|
self.runs = Some(format!("{}", runs));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn optimization(self, optimization: bool) -> Self {
|
|
|
|
if optimization {
|
|
|
|
self.optimized()
|
|
|
|
} else {
|
|
|
|
self.not_optimized()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn optimized(mut self) -> Self {
|
|
|
|
self.optimization_used = Some("1".to_string());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn not_optimized(mut self) -> Self {
|
|
|
|
self.optimization_used = Some("0".to_string());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn code_format(mut self, code_format: CodeFormat) -> Self {
|
|
|
|
self.code_format = code_format;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn evm_version(mut self, evm_version: impl Into<String>) -> Self {
|
|
|
|
self.evm_version = Some(evm_version.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn constructor_arguments(
|
|
|
|
mut self,
|
|
|
|
constructor_arguments: Option<impl Into<String>>,
|
|
|
|
) -> Self {
|
|
|
|
self.constructor_arguments = constructor_arguments.map(|s| {
|
|
|
|
s.into()
|
|
|
|
// TODO is this correct?
|
|
|
|
.trim_start_matches("0x")
|
|
|
|
.to_string()
|
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
|
|
|
pub enum CodeFormat {
|
|
|
|
#[serde(rename = "solidity-single-file")]
|
|
|
|
SingleFile,
|
|
|
|
#[serde(rename = "solidity-standard-json-input")]
|
|
|
|
StandardJsonInput,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for CodeFormat {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
CodeFormat::SingleFile => "solidity-single-file",
|
|
|
|
CodeFormat::StandardJsonInput => "solidity-standard-json-input",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CodeFormat {
|
|
|
|
fn default() -> Self {
|
|
|
|
CodeFormat::SingleFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct ContractMetadata {
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub items: Vec<Metadata>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoIterator for ContractMetadata {
|
|
|
|
type Item = Metadata;
|
|
|
|
type IntoIter = std::vec::IntoIter<Metadata>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.items.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ContractMetadata {
|
|
|
|
/// All ABI from all contracts in the source file
|
|
|
|
pub fn abis(&self) -> Result<Vec<Abi>> {
|
|
|
|
let mut abis = Vec::with_capacity(self.items.len());
|
|
|
|
for item in &self.items {
|
|
|
|
abis.push(serde_json::from_str(&item.abi)?);
|
|
|
|
}
|
|
|
|
Ok(abis)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combined source code of all contracts
|
|
|
|
pub fn source_code(&self) -> String {
|
2021-10-29 12:29:35 +00:00
|
|
|
self.items.iter().map(|c| c.source_code.as_str()).collect::<Vec<_>>().join("\n")
|
2021-10-17 10:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Etherscan contract metadata
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Metadata {
|
|
|
|
#[serde(rename = "SourceCode")]
|
|
|
|
pub source_code: String,
|
|
|
|
#[serde(rename = "ABI")]
|
|
|
|
pub abi: String,
|
|
|
|
#[serde(rename = "ContractName")]
|
|
|
|
pub contract_name: String,
|
|
|
|
#[serde(rename = "CompilerVersion")]
|
|
|
|
pub compiler_version: String,
|
|
|
|
#[serde(rename = "OptimizationUsed")]
|
|
|
|
pub optimization_used: String,
|
|
|
|
#[serde(rename = "Runs")]
|
|
|
|
pub runs: String,
|
|
|
|
#[serde(rename = "ConstructorArguments")]
|
|
|
|
pub constructor_arguments: String,
|
|
|
|
#[serde(rename = "EVMVersion")]
|
|
|
|
pub evm_version: String,
|
|
|
|
#[serde(rename = "Library")]
|
|
|
|
pub library: String,
|
|
|
|
#[serde(rename = "LicenseType")]
|
|
|
|
pub license_type: String,
|
|
|
|
#[serde(rename = "Proxy")]
|
|
|
|
pub proxy: String,
|
|
|
|
#[serde(rename = "Implementation")]
|
|
|
|
pub implementation: String,
|
|
|
|
#[serde(rename = "SwarmSource")]
|
|
|
|
pub swarm_source: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
|
|
|
/// Submit Source Code for Verification
|
|
|
|
pub async fn submit_contract_verification(
|
|
|
|
&self,
|
|
|
|
contract: &VerifyContract,
|
|
|
|
) -> Result<Response<String>> {
|
|
|
|
let body = self.create_query("contract", "verifysourcecode", contract);
|
|
|
|
Ok(self.post_form(&body).await?)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check Source Code Verification Status with receipt received from
|
|
|
|
/// `[Self::submit_contract_verification]`
|
|
|
|
pub async fn check_contract_verification_status(
|
|
|
|
&self,
|
|
|
|
guid: impl AsRef<str>,
|
|
|
|
) -> Result<Response<String>> {
|
2021-10-24 18:41:50 +00:00
|
|
|
let body = self.create_query(
|
|
|
|
"contract",
|
|
|
|
"checkverifystatus",
|
|
|
|
HashMap::from([("guid", guid.as_ref())]),
|
|
|
|
);
|
2021-10-17 10:01:20 +00:00
|
|
|
Ok(self.post_form(&body).await?)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the contract ABI of a verified contract
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2021-10-24 18:41:50 +00:00
|
|
|
/// # use ethers_etherscan::Client;
|
|
|
|
/// # use ethers_core::types::Chain;
|
2021-10-17 10:01:20 +00:00
|
|
|
///
|
|
|
|
/// # #[tokio::main]
|
|
|
|
/// # async fn main() {
|
2021-10-24 18:41:50 +00:00
|
|
|
/// let client = Client::new(Chain::Mainnet, "API_KEY").unwrap();
|
2021-10-17 10:01:20 +00:00
|
|
|
/// let abi = client
|
|
|
|
/// .contract_abi("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".parse().unwrap())
|
|
|
|
/// .await.unwrap();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub async fn contract_abi(&self, address: Address) -> Result<Abi> {
|
2021-10-24 18:41:50 +00:00
|
|
|
let query = self.create_query("contract", "getabi", HashMap::from([("address", address)]));
|
2021-10-17 10:01:20 +00:00
|
|
|
let resp: Response<String> = self.get_json(&query).await?;
|
|
|
|
Ok(serde_json::from_str(&resp.result)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get Contract Source Code for Verified Contract Source Codes
|
|
|
|
/// ```no_run
|
2021-10-24 18:41:50 +00:00
|
|
|
/// # use ethers_etherscan::Client;
|
|
|
|
/// # use ethers_core::types::Chain;
|
2021-10-17 10:01:20 +00:00
|
|
|
///
|
|
|
|
/// # #[tokio::main]
|
|
|
|
/// # async fn main() {
|
2021-10-24 18:41:50 +00:00
|
|
|
/// let client = Client::new(Chain::Mainnet, "API_KEY").unwrap();
|
2021-10-17 10:01:20 +00:00
|
|
|
/// let meta = client
|
|
|
|
/// .contract_source_code("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".parse().unwrap())
|
|
|
|
/// .await.unwrap();
|
|
|
|
/// let code = meta.source_code();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub async fn contract_source_code(&self, address: Address) -> Result<ContractMetadata> {
|
2021-10-29 12:29:35 +00:00
|
|
|
let query =
|
|
|
|
self.create_query("contract", "getsourcecode", HashMap::from([("address", address)]));
|
2021-10-17 10:01:20 +00:00
|
|
|
let response: Response<Vec<Metadata>> = self.get_json(&query).await?;
|
2021-10-29 12:29:35 +00:00
|
|
|
Ok(ContractMetadata { items: response.result })
|
2021-10-17 10:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-11-27 07:54:20 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use serial_test::serial;
|
|
|
|
|
2021-10-24 18:41:50 +00:00
|
|
|
use ethers_core::types::Chain;
|
2021-10-17 10:01:20 +00:00
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
use crate::{contract::VerifyContract, tests::run_at_least_duration, Client};
|
|
|
|
|
2021-10-17 10:01:20 +00:00
|
|
|
#[tokio::test]
|
2021-11-27 07:54:20 +00:00
|
|
|
#[serial]
|
2021-10-17 10:01:20 +00:00
|
|
|
#[ignore]
|
|
|
|
async fn can_fetch_contract_abi() {
|
2021-11-27 07:54:20 +00:00
|
|
|
run_at_least_duration(Duration::from_millis(250), async {
|
|
|
|
let client = Client::new_from_env(Chain::Mainnet).unwrap();
|
|
|
|
|
|
|
|
let _abi = client
|
|
|
|
.contract_abi("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".parse().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
})
|
|
|
|
.await;
|
2021-10-17 10:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2021-11-27 07:54:20 +00:00
|
|
|
#[serial]
|
2021-10-17 10:01:20 +00:00
|
|
|
#[ignore]
|
|
|
|
async fn can_fetch_contract_source_code() {
|
2021-11-27 07:54:20 +00:00
|
|
|
run_at_least_duration(Duration::from_millis(250), async {
|
|
|
|
let client = Client::new_from_env(Chain::Mainnet).unwrap();
|
|
|
|
|
|
|
|
let _meta = client
|
|
|
|
.contract_source_code("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".parse().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
})
|
|
|
|
.await
|
2021-10-17 10:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2021-11-27 07:54:20 +00:00
|
|
|
#[serial]
|
2021-10-17 10:01:20 +00:00
|
|
|
#[ignore]
|
|
|
|
async fn can_verify_contract() {
|
2021-11-27 07:54:20 +00:00
|
|
|
run_at_least_duration(Duration::from_millis(250), async {
|
|
|
|
// TODO this needs further investigation
|
2021-10-17 10:01:20 +00:00
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
// https://etherscan.io/address/0x9e744c9115b74834c0f33f4097f40c02a9ac5c33#code
|
|
|
|
let contract = include_str!("../resources/UniswapExchange.sol");
|
|
|
|
let address = "0x9e744c9115b74834c0f33f4097f40c02a9ac5c33".parse().unwrap();
|
|
|
|
let compiler_version = "v0.5.17+commit.d19bba13";
|
|
|
|
let constructor_args = "0x000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000005f5e1000000000000000000000000000000000000000000000000000000000000000007596179537761700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035941590000000000000000000000000000000000000000000000000000000000";
|
2021-10-17 10:01:20 +00:00
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
let client = Client::new_from_env(Chain::Mainnet).unwrap();
|
2021-10-17 10:01:20 +00:00
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
let contract =
|
|
|
|
VerifyContract::new(address, contract.to_string(), compiler_version.to_string())
|
|
|
|
.constructor_arguments(Some(constructor_args))
|
|
|
|
.optimization(true)
|
|
|
|
.runs(200);
|
2021-10-17 10:01:20 +00:00
|
|
|
|
2021-11-27 07:54:20 +00:00
|
|
|
let _resp = client.submit_contract_verification(&contract).await;
|
|
|
|
}).await
|
2021-10-17 10:01:20 +00:00
|
|
|
}
|
|
|
|
}
|