-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
forge upload-selectors
command (#1676)
* feat: add upload selectors command to forge This commit adds a new command to forge to upload a contract's abi to sig.eth.samczsun.com selector database * fix: review comments - added default for CoreBuildArgs - cleaned up code ordering - moved url to constant * fix: derive CoreBuildArgs::Default
- Loading branch information
Showing
7 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use crate::{ | ||
cmd::forge::build::{CoreBuildArgs, ProjectPathsArgs}, | ||
compile, | ||
opts::forge::CompilerArgs, | ||
}; | ||
use clap::Parser; | ||
use ethers::prelude::artifacts::{output_selection::ContractOutputSelection, LosslessAbi}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
use tracing::trace; | ||
|
||
static SELECTOR_DATABASE_URL: &str = "https://sig.eth.samczsun.com/api/v1/import"; | ||
|
||
#[derive(Debug, Clone, Parser)] | ||
pub struct UploadSelectorsArgs { | ||
#[clap(help = "The name of the contract to upload selectors for.")] | ||
pub contract: String, | ||
|
||
#[clap(flatten, next_help_heading = "PROJECT OPTIONS")] | ||
pub project_paths: ProjectPathsArgs, | ||
} | ||
|
||
impl UploadSelectorsArgs { | ||
/// Builds a contract and uploads the ABI to selector database | ||
pub async fn run(self) -> eyre::Result<()> { | ||
let UploadSelectorsArgs { contract, project_paths } = self; | ||
|
||
let build_args = CoreBuildArgs { | ||
project_paths: project_paths.clone(), | ||
compiler: CompilerArgs { | ||
extra_output: vec![ContractOutputSelection::Abi], | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}; | ||
|
||
trace!("Building project"); | ||
let project = build_args.project()?; | ||
let outcome = compile::suppress_compile(&project)?; | ||
let found_artifact = outcome.find(&contract); | ||
let artifact = found_artifact.ok_or_else(|| { | ||
eyre::eyre!("Could not find artifact `{contract}` in the compiled artifacts") | ||
})?; | ||
|
||
let body = ImportRequest { | ||
import_type: "abi".to_string(), | ||
data: vec![artifact.abi.clone().ok_or(eyre::eyre!("Unable to fetch abi"))?], | ||
}; | ||
|
||
// upload abi to selector database | ||
trace!("Uploading selector args {:?}", body); | ||
let res: ImportResponse = reqwest::Client::new() | ||
.post(SELECTOR_DATABASE_URL) | ||
.json(&body) | ||
.send() | ||
.await? | ||
.json() | ||
.await?; | ||
trace!("Got response: {:?}", res); | ||
res.describe_upload(); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Debug)] | ||
struct ImportRequest { | ||
#[serde(rename = "type")] | ||
import_type: String, | ||
data: Vec<LosslessAbi>, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct ImportTypeData { | ||
imported: HashMap<String, String>, | ||
duplicated: HashMap<String, String>, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct ImportData { | ||
function: ImportTypeData, | ||
event: ImportTypeData, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct ImportResponse { | ||
result: ImportData, | ||
} | ||
|
||
impl ImportResponse { | ||
/// Print info about the functions which were uploaded or already known | ||
pub fn describe_upload(&self) { | ||
self.result | ||
.function | ||
.imported | ||
.iter() | ||
.for_each(|(k, v)| println!("Imported: Function {k}: {v}")); | ||
self.result.event.imported.iter().for_each(|(k, v)| println!("Imported: Event {k}: {v}")); | ||
self.result | ||
.function | ||
.duplicated | ||
.iter() | ||
.for_each(|(k, v)| println!("Duplicated: Function {k}: {v}")); | ||
self.result | ||
.event | ||
.duplicated | ||
.iter() | ||
.for_each(|(k, v)| println!("Duplicated: Event {k}: {v}")); | ||
|
||
println!("Selectors successfully uploaded to https://sig.eth.samczsun.com"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters