@@ -129,8 +129,8 @@ impl<T> Response<T> {
129
129
///
130
130
/// The `wasm-` prefix will be appended by the runtime to the provided type
131
131
/// of event.
132
- pub fn add_event ( mut self , event : Event ) -> Self {
133
- self . events . push ( event) ;
132
+ pub fn add_event ( mut self , event : impl Into < Event > ) -> Self {
133
+ self . events . push ( event. into ( ) ) ;
134
134
self
135
135
}
136
136
@@ -219,8 +219,11 @@ impl<T> Response<T> {
219
219
///
220
220
/// The `wasm-` prefix will be appended by the runtime to the provided types
221
221
/// of events.
222
- pub fn add_events ( mut self , events : impl IntoIterator < Item = Event > ) -> Self {
223
- self . events . extend ( events) ;
222
+ pub fn add_events < E > ( mut self , events : impl IntoIterator < Item = E > ) -> Self
223
+ where
224
+ E : Into < Event > ,
225
+ {
226
+ self . events . extend ( events. into_iter ( ) . map ( |e| e. into ( ) ) ) ;
224
227
self
225
228
}
226
229
@@ -236,7 +239,7 @@ mod tests {
236
239
use super :: super :: BankMsg ;
237
240
use super :: * ;
238
241
use crate :: results:: submessages:: { ReplyOn , UNUSED_MSG_ID } ;
239
- use crate :: { coins, from_json, to_json_vec, ContractResult } ;
242
+ use crate :: { attr , coins, from_json, to_json_vec, Addr , Coin , ContractResult , IntoEvent } ;
240
243
241
244
#[ test]
242
245
fn response_add_attributes_works ( ) {
@@ -331,4 +334,75 @@ mod tests {
331
334
assert ! ( failure. is_err( ) ) ;
332
335
assert ! ( !success. is_err( ) ) ;
333
336
}
337
+
338
+ #[ test]
339
+ fn using_into_event ( ) {
340
+ // IntoEvent can be used only when cosmwasm_std is imported as `cosmwasm_std`
341
+ use crate as cosmwasm_std;
342
+
343
+ fn coins_to_string ( coins : Vec < Coin > ) -> String {
344
+ format ! (
345
+ "[{}]" ,
346
+ coins
347
+ . iter( )
348
+ . map( |c| c. to_string( ) )
349
+ . collect:: <Vec <String >>( )
350
+ . join( ", " )
351
+ )
352
+ }
353
+
354
+ #[ derive( Clone , IntoEvent ) ]
355
+ struct TransferEvent {
356
+ from : Addr ,
357
+ receiver : Addr ,
358
+ #[ to_string_fn( coins_to_string) ]
359
+ amount : Vec < Coin > ,
360
+ }
361
+
362
+ let transfer_event = TransferEvent {
363
+ from : Addr :: unchecked ( "alice" ) ,
364
+ receiver : Addr :: unchecked ( "bob" ) ,
365
+ amount : coins ( 42 , "link" ) ,
366
+ } ;
367
+ let expected =
368
+ Response :: < Empty > :: new ( ) . add_event ( Event :: new ( "transfer_event" ) . add_attributes ( vec ! [
369
+ attr( "from" , "alice" ) ,
370
+ attr( "receiver" , "bob" ) ,
371
+ attr( "amount" , coins_to_string( coins( 42 , "link" ) ) ) ,
372
+ ] ) ) ;
373
+ let actual = Response :: < Empty > :: new ( ) . add_event ( transfer_event) ;
374
+ assert_eq ! ( actual, expected) ;
375
+ }
376
+
377
+ #[ test]
378
+ fn using_into_event_add_events ( ) {
379
+ use crate as cosmwasm_std;
380
+
381
+ fn u32_to_string ( n : u32 ) -> String {
382
+ n. to_string ( )
383
+ }
384
+
385
+ #[ derive( IntoEvent ) ]
386
+ struct Act {
387
+ name : String ,
388
+ #[ to_string_fn( u32_to_string) ]
389
+ amount : u32 ,
390
+ }
391
+
392
+ let act1 = Act {
393
+ name : "mint" . to_string ( ) ,
394
+ amount : 42 ,
395
+ } ;
396
+ let act2 = Act {
397
+ name : "burn" . to_string ( ) ,
398
+ amount : 21 ,
399
+ } ;
400
+ let event1 =
401
+ Event :: new ( "act" ) . add_attributes ( vec ! [ attr( "name" , "mint" ) , attr( "amount" , "42" ) ] ) ;
402
+ let event2 =
403
+ Event :: new ( "act" ) . add_attributes ( vec ! [ attr( "name" , "burn" ) , attr( "amount" , "21" ) ] ) ;
404
+ let expected = Response :: < Empty > :: new ( ) . add_events ( vec ! [ event1, event2] ) ;
405
+ let actual = Response :: < Empty > :: new ( ) . add_events ( vec ! [ act1, act2] ) ;
406
+ assert_eq ! ( actual, expected) ;
407
+ }
334
408
}
0 commit comments