# Configure the Smart Accounts Kit

> Learn how to configure the bundler client, paymaster client, and toolkit environment.

# Configure the Smart Accounts Kit

The Smart Accounts Kit is highly configurable, providing support for custom <GlossaryTerm term="Bundler">bundlers</GlossaryTerm> and <GlossaryTerm term="Paymaster">paymasters</GlossaryTerm>.
You can also configure the toolkit environment to interact with the
<GlossaryTerm term="Delegation Framework" />.

## Prerequisites

[Install and set up the Smart Accounts Kit.](../get-started/install.md)

## Configure the bundler

The toolkit uses Viem's Account Abstraction API to configure custom <GlossaryTerm term="Bundler">bundlers</GlossaryTerm> and <GlossaryTerm term="Paymaster">paymasters</GlossaryTerm>.
This provides a robust and flexible foundation for creating and managing [MetaMask Smart Accounts](../concepts/smart-accounts.md).
See Viem's [account abstraction documentation](https://viem.sh/account-abstraction) for more information on the API's features, methods, and best practices.

To use the bundler and paymaster clients with the toolkit, create instances of these clients and configure them as follows:

```typescript

// Replace these URLs with your actual bundler and paymaster endpoints.
const bundlerUrl = 'https://your-bundler-url.com'
const paymasterUrl = 'https://your-paymaster-url.com'

// The paymaster is optional.
const paymasterClient = createPaymasterClient({
  transport: http(paymasterUrl),
})

const bundlerClient = createBundlerClient({
  transport: http(bundlerUrl),
  paymaster: paymasterClient,
  chain,
})
```

Replace the bundler and paymaster URLs with your bundler and paymaster endpoints.
For example, you can use endpoints from [Pimlico](https://docs.pimlico.io/references/bundler), [Infura](/services), or [ZeroDev](https://docs.zerodev.app/meta-infra/intro).

:::note
Providing a paymaster is optional when configuring your bundler client. However, if you choose not to use a paymaster, the smart account must have enough funds to pay gas fees.
:::

## (Optional) Configure the toolkit environment

The toolkit environment (`SmartAccountsEnvironment`) defines the contract addresses necessary for interacting with the [Delegation Framework](../concepts/delegation/overview.md#delegation-framework) on a specific network.
It serves several key purposes:

- It provides a centralized configuration for all the contract addresses required by the Delegation Framework.
- It enables easy switching between different networks (for example, Mainnet and testnet) or custom deployments.
- It ensures consistency across different parts of the application that interact with the Delegation Framework.

### Resolve the environment

When you create a <GlossaryTerm term="MetaMask smart account" />, the toolkit automatically
resolves the environment based on the version it requires and the chain configured.
If no environment is found for the specified chain, it throws an error.

<Tabs>
<TabItem value="example.ts">

```typescript

const environment: SmartAccountsEnvironment = delegatorSmartAccount.environment
```

</TabItem>
<TabItem value="config.ts">

```typescript

  Implementation,
  toMetaMaskSmartAccount,
} from "@metamask/smart-accounts-kit";

const publicClient = createPublicClient({
  chain,
  transport: http(),
});

const delegatorAccount = privateKeyToAccount("0x...");

const delegatorSmartAccount = await toMetaMaskSmartAccount({
  client: publicClient,
  implementation: Implementation.Hybrid,
  deployParams: [delegatorAccount.address, [], [], []],
  deploySalt: "0x",
  signer: { account: delegatorAccount },
});

export delegatorSmartAccount;
```

</TabItem>
</Tabs>

:::note
See the changelog of the toolkit version you are using (in the left sidebar) for supported chains.
:::

Alternatively, you can use the [`getSmartAccountsEnvironment`](../reference/delegation/index.md#getsmartaccountsenvironment) function to resolve the environment.
This function is especially useful if your <GlossaryTerm term="Delegator account">delegator</GlossaryTerm> is not a smart account when
creating a <GlossaryTerm term="Redelegation">redelegation</GlossaryTerm>.

```typescript

// Resolves the SmartAccountsEnvironment for Sepolia
const environment: SmartAccountsEnvironment = getSmartAccountsEnvironment(sepolia.id)
```

### Deploy a custom environment

You can deploy the contracts using any method, but the toolkit provides a convenient [`deploySmartAccountsEnvironment`](../reference/delegation/index.md#deploysmartaccountsenvironment) function. This function simplifies deploying the <GlossaryTerm term="Delegation Framework" /> contracts to your desired EVM chain.

This function requires a Viem [Public Client](https://viem.sh/docs/clients/public), [Wallet Client](https://viem.sh/docs/clients/wallet), and [Chain](https://viem.sh/docs/glossary/types#chain)
to deploy the contracts and resolve the `SmartAccountsEnvironment`.

Your wallet must have a sufficient native token balance to deploy the contracts.

<Tabs>
<TabItem value="example.ts">

```typescript

const environment = await deploySmartAccountsEnvironment(walletClient, publicClient, chain)
```

</TabItem>
<TabItem value="config.ts">

```typescript

// Your deployer wallet private key.
const privateKey = '0x123..'
const account = privateKeyToAccount(privateKey)

export const walletClient = createWalletClient({
  account,
  chain,
  transport: http(),
})

export const publicClient = createPublicClient({
  transport: http(),
  chain,
})
```

</TabItem>
</Tabs>

You can also override specific contracts when calling `deploySmartAccountsEnvironment`.
For example, if you've already deployed the `EntryPoint` contract on the target chain, you can pass the contract address to the function.

```typescript
// The config.ts is the same as in the previous example.

const environment = await deploySmartAccountsEnvironment(
  walletClient,
  publicClient,
  chain,
  // add-start
+ {
+   EntryPoint: "0x0000000071727De22E5E9d8BAf0edAc6f37da032"
+ }
  // add-end
);
```

Once the contracts are deployed, you can use them to override the environment.

### Override the environment

To override the environment, the toolkit provides an [`overrideDeployedEnvironment`](../reference/delegation/index.md#overridedeployedenvironment) function to resolve
`SmartAccountsEnvironment` with specified contracts for the given chain and contract version.

```typescript
// The config.ts is the same as in the previous example.

  overrideDeployedEnvironment,
  deploySmartAccountsEnvironment,
} from '@metamask/smart-accounts-kit'

const environment: SmartAccountsEnvironment = await deploySmartAccountsEnvironment(
  walletClient,
  publicClient,
  chain
)

overrideDeployedEnvironment(chain.id, '1.3.0', environment)
```

If you've already deployed the contracts using a different method, you can create a `SmartAccountsEnvironment` instance with the required contract addresses, and pass it to the function.

```typescript
// remove-start
- import { walletClient, publicClient } from "./config.ts";
- import { sepolia as chain } from "viem/chains";
// remove-end

  overrideDeployedEnvironment,
  // remove-next-line
- deploySmartAccountsEnvironment
} from "@metamask/smart-accounts-kit";

// remove-start
- const environment: SmartAccountsEnvironment = await deploySmartAccountsEnvironment(
-  walletClient,
-  publicClient,
-  chain
- );
// remove-end

// add-start
+ const environment: SmartAccountsEnvironment = {
+  SimpleFactory: "0x124..",
+  // ...
+  implementations: {
+    // ...
+  },
+ };
// add-end

overrideDeployedEnvironment(
  chain.id,
  "1.3.0",
  environment
);
```

:::note
Make sure to specify the <GlossaryTerm term="Delegation Framework" /> version required by the toolkit.
See the changelog of the toolkit version you are using (in the left sidebar) for its required Framework version.
:::
