Next, we will define our ABI. ABI stands for Application Binary Interface. In a Sway contract, it serves as an outline of all the functions within the contract. For each function, you need to specify its name, input types, return types, and level of storage access.
Our contract's ABI will be structured as follows:
abi SwayStore {
// a function to list an item for sale
// takes the price and metadata as args
#[storage(read, write)]
fn list_item(price: u64, metadata: String);
// a function to buy an item
// takes the item id as the arg
#[storage(read, write), payable]
fn buy_item(item_id: u64);
// a function to get a certain item
#[storage(read)]
fn get_item(item_id: u64) -> Item;
// a function to set the contract owner
#[storage(read, write)]
fn initialize_owner() -> Identity;
// a function to withdraw contract funds
#[storage(read)]
fn withdraw_funds();
// return the number of items listed
#[storage(read)]
fn get_count() -> u64;
}
A function is defined using the fn
keyword. In Sway, snake case is the convention, so instead of naming a function myFunction
, you would name it my_function
.
If the function returns a value, its return type must be defined using a skinny arrow. Additionally, if the function has parameters, their types must also be specified. Semicolons are required at the end of each statement.
If a function either reads from or writes to storage, you need to specify the access level above the function using annotations like #[storage(read)]
or #[storage(read, write)]
.
For functions that are expected to receive funds when called, such as the buy_item
function, the #[payable]
annotation should be used.
Was this page helpful?