In Sway, addresses, contract IDs, and identities are key concepts used to handle different entities on the blockchain.
Address type.ContractId type.Identity::Address or Identity::ContractId.In the provided code, we define a smart contract named MyContract with a function called test_func that returns a value of type Identity.
addr and convert it into a b256 type value.my_contract_id and convert it into a b256 type value.my_id that is an identity of the address type.my_id and perform different actions based on its type.The test_func function of this smart contract ultimately returns a value of the identity type. This simple example is intended to show you how to use addresses, contract IDs, and identity types in Sway. We hope this tutorial helps you better understand these key concepts in Sway.
contract;
// Functions
// - Internal and external functions
// - ref mut
// - Return multiple outputs
abi MyContract {
fn test_func() -> (u64, bool);
}
fn eq(x: u64, y: u64) -> bool {
x == y
}
fn inc(ref mut num: u64) {
num += 1;
}
fn swap_mut(ref mut pair: (u64, u64)) {
let tmp = pair.0;
pair.0 = pair.1;
pair.1 = tmp;
}
fn swap(x: u64, y: u64) -> (u64, u64) {
(y, x)
}
impl MyContract for Contract {
fn test_func() -> (u64, bool) {
assert(eq(11, 11));
assert(!eq(11, 12));
let mut num: u64 = 123;
inc(num);
assert(num == 123 + 1);
let mut pair = (12, 13);
swap_mut(pair);
assert(pair.0 == 13);
assert(pair.1 == 12);
let x = 1;
let y = 2;
let (y, x) = swap(x, y);
assert(y == 1);
assert(x == 2);
(123, true)
}
}