48 |
List an Item
49 | {status === 'none' &&
50 |
83 | }
84 |
85 | {status === 'success' &&
Item successfully listed!
}
86 | {status === 'error' &&
Error listing item. Please try again.
}
87 | {status === 'loading' &&
Listing item...
}
88 |
89 |
90 | )
91 | }
92 | // ANCHOR_END: fe_list_items_form
93 | /* ANCHOR_END: fe_list_items_all */
94 |
--------------------------------------------------------------------------------
/sway-store/sway-programs/contract/src/main.sw:
--------------------------------------------------------------------------------
1 | /* ANCHOR: all */
2 | // ANCHOR: contract
3 | contract;
4 | // ANCHOR_END: contract
5 |
6 | // ANCHOR: import
7 | use std::{
8 | auth::msg_sender,
9 | call_frames::msg_asset_id,
10 | context::{
11 | msg_amount,
12 | this_balance,
13 | },
14 | asset::transfer,
15 | hash::Hash,
16 | };
17 | // ANCHOR_END: import
18 |
19 | // ANCHOR: struct
20 | struct Item {
21 | id: u64,
22 | price: u64,
23 | owner: Identity,
24 | metadata: str[20],
25 | total_bought: u64,
26 | }
27 | // ANCHOR_END: struct
28 |
29 | // ANCHOR: abi
30 | abi SwayStore {
31 | // a function to list an item for sale
32 | // takes the price and metadata as args
33 | #[storage(read, write)]
34 | fn list_item(price: u64, metadata: str[20]);
35 |
36 | // a function to buy an item
37 | // takes the item id as the arg
38 | #[storage(read, write), payable]
39 | fn buy_item(item_id: u64);
40 |
41 | // a function to get a certain item
42 | #[storage(read)]
43 | fn get_item(item_id: u64) -> Item;
44 |
45 | // a function to set the contract owner
46 | #[storage(read, write)]
47 | fn initialize_owner() -> Identity;
48 |
49 | // a function to withdraw contract funds
50 | #[storage(read)]
51 | fn withdraw_funds();
52 |
53 | // return the number of items listed
54 | #[storage(read)]
55 | fn get_count() -> u64;
56 | }
57 | // ANCHOR_END: abi
58 |
59 | // ANCHOR: storage
60 | storage {
61 | // counter for total items listed
62 | item_counter: u64 = 0,
63 |
64 | // ANCHOR: storage_map
65 | // map of item IDs to Items
66 | item_map: StorageMap