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

# Struct ContractSelfPublic

Module: `noir_aztec::contract_self::contract_self_public`

```rust
pub struct ContractSelfPublic<Storage, CallSelf, CallSelfStatic, CallInternal> {
    pub address: AztecAddress,
    pub storage: Storage,
    pub context: PublicContext,
    pub call_self: CallSelf,
    pub call_self_static: CallSelfStatic,
    pub internal: CallInternal,
}
```

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

This struct is automatically injected into every [`external`](/aztec-nr-api/v6.0.0/noir_aztec/macros/functions/fn.external.md) and [`internal`](/aztec-nr-api/v6.0.0/noir_aztec/macros/functions/fn.internal.md) contract function marked with `"public"` 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 contract's own non-view functions
- `CallSelfStatic`: Macro-generated type for calling contract's own view functions
- `CallInternal`: Macro-generated type for calling internal functions

## 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 (public) 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: PublicContext
```

The public execution context.

```rust
call_self: CallSelf
```

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

Example API:

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

```rust
call_self_static: CallSelfStatic
```

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

Example API:

```rust
self.call_self_static.some_view_function(args)
```

```rust
internal: CallInternal
```

Provides type-safe methods for calling internal functions.

Example API:

```rust
self.internal.some_internal_function(args)
```

## Implementations

```rust
impl<Storage, CallSelf, CallSelfStatic, CallInternal> ContractSelfPublic<Storage, CallSelf, CallSelfStatic, CallInternal>
```

```rust
pub fn new(
    context: PublicContext,
    storage: Storage,
    call_self: CallSelf,
    call_self_static: CallSelfStatic,
    internal: CallInternal,
) -> Self
```

Creates a new `ContractSelfPublic` instance for a public function.

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

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

The address of the contract address that made this function call.

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

#### Incognito Calls

Contracts can call public functions from private ones hiding their identity (see

[`ContractSelfPrivate::enqueue_incognito`](/aztec-nr-api/v6.0.0/noir_aztec/contract_self/contract_self_private/struct.ContractSelfPrivate.md#enqueue_incognito)). This function reverts when executed in such a context.

If you need to handle these cases, use [`PublicContext::maybe_msg_sender`](/aztec-nr-api/v6.0.0/noir_aztec/context/struct.PublicContext.md#maybe_msg_sender).

```rust
pub unconstrained fn emit<Event>(&mut self, event: Event)
where
    Event: EventInterface,
    Event: Serialize
```

Emits an event publicly.

Public events are emitted as plaintext and are therefore visible to everyone. This is is the same as Solidity events on EVM chains.

Unlike private events, they don't require delivery of an event message.

#### Example

```rust
#[event]
struct Update { value: Field }

#[external("public")]
fn publish_update(value: Field) {
    self.emit(Update { value });
}
```

#### Cost

Public event emission is achieved by emitting public transaction logs. A total of `N+1` fields are emitted, where `N` is the serialization length of the event.

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

Makes a public contract call.

Will revert if the called function reverts or runs out of gas.

#### Arguments

- `call` - The object representing the public function to invoke.

#### Returns

- `T` - Whatever data the called function has returned.

#### Example

```rust
self.call(Token::at(address).transfer_in_public(recipient, amount));
```

```rust
pub unconstrained fn view<let M: u32, let N: u32, T>(
    self,
    call: PublicStaticCall<M, N, T>,
) -> T
where
    T: Deserialize
```

Makes a public read-only contract call.

This is similar to Solidity's `staticcall`. The called function cannot modify state or emit events. Any nested calls are constrained to also be static calls.

Will revert if the called function reverts or runs out of gas.

#### Arguments

- `call` - The object representing the read-only public function to invoke.

#### Returns

- `T` - Whatever data the called function has returned.

#### Example

```rust
self.view(Token::at(address).balance_of_public(recipient));
```
