1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use async_trait::async_trait;
use hedera_proto::services;
use hedera_proto::services::smart_contract_service_client::SmartContractServiceClient;
use tonic::transport::Channel;
use crate::protobuf::ToProtobuf;
use crate::transaction::{
AnyTransactionData,
ToTransactionDataProtobuf,
TransactionExecute,
};
use crate::{
AccountId,
ContractId,
Transaction,
};
pub type ContractDeleteTransaction = Transaction<ContractDeleteTransactionData>;
#[cfg_attr(feature = "ffi", serde_with::skip_serializing_none)]
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(rename_all = "camelCase"))]
pub struct ContractDeleteTransactionData {
pub contract_id: Option<ContractId>,
pub transfer_account_id: Option<AccountId>,
pub transfer_contract_id: Option<ContractId>,
}
impl ContractDeleteTransaction {
pub fn contract_id(&mut self, id: ContractId) -> &mut Self {
self.body.data.contract_id = Some(id);
self
}
pub fn transfer_account_id(&mut self, id: AccountId) -> &mut Self {
self.body.data.transfer_account_id = Some(id);
self
}
pub fn transfer_contract_id(&mut self, id: ContractId) -> &mut Self {
self.body.data.transfer_contract_id = Some(id);
self
}
}
#[async_trait]
impl TransactionExecute for ContractDeleteTransactionData {
async fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> Result<tonic::Response<services::TransactionResponse>, tonic::Status> {
SmartContractServiceClient::new(channel).delete_contract(request).await
}
}
impl ToTransactionDataProtobuf for ContractDeleteTransactionData {
fn to_transaction_data_protobuf(
&self,
_node_account_id: AccountId,
_transaction_id: &crate::TransactionId,
) -> services::transaction_body::Data {
let delete_contract_id = self.contract_id.to_protobuf();
let obtainers = match (&self.transfer_account_id, &self.transfer_contract_id) {
(Some(account_id), None) => {
Some(services::contract_delete_transaction_body::Obtainers::TransferAccountId(
account_id.to_protobuf(),
))
}
(None, Some(contract_id)) => {
Some(services::contract_delete_transaction_body::Obtainers::TransferContractId(
contract_id.to_protobuf(),
))
}
_ => None,
};
services::transaction_body::Data::ContractDeleteInstance(
services::ContractDeleteTransactionBody {
contract_id: delete_contract_id,
permanent_removal: false,
obtainers,
},
)
}
}
impl From<ContractDeleteTransactionData> for AnyTransactionData {
fn from(transaction: ContractDeleteTransactionData) -> Self {
Self::ContractDelete(transaction)
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "ffi")]
mod ffi {
use assert_matches::assert_matches;
use crate::contract::ContractDeleteTransaction;
use crate::transaction::{
AnyTransaction,
AnyTransactionData,
};
use crate::{
AccountId,
ContractId,
};
const CONTRACT_DELETE_TRANSACTION_JSON: &str = r#"{
"$type": "contractDelete",
"contractId": "0.0.1001",
"transferAccountId": "0.0.1002",
"transferContractId": "0.0.1003"
}"#;
#[test]
fn it_should_serialize() -> anyhow::Result<()> {
let mut transaction = ContractDeleteTransaction::new();
transaction
.contract_id(ContractId::from(1001))
.transfer_account_id(AccountId::from(1002))
.transfer_contract_id(ContractId::from(1003));
let transaction_json = serde_json::to_string_pretty(&transaction)?;
assert_eq!(transaction_json, CONTRACT_DELETE_TRANSACTION_JSON);
Ok(())
}
#[test]
fn it_should_deserialize() -> anyhow::Result<()> {
let transaction: AnyTransaction =
serde_json::from_str(CONTRACT_DELETE_TRANSACTION_JSON)?;
let data = assert_matches!(transaction.body.data, AnyTransactionData::ContractDelete(transaction) => transaction);
assert_eq!(data.contract_id.unwrap(), ContractId::from(1001));
assert_eq!(data.transfer_contract_id.unwrap(), ContractId::from(1003));
assert_eq!(data.transfer_account_id, Some(AccountId::from(1002)));
Ok(())
}
}
}