Skip to content

Commit

Permalink
broomhilda
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Apr 30, 2024
1 parent 19e37be commit 3eb88d9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 160 deletions.
77 changes: 0 additions & 77 deletions input.json

This file was deleted.

42 changes: 0 additions & 42 deletions out.md

This file was deleted.

11 changes: 11 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
reorder_imports = true
imports_granularity = "Crate"
use_small_heuristics = "Max"
comment_width = 100
wrap_comments = true
binop_separator = "Back"
trailing_comma = "Vertical"
trailing_semicolon = false
use_field_init_shorthand = true
format_code_in_doc_comments = true
doc_comment_code_block_width = 100
56 changes: 24 additions & 32 deletions src/generate.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
//! This module contains CLI prompts for generating a [MultisigBatch] definition from user input, with type safety.
//! This module contains CLI prompts for generating a [MultisigBatch] definition from user input,
//! with type safety.
use crate::types::{BatchTransaction, MultisigBatch, ObjectMetadata};
use crate::util::encode_function_args;
use crate::{
types::{BatchTransaction, MultisigBatch, ObjectMetadata},
util::encode_function_args,
};
use alloy_json_abi::Function;
use alloy_primitives::{hex::FromHex, Address, U256};
use anyhow::Result;
use inquire::Text;
use std::path::PathBuf;
use std::{fs::File, io::Write};
use std::{fs::File, io::Write, path::Path};
use yansi::Paint;

pub(crate) fn generate_batch_definition(output_path: &PathBuf) -> Result<()> {
let num_transactions = u64::from_str_radix(
&Text::new("Number of transactions in the multisig batch:").prompt()?,
10,
)?;
pub(crate) fn generate_batch_definition(output_path: &Path) -> Result<()> {
let num_transactions =
(Text::new("Number of transactions in the multisig batch:").prompt()?).parse::<u64>()?;

let mut batch_definition = MultisigBatch {
chain_id: u64::from_str_radix(
&Text::new("Chain ID that the batch transaction will be performed on:").prompt()?,
10,
)?,
chain_id: (Text::new("Chain ID that the batch transaction will be performed on:")
.prompt()?)
.parse::<u64>()?,
metadata: ObjectMetadata {
name: Text::new("Enter the name of the batch:").prompt()?,
description: Text::new("Enter the description of the batch:").prompt()?,
Expand All @@ -35,7 +34,7 @@ pub(crate) fn generate_batch_definition(output_path: &PathBuf) -> Result<()> {
Ok::<_, anyhow::Error>(())
})?;

let mut output = File::create("input.json")?;
let mut output = File::create(output_path)?;
output.write_all(serde_json::to_string_pretty(&batch_definition)?.as_bytes())?;

println!("Batch definition saved to {}", output_path.display().cyan());
Expand All @@ -51,22 +50,18 @@ fn prompt_batch_transaction(i: u64) -> Result<BatchTransaction> {
description: Text::new("Description:").prompt()?,
};

let to = Address::from_hex(&Text::new("Address of the contract to call:").prompt()?)?;
let to = Address::from_hex(Text::new("Address of the contract to call:").prompt()?)?;
let value = U256::from_str_radix(&Text::new("Value to send (in WEI):").prompt()?, 10)?;

let contract_signature = Text::new("Enter the function signature of the contract to call")
let contract_signature = Text::new("Enter the function signature of the contract to call:")
.with_help_message("Example: `deposit(uint256 amount)(bytes32 depositHash)`")
.prompt()?;
let mut function = Function::parse(&contract_signature)?;
function
.inputs
.iter_mut()
.enumerate()
.for_each(|(i, input)| {
input.name = (input.name.is_empty())
.then(|| format!("unnamed_param{}", i))
.unwrap_or_else(|| input.name.clone())
});
function.inputs.iter_mut().enumerate().for_each(|(i, input)| {
input.name = (input.name.is_empty())
.then(|| format!("unnamed_param{}", i))
.unwrap_or_else(|| input.name.clone())
});

let inputs = prompt_raw_function_inputs(&function)?;
let input_map = function
Expand All @@ -93,12 +88,9 @@ fn prompt_raw_function_inputs(function: &Function) -> Result<Vec<String>> {
.iter()
.enumerate()
.map(|(i, input)| {
let input_value = Text::new(&format!(
"Enter the value for input #{} ({}):",
i + 1,
input.name
))
.prompt()?;
let input_value =
Text::new(&format!("Enter the value for input #{} ({}):", i + 1, input.name))
.prompt()?;

Ok(input_value)
})
Expand Down
15 changes: 7 additions & 8 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::types::{BatchTransaction, MultisigBatch};
use anyhow::Result;
use std::{fs::File, io::Write, path::PathBuf};
use yansi::Paint;

/// Renders a Markdown document from a [MultisigBatch] definition.
pub fn render_batch_doc(input: &PathBuf, output: &PathBuf) -> Result<()> {
Expand All @@ -20,6 +21,8 @@ pub fn render_batch_doc(input: &PathBuf, output: &PathBuf) -> Result<()> {
// Write the document to the output file
File::create(output)?.write_all(document.as_bytes())?;

println!("Document rendered to {}", output.display().green());

Ok(())
}

Expand All @@ -43,29 +46,25 @@ fn append_header(writer: &mut String, batch: &MultisigBatch) {
/// Appends a [BatchTransaction] at index `i` to the writer.
fn append_transaction(writer: &mut String, i: usize, tx: &BatchTransaction) {
// Newline
writer.push_str("\n");
writer.push('\n');

// Transaction Header
writer.push_str(format!("## Tx #{}: {}\n", i, tx.metadata.name).as_ref());
writer.push_str(format!("{}\n", tx.metadata.description).as_ref());

// Newline
writer.push_str("\n");
writer.push('\n');

// Transaction Details
writer.push_str(
format!(
"**Function Signature:** `{}`\n\n",
tx.contract_method.signature()
)
.as_ref(),
format!("**Function Signature:** `{}`\n\n", tx.contract_method.signature()).as_ref(),
);
writer.push_str(format!("**To:** `{}`\n\n", tx.to).as_ref());
writer.push_str(format!("**Value:** `{} WEI`\n\n", tx.value).as_ref());
writer.push_str(format!("**Raw Input Data:** `{}`\n", tx.data).as_ref());

// Newline
writer.push_str("\n");
writer.push('\n');

// Transaction Inputs
writer.push_str("### Inputs\n");
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! This module contains the definiteions for the upgrade JSON schema.
use std::collections::HashMap;
use alloy_json_abi::Function;
use alloy_primitives::{Address, Bytes, U256};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Describes the definition of a multisig batch.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
Expand Down

0 comments on commit 3eb88d9

Please sign in to comment.