|
| 1 | +use super::command::CommandResponse; |
1 | 2 | use crate::helpers::block_explorer::LinkProvider;
|
| 3 | +use crate::response::cast_message::SncastMessage; |
| 4 | +use crate::response::declare::DeclareTransactionResponse; |
| 5 | +use crate::response::explorer_link::OutputLink; |
2 | 6 | use conversions::string::IntoPaddedHexStr;
|
3 | 7 | use conversions::{padded_felt::PaddedFelt, serde::serialize::CairoSerialize};
|
4 | 8 | use foundry_ui::Message;
|
5 | 9 | use foundry_ui::styling;
|
6 | 10 | use indoc::formatdoc;
|
7 | 11 | use serde::{Deserialize, Serialize};
|
8 |
| -use serde_json::Value; |
9 |
| -use serde_json::json; |
| 12 | +use serde_json::{Value, json}; |
10 | 13 |
|
11 |
| -use super::{command::CommandResponse, explorer_link::OutputLink}; |
12 |
| -use crate::response::cast_message::SncastMessage; |
| 14 | +#[derive(Clone, Serialize, Deserialize, CairoSerialize, Debug, PartialEq)] |
| 15 | +#[serde(untagged)] |
| 16 | +pub enum DeployResponseKind { |
| 17 | + Deploy(DeployResponse), |
| 18 | + DeployWithDeclare(DeployResponseWithDeclare), |
| 19 | +} |
| 20 | + |
| 21 | +impl CommandResponse for DeployResponseKind {} |
| 22 | + |
| 23 | +impl Message for SncastMessage<DeployResponseKind> { |
| 24 | + fn text(&self) -> String { |
| 25 | + match &self.command_response { |
| 26 | + DeployResponseKind::Deploy(response) => response.text(), |
| 27 | + DeployResponseKind::DeployWithDeclare(response) => response.text(), |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fn json(&self) -> Value { |
| 32 | + serde_json::to_value(&self.command_response).unwrap_or_else(|err| { |
| 33 | + json!({ |
| 34 | + "error": "Failed to serialize response", |
| 35 | + "command": self.command, |
| 36 | + "details": err.to_string() |
| 37 | + }) |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl OutputLink for DeployResponseKind { |
| 43 | + const TITLE: &'static str = "deployment"; |
| 44 | + |
| 45 | + fn format_links(&self, provider: Box<dyn LinkProvider>) -> String { |
| 46 | + match self { |
| 47 | + DeployResponseKind::Deploy(deploy) => { |
| 48 | + formatdoc!( |
| 49 | + " |
| 50 | + contract: {} |
| 51 | + transaction: {} |
| 52 | + ", |
| 53 | + provider.contract(deploy.contract_address), |
| 54 | + provider.transaction(deploy.transaction_hash) |
| 55 | + ) |
| 56 | + } |
| 57 | + DeployResponseKind::DeployWithDeclare(deploy_with_declare) => { |
| 58 | + formatdoc!( |
| 59 | + " |
| 60 | + contract: {} |
| 61 | + class: {} |
| 62 | + declare transaction: {} |
| 63 | + deploy transaction: {} |
| 64 | + ", |
| 65 | + provider.contract(deploy_with_declare.contract_address), |
| 66 | + provider.class(deploy_with_declare.class_hash), |
| 67 | + provider.transaction(deploy_with_declare.declare_transaction_hash), |
| 68 | + provider.transaction(deploy_with_declare.deploy_transaction_hash) |
| 69 | + ) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
13 | 74 |
|
14 | 75 | #[derive(Clone, Serialize, Deserialize, CairoSerialize, Debug, PartialEq)]
|
15 | 76 | pub struct DeployResponse {
|
16 | 77 | pub contract_address: PaddedFelt,
|
17 | 78 | pub transaction_hash: PaddedFelt,
|
18 | 79 | }
|
19 | 80 |
|
20 |
| -impl CommandResponse for DeployResponse {} |
21 |
| - |
22 |
| -impl Message for SncastMessage<DeployResponse> { |
| 81 | +impl DeployResponse { |
23 | 82 | fn text(&self) -> String {
|
24 | 83 | styling::OutputBuilder::new()
|
25 | 84 | .success_message("Deployment completed")
|
26 | 85 | .blank_line()
|
27 | 86 | .field(
|
28 | 87 | "Contract Address",
|
29 |
| - &self.command_response.contract_address.into_padded_hex_str(), |
| 88 | + &self.contract_address.into_padded_hex_str(), |
30 | 89 | )
|
31 | 90 | .field(
|
32 | 91 | "Transaction Hash",
|
33 |
| - &self.command_response.transaction_hash.into_padded_hex_str(), |
| 92 | + &self.transaction_hash.into_padded_hex_str(), |
34 | 93 | )
|
35 | 94 | .build()
|
36 | 95 | }
|
| 96 | +} |
37 | 97 |
|
38 |
| - fn json(&self) -> Value { |
39 |
| - serde_json::to_value(&self.command_response).unwrap_or_else(|err| { |
40 |
| - json!({ |
41 |
| - "error": "Failed to serialize response", |
42 |
| - "command": self.command, |
43 |
| - "details": err.to_string() |
44 |
| - }) |
45 |
| - }) |
46 |
| - } |
| 98 | +#[derive(Clone, Serialize, Deserialize, CairoSerialize, Debug, PartialEq)] |
| 99 | +pub struct DeployResponseWithDeclare { |
| 100 | + pub contract_address: PaddedFelt, |
| 101 | + pub class_hash: PaddedFelt, |
| 102 | + pub deploy_transaction_hash: PaddedFelt, |
| 103 | + pub declare_transaction_hash: PaddedFelt, |
47 | 104 | }
|
48 | 105 |
|
49 |
| -impl OutputLink for DeployResponse { |
50 |
| - const TITLE: &'static str = "deployment"; |
| 106 | +impl DeployResponseWithDeclare { |
| 107 | + #[must_use] |
| 108 | + pub fn from_responses(deploy: &DeployResponse, declare: &DeclareTransactionResponse) -> Self { |
| 109 | + Self { |
| 110 | + contract_address: deploy.contract_address, |
| 111 | + class_hash: declare.class_hash, |
| 112 | + deploy_transaction_hash: deploy.transaction_hash, |
| 113 | + declare_transaction_hash: declare.transaction_hash, |
| 114 | + } |
| 115 | + } |
| 116 | +} |
51 | 117 |
|
52 |
| - fn format_links(&self, provider: Box<dyn LinkProvider>) -> String { |
53 |
| - formatdoc!( |
54 |
| - " |
55 |
| - contract: {} |
56 |
| - transaction: {} |
57 |
| - ", |
58 |
| - provider.contract(self.contract_address), |
59 |
| - provider.transaction(self.transaction_hash) |
60 |
| - ) |
| 118 | +impl DeployResponseWithDeclare { |
| 119 | + fn text(&self) -> String { |
| 120 | + styling::OutputBuilder::new() |
| 121 | + .success_message("Deployment completed") |
| 122 | + .blank_line() |
| 123 | + .field( |
| 124 | + "Contract Address", |
| 125 | + &self.contract_address.into_padded_hex_str(), |
| 126 | + ) |
| 127 | + .field("Class Hash", &self.class_hash.into_padded_hex_str()) |
| 128 | + .field( |
| 129 | + "Declare Transaction Hash", |
| 130 | + &self.declare_transaction_hash.into_padded_hex_str(), |
| 131 | + ) |
| 132 | + .field( |
| 133 | + "Deploy Transaction Hash", |
| 134 | + &self.deploy_transaction_hash.into_padded_hex_str(), |
| 135 | + ) |
| 136 | + .build() |
61 | 137 | }
|
62 | 138 | }
|
0 commit comments