# initialize\_controller

### Flow

* Checks
  * Anchor IDL accounts checks
  * Validates: check decimal input parameter
* Handler
  * Initialises the `controller` PDA
  * Initialises the `redeemable_mint` PDA
  * Writes initial `controller` data
  * Emits Anchor `InitializeControllerEvent` event

### Specificities

There can ever only be a single `Controller` for a given deployed program due to the seed derivation being a function of a static string and the programID

This instruction is preferably called right after program deployment. In the very unlikely event that  someone front run you, nothing at stake yet so wipe and redeploy and call it faster.

This instruction is preferably called by a multisig/DAO instead of a specific user account, for safety.

### Parameters

`redeemable_mint_decimals` the number of decimal the `redeemable mint` (UXD in our case) should be created with

### Accounts input

{% code title="programs/uxd/src/instructions/initialize\_controller.rs" %}

```rust
// ...

/// Takes 7 accounts - 4 used locally - 0 for CPI - 2 Programs - 1 Sysvar
#[derive(Accounts)]
#[instruction(
    redeemable_mint_decimals: u8,
)]
pub struct InitializeController<'info> {
    /// #1 Authored call accessible only to the signer matching Controller.authority
    pub authority: Signer<'info>,

    /// #2
    #[account(mut)]
    pub payer: Signer<'info>,

    /// #3 The top level UXDProgram on chain account managing the redeemable mint
    #[account(
        init,
        seeds = [CONTROLLER_NAMESPACE],
        bump,
        payer = payer,
        space = CONTROLLER_SPACE
    )]
    pub controller: AccountLoader<'info, Controller>,

    /// #4 The redeemable mint managed by the `controller` instance
    #[account(
        init,
        seeds = [REDEEMABLE_MINT_NAMESPACE],
        bump,
        mint::authority = controller,
        mint::decimals = redeemable_mint_decimals,
        payer = payer,
        constraint = redeemable_mint_decimals <= SOLANA_MAX_MINT_DECIMALS
    )]
    pub redeemable_mint: Account<'info, Mint>,

    /// #5 System Program
    pub system_program: Program<'info, System>,

    /// #6 Token Program
    pub token_program: Program<'info, Token>,

    /// #7 Rent Sysvar
    pub rent: Sysvar<'info, Rent>,
}

// ...
```

{% endcode %}
