11use super :: { PlatformAddressLikeJs , PlatformAddressWasm } ;
2- use crate :: error:: WasmDppResult ;
3- use crate :: utils:: try_to_u64;
2+ use crate :: error:: { WasmDppError , WasmDppResult } ;
3+ use crate :: impl_try_from_js_value;
4+ use crate :: impl_wasm_type_info;
5+ use crate :: utils:: { IntoWasm , try_from_options_with, try_to_array, try_to_u64} ;
46use dpp:: address_funds:: PlatformAddress ;
57use dpp:: fee:: Credits ;
68use dpp:: prelude:: AddressNonce ;
@@ -22,6 +24,8 @@ pub struct PlatformAddressInputWasm {
2224 amount : Credits ,
2325}
2426
27+ impl_wasm_type_info ! ( PlatformAddressInputWasm , PlatformAddressInput ) ;
28+
2529#[ wasm_bindgen( js_class = PlatformAddressInput ) ]
2630impl PlatformAddressInputWasm {
2731 /// Creates a new PlatformAddressInput.
@@ -65,6 +69,14 @@ impl PlatformAddressInputWasm {
6569}
6670
6771impl PlatformAddressInputWasm {
72+ pub fn new ( address : PlatformAddress , nonce : AddressNonce , amount : Credits ) -> Self {
73+ Self {
74+ address : address. into ( ) ,
75+ nonce,
76+ amount,
77+ }
78+ }
79+
6880 /// Returns the inner values as a tuple suitable for BTreeMap insertion.
6981 pub fn into_inner ( self ) -> ( PlatformAddress , ( AddressNonce , Credits ) ) {
7082 ( self . address . into ( ) , ( self . nonce , self . amount ) )
@@ -86,6 +98,9 @@ pub struct PlatformAddressOutputWasm {
8698 amount : Option < Credits > ,
8799}
88100
101+ impl_wasm_type_info ! ( PlatformAddressOutputWasm , PlatformAddressOutput ) ;
102+ impl_try_from_js_value ! ( PlatformAddressOutputWasm , "PlatformAddressOutput" ) ;
103+
89104#[ wasm_bindgen( js_class = PlatformAddressOutput ) ]
90105impl PlatformAddressOutputWasm {
91106 /// Creates a new PlatformAddressOutput with a specific amount.
@@ -122,13 +137,26 @@ impl PlatformAddressOutputWasm {
122137}
123138
124139impl PlatformAddressOutputWasm {
125- /// Returns the inner values as a tuple suitable for BTreeMap insertion.
126- /// Panics if amount is None - use `into_inner_optional` for optional amounts.
127- pub fn into_inner ( self ) -> ( PlatformAddress , Credits ) {
128- (
129- self . address . into ( ) ,
130- self . amount . expect ( "amount is required for this operation" ) ,
131- )
140+ pub fn new ( address : PlatformAddress , amount : Credits ) -> Self {
141+ Self {
142+ address : address. into ( ) ,
143+ amount : Some ( amount) ,
144+ }
145+ }
146+
147+ pub fn new_optional ( address : PlatformAddress , amount : Option < Credits > ) -> Self {
148+ Self {
149+ address : address. into ( ) ,
150+ amount,
151+ }
152+ }
153+
154+ /// Returns the inner values as a tuple, or an error if amount is None.
155+ pub fn try_into_inner ( self ) -> WasmDppResult < ( PlatformAddress , Credits ) > {
156+ let amount = self . amount . ok_or_else ( || {
157+ WasmDppError :: invalid_argument ( "PlatformAddressOutput: amount is required" )
158+ } ) ?;
159+ Ok ( ( self . address . into ( ) , amount) )
132160 }
133161
134162 /// Returns the inner values with optional amount.
@@ -138,10 +166,11 @@ impl PlatformAddressOutputWasm {
138166}
139167
140168/// Converts a vector of PlatformAddressOutput into a BTreeMap.
169+ /// Returns an error if any output has no amount set.
141170pub fn outputs_to_btree_map (
142171 outputs : Vec < PlatformAddressOutputWasm > ,
143- ) -> BTreeMap < PlatformAddress , Credits > {
144- outputs. into_iter ( ) . map ( |o| o. into_inner ( ) ) . collect ( )
172+ ) -> WasmDppResult < BTreeMap < PlatformAddress , Credits > > {
173+ outputs. into_iter ( ) . map ( |o| o. try_into_inner ( ) ) . collect ( )
145174}
146175
147176/// Converts a vector of PlatformAddressOutput into a BTreeMap with optional amounts.
@@ -156,3 +185,53 @@ pub fn outputs_to_optional_btree_map(
156185 . map ( |o| o. into_inner_optional ( ) )
157186 . collect ( )
158187}
188+
189+ /// Extract a Vec<PlatformAddressInputWasm> from a JS options object property.
190+ ///
191+ /// Reads the named property as a JS array, then extracts each element
192+ /// as a PlatformAddressInput wasm-bindgen object via its __wbg_ptr.
193+ pub fn inputs_from_js_options (
194+ options : & JsValue ,
195+ field_name : & str ,
196+ ) -> WasmDppResult < Vec < PlatformAddressInputWasm > > {
197+ let array = try_from_options_with ( options, field_name, |v| try_to_array ( v, field_name) ) ?;
198+ array
199+ . iter ( )
200+ . enumerate ( )
201+ . map ( |( i, item) | {
202+ item. to_wasm :: < PlatformAddressInputWasm > ( "PlatformAddressInput" )
203+ . map ( |r| ( * r) . clone ( ) )
204+ . map_err ( |_| {
205+ WasmDppError :: invalid_argument ( format ! (
206+ "{}[{}] is not a PlatformAddressInput" ,
207+ field_name, i
208+ ) )
209+ } )
210+ } )
211+ . collect ( )
212+ }
213+
214+ /// Extract a Vec<PlatformAddressOutputWasm> from a JS options object property.
215+ ///
216+ /// Reads the named property as a JS array, then extracts each element
217+ /// as a PlatformAddressOutput wasm-bindgen object via its __wbg_ptr.
218+ pub fn outputs_from_js_options (
219+ options : & JsValue ,
220+ field_name : & str ,
221+ ) -> WasmDppResult < Vec < PlatformAddressOutputWasm > > {
222+ let array = try_from_options_with ( options, field_name, |v| try_to_array ( v, field_name) ) ?;
223+ array
224+ . iter ( )
225+ . enumerate ( )
226+ . map ( |( i, item) | {
227+ item. to_wasm :: < PlatformAddressOutputWasm > ( "PlatformAddressOutput" )
228+ . map ( |r| ( * r) . clone ( ) )
229+ . map_err ( |_| {
230+ WasmDppError :: invalid_argument ( format ! (
231+ "{}[{}] is not a PlatformAddressOutput" ,
232+ field_name, i
233+ ) )
234+ } )
235+ } )
236+ . collect ( )
237+ }
0 commit comments