11use crate :: protos:: generated:: klv:: proto;
22use crate :: protos:: generated:: klv:: proto:: tx_contract:: ContractType ;
3- use tiny_json_rs:: mapper;
4- use tiny_json_rs:: serializer;
5- use tiny_json_rs:: Deserialize ;
6- use tiny_json_rs:: Serialize ;
3+ use serde:: { Deserialize , Serialize } ;
74
85use crate :: protos;
96use kos:: crypto:: base64:: simple_base64_decode;
107
11- #[ derive( Serialize , Deserialize ) ]
8+ #[ derive( Serialize , Deserialize , Clone , PartialEq ) ]
129#[ allow( clippy:: derive_partial_eq_without_eq) ]
13- #[ derive( Clone , PartialEq ) ]
1410pub struct Transaction {
15- #[ Rename = "RawData" ]
11+ #[ serde ( rename = "RawData" ) ]
1612 pub raw_data : Option < Raw > ,
17- #[ Rename = "Signature" ]
18- pub signature : Option < Vec < String > > , //Base64 encoded
19- #[ Rename = "Result" ]
13+
14+ #[ serde( rename = "Signature" ) ]
15+ pub signature : Option < Vec < String > > , // Base64 encoded
16+
17+ #[ serde( rename = "Result" ) ]
2018 pub result : Option < i32 > ,
21- #[ Rename = "ResultCode" ]
19+
20+ #[ serde( rename = "ResultCode" ) ]
2221 pub result_code : Option < i32 > ,
23- #[ Rename = "Receipts" ]
22+
23+ #[ serde( rename = "Receipts" ) ]
2424 pub receipts : Option < Vec < Receipt > > ,
25- #[ Rename = "Block" ]
25+
26+ #[ serde( rename = "Block" ) ]
2627 pub block : Option < u64 > ,
2728}
2829
2930#[ derive( Clone , PartialEq , Serialize , Deserialize ) ]
3031pub struct Raw {
31- #[ Rename = "Nonce" ]
32+ #[ serde ( rename = "Nonce" ) ]
3233 pub nonce : Option < u64 > ,
33- #[ Rename = "Sender" ]
34+
35+ #[ serde( rename = "Sender" ) ]
3436 pub sender : String ,
35- #[ Rename = "Contract" ]
37+
38+ #[ serde( rename = "Contract" ) ]
3639 pub contract : Vec < TxContract > ,
37- #[ Rename = "PermissionID" ]
40+
41+ #[ serde( rename = "PermissionID" ) ]
3842 pub permission_id : Option < i32 > ,
39- #[ Rename = "Data" ]
43+
44+ #[ serde( rename = "Data" ) ]
4045 pub data : Option < Vec < String > > ,
41- #[ Rename = "KAppFee" ] // Use this to match the exact JSON field name for this field
46+
47+ #[ serde( rename = "KAppFee" ) ]
4248 pub k_app_fee : Option < i64 > ,
43- #[ Rename = "BandwidthFee" ] // Use this to match the exact JSON field name for this field
49+
50+ #[ serde( rename = "BandwidthFee" ) ]
4451 pub bandwidth_fee : Option < i64 > ,
45- #[ Rename = "Version" ]
52+
53+ #[ serde( rename = "Version" ) ]
4654 pub version : Option < u32 > ,
47- #[ Rename = "ChainID" ]
55+
56+ #[ serde( rename = "ChainID" ) ]
4857 pub chain_id : String ,
49- #[ Rename = "KDAFee" ] // Use this to match the exact JSON field name for this field
50- pub kda_fee : :: core:: option:: Option < KdaFee > ,
58+
59+ #[ serde( rename = "KDAFee" ) ]
60+ pub kda_fee : Option < KdaFee > ,
5161}
5262
5363#[ derive( Serialize , Deserialize , Clone , PartialEq ) ]
5464pub struct TxContract {
55- #[ Rename = "Parameter" ]
65+ #[ serde ( rename = "Parameter" ) ]
5666 pub parameter : Parameter ,
57- #[ Rename = "Type" ]
67+
68+ #[ serde( rename = "Type" ) ]
5869 pub r#type : Option < i32 > ,
59- // ... other fields
6070}
6171
6272#[ derive( Serialize , Deserialize , Clone , PartialEq ) ]
6373pub struct Parameter {
6474 pub type_url : String ,
6575 pub value : Option < String > ,
66- // ... other fields
6776}
6877
6978#[ derive( Clone , PartialEq , Serialize , Deserialize ) ]
7079pub struct KdaFee {
71- #[ Rename = "KDA" ]
80+ #[ serde ( rename = "KDA" ) ]
7281 pub kda : String ,
73- #[ Rename = "Amount" ]
82+
83+ #[ serde( rename = "Amount" ) ]
7484 pub amount : i64 ,
7585}
7686
7787#[ derive( Clone , PartialEq , Serialize , Deserialize ) ]
7888pub struct Receipt {
79- #[ Rename = "Data" ]
89+ #[ serde ( rename = "Data" ) ]
8090 pub data : Vec < String > ,
8191}
8292
@@ -85,7 +95,6 @@ pub struct Receipt {
8595pub enum ConversionError {
8696 InvalidData ( & ' static str ) ,
8797 Base64Error ,
88- // You can add more error types as needed for detailed error handling
8998}
9099
91100impl TryFrom < Transaction > for proto:: Transaction {
@@ -119,7 +128,7 @@ impl TryFrom<Transaction> for proto::Transaction {
119128 result_code : value. result_code . unwrap_or ( 0 ) ,
120129 receipts,
121130 block : value. block . unwrap_or ( 0 ) ,
122- ..Default :: default ( ) // Include other fields as necessary
131+ ..Default :: default ( )
123132 } ;
124133
125134 Ok ( proto_tx)
@@ -173,17 +182,19 @@ impl TryFrom<TxContract> for proto::TxContract {
173182 type Error = ConversionError ;
174183
175184 fn try_from ( value : TxContract ) -> Result < Self , Self :: Error > {
176- // Remove escapes
185+ // Remove escapes - serde_json handles this automatically now!
177186 let contract_name = value. parameter . type_url . replace ( "\\ " , "" ) ;
178187
179- //Remove the "type.googleapis.com/" prefix
188+ // Remove the "type.googleapis.com/" prefix
180189 let contract_name = contract_name
181190 . strip_prefix ( "type.googleapis.com/proto." )
182191 . ok_or ( ConversionError :: InvalidData ( "Invalid contract name" ) ) ?;
183- //Add Type at the end of str
192+
193+ // Add Type at the end of str
184194 let contract_name = format ! ( "{contract_name}Type" ) ;
185195 let contract_type = ContractType :: from_str_name ( & contract_name)
186196 . ok_or ( ConversionError :: InvalidData ( "Invalid contract type" ) ) ?;
197+
187198 let proto_contract = proto:: TxContract {
188199 r#type : contract_type as i32 ,
189200 parameter : Option :: from ( protos:: Any :: try_from ( value. parameter ) ?) ,
0 commit comments