How to limit contract interaction only to AccountAddress

how to write better code here?

According to Documentation.

Since a contract running on the chain will typically not be able to recover from panics, and error traces are not reported, it is useful not to bloat code size with them. Setting panic=abort will make it so that the compiler will generate simple Wasm traps on any panic that occurs. This option can be specified either in .cargo/config as exemplified in counter/.cargo/config, or in the Cargo.toml file as

What we recommend is that you define an error type with specific errors that you contract may trigger.

For example

enum MyError {

and then do

let y = match ctx.sender() {
    Address::Account(y) => y,
    _ => return Err(MyError::OnlyAccountsAllowed)

If you don’t need to extract the account you can make it a little shorter

ensure!(matches!(ctx.sender(), Address::Account(_)), MyError::OnlyAccountsAllowed)