> Aztec.nr API reference, v6.0.0 (mainnet and testnet). Index: https://docs.aztec.network/aztec-nr-api/v6.0.0/llms.txt

# Struct ContractSelfUtility

Module: `noir_aztec::contract_self::contract_self_utility`

```rust
pub struct ContractSelfUtility<Storage, CallSelf> {
    pub address: AztecAddress,
    pub storage: Storage,
    pub context: UtilityContext,
    pub call_self: CallSelf,
}
```

Core interface for interacting with aztec-nr contract features in utility execution contexts.

This struct is automatically injected into every [`external`](/aztec-nr-api/v6.0.0/noir_aztec/macros/functions/fn.external.md) contract function marked with `"utility"` by the Aztec macro system and is accessible through the `self` variable.

## Type Parameters

- `Storage`: The contract's storage struct (defined with [`storage`](/aztec-nr-api/v6.0.0/noir_aztec/macros/storage/fn.storage.md), or `()` if the contract has no storage
- `CallSelf`: Macro-generated type for calling the contract's own utility functions (same type as `CallSelf` in \[`PrivateUtilityCalls`])

## Fields

```rust
address: AztecAddress
```

The address of this contract

```rust
storage: Storage
```

The contract's storage instance, representing the struct to which the [`storage`](/aztec-nr-api/v6.0.0/noir_aztec/macros/storage/fn.storage.md) macro was applied in your contract. If the contract has no storage, the type of this will be `()`.

This storage instance is specialized for the current execution context (utility) and provides access to the contract's state variables.

#### Developer Note

If you've arrived here while trying to access your contract's storage while the `Storage` generic type is set to unit type `()`, it means you haven't yet defined a Storage struct using the [`storage`](/aztec-nr-api/v6.0.0/noir_aztec/macros/storage/fn.storage.md) macro in your contract. For guidance on setting this up, please refer to our docs: <https://docs.aztec.network/developers/docs/guides/smart_contracts/storage>

```rust
context: UtilityContext
```

The utility execution context.

```rust
call_self: CallSelf
```

Provides type-safe methods for calling this contract's own utility functions.

Example API:

```rust
self.call_self.some_utility_function(args)
```

## Implementations

```rust
impl<Storage, CallSelf> ContractSelfUtility<Storage, CallSelf>
```

```rust
pub fn new(context: UtilityContext, storage: Storage, call_self: CallSelf) -> Self
```

Creates a new `ContractSelfUtility` instance for a utility function.

This constructor is called automatically by the macro system and should not be called directly.

```rust
pub unconstrained fn msg_sender(self) -> AztecAddress
```

The address of the contract that made this function call.

This is similar to Solidity's `msg.sender` value.

#### Direct Invocation

A utility function invoked directly (e.g. by a wallet or dApp) rather than from another contract has no caller, and this function panics. Only a nested (cross-contract) utility call has a `msg_sender`.

If you need to handle the no-caller case, use [`UtilityContext::maybe_msg_sender`](/aztec-nr-api/v6.0.0/noir_aztec/context/struct.UtilityContext.md#maybe_msg_sender).

Note that utility functions are simulated client-side and never proven, so this value is unconstrained: see [`UtilityContext::maybe_msg_sender`](/aztec-nr-api/v6.0.0/noir_aztec/context/struct.UtilityContext.md#maybe_msg_sender) for why it must not be relied on as a security guarantee.

```rust
pub unconstrained fn call<let M: u32, let N: u32, T>(
    _self: Self,
    call: UtilityCall<M, N, T>,
) -> T
where
    T: Deserialize
```

Makes a utility contract call from another utility function.

The call runs entirely offchain in PXE and is never proven, so no guarantees are made on the correctness of its result.

The callee observes this contract's address as its `msg_sender`: see [`ContractSelfUtility::msg_sender`](/aztec-nr-api/v6.0.0/noir_aztec/contract_self/contract_self_utility/struct.ContractSelfUtility.md#msg_sender).

To call one of this contract's own utility functions, prefer the type-safe `self.call_self` stubs.

#### Authorization

A call to this contract itself always succeeds. A call to a different contract can expose that contract's private state to the caller, so it requires explicit authorization. A call that is not authorized fails.

#### Example

```rust
self.call(Token::at(address).get_balance_of(owner));
```
