Returning a value from an entrypoint

The following question has been asked on Telegram:

Hi, how can i return a message to the sender? Like a ‘raise’ but in a case where’s no error

In Tezos smart contracts, there isn’t a direct “return mechanism” for entrypoints like the raise statement used in error handling. However, there are several alternatives depending on your requirements:

1. On-chain views:

On-chain views are similar to entrypoints but are read-only. They are defined using the @sp.onchain_view decorator. These views are useful for retrieving information from the contract without altering its state.

For instance, to get an administrator from a contract:

def get_administrator(self):

They can be called using sp.view(view_name, contract_address, value, t) -> sp.option[t].

For example:

def do_something(self, address):
    # `address` is the address of the contract
    # onto which we call `get_administrator`.
    is_administrator = sp.view("get_administrator", address, (), sp.bool).unwrap_some()

On-chain views can also be called off-chain.

2. Events:

Events in Tezos are mechanisms to notify off-chain systems of occurrences within the contract. They are particularly useful for tracking contract activities in indexers or explorers but are not accessible to on-chain code.

Example of emitting an event:

sp.emit(event_name, event_data)

For more details, check SmartPy’s documentation on events.

3. Callback:

See How 'callback' parameter must be used?

Is possible to handle event in scenario?