> ## Documentation Index
> Fetch the complete documentation index at: https://docs.groundtech.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Subscribe from an external wallet

> Approve USDC and mint MicroVault shares from a browser wallet.

Subscriptions are synchronous wallet transactions. The shareholder approves the vault to spend USDC, then deposits USDC and receives ERC-20 MicroVault shares.

## Prerequisites

* An active MicroVault and its contract address
* A shareholder wallet connected to Ethereum Sepolia (`chainId: 11155111`)
* Sepolia USDC in that wallet
* An active shareholder proof
* A Viem `publicClient` and `walletClient`

Fetch the proof immediately before constructing the wallet transaction.

## 1. Fetch the shareholder proof

```javascript theme={"theme":{"light":"github-light","dark":"github-dark"}}
export async function getShareholderProof(vaultId, walletAddress) {
  const response = await fetch(
    `${process.env.GROUND_API}/v2/microvaults/vaults/${vaultId}/shareholders/${walletAddress}/proof`,
    { headers: { Authorization: `Bearer ${process.env.GROUND_API_KEY}` } },
  );
  if (!response.ok) throw new Error(await response.text());
  return (await response.json()).proof;
}
```

A `404` means the wallet is not yet on the active allowlist. Complete [shareholder approval](/docs/microvaults/shareholders) before enabling the subscribe action.

## 2. Define the required contract calls

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const erc20Abi = [
  {
    type: "function", name: "approve", stateMutability: "nonpayable",
    inputs: [{ name: "spender", type: "address" }, { name: "amount", type: "uint256" }],
    outputs: [{ type: "bool" }],
  },
] as const;

const microVaultAbi = [
  {
    type: "function", name: "convertToShares", stateMutability: "view",
    inputs: [{ name: "assets", type: "uint256" }], outputs: [{ type: "uint256" }],
  },
  {
    type: "function", name: "depositWithProof", stateMutability: "nonpayable",
    inputs: [
      { name: "assets", type: "uint256" },
      { name: "receiver", type: "address" },
      { name: "minSharesOut", type: "uint256" },
      { name: "callerProof", type: "bytes32[]" },
      { name: "receiverProof", type: "bytes32[]" },
    ],
    outputs: [{ type: "uint256" }],
  },
] as const;
```

## 3. Approve USDC

Convert the human amount using the base asset's decimals. USDC uses six decimals.

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { parseUnits, type Address, type Hex } from "viem";

const assets = parseUnits("100", 6);
const approvalHash = await walletClient.writeContract({
  address: usdcAddress as Address,
  abi: erc20Abi,
  functionName: "approve",
  args: [vaultAddress as Address, assets],
});
await publicClient.waitForTransactionReceipt({ hash: approvalHash });
```

Skip this transaction only when the existing allowance is at least `assets`.

## 4. Submit the subscription

Quote the expected shares immediately before submission and apply your minimum-output policy. This example permits up to `0.5%` quote movement.

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const quotedShares = await publicClient.readContract({
  address: vaultAddress as Address,
  abi: microVaultAbi,
  functionName: "convertToShares",
  args: [assets],
});
const minSharesOut = quotedShares * 9950n / 10000n;

const depositHash = await walletClient.writeContract({
  address: vaultAddress as Address,
  abi: microVaultAbi,
  functionName: "depositWithProof",
  args: [
    assets,
    walletAddress as Address,
    minSharesOut,
    proof.proof as Hex[],
    proof.proof as Hex[],
  ],
});
const receipt = await publicClient.waitForTransactionReceipt({ hash: depositHash });
if (receipt.status !== "success") throw new Error("Subscription reverted");
```

The example uses the connected wallet as both caller and share recipient, so the same proof is supplied twice. If they differ, fetch and supply an active proof for each address.

## Confirm the subscription

After the receipt succeeds, refresh the vault detail and activity endpoints. The new subscription should appear in activity, `totalAssetsUnits` and `idleAssetsUnits` should include the deposit, and the wallet's ERC-20 share balance should increase.

The MicroVault contract address is also its share-token address. Compatible wallets can add it with `wallet_watchAsset` using the vault address, share symbol, and `18` decimals.

If the USDC approval succeeds but the deposit fails, do not repeat the approval unless the allowance is insufficient. Re-fetch the active proof and share quote, then submit a new deposit transaction.
