11#![ no_std]
22use soroban_sdk:: {
3- contract, contractevent, contractimpl, symbol_short , Address , Env , Event , IntoVal ,
3+ contract, contractevent, contractimpl, Address , Env , Event , IntoVal , MuxedAddress ,
44} ;
55
66#[ contract]
77pub struct Contract ;
88
9- #[ contractevent(
10- data_format = "single-value" ,
11- prefix_topics = [ "transfer" ] ,
12- ) ]
13- pub struct TransferSingleValueEvent {
14- #[ topic]
15- from : Address ,
16- #[ topic]
17- to : Address ,
18- amount : i128 ,
19- }
20-
21- #[ contractevent(
22- data_format = "vec" ,
23- prefix_topics = [ "transfer" ] ,
24- ) ]
25- pub struct TransferVecEvent {
26- #[ topic]
27- from : Address ,
28- #[ topic]
29- to : Address ,
30- amount : i128 ,
31- to_muxed_id : u64 ,
32- }
33-
34- #[ contractevent(
35- data_format = "map" ,
36- prefix_topics = [ "transfer" ] ,
37- ) ]
38- pub struct TransferMapEvent {
39- #[ topic]
40- from : Address ,
41- #[ topic]
42- to : Address ,
43- amount : i128 ,
44- to_muxed_id : u64 ,
45- }
46-
479#[ contractevent]
4810pub struct Transfer {
4911 #[ topic]
5012 from : Address ,
5113 #[ topic]
5214 to : Address ,
5315 amount : i128 ,
54- to_muxed_id : u64 ,
55- }
56-
57- #[ contractevent]
58- pub struct TransferNoData {
59- #[ topic]
60- from : Address ,
61- #[ topic]
62- to : Address ,
63- }
64-
65- #[ contractevent( data_format = "single-value" ) ]
66- pub struct TransferNoDataSingleValue {
67- #[ topic]
68- from : Address ,
69- #[ topic]
70- to : Address ,
16+ to_muxed_id : Option < u64 > ,
7117}
7218
7319#[ contractimpl]
7420impl Contract {
75- pub fn transfer_events ( env : Env , from : Address , to : Address ) {
76- let events = env. events ( ) ;
77-
78- // A freely expressed event using a topic list and data.
79- events. publish ( ( symbol_short ! ( "transfer" ) , & from, & to) , & 1i128 ) ;
80-
81- // A contract event type that expresses its data as a single-value.
82- events. publish_event ( & TransferSingleValueEvent {
83- from : from. clone ( ) ,
84- to : to. clone ( ) ,
85- amount : 1 ,
86- } ) ;
87-
88- // A contract event type that expresses its data fields as a vec.
89- events. publish_event ( & TransferVecEvent {
90- from : from. clone ( ) ,
91- to : to. clone ( ) ,
92- amount : 1 ,
93- to_muxed_id : 2 ,
94- } ) ;
95-
96- // A contract event type that expresses its data fields as a map.
97- events. publish_event ( & TransferMapEvent {
98- from : from. clone ( ) ,
99- to : to. clone ( ) ,
100- amount : 1 ,
101- to_muxed_id : 2 ,
102- } ) ;
103-
104- // Publish callable on the event.
105- TransferMapEvent {
106- from : from. clone ( ) ,
107- to : to. clone ( ) ,
108- amount : 1 ,
109- to_muxed_id : 2 ,
110- }
111- . publish ( & env) ;
112-
113- // A contract event type that relies on the defaults.
21+ pub fn transfer ( env : Env , from : Address , to : MuxedAddress , amount : i128 ) {
11422 Transfer {
11523 from : from. clone ( ) ,
116- to : to. clone ( ) ,
117- amount : 1 ,
118- to_muxed_id : 2 ,
119- }
120- . publish ( & env) ;
121-
122- // A contract event with no data.
123- TransferNoData {
124- from : from. clone ( ) ,
125- to : to. clone ( ) ,
126- }
127- . publish ( & env) ;
128-
129- // A contract event with no data, and single-value.
130- TransferNoDataSingleValue {
131- from : from. clone ( ) ,
132- to : to. clone ( ) ,
24+ to : to. address ( ) ,
25+ amount,
26+ to_muxed_id : to. id ( ) ,
13327 }
13428 . publish ( & env) ;
13529 }
@@ -140,22 +34,23 @@ mod test {
14034 extern crate alloc;
14135 use soroban_sdk:: {
14236 map, symbol_short,
143- testutils:: { Address as _, Events } ,
144- vec, Address , Env , IntoVal , Map , Symbol , Val ,
37+ testutils:: { Address as _, Events , MuxedAddress as _ } ,
38+ vec, Address , Env , IntoVal , MuxedAddress , Symbol , Val ,
14539 } ;
14640
14741 use crate :: { Contract , ContractClient } ;
14842
14943 #[ test]
150- fn test_pub_event ( ) {
44+ fn test_event ( ) {
15145 let env = Env :: default ( ) ;
15246 let contract_id = env. register ( Contract , ( ) ) ;
15347 let client = ContractClient :: new ( & env, & contract_id) ;
15448
15549 let from = Address :: generate ( & env) ;
156- let to = Address :: generate ( & env) ;
50+ let to = MuxedAddress :: generate ( & env) ;
51+ let amount = 1i128 ;
15752
158- client. transfer_events ( & from, & to) ;
53+ client. transfer ( & from, & to, & amount ) ;
15954
16055 assert_eq ! (
16156 env. events( ) . all( ) ,
@@ -164,28 +59,7 @@ mod test {
16459 (
16560 contract_id. clone( ) ,
16661 // Expect these event topics.
167- ( symbol_short!( "transfer" ) , & from, & to) . into_val( & env) ,
168- // Expect this event body.
169- 1i128 . into_val( & env) ,
170- ) ,
171- (
172- contract_id. clone( ) ,
173- // Expect these event topics.
174- ( symbol_short!( "transfer" ) , & from, & to) . into_val( & env) ,
175- // Expect this event body.
176- 1i128 . into_val( & env) ,
177- ) ,
178- (
179- contract_id. clone( ) ,
180- // Expect these event topics.
181- ( symbol_short!( "transfer" ) , & from, & to) . into_val( & env) ,
182- // Expect this event body.
183- ( 1i128 , 2u64 ) . into_val( & env) ,
184- ) ,
185- (
186- contract_id. clone( ) ,
187- // Expect these event topics.
188- ( symbol_short!( "transfer" ) , & from, & to) . into_val( & env) ,
62+ ( Symbol :: new( & env, "transfer" ) , & from, to. address( ) ) . into_val( & env) ,
18963 // Expect this event body.
19064 map![
19165 & env,
@@ -195,29 +69,31 @@ mod test {
19569 ) ,
19670 (
19771 Symbol :: new( & env, "to_muxed_id" ) ,
198- <_ as IntoVal <Env , Val >>:: into_val( & 2u64 , & env)
199- ) ,
200- ]
201- . to_val( )
202- ) ,
203- (
204- contract_id. clone( ) ,
205- // Expect these event topics.
206- ( symbol_short!( "transfer" ) , & from, & to) . into_val( & env) ,
207- // Expect this event body.
208- map![
209- & env,
210- (
211- symbol_short!( "amount" ) ,
212- <_ as IntoVal <Env , Val >>:: into_val( & 1i128 , & env)
213- ) ,
214- (
215- Symbol :: new( & env, "to_muxed_id" ) ,
216- <_ as IntoVal <Env , Val >>:: into_val( & 2u64 , & env)
72+ <_ as IntoVal <Env , Val >>:: into_val( & to. id( ) . unwrap( ) , & env)
21773 ) ,
21874 ]
21975 . to_val( )
22076 ) ,
77+ ] ,
78+ ) ;
79+ }
80+
81+ #[ test]
82+ fn test_event_with_option_none ( ) {
83+ let env = Env :: default ( ) ;
84+ let contract_id = env. register ( Contract , ( ) ) ;
85+ let client = ContractClient :: new ( & env, & contract_id) ;
86+
87+ let from = Address :: generate ( & env) ;
88+ let to = Address :: generate ( & env) ;
89+ let amount = 1i128 ;
90+
91+ client. transfer ( & from, & ( & to) . into ( ) , & amount) ;
92+
93+ assert_eq ! (
94+ env. events( ) . all( ) ,
95+ vec![
96+ & env,
22197 (
22298 contract_id. clone( ) ,
22399 // Expect these event topics.
@@ -229,32 +105,10 @@ mod test {
229105 symbol_short!( "amount" ) ,
230106 <_ as IntoVal <Env , Val >>:: into_val( & 1i128 , & env)
231107 ) ,
232- (
233- Symbol :: new( & env, "to_muxed_id" ) ,
234- <_ as IntoVal <Env , Val >>:: into_val( & 2u64 , & env)
235- ) ,
108+ ( Symbol :: new( & env, "to_muxed_id" ) , ( ) . into_val( & env) , ) ,
236109 ]
237110 . to_val( )
238111 ) ,
239- (
240- contract_id. clone( ) ,
241- // Expect these event topics.
242- ( Symbol :: new( & env, "transfer_no_data" ) , & from, & to) . into_val( & env) ,
243- // Expect this event body.
244- Map :: <Symbol , Val >:: new( & env) . to_val( ) ,
245- ) ,
246- (
247- contract_id. clone( ) ,
248- // Expect these event topics.
249- (
250- Symbol :: new( & env, "transfer_no_data_single_value" ) ,
251- & from,
252- & to
253- )
254- . into_val( & env) ,
255- // Expect this event body.
256- ( ) . into_val( & env) ,
257- ) ,
258112 ] ,
259113 ) ;
260114 }
0 commit comments