1+ #[system]
2+ mod Buy {
3+ use traits :: Into ;
4+ use array :: ArrayTrait ;
5+ use dojo_defi :: constant_product_market :: components :: Item ;
6+ use dojo_defi :: constant_product_market :: components :: Cash ;
7+ use dojo_defi :: constant_product_market :: components :: Market ;
8+ use dojo_defi :: constant_product_market :: components :: MarketTrait ;
9+
10+ fn execute (game_id : felt252 , item_id : felt252 , quantity : usize ) {
11+ let player : felt252 = starknet :: get_caller_address (). into ();
12+
13+ let cash_sk : Query = (game_id , (player )). into ();
14+ let player_cash = commands :: <Cash >:: entity (cash_sk );
15+
16+ let market_sk : Query = (game_id , (item_id )). into ();
17+ let market = commands :: <Market >:: entity (market_sk );
18+
19+ let cost = market . buy (quantity );
20+ assert (cost < player_cash . amount, ' not enough cash' );
21+
22+ // update market
23+ commands :: set_entity (
24+ market_sk ,
25+ (Market {
26+ cash_amount : market . cash_amount + cost ,
27+ item_quantity : market . item_quantity - quantity ,
28+ })
29+ );
30+
31+ // update player cash
32+ commands :: set_entity (cash_sk , (Cash { amount : player_cash . amount - cost }));
33+
34+ // update player item
35+ let item_sk : Query = (game_id , (player , item_id )). into ();
36+ let maybe_item = commands :: <Item >:: try_entity (item_sk );
37+ let player_quantity = match maybe_item {
38+ Option :: Some (item ) => item . quantity + quantity ,
39+ Option :: None (_ ) => quantity ,
40+ };
41+ commands :: set_entity (item_sk , (Item { quantity : player_quantity }));
42+ }
43+ }
44+
45+ #[system]
46+ mod Sell {
47+ use traits :: Into ;
48+ use array :: ArrayTrait ;
49+ use dojo_defi :: constant_product_market :: components :: Item ;
50+ use dojo_defi :: constant_product_market :: components :: Cash ;
51+ use dojo_defi :: constant_product_market :: components :: Market ;
52+ use dojo_defi :: constant_product_market :: components :: MarketTrait ;
53+
54+ fn execute (game_id : felt252 , item_id : felt252 , quantity : usize ) {
55+ let player : felt252 = starknet :: get_caller_address (). into ();
56+
57+ let item_sk : Query = (game_id , (player , item_id )). into ();
58+ let maybe_item = commands :: <Item >:: try_entity (item_sk );
59+ let player_quantity = match maybe_item {
60+ Option :: Some (item ) => item . quantity,
61+ Option :: None (_ ) => 0_u32 ,
62+ };
63+ assert (player_quantity >= quantity , ' not enough items' );
64+
65+ let cash_sk : Query = (game_id , (player )). into ();
66+ let player_cash = commands :: <Cash >:: entity (cash_sk );
67+
68+ let market_sk : Query = (game_id , (item_id )). into ();
69+ let market = commands :: <Market >:: entity (market_sk );
70+ let payout = market . sell (quantity );
71+
72+ // update market
73+ commands :: set_entity (
74+ market_sk ,
75+ (Market {
76+ cash_amount : market . cash_amount - payout ,
77+ item_quantity : market . item_quantity + quantity
78+ })
79+ );
80+
81+ // update player cash
82+ commands :: set_entity (cash_sk , (Cash { amount : player_cash . amount + payout }));
83+
84+ // update player item
85+ commands :: set_entity (item_sk , (Item { quantity : player_quantity - quantity }));
86+ }
87+ }
0 commit comments